aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/crepe/api/Button.cpp2
-rw-r--r--src/crepe/api/Button.h28
-rw-r--r--src/crepe/api/Event.h6
-rw-r--r--src/crepe/api/KeyCodes.h284
-rw-r--r--src/crepe/api/LoopManager.cpp6
-rw-r--r--src/crepe/api/UiObject.h16
-rw-r--r--src/crepe/facade/SDLContext.cpp357
-rw-r--r--src/crepe/facade/SDLContext.h16
-rw-r--r--src/crepe/system/InputSystem.cpp259
-rw-r--r--src/crepe/system/InputSystem.h43
-rw-r--r--src/example/gameloop.cpp22
-rw-r--r--src/test/EventTest.cpp74
-rw-r--r--src/test/inputTest.cpp307
-rw-r--r--src/test/loopTimerTest.cpp25
14 files changed, 713 insertions, 732 deletions
diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp
index 70f749d..547c0fc 100644
--- a/src/crepe/api/Button.cpp
+++ b/src/crepe/api/Button.cpp
@@ -2,4 +2,4 @@
using namespace crepe;
-Button::Button(game_object_id_t id) : UiObject(id){}
+Button::Button(game_object_id_t id) : UiObject(id) {}
diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h
index f769d58..0056238 100644
--- a/src/crepe/api/Button.h
+++ b/src/crepe/api/Button.h
@@ -12,33 +12,33 @@ namespace crepe {
*/
class Button : public UiObject {
public:
- /**
+ /**
* \brief Constructs a Button with the specified game object ID.
* \param id The unique ID of the game object associated with this button.
*/
- Button(game_object_id_t id);
+ Button(game_object_id_t id);
- //! Indicates if the button is interactable (can be clicked).
- bool interactable = true;
+ //! Indicates if the button is interactable (can be clicked).
+ bool interactable = true;
- //! Indicates if the button is a toggle button (can be pressed and released).
- bool is_toggle = false;
+ //! Indicates if the button is a toggle button (can be pressed and released).
+ bool is_toggle = false;
- //! Indicates whether the button is currently pressed.
- bool is_pressed = false;
+ //! Indicates whether the button is currently pressed.
+ bool is_pressed = false;
- //! Indicates whether the mouse is currently hovering over the button.
- bool hover = false;
+ //! Indicates whether the mouse is currently hovering over the button.
+ bool hover = false;
- //! The callback function to be executed when the button is clicked.
- std::function<void()> on_click;
+ //! The callback function to be executed when the button is clicked.
+ std::function<void()> on_click;
public:
- /**
+ /**
* \brief Retrieves the maximum number of instances allowed for this button type.
* \return Always returns 1, as only a single instance is allowed.
*/
- virtual int get_instances_max() const override { return 1; }
+ virtual int get_instances_max() const override { return 1; }
};
} // namespace crepe
diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h
index 46dd86b..2018d52 100644
--- a/src/crepe/api/Event.h
+++ b/src/crepe/api/Event.h
@@ -89,9 +89,9 @@ public:
//! Y-coordinate of the mouse position at the time of the event.
int mouse_y = 0;
// Relative movement in x
- int rel_x;
+ int rel_x;
// Relative movement in y
- int rel_y;
+ int rel_y;
};
/**
@@ -127,4 +127,4 @@ class ShutDownEvent : public Event {
public:
};
-}
+} // namespace crepe
diff --git a/src/crepe/api/KeyCodes.h b/src/crepe/api/KeyCodes.h
index 4b026c5..fcfc080 100644
--- a/src/crepe/api/KeyCodes.h
+++ b/src/crepe/api/KeyCodes.h
@@ -1,154 +1,154 @@
#pragma once
namespace crepe {
- //! Enumeration for mouse button inputs, including standard and extended buttons.
- enum class MouseButton {
- NONE = 0, //!< No mouse button input.
- LEFT_MOUSE = 1, //!< Left mouse button.
- RIGHT_MOUSE = 2, //!< Right mouse button.
- MIDDLE_MOUSE = 3, //!< Middle mouse button (scroll wheel press).
- X1_MOUSE = 4, //!< First extended mouse button.
- X2_MOUSE = 5, //!< Second extended mouse button.
- SCROLL_UP = 6, //!< Scroll wheel upward movement.
- SCROLL_DOWN = 7, //!< Scroll wheel downward movement.
- };
+//! Enumeration for mouse button inputs, including standard and extended buttons.
+enum class MouseButton {
+ NONE = 0, //!< No mouse button input.
+ LEFT_MOUSE = 1, //!< Left mouse button.
+ RIGHT_MOUSE = 2, //!< Right mouse button.
+ MIDDLE_MOUSE = 3, //!< Middle mouse button (scroll wheel press).
+ X1_MOUSE = 4, //!< First extended mouse button.
+ X2_MOUSE = 5, //!< Second extended mouse button.
+ SCROLL_UP = 6, //!< Scroll wheel upward movement.
+ SCROLL_DOWN = 7, //!< Scroll wheel downward movement.
+};
- //! Enumeration for keyboard key inputs, including printable characters, function keys, and keypad keys.
- enum class Keycode {
- NONE = 0, //!< No key input.
- SPACE = 32, //!< Spacebar.
- APOSTROPHE = 39, //!< Apostrophe (').
- COMMA = 44, //!< Comma (,).
- MINUS = 45, //!< Minus (-).
- PERIOD = 46, //!< Period (.).
- SLASH = 47, //!< Slash (/).
- D0 = 48, //!< Digit 0.
- D1 = 49, //!< Digit 1.
- D2 = 50, //!< Digit 2.
- D3 = 51, //!< Digit 3.
- D4 = 52, //!< Digit 4.
- D5 = 53, //!< Digit 5.
- D6 = 54, //!< Digit 6.
- D7 = 55, //!< Digit 7.
- D8 = 56, //!< Digit 8.
- D9 = 57, //!< Digit 9.
- SEMICOLON = 59, //!< Semicolon (;).
- EQUAL = 61, //!< Equal sign (=).
- A = 65, //!< Key 'A'.
- B = 66, //!< Key 'B'.
- C = 67, //!< Key 'C'.
- D = 68, //!< Key 'D'.
- E = 69, //!< Key 'E'.
- F = 70, //!< Key 'F'.
- G = 71, //!< Key 'G'.
- H = 72, //!< Key 'H'.
- I = 73, //!< Key 'I'.
- J = 74, //!< Key 'J'.
- K = 75, //!< Key 'K'.
- L = 76, //!< Key 'L'.
- M = 77, //!< Key 'M'.
- N = 78, //!< Key 'N'.
- O = 79, //!< Key 'O'.
- P = 80, //!< Key 'P'.
- Q = 81, //!< Key 'Q'.
- R = 82, //!< Key 'R'.
- S = 83, //!< Key 'S'.
- T = 84, //!< Key 'T'.
- U = 85, //!< Key 'U'.
- V = 86, //!< Key 'V'.
- W = 87, //!< Key 'W'.
- X = 88, //!< Key 'X'.
- Y = 89, //!< Key 'Y'.
- Z = 90, //!< Key 'Z'.
- LEFT_BRACKET = 91, //!< Left bracket ([).
- BACKSLASH = 92, //!< Backslash (\).
- RIGHT_BRACKET = 93, //!< Right bracket (]).
- GRAVE_ACCENT = 96, //!< Grave accent (`).
- WORLD1 = 161, //!< Non-US key #1.
- WORLD2 = 162, //!< Non-US key #2.
- ESCAPE = 256, //!< Escape key.
- ENTER = 257, //!< Enter key.
- TAB = 258, //!< Tab key.
- BACKSPACE = 259, //!< Backspace key.
- INSERT = 260, //!< Insert key.
- DELETE = 261, //!< Delete key.
- RIGHT = 262, //!< Right arrow key.
- LEFT = 263, //!< Left arrow key.
- DOWN = 264, //!< Down arrow key.
- UP = 265, //!< Up arrow key.
- PAGE_UP = 266, //!< Page Up key.
- PAGE_DOWN = 267, //!< Page Down key.
- HOME = 268, //!< Home key.
- END = 269, //!< End key.
- CAPS_LOCK = 280, //!< Caps Lock key.
- SCROLL_LOCK = 281, //!< Scroll Lock key.
- NUM_LOCK = 282, //!< Num Lock key.
- PRINT_SCREEN = 283, //!< Print Screen key.
- PAUSE = 284, //!< Pause key.
- /**
+//! Enumeration for keyboard key inputs, including printable characters, function keys, and keypad keys.
+enum class Keycode {
+ NONE = 0, //!< No key input.
+ SPACE = 32, //!< Spacebar.
+ APOSTROPHE = 39, //!< Apostrophe (').
+ COMMA = 44, //!< Comma (,).
+ MINUS = 45, //!< Minus (-).
+ PERIOD = 46, //!< Period (.).
+ SLASH = 47, //!< Slash (/).
+ D0 = 48, //!< Digit 0.
+ D1 = 49, //!< Digit 1.
+ D2 = 50, //!< Digit 2.
+ D3 = 51, //!< Digit 3.
+ D4 = 52, //!< Digit 4.
+ D5 = 53, //!< Digit 5.
+ D6 = 54, //!< Digit 6.
+ D7 = 55, //!< Digit 7.
+ D8 = 56, //!< Digit 8.
+ D9 = 57, //!< Digit 9.
+ SEMICOLON = 59, //!< Semicolon (;).
+ EQUAL = 61, //!< Equal sign (=).
+ A = 65, //!< Key 'A'.
+ B = 66, //!< Key 'B'.
+ C = 67, //!< Key 'C'.
+ D = 68, //!< Key 'D'.
+ E = 69, //!< Key 'E'.
+ F = 70, //!< Key 'F'.
+ G = 71, //!< Key 'G'.
+ H = 72, //!< Key 'H'.
+ I = 73, //!< Key 'I'.
+ J = 74, //!< Key 'J'.
+ K = 75, //!< Key 'K'.
+ L = 76, //!< Key 'L'.
+ M = 77, //!< Key 'M'.
+ N = 78, //!< Key 'N'.
+ O = 79, //!< Key 'O'.
+ P = 80, //!< Key 'P'.
+ Q = 81, //!< Key 'Q'.
+ R = 82, //!< Key 'R'.
+ S = 83, //!< Key 'S'.
+ T = 84, //!< Key 'T'.
+ U = 85, //!< Key 'U'.
+ V = 86, //!< Key 'V'.
+ W = 87, //!< Key 'W'.
+ X = 88, //!< Key 'X'.
+ Y = 89, //!< Key 'Y'.
+ Z = 90, //!< Key 'Z'.
+ LEFT_BRACKET = 91, //!< Left bracket ([).
+ BACKSLASH = 92, //!< Backslash (\).
+ RIGHT_BRACKET = 93, //!< Right bracket (]).
+ GRAVE_ACCENT = 96, //!< Grave accent (`).
+ WORLD1 = 161, //!< Non-US key #1.
+ WORLD2 = 162, //!< Non-US key #2.
+ ESCAPE = 256, //!< Escape key.
+ ENTER = 257, //!< Enter key.
+ TAB = 258, //!< Tab key.
+ BACKSPACE = 259, //!< Backspace key.
+ INSERT = 260, //!< Insert key.
+ DELETE = 261, //!< Delete key.
+ RIGHT = 262, //!< Right arrow key.
+ LEFT = 263, //!< Left arrow key.
+ DOWN = 264, //!< Down arrow key.
+ UP = 265, //!< Up arrow key.
+ PAGE_UP = 266, //!< Page Up key.
+ PAGE_DOWN = 267, //!< Page Down key.
+ HOME = 268, //!< Home key.
+ END = 269, //!< End key.
+ CAPS_LOCK = 280, //!< Caps Lock key.
+ SCROLL_LOCK = 281, //!< Scroll Lock key.
+ NUM_LOCK = 282, //!< Num Lock key.
+ PRINT_SCREEN = 283, //!< Print Screen key.
+ PAUSE = 284, //!< Pause key.
+ /**
* \name Function keys (F1-F25).
* \{
*/
- F1 = 290,
- F2 = 291,
- F3 = 292,
- F4 = 293,
- F5 = 294,
- F6 = 295,
- F7 = 296,
- F8 = 297,
- F9 = 298,
- F10 = 299,
- F11 = 300,
- F12 = 301,
- F13 = 302,
- F14 = 303,
- F15 = 304,
- F16 = 305,
- F17 = 306,
- F18 = 307,
- F19 = 308,
- F20 = 309,
- F21 = 310,
- F22 = 311,
- F23 = 312,
- F24 = 313,
- F25 = 314,
- /// \}
- /**
+ F1 = 290,
+ F2 = 291,
+ F3 = 292,
+ F4 = 293,
+ F5 = 294,
+ F6 = 295,
+ F7 = 296,
+ F8 = 297,
+ F9 = 298,
+ F10 = 299,
+ F11 = 300,
+ F12 = 301,
+ F13 = 302,
+ F14 = 303,
+ F15 = 304,
+ F16 = 305,
+ F17 = 306,
+ F18 = 307,
+ F19 = 308,
+ F20 = 309,
+ F21 = 310,
+ F22 = 311,
+ F23 = 312,
+ F24 = 313,
+ F25 = 314,
+ /// \}
+ /**
* \name Keypad digits and operators.
* \{
*/
- KP0 = 320,
- KP1 = 321,
- KP2 = 322,
- KP3 = 323,
- KP4 = 324,
- KP5 = 325,
- KP6 = 326,
- KP7 = 327,
- KP8 = 328,
- KP9 = 329,
- KP_DECIMAL = 330,
- KP_DIVIDE = 331,
- KP_MULTIPLY = 332,
- KP_SUBTRACT = 333,
- KP_ADD = 334,
- KP_ENTER = 335,
- KP_EQUAL = 336,
- /// \}
- /**
+ KP0 = 320,
+ KP1 = 321,
+ KP2 = 322,
+ KP3 = 323,
+ KP4 = 324,
+ KP5 = 325,
+ KP6 = 326,
+ KP7 = 327,
+ KP8 = 328,
+ KP9 = 329,
+ KP_DECIMAL = 330,
+ KP_DIVIDE = 331,
+ KP_MULTIPLY = 332,
+ KP_SUBTRACT = 333,
+ KP_ADD = 334,
+ KP_ENTER = 335,
+ KP_EQUAL = 336,
+ /// \}
+ /**
* \name Modifier keys.
* \{
*/
- LEFT_SHIFT = 340,
- LEFT_CONTROL = 341,
- LEFT_ALT = 342,
- LEFT_SUPER = 343,
- RIGHT_SHIFT = 344,
- RIGHT_CONTROL = 345,
- RIGHT_ALT = 346,
- RIGHT_SUPER = 347,
- /// \}
- MENU = 348, //!< Menu key.
- };
-}
+ LEFT_SHIFT = 340,
+ LEFT_CONTROL = 341,
+ LEFT_ALT = 342,
+ LEFT_SUPER = 343,
+ RIGHT_SHIFT = 344,
+ RIGHT_CONTROL = 345,
+ RIGHT_ALT = 346,
+ RIGHT_SUPER = 347,
+ /// \}
+ MENU = 348, //!< Menu key.
+};
+} // namespace crepe
diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp
index 9599943..b343250 100644
--- a/src/crepe/api/LoopManager.cpp
+++ b/src/crepe/api/LoopManager.cpp
@@ -2,11 +2,11 @@
#include "../system/AnimatorSystem.h"
#include "../system/CollisionSystem.h"
+#include "../system/InputSystem.h"
#include "../system/ParticleSystem.h"
#include "../system/PhysicsSystem.h"
#include "../system/RenderSystem.h"
#include "../system/ScriptSystem.h"
-#include "../system/InputSystem.h"
#include "LoopManager.h"
#include "LoopTimer.h"
@@ -24,9 +24,7 @@ LoopManager::LoopManager() {
this->load_system<InputSystem>();
}
-void LoopManager::process_input() {
- this->get_system<InputSystem>().update();
-}
+void LoopManager::process_input() { this->get_system<InputSystem>().update(); }
void LoopManager::start() {
this->setup();
diff --git a/src/crepe/api/UiObject.h b/src/crepe/api/UiObject.h
index ae2e744..7bd1c2e 100644
--- a/src/crepe/api/UiObject.h
+++ b/src/crepe/api/UiObject.h
@@ -10,24 +10,24 @@ namespace crepe {
*/
class UiObject : public Component {
public:
- /**
+ /**
* \brief Constructs a UiObject with the specified game object ID.
* \param id The unique ID of the game object associated with this UI object.
*/
- UiObject(game_object_id_t id);
+ UiObject(game_object_id_t id);
- //! The width of the UI object.
- int width = 0;
+ //! The width of the UI object.
+ int width = 0;
- //! The height of the UI object.
- int height = 0;
+ //! The height of the UI object.
+ int height = 0;
public:
- /**
+ /**
* \brief Retrieves the maximum number of instances allowed for this UI object type.
* /return Always returns 1, as only a single instance is allowed.
*/
- virtual int get_instances_max() const override { return 1; }
+ virtual int get_instances_max() const override { return 1; }
};
} // namespace crepe
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index a37392f..8ed3654 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -5,23 +5,22 @@
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_video.h>
+#include <array>
#include <cmath>
#include <cstddef>
#include <functional>
#include <memory>
#include <stdexcept>
#include <string>
-#include <array>
#include "../api/Camera.h"
+#include "../api/EventManager.h"
#include "../api/Sprite.h"
#include "../api/Texture.h"
#include "../api/Transform.h"
-#include "../api/EventManager.h"
#include "../api/Vector2.h"
#include "../util/Log.h"
-
#include "SDLContext.h"
using namespace crepe;
@@ -77,138 +76,138 @@ SDLContext::~SDLContext() {
}
Keycode SDLContext::sdl_to_keycode(SDL_Keycode sdl_key) {
- static const std::array<Keycode, SDL_NUM_SCANCODES> LOOKUP_TABLE = [] {
- std::array<Keycode, SDL_NUM_SCANCODES> table{};
- table.fill(Keycode::NONE);
-
- table[SDL_SCANCODE_SPACE] = Keycode::SPACE;
- table[SDL_SCANCODE_APOSTROPHE] = Keycode::APOSTROPHE;
- table[SDL_SCANCODE_COMMA] = Keycode::COMMA;
- table[SDL_SCANCODE_MINUS] = Keycode::MINUS;
- table[SDL_SCANCODE_PERIOD] = Keycode::PERIOD;
- table[SDL_SCANCODE_SLASH] = Keycode::SLASH;
- table[SDL_SCANCODE_0] = Keycode::D0;
- table[SDL_SCANCODE_1] = Keycode::D1;
- table[SDL_SCANCODE_2] = Keycode::D2;
- table[SDL_SCANCODE_3] = Keycode::D3;
- table[SDL_SCANCODE_4] = Keycode::D4;
- table[SDL_SCANCODE_5] = Keycode::D5;
- table[SDL_SCANCODE_6] = Keycode::D6;
- table[SDL_SCANCODE_7] = Keycode::D7;
- table[SDL_SCANCODE_8] = Keycode::D8;
- table[SDL_SCANCODE_9] = Keycode::D9;
- table[SDL_SCANCODE_SEMICOLON] = Keycode::SEMICOLON;
- table[SDL_SCANCODE_EQUALS] = Keycode::EQUAL;
- table[SDL_SCANCODE_A] = Keycode::A;
- table[SDL_SCANCODE_B] = Keycode::B;
- table[SDL_SCANCODE_C] = Keycode::C;
- table[SDL_SCANCODE_D] = Keycode::D;
- table[SDL_SCANCODE_E] = Keycode::E;
- table[SDL_SCANCODE_F] = Keycode::F;
- table[SDL_SCANCODE_G] = Keycode::G;
- table[SDL_SCANCODE_H] = Keycode::H;
- table[SDL_SCANCODE_I] = Keycode::I;
- table[SDL_SCANCODE_J] = Keycode::J;
- table[SDL_SCANCODE_K] = Keycode::K;
- table[SDL_SCANCODE_L] = Keycode::L;
- table[SDL_SCANCODE_M] = Keycode::M;
- table[SDL_SCANCODE_N] = Keycode::N;
- table[SDL_SCANCODE_O] = Keycode::O;
- table[SDL_SCANCODE_P] = Keycode::P;
- table[SDL_SCANCODE_Q] = Keycode::Q;
- table[SDL_SCANCODE_R] = Keycode::R;
- table[SDL_SCANCODE_S] = Keycode::S;
- table[SDL_SCANCODE_T] = Keycode::T;
- table[SDL_SCANCODE_U] = Keycode::U;
- table[SDL_SCANCODE_V] = Keycode::V;
- table[SDL_SCANCODE_W] = Keycode::W;
- table[SDL_SCANCODE_X] = Keycode::X;
- table[SDL_SCANCODE_Y] = Keycode::Y;
- table[SDL_SCANCODE_Z] = Keycode::Z;
- table[SDL_SCANCODE_LEFTBRACKET] = Keycode::LEFT_BRACKET;
- table[SDL_SCANCODE_BACKSLASH] = Keycode::BACKSLASH;
- table[SDL_SCANCODE_RIGHTBRACKET] = Keycode::RIGHT_BRACKET;
- table[SDL_SCANCODE_GRAVE] = Keycode::GRAVE_ACCENT;
- table[SDL_SCANCODE_ESCAPE] = Keycode::ESCAPE;
- table[SDL_SCANCODE_RETURN] = Keycode::ENTER;
- table[SDL_SCANCODE_TAB] = Keycode::TAB;
- table[SDL_SCANCODE_BACKSPACE] = Keycode::BACKSPACE;
- table[SDL_SCANCODE_INSERT] = Keycode::INSERT;
- table[SDL_SCANCODE_DELETE] = Keycode::DELETE;
- table[SDL_SCANCODE_RIGHT] = Keycode::RIGHT;
- table[SDL_SCANCODE_LEFT] = Keycode::LEFT;
- table[SDL_SCANCODE_DOWN] = Keycode::DOWN;
- table[SDL_SCANCODE_UP] = Keycode::UP;
- table[SDL_SCANCODE_PAGEUP] = Keycode::PAGE_UP;
- table[SDL_SCANCODE_PAGEDOWN] = Keycode::PAGE_DOWN;
- table[SDL_SCANCODE_HOME] = Keycode::HOME;
- table[SDL_SCANCODE_END] = Keycode::END;
- table[SDL_SCANCODE_CAPSLOCK] = Keycode::CAPS_LOCK;
- table[SDL_SCANCODE_SCROLLLOCK] = Keycode::SCROLL_LOCK;
- table[SDL_SCANCODE_NUMLOCKCLEAR] = Keycode::NUM_LOCK;
- table[SDL_SCANCODE_PRINTSCREEN] = Keycode::PRINT_SCREEN;
- table[SDL_SCANCODE_PAUSE] = Keycode::PAUSE;
- table[SDL_SCANCODE_F1] = Keycode::F1;
- table[SDL_SCANCODE_F2] = Keycode::F2;
- table[SDL_SCANCODE_F3] = Keycode::F3;
- table[SDL_SCANCODE_F4] = Keycode::F4;
- table[SDL_SCANCODE_F5] = Keycode::F5;
- table[SDL_SCANCODE_F6] = Keycode::F6;
- table[SDL_SCANCODE_F7] = Keycode::F7;
- table[SDL_SCANCODE_F8] = Keycode::F8;
- table[SDL_SCANCODE_F9] = Keycode::F9;
- table[SDL_SCANCODE_F10] = Keycode::F10;
- table[SDL_SCANCODE_F11] = Keycode::F11;
- table[SDL_SCANCODE_F12] = Keycode::F12;
- table[SDL_SCANCODE_KP_0] = Keycode::KP0;
- table[SDL_SCANCODE_KP_1] = Keycode::KP1;
- table[SDL_SCANCODE_KP_2] = Keycode::KP2;
- table[SDL_SCANCODE_KP_3] = Keycode::KP3;
- table[SDL_SCANCODE_KP_4] = Keycode::KP4;
- table[SDL_SCANCODE_KP_5] = Keycode::KP5;
- table[SDL_SCANCODE_KP_6] = Keycode::KP6;
- table[SDL_SCANCODE_KP_7] = Keycode::KP7;
- table[SDL_SCANCODE_KP_8] = Keycode::KP8;
- table[SDL_SCANCODE_KP_9] = Keycode::KP9;
- table[SDL_SCANCODE_LSHIFT] = Keycode::LEFT_SHIFT;
- table[SDL_SCANCODE_LCTRL] = Keycode::LEFT_CONTROL;
- table[SDL_SCANCODE_LALT] = Keycode::LEFT_ALT;
- table[SDL_SCANCODE_LGUI] = Keycode::LEFT_SUPER;
- table[SDL_SCANCODE_RSHIFT] = Keycode::RIGHT_SHIFT;
- table[SDL_SCANCODE_RCTRL] = Keycode::RIGHT_CONTROL;
- table[SDL_SCANCODE_RALT] = Keycode::RIGHT_ALT;
- table[SDL_SCANCODE_RGUI] = Keycode::RIGHT_SUPER;
- table[SDL_SCANCODE_MENU] = Keycode::MENU;
-
- return table;
- }();
-
- if (sdl_key < 0 || sdl_key >= SDL_NUM_SCANCODES) {
- return Keycode::NONE;
- }
-
- return LOOKUP_TABLE[sdl_key];
+ static const std::array<Keycode, SDL_NUM_SCANCODES> LOOKUP_TABLE = [] {
+ std::array<Keycode, SDL_NUM_SCANCODES> table{};
+ table.fill(Keycode::NONE);
+
+ table[SDL_SCANCODE_SPACE] = Keycode::SPACE;
+ table[SDL_SCANCODE_APOSTROPHE] = Keycode::APOSTROPHE;
+ table[SDL_SCANCODE_COMMA] = Keycode::COMMA;
+ table[SDL_SCANCODE_MINUS] = Keycode::MINUS;
+ table[SDL_SCANCODE_PERIOD] = Keycode::PERIOD;
+ table[SDL_SCANCODE_SLASH] = Keycode::SLASH;
+ table[SDL_SCANCODE_0] = Keycode::D0;
+ table[SDL_SCANCODE_1] = Keycode::D1;
+ table[SDL_SCANCODE_2] = Keycode::D2;
+ table[SDL_SCANCODE_3] = Keycode::D3;
+ table[SDL_SCANCODE_4] = Keycode::D4;
+ table[SDL_SCANCODE_5] = Keycode::D5;
+ table[SDL_SCANCODE_6] = Keycode::D6;
+ table[SDL_SCANCODE_7] = Keycode::D7;
+ table[SDL_SCANCODE_8] = Keycode::D8;
+ table[SDL_SCANCODE_9] = Keycode::D9;
+ table[SDL_SCANCODE_SEMICOLON] = Keycode::SEMICOLON;
+ table[SDL_SCANCODE_EQUALS] = Keycode::EQUAL;
+ table[SDL_SCANCODE_A] = Keycode::A;
+ table[SDL_SCANCODE_B] = Keycode::B;
+ table[SDL_SCANCODE_C] = Keycode::C;
+ table[SDL_SCANCODE_D] = Keycode::D;
+ table[SDL_SCANCODE_E] = Keycode::E;
+ table[SDL_SCANCODE_F] = Keycode::F;
+ table[SDL_SCANCODE_G] = Keycode::G;
+ table[SDL_SCANCODE_H] = Keycode::H;
+ table[SDL_SCANCODE_I] = Keycode::I;
+ table[SDL_SCANCODE_J] = Keycode::J;
+ table[SDL_SCANCODE_K] = Keycode::K;
+ table[SDL_SCANCODE_L] = Keycode::L;
+ table[SDL_SCANCODE_M] = Keycode::M;
+ table[SDL_SCANCODE_N] = Keycode::N;
+ table[SDL_SCANCODE_O] = Keycode::O;
+ table[SDL_SCANCODE_P] = Keycode::P;
+ table[SDL_SCANCODE_Q] = Keycode::Q;
+ table[SDL_SCANCODE_R] = Keycode::R;
+ table[SDL_SCANCODE_S] = Keycode::S;
+ table[SDL_SCANCODE_T] = Keycode::T;
+ table[SDL_SCANCODE_U] = Keycode::U;
+ table[SDL_SCANCODE_V] = Keycode::V;
+ table[SDL_SCANCODE_W] = Keycode::W;
+ table[SDL_SCANCODE_X] = Keycode::X;
+ table[SDL_SCANCODE_Y] = Keycode::Y;
+ table[SDL_SCANCODE_Z] = Keycode::Z;
+ table[SDL_SCANCODE_LEFTBRACKET] = Keycode::LEFT_BRACKET;
+ table[SDL_SCANCODE_BACKSLASH] = Keycode::BACKSLASH;
+ table[SDL_SCANCODE_RIGHTBRACKET] = Keycode::RIGHT_BRACKET;
+ table[SDL_SCANCODE_GRAVE] = Keycode::GRAVE_ACCENT;
+ table[SDL_SCANCODE_ESCAPE] = Keycode::ESCAPE;
+ table[SDL_SCANCODE_RETURN] = Keycode::ENTER;
+ table[SDL_SCANCODE_TAB] = Keycode::TAB;
+ table[SDL_SCANCODE_BACKSPACE] = Keycode::BACKSPACE;
+ table[SDL_SCANCODE_INSERT] = Keycode::INSERT;
+ table[SDL_SCANCODE_DELETE] = Keycode::DELETE;
+ table[SDL_SCANCODE_RIGHT] = Keycode::RIGHT;
+ table[SDL_SCANCODE_LEFT] = Keycode::LEFT;
+ table[SDL_SCANCODE_DOWN] = Keycode::DOWN;
+ table[SDL_SCANCODE_UP] = Keycode::UP;
+ table[SDL_SCANCODE_PAGEUP] = Keycode::PAGE_UP;
+ table[SDL_SCANCODE_PAGEDOWN] = Keycode::PAGE_DOWN;
+ table[SDL_SCANCODE_HOME] = Keycode::HOME;
+ table[SDL_SCANCODE_END] = Keycode::END;
+ table[SDL_SCANCODE_CAPSLOCK] = Keycode::CAPS_LOCK;
+ table[SDL_SCANCODE_SCROLLLOCK] = Keycode::SCROLL_LOCK;
+ table[SDL_SCANCODE_NUMLOCKCLEAR] = Keycode::NUM_LOCK;
+ table[SDL_SCANCODE_PRINTSCREEN] = Keycode::PRINT_SCREEN;
+ table[SDL_SCANCODE_PAUSE] = Keycode::PAUSE;
+ table[SDL_SCANCODE_F1] = Keycode::F1;
+ table[SDL_SCANCODE_F2] = Keycode::F2;
+ table[SDL_SCANCODE_F3] = Keycode::F3;
+ table[SDL_SCANCODE_F4] = Keycode::F4;
+ table[SDL_SCANCODE_F5] = Keycode::F5;
+ table[SDL_SCANCODE_F6] = Keycode::F6;
+ table[SDL_SCANCODE_F7] = Keycode::F7;
+ table[SDL_SCANCODE_F8] = Keycode::F8;
+ table[SDL_SCANCODE_F9] = Keycode::F9;
+ table[SDL_SCANCODE_F10] = Keycode::F10;
+ table[SDL_SCANCODE_F11] = Keycode::F11;
+ table[SDL_SCANCODE_F12] = Keycode::F12;
+ table[SDL_SCANCODE_KP_0] = Keycode::KP0;
+ table[SDL_SCANCODE_KP_1] = Keycode::KP1;
+ table[SDL_SCANCODE_KP_2] = Keycode::KP2;
+ table[SDL_SCANCODE_KP_3] = Keycode::KP3;
+ table[SDL_SCANCODE_KP_4] = Keycode::KP4;
+ table[SDL_SCANCODE_KP_5] = Keycode::KP5;
+ table[SDL_SCANCODE_KP_6] = Keycode::KP6;
+ table[SDL_SCANCODE_KP_7] = Keycode::KP7;
+ table[SDL_SCANCODE_KP_8] = Keycode::KP8;
+ table[SDL_SCANCODE_KP_9] = Keycode::KP9;
+ table[SDL_SCANCODE_LSHIFT] = Keycode::LEFT_SHIFT;
+ table[SDL_SCANCODE_LCTRL] = Keycode::LEFT_CONTROL;
+ table[SDL_SCANCODE_LALT] = Keycode::LEFT_ALT;
+ table[SDL_SCANCODE_LGUI] = Keycode::LEFT_SUPER;
+ table[SDL_SCANCODE_RSHIFT] = Keycode::RIGHT_SHIFT;
+ table[SDL_SCANCODE_RCTRL] = Keycode::RIGHT_CONTROL;
+ table[SDL_SCANCODE_RALT] = Keycode::RIGHT_ALT;
+ table[SDL_SCANCODE_RGUI] = Keycode::RIGHT_SUPER;
+ table[SDL_SCANCODE_MENU] = Keycode::MENU;
+
+ return table;
+ }();
+
+ if (sdl_key < 0 || sdl_key >= SDL_NUM_SCANCODES) {
+ return Keycode::NONE;
+ }
+
+ return LOOKUP_TABLE[sdl_key];
}
MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) {
- static const std::array<MouseButton, 5> MOUSE_BUTTON_LOOKUP_TABLE = [] {
- std::array<MouseButton, 5> table{};
- table.fill(MouseButton::NONE);
-
- table[SDL_BUTTON_LEFT] = MouseButton::LEFT_MOUSE;
- table[SDL_BUTTON_RIGHT] = MouseButton::RIGHT_MOUSE;
- table[SDL_BUTTON_MIDDLE] = MouseButton::MIDDLE_MOUSE;
- table[SDL_BUTTON_X1] = MouseButton::X1_MOUSE;
- table[SDL_BUTTON_X2] = MouseButton::X2_MOUSE;
-
- return table;
- }();
-
- if (sdl_button >= MOUSE_BUTTON_LOOKUP_TABLE.size()) {
- // Return NONE for invalid or unmapped button
- return MouseButton::NONE;
- }
+ static const std::array<MouseButton, 5> MOUSE_BUTTON_LOOKUP_TABLE = [] {
+ std::array<MouseButton, 5> table{};
+ table.fill(MouseButton::NONE);
+
+ table[SDL_BUTTON_LEFT] = MouseButton::LEFT_MOUSE;
+ table[SDL_BUTTON_RIGHT] = MouseButton::RIGHT_MOUSE;
+ table[SDL_BUTTON_MIDDLE] = MouseButton::MIDDLE_MOUSE;
+ table[SDL_BUTTON_X1] = MouseButton::X1_MOUSE;
+ table[SDL_BUTTON_X2] = MouseButton::X2_MOUSE;
+
+ return table;
+ }();
+
+ if (sdl_button >= MOUSE_BUTTON_LOOKUP_TABLE.size()) {
+ // Return NONE for invalid or unmapped button
+ return MouseButton::NONE;
+ }
- return MOUSE_BUTTON_LOOKUP_TABLE[sdl_button];
+ return MOUSE_BUTTON_LOOKUP_TABLE[sdl_button];
}
void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); }
void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); }
@@ -310,73 +309,63 @@ int SDLContext::get_height(const Texture & ctx) const {
}
void SDLContext::delay(int ms) const { SDL_Delay(ms); }
-std::vector<SDLContext::EventData> SDLContext::get_events(){
+std::vector<SDLContext::EventData> SDLContext::get_events() {
std::vector<SDLContext::EventData> event_list;
SDL_Event event;
while (SDL_PollEvent(&event)) {
- switch (event.type) {
- case SDL_QUIT:
- event_list.push_back(EventData{
+ switch (event.type) {
+ case SDL_QUIT:
+ event_list.push_back(EventData{
.event_type = SDLContext::Event::SHUTDOWN,
});
break;
- case SDL_KEYDOWN:
+ case SDL_KEYDOWN:
event_list.push_back(EventData{
.event_type = SDLContext::Event::KEYDOWN,
.key = sdl_to_keycode(event.key.keysym.scancode),
.key_repeat = (event.key.repeat != 0),
});
break;
- case SDL_KEYUP:
+ case SDL_KEYUP:
event_list.push_back(EventData{
.event_type = SDLContext::Event::KEYUP,
.key = sdl_to_keycode(event.key.keysym.scancode),
});
- break;
- case SDL_MOUSEBUTTONDOWN:
- {
- int x,y;
- SDL_GetMouseState(&x, &y);
- event_list.push_back(EventData{
- .event_type = SDLContext::Event::MOUSEDOWN,
- .mouse_button = sdl_to_mousebutton(event.button.button),
- .mouse_position = {event.button.x,event.button.y},
- });
- }
- break;
- case SDL_MOUSEBUTTONUP:
- {
- int x,y;
- SDL_GetMouseState(&x, &y);
- event_list.push_back(EventData{
- .event_type = SDLContext::Event::MOUSEUP,
- .mouse_button = sdl_to_mousebutton(event.button.button),
- .mouse_position = {event.button.x,event.button.y},
- });
- }
- break;
-
- case SDL_MOUSEMOTION:
- {
- event_list.push_back(EventData{
- .event_type = SDLContext::Event::MOUSEMOVE,
- .mouse_position = {event.motion.x,event.motion.y},
- .rel_mouse_move = {event.motion.xrel,event.motion.yrel}
- });
- }
- break;
-
- case SDL_MOUSEWHEEL:
- {
- event_list.push_back(EventData{
- .event_type = SDLContext::Event::MOUSEWHEEL,
- .mouse_position = {event.motion.x,event.motion.y},
- .wheel_delta = event.wheel.y,
- });
- }
- break;
- }
- }
+ break;
+ case SDL_MOUSEBUTTONDOWN: {
+ int x, y;
+ SDL_GetMouseState(&x, &y);
+ event_list.push_back(EventData{
+ .event_type = SDLContext::Event::MOUSEDOWN,
+ .mouse_button = sdl_to_mousebutton(event.button.button),
+ .mouse_position = {event.button.x, event.button.y},
+ });
+ } break;
+ case SDL_MOUSEBUTTONUP: {
+ int x, y;
+ SDL_GetMouseState(&x, &y);
+ event_list.push_back(EventData{
+ .event_type = SDLContext::Event::MOUSEUP,
+ .mouse_button = sdl_to_mousebutton(event.button.button),
+ .mouse_position = {event.button.x, event.button.y},
+ });
+ } break;
+
+ case SDL_MOUSEMOTION: {
+ event_list.push_back(
+ EventData{.event_type = SDLContext::Event::MOUSEMOVE,
+ .mouse_position = {event.motion.x, event.motion.y},
+ .rel_mouse_move = {event.motion.xrel, event.motion.yrel}});
+ } break;
+
+ case SDL_MOUSEWHEEL: {
+ event_list.push_back(EventData{
+ .event_type = SDLContext::Event::MOUSEWHEEL,
+ .mouse_position = {event.motion.x, event.motion.y},
+ .wheel_delta = event.wheel.y,
+ });
+ } break;
+ }
+ }
return event_list;
}
-
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 1b881d1..7ec11fd 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -6,15 +6,15 @@
#include <SDL2/SDL_video.h>
#include <cmath>
#include <functional>
-#include <utility>
#include <memory>
#include <string>
+#include <utility>
-#include "../api/Sprite.h"
+#include "../api/Event.h"
#include "../api/KeyCodes.h"
+#include "../api/Sprite.h"
#include "../api/Transform.h"
#include "../api/Vector2.h"
-#include "../api/Event.h"
#include "api/Camera.h"
#include "types.h"
@@ -36,7 +36,7 @@ class InputSystem;
class SDLContext {
public:
- enum Event{
+ enum Event {
NONE = 0,
MOUSEDOWN,
MOUSEUP,
@@ -52,9 +52,9 @@ public:
Keycode key = Keycode::NONE;
bool key_repeat = false;
MouseButton mouse_button = MouseButton::NONE;
- std::pair<int,int> mouse_position = {-1,-1};
+ std::pair<int, int> mouse_position = {-1, -1};
int wheel_delta = -1;
- std::pair<int,int> rel_mouse_move = {-1,-1};
+ std::pair<int, int> rel_mouse_move = {-1, -1};
};
/**
* \brief Gets the singleton instance of SDLContext.
@@ -75,11 +75,12 @@ private:
* \param running Reference to a boolean flag that controls the main loop.
*/
std::vector<SDLContext::EventData> get_events();
-
+
Keycode get_key();
Keycode get_mouse();
Keycode sdl_to_keycode(SDL_Keycode sdlKey);
MouseButton sdl_to_mousebutton(Uint8 sdl_button);
+
private:
//! Will only use get_ticks
friend class AnimatorSystem;
@@ -197,5 +198,4 @@ private:
SDL_Rect viewport = {0, 0, 640, 480};
};
-
} // namespace crepe
diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp
index 1b455cd..7f7f9ec 100644
--- a/src/crepe/system/InputSystem.cpp
+++ b/src/crepe/system/InputSystem.cpp
@@ -1,154 +1,155 @@
-#include "ComponentManager.h"
-#include "../api/EventManager.h"
#include "../api/Event.h"
-
+#include "../api/EventManager.h"
+#include "ComponentManager.h"
#include "system/InputSystem.h"
using namespace crepe;
void InputSystem::update() {
- EventManager &event_mgr = EventManager::get_instance();
- std::vector<SDLContext::EventData> event_list = SDLContext::get_instance().get_events();
-
- for (const SDLContext::EventData &event : event_list) {
- switch (event.event_type) {
- case SDLContext::Event::KEYDOWN: {
- event_mgr.queue_event<KeyPressEvent>(KeyPressEvent{
- .repeat = event.key_repeat,
- .key = event.key,
- });
- break;
- }
- case SDLContext::Event::KEYUP: {
- event_mgr.queue_event<KeyReleaseEvent>(KeyReleaseEvent{
- .key = event.key,
- });
- break;
- }
- case SDLContext::Event::MOUSEDOWN: {
- event_mgr.queue_event<MousePressEvent>(MousePressEvent{
- .mouse_x = event.mouse_position.first,
- .mouse_y = event.mouse_position.second,
- .button = event.mouse_button,
- });
- last_mouse_down_position = event.mouse_position;
- last_mouse_button = event.mouse_button;
- break;
- }
- case SDLContext::Event::MOUSEUP: {
- MouseReleaseEvent mouse_release_event = MouseReleaseEvent{
- .mouse_x = event.mouse_position.first,
- .mouse_y = event.mouse_position.second,
- .button = event.mouse_button,
- };
- event_mgr.queue_event<MouseReleaseEvent>(mouse_release_event);
-
- // Calculate deltas for click detection
- int delta_x = event.mouse_position.first - last_mouse_down_position.first;
- int delta_y = event.mouse_position.second - last_mouse_down_position.second;
-
- if (last_mouse_button == event.mouse_button &&
- std::abs(delta_x) <= click_tolerance &&
- std::abs(delta_y) <= click_tolerance) {
- event_mgr.queue_event<MouseClickEvent>(MouseClickEvent{
- .mouse_x = event.mouse_position.first,
- .mouse_y = event.mouse_position.second,
- .button = event.mouse_button,
- });
-
- handle_click(event);
- }
- break;
- }
- case SDLContext::Event::MOUSEMOVE: {
- event_mgr.queue_event<MouseMoveEvent>(MouseMoveEvent{
- .mouse_x = event.mouse_position.first,
- .mouse_y = event.mouse_position.second,
- .rel_x = event.rel_mouse_move.first,
- .rel_y = event.rel_mouse_move.second,
- });
- handle_move(event);
- break;
- }
- case SDLContext::Event::MOUSEWHEEL: {
- event_mgr.queue_event<MouseScrollEvent>(MouseScrollEvent{
- .scroll_x = event.wheel_delta,
- .scroll_y = 0,
- .direction = event.wheel_delta,
- });
- break;
- }
- case SDLContext::Event::SHUTDOWN: {
- event_mgr.queue_event<ShutDownEvent>(ShutDownEvent{});
- break;
- }
- default:
- break;
- }
- }
+ EventManager & event_mgr = EventManager::get_instance();
+ std::vector<SDLContext::EventData> event_list = SDLContext::get_instance().get_events();
+
+ for (const SDLContext::EventData & event : event_list) {
+ switch (event.event_type) {
+ case SDLContext::Event::KEYDOWN: {
+ event_mgr.queue_event<KeyPressEvent>(KeyPressEvent{
+ .repeat = event.key_repeat,
+ .key = event.key,
+ });
+ break;
+ }
+ case SDLContext::Event::KEYUP: {
+ event_mgr.queue_event<KeyReleaseEvent>(KeyReleaseEvent{
+ .key = event.key,
+ });
+ break;
+ }
+ case SDLContext::Event::MOUSEDOWN: {
+ event_mgr.queue_event<MousePressEvent>(MousePressEvent{
+ .mouse_x = event.mouse_position.first,
+ .mouse_y = event.mouse_position.second,
+ .button = event.mouse_button,
+ });
+ last_mouse_down_position = event.mouse_position;
+ last_mouse_button = event.mouse_button;
+ break;
+ }
+ case SDLContext::Event::MOUSEUP: {
+ MouseReleaseEvent mouse_release_event = MouseReleaseEvent{
+ .mouse_x = event.mouse_position.first,
+ .mouse_y = event.mouse_position.second,
+ .button = event.mouse_button,
+ };
+ event_mgr.queue_event<MouseReleaseEvent>(mouse_release_event);
+
+ // Calculate deltas for click detection
+ int delta_x = event.mouse_position.first - last_mouse_down_position.first;
+ int delta_y = event.mouse_position.second - last_mouse_down_position.second;
+
+ if (last_mouse_button == event.mouse_button
+ && std::abs(delta_x) <= click_tolerance
+ && std::abs(delta_y) <= click_tolerance) {
+ event_mgr.queue_event<MouseClickEvent>(MouseClickEvent{
+ .mouse_x = event.mouse_position.first,
+ .mouse_y = event.mouse_position.second,
+ .button = event.mouse_button,
+ });
+
+ handle_click(event);
+ }
+ break;
+ }
+ case SDLContext::Event::MOUSEMOVE: {
+ event_mgr.queue_event<MouseMoveEvent>(MouseMoveEvent{
+ .mouse_x = event.mouse_position.first,
+ .mouse_y = event.mouse_position.second,
+ .rel_x = event.rel_mouse_move.first,
+ .rel_y = event.rel_mouse_move.second,
+ });
+ handle_move(event);
+ break;
+ }
+ case SDLContext::Event::MOUSEWHEEL: {
+ event_mgr.queue_event<MouseScrollEvent>(MouseScrollEvent{
+ .scroll_x = event.wheel_delta,
+ .scroll_y = 0,
+ .direction = event.wheel_delta,
+ });
+ break;
+ }
+ case SDLContext::Event::SHUTDOWN: {
+ event_mgr.queue_event<ShutDownEvent>(ShutDownEvent{});
+ break;
+ }
+ default:
+ break;
+ }
+ }
}
-void InputSystem::handle_move(const SDLContext::EventData &event_data) {
- ComponentManager &mgr = this->component_manager;
+void InputSystem::handle_move(const SDLContext::EventData & event_data) {
+ ComponentManager & mgr = this->component_manager;
- std::vector<std::reference_wrapper<Button>> buttons = mgr.get_components_by_type<Button>();
- std::vector<std::reference_wrapper<Transform>> transforms = mgr.get_components_by_type<Transform>();
+ std::vector<std::reference_wrapper<Button>> buttons = mgr.get_components_by_type<Button>();
+ std::vector<std::reference_wrapper<Transform>> transforms
+ = mgr.get_components_by_type<Transform>();
- for (Button &button : buttons) {
- Transform *transform = find_transform_for_button(button, transforms);
- if (!transform)
- continue;
+ for (Button & button : buttons) {
+ Transform * transform = find_transform_for_button(button, transforms);
+ if (!transform) continue;
- if (button.interactable && is_mouse_inside_button(event_data, button, *transform)) {
- button.hover = true;
- } else {
- button.hover = false;
- }
- }
+ if (button.interactable && is_mouse_inside_button(event_data, button, *transform)) {
+ button.hover = true;
+ } else {
+ button.hover = false;
+ }
+ }
}
-void InputSystem::handle_click(const SDLContext::EventData &event_data) {
- ComponentManager &mgr = this->component_manager;
+void InputSystem::handle_click(const SDLContext::EventData & event_data) {
+ ComponentManager & mgr = this->component_manager;
- std::vector<std::reference_wrapper<Button>> buttons = mgr.get_components_by_type<Button>();
- std::vector<std::reference_wrapper<Transform>> transforms = mgr.get_components_by_type<Transform>();
+ std::vector<std::reference_wrapper<Button>> buttons = mgr.get_components_by_type<Button>();
+ std::vector<std::reference_wrapper<Transform>> transforms
+ = mgr.get_components_by_type<Transform>();
- for (Button &button : buttons) {
- Transform *transform = find_transform_for_button(button, transforms);
- if (!transform)
- continue;
+ for (Button & button : buttons) {
+ Transform * transform = find_transform_for_button(button, transforms);
+ if (!transform) continue;
- if (button.interactable && is_mouse_inside_button(event_data, button, *transform)) {
- handle_button_press(button);
- }
- }
+ if (button.interactable && is_mouse_inside_button(event_data, button, *transform)) {
+ handle_button_press(button);
+ }
+ }
}
-Transform *InputSystem::find_transform_for_button(Button &button, std::vector<std::reference_wrapper<Transform>> &transforms) {
- for (Transform &transform : transforms) {
- if (button.game_object_id == transform.game_object_id) {
- return &transform;
- }
- }
- return nullptr;
+Transform * InputSystem::find_transform_for_button(
+ Button & button, std::vector<std::reference_wrapper<Transform>> & transforms) {
+ for (Transform & transform : transforms) {
+ if (button.game_object_id == transform.game_object_id) {
+ return &transform;
+ }
+ }
+ return nullptr;
}
-bool InputSystem::is_mouse_inside_button(const SDLContext::EventData &event_data, const Button &button, const Transform &transform) {
- return event_data.mouse_position.first >= transform.position.x &&
- event_data.mouse_position.first <= transform.position.x + button.width &&
- event_data.mouse_position.second >= transform.position.y &&
- event_data.mouse_position.second <= transform.position.y + button.height;
+bool InputSystem::is_mouse_inside_button(const SDLContext::EventData & event_data,
+ const Button & button, const Transform & transform) {
+ return event_data.mouse_position.first >= transform.position.x
+ && event_data.mouse_position.first <= transform.position.x + button.width
+ && event_data.mouse_position.second >= transform.position.y
+ && event_data.mouse_position.second <= transform.position.y + button.height;
}
-void InputSystem::handle_button_press(Button &button) {
- if (button.is_toggle) {
- if (!button.is_pressed && button.on_click) {
- button.on_click();
- }
- button.is_pressed = !button.is_pressed;
- } else if (button.on_click) {
- button.on_click();
- }
+void InputSystem::handle_button_press(Button & button) {
+ if (button.is_toggle) {
+ if (!button.is_pressed && button.on_click) {
+ button.on_click();
+ }
+ button.is_pressed = !button.is_pressed;
+ } else if (button.on_click) {
+ button.on_click();
+ }
}
diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h
index 672f225..5f94446 100644
--- a/src/crepe/system/InputSystem.h
+++ b/src/crepe/system/InputSystem.h
@@ -2,8 +2,8 @@
#include "System.h"
-#include "../api/Event.h"
#include "../api/Button.h"
+#include "../api/Event.h"
#include "../api/Transform.h"
#include "../facade/SDLContext.h"
@@ -19,64 +19,67 @@ namespace crepe {
*/
class InputSystem : public System {
public:
- using System::System;
+ using System::System;
- /**
+ /**
* \brief Updates the system, processing all input events.
* This method processes all events and triggers corresponding actions.
*/
- void update() override;
+ void update() override;
private:
- //! Stores the last position of the mouse when the button was pressed.
- std::pair<int, int> last_mouse_down_position{-1, -1};
+ //! Stores the last position of the mouse when the button was pressed.
+ std::pair<int, int> last_mouse_down_position{-1, -1};
- //! Stores the last mouse button pressed.
- MouseButton last_mouse_button = MouseButton::NONE;
+ //! Stores the last mouse button pressed.
+ MouseButton last_mouse_button = MouseButton::NONE;
- //! The tolerance in pixels for detecting a mouse click.
- const int click_tolerance = 5;
+ //! The tolerance in pixels for detecting a mouse click.
+ const int click_tolerance = 5;
- /**
+ /**
* \brief Handles the click event.
* \param eventData The event data containing information about the mouse click.
*
* This method processes the mouse click event and triggers the corresponding button action.
*/
- void handle_click(const SDLContext::EventData &eventData);
+ void handle_click(const SDLContext::EventData & eventData);
- /**
+ /**
* \brief Handles the mouse movement event.
* \param eventData The event data containing information about the mouse movement.
*
* This method processes the mouse movement event and updates the button hover state.
*/
- void handle_move(const SDLContext::EventData &eventData);
+ void handle_move(const SDLContext::EventData & eventData);
- /**
+ /**
* \brief Finds the transform component associated with a button.
* \param button The button to find the associated transform for.
* \param transforms A list of transforms to search through.
* \return A pointer to the transform of the button, or nullptr if not found.
*/
- Transform* find_transform_for_button(Button &button, std::vector<std::reference_wrapper<Transform>> &transforms);
+ Transform *
+ find_transform_for_button(Button & button,
+ std::vector<std::reference_wrapper<Transform>> & transforms);
- /**
+ /**
* \brief Checks if the mouse position is inside the bounds of the button.
* \param eventData The event data containing the mouse position.
* \param button The button to check.
* \param transform The transform component of the button.
* \return True if the mouse is inside the button, false otherwise.
*/
- bool is_mouse_inside_button(const SDLContext::EventData &eventData, const Button &button, const Transform &transform);
+ bool is_mouse_inside_button(const SDLContext::EventData & eventData, const Button & button,
+ const Transform & transform);
- /**
+ /**
* \brief Handles the button press event, calling the on_click callback if necessary.
* \param button The button being pressed.
*
* This method triggers the on_click action for the button when it is pressed.
*/
- void handle_button_press(Button &button);
+ void handle_button_press(Button & button);
};
} // namespace crepe
diff --git a/src/example/gameloop.cpp b/src/example/gameloop.cpp
index d45b3ce..b8ee9d8 100644
--- a/src/example/gameloop.cpp
+++ b/src/example/gameloop.cpp
@@ -1,17 +1,16 @@
-#include <iostream>
#include "crepe/api/LoopManager.h"
#include <crepe/api/EventManager.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/IKeyListener.h>
#include <crepe/api/IMouseListener.h>
+#include <iostream>
using namespace crepe;
class TestKeyListener : public IKeyListener {
public:
bool on_key_pressed(const KeyPressEvent & event) override {
std::cout << "TestKeyListener: Key Pressed - Code: " << static_cast<int>(event.key)
<< std::endl;
- if(event.key == Keycode::ESCAPE){
-
+ if (event.key == Keycode::ESCAPE) {
}
return false;
}
@@ -21,20 +20,19 @@ public:
return false;
}
};
-bool on_key_pressed(const KeyPressEvent & event){
- std::cout << "TestKeyListener: Key Pressed - Code: " << static_cast<int>(event.key)
- << std::endl;
- if(event.key == Keycode::ESCAPE){
- return true;
- }
- return false;
+bool on_key_pressed(const KeyPressEvent & event) {
+ std::cout << "TestKeyListener: Key Pressed - Code: " << static_cast<int>(event.key)
+ << std::endl;
+ if (event.key == Keycode::ESCAPE) {
+ return true;
}
+ return false;
+}
int main() {
LoopManager gameloop;
TestKeyListener key_listener;
EventManager::get_instance().subscribe<KeyPressEvent>(on_key_pressed);
gameloop.start();
-
-
+
return 1;
}
diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp
index 4af0830..c75bcc1 100644
--- a/src/test/EventTest.cpp
+++ b/src/test/EventTest.cpp
@@ -154,44 +154,44 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) {
}
TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) {
- EventManager & event_manager = EventManager::get_instance();
- bool triggered1 = false;
- bool triggered2 = false;
- int test_channel = 1;
-
- // Adjusted to use KeyPressEvent with repeat as the first variable
- EventHandler<KeyPressEvent> key_handler1 = [&](const KeyPressEvent & e) {
- triggered1 = true;
- EXPECT_EQ(e.repeat, false); // Expecting repeat to be false
- EXPECT_EQ(e.key, Keycode::A); // Adjust expected key code
- return false; // Allows propagation
- };
-
- EventHandler<KeyPressEvent> key_handler2 = [&](const KeyPressEvent & e) {
- triggered2 = true;
- EXPECT_EQ(e.repeat, false); // Expecting repeat to be false
- EXPECT_EQ(e.key, Keycode::A); // Adjust expected key code
- return false; // Allows propagation
- };
-
- // Subscribe handlers to KeyPressEvent
- event_manager.subscribe<KeyPressEvent>(key_handler1);
- event_manager.subscribe<KeyPressEvent>(key_handler2, test_channel);
-
- // Queue a KeyPressEvent instead of KeyDownEvent
- event_manager.queue_event<KeyPressEvent>(
- KeyPressEvent{.repeat = false, .key = Keycode::A}); // Adjust event with repeat flag first
-
- event_manager.queue_event<KeyPressEvent>(
- KeyPressEvent{.repeat = false, .key = Keycode::A}, // Adjust event for second subscription
- test_channel);
-
- event_manager.dispatch_events();
-
- EXPECT_TRUE(triggered1);
- EXPECT_TRUE(triggered2);
-}
+ EventManager & event_manager = EventManager::get_instance();
+ bool triggered1 = false;
+ bool triggered2 = false;
+ int test_channel = 1;
+ // Adjusted to use KeyPressEvent with repeat as the first variable
+ EventHandler<KeyPressEvent> key_handler1 = [&](const KeyPressEvent & e) {
+ triggered1 = true;
+ EXPECT_EQ(e.repeat, false); // Expecting repeat to be false
+ EXPECT_EQ(e.key, Keycode::A); // Adjust expected key code
+ return false; // Allows propagation
+ };
+
+ EventHandler<KeyPressEvent> key_handler2 = [&](const KeyPressEvent & e) {
+ triggered2 = true;
+ EXPECT_EQ(e.repeat, false); // Expecting repeat to be false
+ EXPECT_EQ(e.key, Keycode::A); // Adjust expected key code
+ return false; // Allows propagation
+ };
+
+ // Subscribe handlers to KeyPressEvent
+ event_manager.subscribe<KeyPressEvent>(key_handler1);
+ event_manager.subscribe<KeyPressEvent>(key_handler2, test_channel);
+
+ // Queue a KeyPressEvent instead of KeyDownEvent
+ event_manager.queue_event<KeyPressEvent>(KeyPressEvent{
+ .repeat = false, .key = Keycode::A}); // Adjust event with repeat flag first
+
+ event_manager.queue_event<KeyPressEvent>(
+ KeyPressEvent{.repeat = false,
+ .key = Keycode::A}, // Adjust event for second subscription
+ test_channel);
+
+ event_manager.dispatch_events();
+
+ EXPECT_TRUE(triggered1);
+ EXPECT_TRUE(triggered2);
+}
TEST_F(EventManagerTest, EventManagerTest_unsubscribe) {
EventManager & event_manager = EventManager::get_instance();
diff --git a/src/test/inputTest.cpp b/src/test/inputTest.cpp
index 8f3eb48..3a9d341 100644
--- a/src/test/inputTest.cpp
+++ b/src/test/inputTest.cpp
@@ -1,18 +1,17 @@
#include <gtest/gtest.h>
#define protected public
-#include <SDL2/SDL.h>
-#include <SDL2/SDL_keycode.h>
-#include "system/InputSystem.h"
-#include <crepe/ComponentManager.h>
#include "api/EventManager.h"
#include "api/KeyCodes.h"
-#include <gmock/gmock.h>
+#include "system/InputSystem.h"
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_keycode.h>
#include <crepe/ComponentManager.h>
+#include <crepe/api/Button.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Metadata.h>
#include <crepe/api/Transform.h>
#include <crepe/api/Vector2.h>
-#include <crepe/api/Button.h>
+#include <gmock/gmock.h>
using namespace std;
using namespace std::chrono_literals;
@@ -20,77 +19,75 @@ using namespace crepe;
class InputTest : public ::testing::Test {
public:
- ComponentManager mgr{};
- InputSystem input_system{mgr};
+ ComponentManager mgr{};
+ InputSystem input_system{mgr};
- EventManager& event_manager = EventManager::get_instance();
+ EventManager & event_manager = EventManager::get_instance();
protected:
- void SetUp() override {
- event_manager.clear();
- }
-
- void simulate_mouse_click(int mouse_x, int mouse_y, Uint8 mouse_button) {
- SDL_Event event;
-
- // Simulate Mouse Button Down event
- SDL_zero(event);
- event.type = SDL_MOUSEBUTTONDOWN;
- event.button.x = mouse_x;
- event.button.y = mouse_y;
- event.button.button = mouse_button;
- SDL_PushEvent(&event);
- SDL_zero(event);
- event.type = SDL_MOUSEBUTTONUP;
- event.button.x = mouse_x;
- event.button.y = mouse_y;
- event.button.button = mouse_button;
- SDL_PushEvent(&event);
- }
+ void SetUp() override { event_manager.clear(); }
+
+ void simulate_mouse_click(int mouse_x, int mouse_y, Uint8 mouse_button) {
+ SDL_Event event;
+
+ // Simulate Mouse Button Down event
+ SDL_zero(event);
+ event.type = SDL_MOUSEBUTTONDOWN;
+ event.button.x = mouse_x;
+ event.button.y = mouse_y;
+ event.button.button = mouse_button;
+ SDL_PushEvent(&event);
+ SDL_zero(event);
+ event.type = SDL_MOUSEBUTTONUP;
+ event.button.x = mouse_x;
+ event.button.y = mouse_y;
+ event.button.button = mouse_button;
+ SDL_PushEvent(&event);
+ }
};
TEST_F(InputTest, MouseDown) {
- bool mouse_triggered = false;
- EventHandler<MousePressEvent> on_mouse_down = [&](const MousePressEvent& event) {
- mouse_triggered = true;
- EXPECT_EQ(event.mouse_x, 10);
- EXPECT_EQ(event.mouse_y, 10);
- EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE);
- return false;
- };
- event_manager.subscribe<MousePressEvent>(on_mouse_down);
-
- SDL_Event event;
- SDL_zero(event);
- event.type = SDL_MOUSEBUTTONDOWN;
- event.button.x = 10;
- event.button.y = 10;
- event.button.button = SDL_BUTTON_LEFT;
- SDL_PushEvent(&event);
+ bool mouse_triggered = false;
+ EventHandler<MousePressEvent> on_mouse_down = [&](const MousePressEvent & event) {
+ mouse_triggered = true;
+ EXPECT_EQ(event.mouse_x, 10);
+ EXPECT_EQ(event.mouse_y, 10);
+ EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE);
+ return false;
+ };
+ event_manager.subscribe<MousePressEvent>(on_mouse_down);
+
+ SDL_Event event;
+ SDL_zero(event);
+ event.type = SDL_MOUSEBUTTONDOWN;
+ event.button.x = 10;
+ event.button.y = 10;
+ event.button.button = SDL_BUTTON_LEFT;
+ SDL_PushEvent(&event);
input_system.update();
event_manager.dispatch_events();
- EXPECT_TRUE(mouse_triggered);
+ EXPECT_TRUE(mouse_triggered);
}
TEST_F(InputTest, MouseUp) {
bool function_triggered = false;
- EventHandler<MouseReleaseEvent> on_mouse_release = [&](const MouseReleaseEvent& e) {
- // Handle the mouse click event here
+ EventHandler<MouseReleaseEvent> on_mouse_release = [&](const MouseReleaseEvent & e) {
+ // Handle the mouse click event here
function_triggered = true;
- EXPECT_EQ(e.mouse_x, 10);
- EXPECT_EQ(e.mouse_y, 10);
- EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE);
- return false;
- };
- event_manager.subscribe<MouseReleaseEvent>(on_mouse_release);
-
- SDL_Event event;
- SDL_zero(event);
- event.type = SDL_MOUSEBUTTONUP;
- event.button.x = 10;
- event.button.y = 10;
- event.button.button = SDL_BUTTON_LEFT;
- SDL_PushEvent(&event);
+ EXPECT_EQ(e.mouse_x, 10);
+ EXPECT_EQ(e.mouse_y, 10);
+ EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE);
+ return false;
+ };
+ event_manager.subscribe<MouseReleaseEvent>(on_mouse_release);
+
+ SDL_Event event;
+ SDL_zero(event);
+ event.type = SDL_MOUSEBUTTONUP;
+ event.button.x = 10;
+ event.button.y = 10;
+ event.button.button = SDL_BUTTON_LEFT;
+ SDL_PushEvent(&event);
input_system.update();
event_manager.dispatch_events();
EXPECT_TRUE(function_triggered);
@@ -98,150 +95,148 @@ TEST_F(InputTest, MouseUp) {
TEST_F(InputTest, MouseMove) {
bool function_triggered = false;
- EventHandler<MouseMoveEvent> on_mouse_move = [&](const MouseMoveEvent& e) {
- // Handle the mouse click event here
+ EventHandler<MouseMoveEvent> on_mouse_move = [&](const MouseMoveEvent & e) {
+ // Handle the mouse click event here
function_triggered = true;
- EXPECT_EQ(e.mouse_x, 10);
- EXPECT_EQ(e.mouse_y, 10);
+ EXPECT_EQ(e.mouse_x, 10);
+ EXPECT_EQ(e.mouse_y, 10);
EXPECT_EQ(e.rel_x, 10);
- EXPECT_EQ(e.rel_y, 10);
- return false;
- };
- event_manager.subscribe<MouseMoveEvent>(on_mouse_move);
-
- SDL_Event event;
- SDL_zero(event);
- event.type = SDL_MOUSEMOTION;
- event.motion.x = 10;
- event.motion.y = 10;
+ EXPECT_EQ(e.rel_y, 10);
+ return false;
+ };
+ event_manager.subscribe<MouseMoveEvent>(on_mouse_move);
+
+ SDL_Event event;
+ SDL_zero(event);
+ event.type = SDL_MOUSEMOTION;
+ event.motion.x = 10;
+ event.motion.y = 10;
event.motion.xrel = 10;
event.motion.yrel = 10;
- SDL_PushEvent(&event);
+ SDL_PushEvent(&event);
input_system.update();
event_manager.dispatch_events();
EXPECT_TRUE(function_triggered);
}
TEST_F(InputTest, KeyDown) {
- bool function_triggered = false;
-
- // Define event handler for KeyPressEvent
- EventHandler<KeyPressEvent> on_key_press = [&](const KeyPressEvent& event) {
- function_triggered = true;
- EXPECT_EQ(event.key, Keycode::B); // Validate the key is 'B'
- EXPECT_EQ(event.repeat, true); // Validate repeat flag
- return false;
- };
-
- event_manager.subscribe<KeyPressEvent>(on_key_press);
-
- // Simulate SDL_KEYDOWN event
- SDL_Event test_event;
- SDL_zero(test_event);
- test_event.type = SDL_KEYDOWN; // Key down event
- test_event.key.keysym.scancode = SDL_SCANCODE_B; // Set scancode for 'B'
- test_event.key.repeat = 1; // Set repeat flag
- SDL_PushEvent(&test_event);
-
- input_system.update(); // Process the event
- event_manager.dispatch_events(); // Dispatch events to handlers
-
- EXPECT_TRUE(function_triggered); // Check if the handler was triggered
+ bool function_triggered = false;
+
+ // Define event handler for KeyPressEvent
+ EventHandler<KeyPressEvent> on_key_press = [&](const KeyPressEvent & event) {
+ function_triggered = true;
+ EXPECT_EQ(event.key, Keycode::B); // Validate the key is 'B'
+ EXPECT_EQ(event.repeat, true); // Validate repeat flag
+ return false;
+ };
+
+ event_manager.subscribe<KeyPressEvent>(on_key_press);
+
+ // Simulate SDL_KEYDOWN event
+ SDL_Event test_event;
+ SDL_zero(test_event);
+ test_event.type = SDL_KEYDOWN; // Key down event
+ test_event.key.keysym.scancode = SDL_SCANCODE_B; // Set scancode for 'B'
+ test_event.key.repeat = 1; // Set repeat flag
+ SDL_PushEvent(&test_event);
+
+ input_system.update(); // Process the event
+ event_manager.dispatch_events(); // Dispatch events to handlers
+
+ EXPECT_TRUE(function_triggered); // Check if the handler was triggered
}
TEST_F(InputTest, KeyUp) {
bool function_triggered = false;
- EventHandler<KeyReleaseEvent> on_key_release = [&](const KeyReleaseEvent& event) {
- function_triggered = true;
- EXPECT_EQ(event.key, Keycode::B);
- return false;
- };
- event_manager.subscribe<KeyReleaseEvent>(on_key_release);
-
- SDL_Event event;
- SDL_zero(event);
- event.type = SDL_KEYUP;
- event.key.keysym.scancode = SDL_SCANCODE_B;
- SDL_PushEvent(&event);
+ EventHandler<KeyReleaseEvent> on_key_release = [&](const KeyReleaseEvent & event) {
+ function_triggered = true;
+ EXPECT_EQ(event.key, Keycode::B);
+ return false;
+ };
+ event_manager.subscribe<KeyReleaseEvent>(on_key_release);
+
+ SDL_Event event;
+ SDL_zero(event);
+ event.type = SDL_KEYUP;
+ event.key.keysym.scancode = SDL_SCANCODE_B;
+ SDL_PushEvent(&event);
input_system.update();
event_manager.dispatch_events();
EXPECT_TRUE(function_triggered);
}
TEST_F(InputTest, MouseClick) {
- bool on_click_triggered = false;
- EventHandler<MouseClickEvent> on_mouse_click = [&](const MouseClickEvent& event) {
- on_click_triggered = true;
- EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE);
- EXPECT_EQ(event.mouse_x, 10);
- EXPECT_EQ(event.mouse_y, 10);
- return false;
- };
- event_manager.subscribe<MouseClickEvent>(on_mouse_click);
-
- this->simulate_mouse_click(10,10,SDL_BUTTON_LEFT);
+ bool on_click_triggered = false;
+ EventHandler<MouseClickEvent> on_mouse_click = [&](const MouseClickEvent & event) {
+ on_click_triggered = true;
+ EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE);
+ EXPECT_EQ(event.mouse_x, 10);
+ EXPECT_EQ(event.mouse_y, 10);
+ return false;
+ };
+ event_manager.subscribe<MouseClickEvent>(on_mouse_click);
+
+ this->simulate_mouse_click(10, 10, SDL_BUTTON_LEFT);
input_system.update();
event_manager.dispatch_events();
- EXPECT_TRUE(on_click_triggered);
+ EXPECT_TRUE(on_click_triggered);
}
TEST_F(InputTest, testButtonClick) {
- GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
- auto& button = obj.add_component<Button>();
+ GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
+ auto & button = obj.add_component<Button>();
bool button_clicked = false;
bool hover = false;
- button.active = true;
+ button.active = true;
button.interactable = true;
- button.width = 100;
- button.height = 100;
- std::function<void()> on_click = [&]() {
- button_clicked = true;
- };
+ button.width = 100;
+ button.height = 100;
+ std::function<void()> on_click = [&]() { button_clicked = true; };
button.on_click = on_click;
- button.is_pressed = false;
- button.is_toggle = false;
- this->simulate_mouse_click(101,101, SDL_BUTTON_LEFT);
+ button.is_pressed = false;
+ button.is_toggle = false;
+ this->simulate_mouse_click(101, 101, SDL_BUTTON_LEFT);
input_system.update();
event_manager.dispatch_events();
EXPECT_FALSE(button_clicked);
- this->simulate_mouse_click(10,10, SDL_BUTTON_LEFT);
+ this->simulate_mouse_click(10, 10, SDL_BUTTON_LEFT);
input_system.update();
event_manager.dispatch_events();
EXPECT_TRUE(button_clicked);
}
TEST_F(InputTest, testButtonHover) {
- GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
- auto& button = obj.add_component<Button>();
+ GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
+ auto & button = obj.add_component<Button>();
bool button_clicked = false;
- button.active = true;
+ button.active = true;
button.interactable = true;
- button.width = 100;
- button.height = 100;
- button.is_pressed = false;
- button.is_toggle = false;
+ button.width = 100;
+ button.height = 100;
+ button.is_pressed = false;
+ button.is_toggle = false;
//mouse not on button
SDL_Event event;
- SDL_zero(event);
- event.type = SDL_MOUSEMOTION;
- event.motion.x = 200;
- event.motion.y = 200;
+ SDL_zero(event);
+ event.type = SDL_MOUSEMOTION;
+ event.motion.x = 200;
+ event.motion.y = 200;
event.motion.xrel = 10;
event.motion.yrel = 10;
- SDL_PushEvent(&event);
+ SDL_PushEvent(&event);
input_system.update();
event_manager.dispatch_events();
EXPECT_FALSE(button.hover);
//mouse on button
SDL_Event hover_event;
- SDL_zero(hover_event);
- hover_event.type = SDL_MOUSEMOTION;
- hover_event.motion.x = 10;
- hover_event.motion.y = 10;
+ SDL_zero(hover_event);
+ hover_event.type = SDL_MOUSEMOTION;
+ hover_event.motion.x = 10;
+ hover_event.motion.y = 10;
hover_event.motion.xrel = 10;
hover_event.motion.yrel = 10;
- SDL_PushEvent(&hover_event);
+ SDL_PushEvent(&hover_event);
input_system.update();
event_manager.dispatch_events();
EXPECT_TRUE(button.hover);
diff --git a/src/test/loopTimerTest.cpp b/src/test/loopTimerTest.cpp
index a3b1646..327e577 100644
--- a/src/test/loopTimerTest.cpp
+++ b/src/test/loopTimerTest.cpp
@@ -1,7 +1,7 @@
#define private public
#define protected public
-#include "api/LoopManager.h"
#include "api/LoopTimer.h"
+#include "api/LoopManager.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -11,22 +11,19 @@ using namespace crepe;
class LoopTimerTest : public ::testing::Test {
public:
-LoopTimer loop_timer = LoopTimer::get_instance();
+ LoopTimer loop_timer = LoopTimer::get_instance();
+
protected:
- void SetUp() override {
- loop_timer.start();
- }
+ void SetUp() override { loop_timer.start(); }
- void TearDown() override {
-
- }
+ void TearDown() override {}
};
TEST_F(LoopTimerTest, TestDeltaTime) {
- auto start_time = std::chrono::steady_clock::now();
-
- loop_timer.update();
- double delta_time = loop_timer.get_delta_time();
+ auto start_time = std::chrono::steady_clock::now();
+
+ loop_timer.update();
+ double delta_time = loop_timer.get_delta_time();
- auto elapsed_time = std::chrono::steady_clock::now() - start_time;
- EXPECT_LE(delta_time, std::chrono::duration<double>(elapsed_time).count());
+ auto elapsed_time = std::chrono::steady_clock::now() - start_time;
+ EXPECT_LE(delta_time, std::chrono::duration<double>(elapsed_time).count());
}