diff --git a/include/cpp3ds/Window/Event.hpp b/include/cpp3ds/Window/Event.hpp index dceed00..8a78a39 100644 --- a/include/cpp3ds/Window/Event.hpp +++ b/include/cpp3ds/Window/Event.hpp @@ -28,8 +28,8 @@ public : /// //////////////////////////////////////////////////////////// struct JoystickMoveEvent { - Joystick::Axis axis; ///< Axis on which the joystick moved - float position; ///< New position on the axis (in range [-100 .. 100]) + int x; ///< New position on the x-axis (in range [-100 .. 100]) + int y; ///< New position on the y-axis (in range [-100 .. 100]) }; //////////////////////////////////////////////////////////// @@ -52,6 +52,14 @@ public : float z; ///< Current value of the sensor on Z axis }; + //////////////////////////////////////////////////////////// + /// \brief Slider event parameters (Slider3DChanged, SliderVolumeChanged) + /// + //////////////////////////////////////////////////////////// + struct SliderEvent { + float value; ///< Current value of the slider [0-1] range + }; + //////////////////////////////////////////////////////////// /// \brief Enumeration of the different types of events /// @@ -65,6 +73,8 @@ public : TouchEnded, ///< A touch event ended (data in event.touch) TouchSwiped, SensorChanged, ///< A sensor value changed (data in event.sensor) + Slider3DChanged, + SliderVolumeChanged, Count ///< Keep last -- the total number of event types }; @@ -79,7 +89,8 @@ public : JoystickMoveEvent joystickMove; ///< Joystick move event parameters (Event::JoystickMoved) TouchEvent touch; ///< Touch events parameters (Event::TouchBegan, Event::TouchMoved, Event::TouchEnded) SensorEvent sensor; ///< Sensor event parameters (Event::SensorChanged) - }; + SliderEvent slider; + }; }; } diff --git a/include/cpp3ds/Window/EventManager.hpp b/include/cpp3ds/Window/EventManager.hpp index fa4de59..f075adb 100644 --- a/include/cpp3ds/Window/EventManager.hpp +++ b/include/cpp3ds/Window/EventManager.hpp @@ -3,11 +3,17 @@ #include #include +#ifndef EMULATION +#include <3ds.h> +#endif namespace cpp3ds { class EventManager { public: + + EventManager(); + //////////////////////////////////////////////////////////// /// \brief Pop the event on top of the event queue, if any, and return it /// @@ -111,7 +117,13 @@ private: // JoystickState m_joystickStates[Joystick::Count]; ///< Previous state of the joysticks Vector3f m_sensorValue[Sensor::Count]; ///< Previous value of the sensors float m_joystickThreshold; ///< Joystick threshold (minimum motion for "move" event to be generated) - + float m_slider3d; ///< Position of 3D slider with [0,1] range + float m_sliderVolume; ///< Position of volume slider with [0,1] range + #ifndef EMULATION + touchPosition m_touch; + circlePosition m_circle; + u32 m_heldKeys; + #endif }; } diff --git a/include/cpp3ds/Window/Keyboard.hpp b/include/cpp3ds/Window/Keyboard.hpp index f4b79f1..94032d1 100644 --- a/include/cpp3ds/Window/Keyboard.hpp +++ b/include/cpp3ds/Window/Keyboard.hpp @@ -1,22 +1,17 @@ -#ifdef EMULATION -#include -#endif - #ifndef CPP3DS_KEYBOARD_HPP #define CPP3DS_KEYBOARD_HPP -namespace cpp3ds { +#ifndef EMULATION +#include <3ds.h> +#endif -#define HID 0x10146000 ///< Address at which to get button states -#define SLIDERSTATE 0x10144000 ///< Address at which to access 3D slider +namespace cpp3ds { //////////////////////////////////////////////////////////// /// \brief Give access to the real-time state of the keyboard /// //////////////////////////////////////////////////////////// class Keyboard { -protected: - static float m_slider; public : //////////////////////////////////////////////////////////// @@ -24,24 +19,47 @@ public : /// //////////////////////////////////////////////////////////// enum Key { - A = 1, ///< The A button - B = 2, ///< The B button - X = 1024, ///< The X button - Y = 2048, ///< The Y button - Up = 64, ///< The Up button - Down = 128, ///< The Down button - Left = 32, ///< The Left button - Right = 16, ///< The Right button - L = 512, ///< The L button - R = 256, ///< The R button - Start = 8, ///< The Start button - Select = 4, ///< The Select button - - KeyCount = 12 ///< Keep last -- the total number of keyboard keys + A = 1, ///< The A button + B = 1 << 1, ///< The B button + Select = 1 << 2, ///< The Select button + Start = 1 << 3, ///< The Start button + DPadRight = 1 << 4, + DPadLeft = 1 << 5, + DPadUp = 1 << 6, + DPadDown = 1 << 7, + R = 1 << 8, ///< The R button + L = 1 << 9, ///< The L button + X = 1 << 10, ///< The X button + Y = 1 << 11, ///< The Y button + ZL = 1 << 14, ///< The ZL button (New 3DS only) + ZR = 1 << 15, ///< The ZR button (New 3DS only) + Touchpad = 1 << 20, + CStickRight = 1 << 24, + CStickLeft = 1 << 25, + CStickUp = 1 << 26, + CStickDown = 1 << 27, + CPadRight = 1 << 28, + CPadLeft = 1 << 29, + CPadUp = 1 << 30, + CPadDown = 1 << 31, + Up = DPadUp | CPadUp, ///< Either DPadUp or CPadUp + Down = DPadDown | CPadDown, ///< Either DPadDown or CPadDown + Left = DPadLeft | CPadLeft, ///< Either DPadLeft or CPadLeft + Right = DPadRight | CPadRight, ///< Either DPadRight or CPadRight }; //////////////////////////////////////////////////////////// - /// \brief Check if a key is pressed + /// \brief Check if a key is still being pressed + /// + /// \param key Key to check + /// + /// \return True if the key is pressed, false otherwise + /// + //////////////////////////////////////////////////////////// + static bool isKeyDown(Key key); + + //////////////////////////////////////////////////////////// + /// \brief Check if a key is now being pressed /// /// \param key Key to check /// @@ -51,19 +69,37 @@ public : static bool isKeyPressed(Key key); //////////////////////////////////////////////////////////// - /// \brief Get current value of 3D slider + /// \brief Check if a key is now being released /// - /// \return float value from 0 to 1.0 (0 being completely off) + /// \param key Key to check + /// + /// \return True if the key is pressed, false otherwise /// //////////////////////////////////////////////////////////// - static float get3DSlider(); + static bool isKeyReleased(Key key); + + static float getSlider3D(); + + static float getSliderVolume(); //////////////////////////////////////////////////////////// /// \brief Update cache values for input //////////////////////////////////////////////////////////// static void update(); + +private: + + static float m_slider3d; + static float m_sliderVolume; + + #ifndef EMULATION + static u32 m_keysHeld; + static u32 m_keysPressed; + static u32 m_keysReleased; + #endif }; + } @@ -90,7 +126,7 @@ public : /// /// Usage example: /// \code -/// if (cpp3ds::Keyboard::isKeyPressed(csf::Mouse, pp3ds::Keyboard::Left)) +/// if (cpp3ds::Keyboard::isKeyPressed(cpp3ds::Mouse, cpp3ds::Keyboard::Left)) /// { /// // move left... /// } diff --git a/include/cpp3ds/Window/Keyboard_emu.hpp b/include/cpp3ds/Window/Keyboard_emu.hpp deleted file mode 100644 index 39998c7..0000000 --- a/include/cpp3ds/Window/Keyboard_emu.hpp +++ /dev/null @@ -1,191 +0,0 @@ -#ifndef CPP3DS_KEYBOARD_HPP -#define CPP3DS_KEYBOARD_HPP - -namespace cpp3ds { -//////////////////////////////////////////////////////////// -/// \brief Give access to the real-time state of the keyboard -/// -//////////////////////////////////////////////////////////// -class Keyboard { -protected: - static float m_slider; -public : - - //////////////////////////////////////////////////////////// - /// \brief Key codes - /// - //////////////////////////////////////////////////////////// - enum Key { - Unknown = -1, ///< Unhandled key - A = 0, ///< The A key - B, ///< The B key - C, ///< The C key - D, ///< The D key - E, ///< The E key - F, ///< The F key - G, ///< The G key - H, ///< The H key - I, ///< The I key - J, ///< The J key - K, ///< The K key - L, ///< The L key - M, ///< The M key - N, ///< The N key - O, ///< The O key - P, ///< The P key - Q, ///< The Q key - R, ///< The R key - S, ///< The S key - T, ///< The T key - U, ///< The U key - V, ///< The V key - W, ///< The W key - X, ///< The X key - Y, ///< The Y key - Z, ///< The Z key - Num0, ///< The 0 key - Num1, ///< The 1 key - Num2, ///< The 2 key - Num3, ///< The 3 key - Num4, ///< The 4 key - Num5, ///< The 5 key - Num6, ///< The 6 key - Num7, ///< The 7 key - Num8, ///< The 8 key - Num9, ///< The 9 key - Escape, ///< The Escape key - LControl, ///< The left Control key - LShift, ///< The left Shift key - LAlt, ///< The left Alt key - LSystem, ///< The left OS specific key: window (Windows and Linux), apple (MacOS X), ... - RControl, ///< The right Control key - RShift, ///< The right Shift key - RAlt, ///< The right Alt key - RSystem, ///< The right OS specific key: window (Windows and Linux), apple (MacOS X), ... - Menu, ///< The Menu key - LBracket, ///< The [ key - RBracket, ///< The ] key - SemiColon, ///< The ; key - Comma, ///< The , key - Period, ///< The . key - Quote, ///< The ' key - Slash, ///< The / key - BackSlash, ///< The \ key - Tilde, ///< The ~ key - Equal, ///< The = key - Dash, ///< The - key - Space, ///< The Space key - Start, ///< The Return key - Select, ///< The Backspace key - Tab, ///< The Tabulation key - PageUp, ///< The Page up key - PageDown, ///< The Page down key - End, ///< The End key - Home, ///< The Home key - Insert, ///< The Insert key - Delete, ///< The Delete key - Add, ///< The + key - Subtract, ///< The - key - Multiply, ///< The * key - Divide, ///< The / key - Left, ///< Left arrow - Right, ///< Right arrow - Up, ///< Up arrow - Down, ///< Down arrow - Numpad0, ///< The numpad 0 key - Numpad1, ///< The numpad 1 key - Numpad2, ///< The numpad 2 key - Numpad3, ///< The numpad 3 key - Numpad4, ///< The numpad 4 key - Numpad5, ///< The numpad 5 key - Numpad6, ///< The numpad 6 key - Numpad7, ///< The numpad 7 key - Numpad8, ///< The numpad 8 key - Numpad9, ///< The numpad 9 key - F1, ///< The F1 key - F2, ///< The F2 key - F3, ///< The F3 key - F4, ///< The F4 key - F5, ///< The F5 key - F6, ///< The F6 key - F7, ///< The F7 key - F8, ///< The F8 key - F9, ///< The F9 key - F10, ///< The F10 key - F11, ///< The F11 key - F12, ///< The F12 key - F13, ///< The F13 key - F14, ///< The F14 key - F15, ///< The F15 key - Pause, ///< The Pause key - - KeyCount ///< Keep last -- the total number of keyboard keys - }; - - //////////////////////////////////////////////////////////// - /// \brief Check if a key is pressed - /// - /// \param key Key to check - /// - /// \return True if the key is pressed, false otherwise - /// - //////////////////////////////////////////////////////////// - static bool isKeyPressed(Key key); - - //////////////////////////////////////////////////////////// - /// \brief Get current value of 3D slider - /// - /// \return float value from 0 to 1.0 (0 being completely off) - /// - //////////////////////////////////////////////////////////// - static float get3DSlider(); - - //////////////////////////////////////////////////////////// - /// \brief Update cache values for input - //////////////////////////////////////////////////////////// - static void update(); -}; - -} - - -#endif - - -//////////////////////////////////////////////////////////// -/// \class cpp3ds::Keyboard -/// \ingroup window -/// -/// cpp3ds::Keyboard provides an interface to the state of the -/// keyboard. It only contains static functions (a single -/// keyboard is assumed), so it's not meant to be instantiated. -/// -/// This class allows users to query the keyboard state at any -/// time and directly, without having to deal with a window and -/// its events. Compared to the KeyPressed and KeyReleased events, -/// cpp3ds::Keyboard can retrieve the state of a key at any time -/// (you don't need to store and update a boolean on your side -/// in order to know if a key is pressed or released), and you -/// always get the real state of the keyboard, even if keys are -/// pressed or released when your window is out of focus and no -/// event is triggered. -/// -/// Usage example: -/// \code -/// if (cpp3ds::Keyboard::isKeyPressed(csf::Mouse, pp3ds::Keyboard::Left)) -/// { -/// // move left... -/// } -/// else if (cpp3ds::Keyboard::isKeyPressed(cpp3ds::Keyboard::Right)) -/// { -/// // move right... -/// } -/// else if (cpp3ds::Keyboard::isKeyPressed(cpp3ds::Keyboard::Escape)) -/// { -/// // quit... -/// } -/// \endcode -/// -/// \see cpp3ds::Joystick, cpp3ds::Touch -/// -//////////////////////////////////////////////////////////// diff --git a/include/cpp3ds/Window/Sensor.hpp b/include/cpp3ds/Window/Sensor.hpp index 012ba26..05738f4 100644 --- a/include/cpp3ds/Window/Sensor.hpp +++ b/include/cpp3ds/Window/Sensor.hpp @@ -4,7 +4,6 @@ #include #include - namespace cpp3ds { //////////////////////////////////////////////////////////// /// \brief Give access to the real-time state of the sensors @@ -53,6 +52,22 @@ public: //////////////////////////////////////////////////////////// static void setEnabled(Type sensor, bool enabled); + //////////////////////////////////////////////////////////// + /// \brief Check if a sensor is enabled + /// + /// \param sensor Sensor to check + /// + /// \return True if the sensor is enabled, false otherwise + /// + //////////////////////////////////////////////////////////// + static bool isEnabled(Type sensor); + + //////////////////////////////////////////////////////////// + /// \brief Update the state of all the sensors + /// + //////////////////////////////////////////////////////////// + static void update(); + //////////////////////////////////////////////////////////// /// \brief Get the current sensor value /// @@ -62,6 +77,14 @@ public: /// //////////////////////////////////////////////////////////// static Vector3f getValue(Type sensor); + +private: + + static bool m_accel_enabled; + static bool m_gyro_enabled; + static Vector3f m_accel; + static Vector3f m_gyro; + }; } diff --git a/src/cpp3ds/Window/EventManager.cpp b/src/cpp3ds/Window/EventManager.cpp index f636acf..318ba52 100644 --- a/src/cpp3ds/Window/EventManager.cpp +++ b/src/cpp3ds/Window/EventManager.cpp @@ -1,8 +1,14 @@ #include +#include #include +#include <3ds.h> namespace cpp3ds { +EventManager::EventManager() { + m_joystickThreshold = 10.f; +} + bool EventManager::pollEvent(Event& event) { if (popEvent(event, false)) { return filterEvent(event); @@ -20,11 +26,12 @@ bool EventManager::waitEvent(Event& event) { } void EventManager::setJoystickThreshold(float threshold) { - // + if (threshold >=0 && threshold <= 100) + m_joystickThreshold = threshold; } void EventManager::pushEvent(const Event& event) { - // + m_events.push(event); } bool EventManager::filterEvent(const Event& event) { @@ -34,6 +41,105 @@ bool EventManager::filterEvent(const Event& event) { //////////////////////////////////////////////////////////// void EventManager::processEvents() { // Check all inputs and push Events that are triggered + Event e; + int i; + + Keyboard::update(); + + // 3D slider + float slider3d = Keyboard::getSlider3D(); + if (slider3d != m_slider3d) { + m_slider3d = slider3d; + e.type = Event::Slider3DChanged; + e.slider.value = slider3d; + pushEvent(e); + } + + // Volume slider + float sliderVolume = Keyboard::getSliderVolume(); + if (sliderVolume != m_sliderVolume) { + m_sliderVolume = sliderVolume; + e.type = Event::SliderVolumeChanged; + e.slider.value = sliderVolume; + pushEvent(e); + } + + // Circle pad + const float circleRatio = 100.f / 156.f; // Ratio to convert to [-100, 100] range + circlePosition circle; + hidCircleRead(&circle); + circle.dx = static_cast(circleRatio * circle.dx); + circle.dy = static_cast(circleRatio * circle.dy); + if (static_cast(circle.dx) > -m_joystickThreshold && static_cast(circle.dx) < m_joystickThreshold) + circle.dx = 0; + if (static_cast(circle.dy) > -m_joystickThreshold && static_cast(circle.dy) < m_joystickThreshold) + circle.dy = 0; + if (circle.dx != m_circle.dx || circle.dy != m_circle.dy) { + m_circle = circle; + e.type = Event::JoystickMoved; + e.joystickMove.x = circle.dx; + e.joystickMove.y = circle.dy; + pushEvent(e); + } + + // TODO: implement TouchSwiped event + + // Touch screen + touchPosition touch; + static bool isTouching; + if (Keyboard::isKeyDown(Keyboard::Touchpad)) { + hidTouchRead(&touch); + if (touch.px != m_touch.px || touch.py != m_touch.py || !isTouching) { + m_touch = touch; + if (isTouching) { + e.type = Event::TouchMoved; + } else { + e.type = Event::TouchBegan; + isTouching = true; + } + e.touch.x = touch.px; + e.touch.y = touch.py; + pushEvent(e); + } + } else if (isTouching) { + isTouching = false; + e.type = Event::TouchEnded; + e.touch.x = m_touch.px; + e.touch.y = m_touch.py; + pushEvent(e); + } + + // Sensors (gyroscope & accelerometer) + for (i = 0; i < Sensor::Count; ++i) { + Sensor::Type type = static_cast(i); + if (Sensor::isEnabled(type)) { + Vector3f val = Sensor::getValue(type); + if (val != m_sensorValue[type]) { + m_sensorValue[type] = val; + e.type = Event::SensorChanged; + e.sensor.type = type; + e.sensor.x = val.x; + e.sensor.y = val.y; + e.sensor.z = val.z; + pushEvent(e); + } + } + } + + // Buttons + for (i = 0; i < 32; ++i) { + Keyboard::Key key = static_cast(1 << i); + if (Keyboard::isKeyPressed(key)) { + e.type = Event::KeyPressed; + e.key.code = key; + pushEvent(e); + } + if (Keyboard::isKeyReleased(key)) { + e.type = Event::KeyReleased; + e.key.code = key; + pushEvent(e); + } + } } //////////////////////////////////////////////////////////// diff --git a/src/cpp3ds/Window/Keyboard.cpp b/src/cpp3ds/Window/Keyboard.cpp index 1daee72..695203e 100644 --- a/src/cpp3ds/Window/Keyboard.cpp +++ b/src/cpp3ds/Window/Keyboard.cpp @@ -1,23 +1,51 @@ #include -#include +#include <3ds.h> namespace cpp3ds { -float Keyboard::m_slider = 0; +u32 Keyboard::m_keysHeld = 0; +u32 Keyboard::m_keysPressed = 0; +u32 Keyboard::m_keysReleased = 0; +float Keyboard::m_slider3d = 0; +float Keyboard::m_sliderVolume = 0; + +//////////////////////////////////////////////////////////// +bool Keyboard::isKeyDown(Key key) { + return (m_keysHeld & static_cast(key)); +} //////////////////////////////////////////////////////////// bool Keyboard::isKeyPressed(Key key) { - return (~read_word(HID) & static_cast(key)); + return (m_keysPressed& static_cast(key)); } - //////////////////////////////////////////////////////////// -float Keyboard::get3DSlider() { - return m_slider; +bool Keyboard::isKeyReleased(Key key) { + return (m_keysReleased & static_cast(key)); } +//////////////////////////////////////////////////////////// +float Keyboard::getSlider3D() { + return m_slider3d; +} + +//////////////////////////////////////////////////////////// +float Keyboard::getSliderVolume() { + return m_sliderVolume; +} + +//////////////////////////////////////////////////////////// void Keyboard::update() { - m_slider = (float)(read_word(SLIDERSTATE) & 0xFF) / 255; + hidScanInput(); + m_keysHeld = hidKeysHeld(); + m_keysPressed = hidKeysDown(); + m_keysReleased = hidKeysUp(); + + u8 volume; + HIDUSER_GetSoundVolume(&volume); + m_sliderVolume = static_cast(volume) / 63; + + m_slider3d = *(float*)0x1FF81080; } } diff --git a/src/emu3ds/Window/EventManager.cpp b/src/emu3ds/Window/EventManager.cpp index a8053a8..acd669b 100644 --- a/src/emu3ds/Window/EventManager.cpp +++ b/src/emu3ds/Window/EventManager.cpp @@ -2,9 +2,20 @@ #include #include #include +#include namespace cpp3ds { +// TODO: configurable key-mapping +std::map keyMap = { + {sf::Keyboard::A, cpp3ds::Keyboard::A}, + {sf::Keyboard::B, cpp3ds::Keyboard::B} +}; + +EventManager::EventManager() { + m_joystickThreshold = 10.f; +} + bool EventManager::pollEvent(Event& event) { if (popEvent(event, false)) { return filterEvent(event); @@ -22,7 +33,8 @@ bool EventManager::waitEvent(Event& event) { } void EventManager::setJoystickThreshold(float threshold) { - // + if (threshold >=0 && threshold <= 100) + m_joystickThreshold = threshold; } void EventManager::pushEvent(const Event& event) { @@ -35,8 +47,8 @@ bool EventManager::filterEvent(const Event& event) { //////////////////////////////////////////////////////////// void EventManager::processEvents() { - int BOTTOM_X = 20, - BOTTOM_Y = 320, + int BOTTOM_X = 40, + BOTTOM_Y = 240, BOTTOM_WIDTH = 320; // Check all inputs and push Events that are triggered sf::Event event; @@ -74,20 +86,29 @@ void EventManager::processEvents() { } } while (_emulator->screen->pollEvent(event)) { -// std::cout << "EVENT: " << event.type << std::endl; + std::cout << "EVENT: " << event.type << std::endl; cpp3ds::Event e; + std::map::iterator key; switch (event.type) { case sf::Event::KeyPressed: - std::cout << "EVENT: " << event.type << std::endl; - e.type = Event::KeyPressed; - pushEvent(e); + std::cout << "sf::KeyPressed"; + key = keyMap.find(event.key.code); + if (key != keyMap.end()) { + e.type = Event::KeyPressed; + e.key.code = key->second; + pushEvent(e); + } break; case sf::Event::KeyReleased: - std::cout << "EVENT: " << event.type << std::endl; +// std::cout << "EVENT: " << event.type << std::endl; e.type = Event::KeyReleased; pushEvent(e); break; case sf::Event::JoystickMoved: + e.type = Event::JoystickMoved; + // TODO: implement joystick for emulator + e.joystickMove.x = event.joystickMove.position; +// pushEvent(e); break; default: break; diff --git a/src/emu3ds/Window/Keyboard.cpp b/src/emu3ds/Window/Keyboard.cpp index 68d9779..0f4728e 100644 --- a/src/emu3ds/Window/Keyboard.cpp +++ b/src/emu3ds/Window/Keyboard.cpp @@ -4,21 +4,28 @@ namespace cpp3ds { -float Keyboard::m_slider = 0; +float Keyboard::m_slider3d = 0; +float Keyboard::m_sliderVolume = 0; //////////////////////////////////////////////////////////// bool Keyboard::isKeyPressed(Key key) { return sf::Keyboard::isKeyPressed(static_cast(key)); } +//////////////////////////////////////////////////////////// +float Keyboard::getSlider3D() { + return m_slider3d; +} //////////////////////////////////////////////////////////// -float Keyboard::get3DSlider() { - return m_slider; +float Keyboard::getSliderVolume() { + return m_sliderVolume; } +//////////////////////////////////////////////////////////// void Keyboard::update() { - m_slider = _emulator->get_slider3d(); + m_slider3d = _emulator->get_slider3d(); + m_sliderVolume = 0; } }