aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/crepe/api/Event.h2
-rw-r--r--src/crepe/facade/SDLContext.cpp13
-rw-r--r--src/crepe/facade/SDLContext.h20
-rw-r--r--src/example/button.cpp51
-rw-r--r--src/test/InputTest.cpp1
5 files changed, 42 insertions, 45 deletions
diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h
index d353a5b..4e57b45 100644
--- a/src/crepe/api/Event.h
+++ b/src/crepe/api/Event.h
@@ -3,7 +3,7 @@
#include <string>
-#include "KeyCodes.h"
+#include "api/KeyCodes.h"
#include "types.h"
namespace crepe {
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index bb65e3b..e5b0284 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -6,7 +6,6 @@
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
-#include <SDL2/SDL_video.h>
#include <array>
#include <cmath>
#include <cstddef>
@@ -82,22 +81,18 @@ Keycode SDLContext::sdl_to_keycode(SDL_Scancode sdl_key) {
return LOOKUP_TABLE.at(sdl_key);
}
-const keyboard_state_t& SDLContext::get_keyboard_state() const{
- return this->keyboard_state;
-}
-
-void SDLContext::update_keyboard_state() {
- // Array to hold the key states (true if pressed, false if not)
+const keyboard_state_t& SDLContext::get_keyboard_state(){
SDL_PumpEvents();
const Uint8 * current_state = SDL_GetKeyboardState(nullptr);
for (int i = 0; i < SDL_NUM_SCANCODES; ++i) {
+
Keycode key = sdl_to_keycode(static_cast<SDL_Scancode>(i));
-
if (key != Keycode::NONE) {
this->keyboard_state[key] = current_state[i] != 0;
}
}
+ return this->keyboard_state;
}
MouseButton SDLContext::sdl_to_mousebutton(Uint8 sdl_button) {
@@ -289,7 +284,6 @@ std::vector<EventData> SDLContext::get_events() {
event_list.push_back({.event_type = EventType::SHUTDOWN});
break;
case SDL_KEYDOWN:
- this->update_keyboard_state();
event_list.push_back(EventData{
.event_type = EventType::KEY_DOWN,
.data = {
@@ -302,7 +296,6 @@ std::vector<EventData> SDLContext::get_events() {
break;
case SDL_KEYUP:
- this->update_keyboard_state();
event_list.push_back(EventData{
.event_type = EventType::KEY_UP,
.data = {
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 34a4a09..3531680 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -137,7 +137,16 @@ public:
* \return The corresponding `MouseButton` value or `MouseButton::NONE` if the key is unrecognized
*/
MouseButton sdl_to_mousebutton(Uint8 sdl_button);
- const keyboard_state_t& get_keyboard_state() const;
+ /**
+ * \brief Gets the current state of the keyboard.
+ *
+ * Updates the internal keyboard state by checking the current key states using
+ * SDL's `SDL_GetKeyboardState()`, and returns a reference to the `keyboard_state_t`.
+ *
+ * \return A constant reference to the `keyboard_state_t`, which holds the state
+ * of each key (true = pressed, false = not pressed).
+ */
+ const keyboard_state_t& get_keyboard_state();
public:
/**
@@ -235,15 +244,6 @@ private:
CameraAuxiliaryData cam_aux_data;
private:
- /**
- * \brief Retrieves the current state of the keyboard.
- *
- * This method updates the state of all keys on the keyboard. Each element of the unordered map corresponds to a
- * specific key defined in the `Keycode` enum, and the value indicates whether
- * the key is currently pressed (true) or not pressed (false).
- *
- */
- void update_keyboard_state();
//! variable to store the state of each key (true = pressed, false = not pressed)
keyboard_state_t keyboard_state;
//! lookup table for converting SDL_SCANCODES to Keycodes
diff --git a/src/example/button.cpp b/src/example/button.cpp
index f2e77f6..c4e9a47 100644
--- a/src/example/button.cpp
+++ b/src/example/button.cpp
@@ -1,17 +1,18 @@
#include <SDL2/SDL_timer.h>
#include <chrono>
#include <crepe/Component.h>
-#include <crepe/ComponentManager.h>
+#include <crepe/manager/ComponentManager.h>
#include <crepe/api/Animator.h>
#include <crepe/api/Button.h>
#include <crepe/api/Camera.h>
#include <crepe/api/Color.h>
-#include <crepe/api/EventManager.h>
+#include <crepe/manager/EventManager.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Sprite.h>
#include <crepe/api/Texture.h>
#include <crepe/api/Transform.h>
#include <crepe/system/AnimatorSystem.h>
+#include <crepe/facade/SDLContext.h>
#include <crepe/system/InputSystem.h>
#include <crepe/system/RenderSystem.h>
#include <crepe/types.h>
@@ -19,33 +20,35 @@ using namespace crepe;
using namespace std;
int main(int argc, char * argv[]) {
- ComponentManager mgr;
- RenderSystem sys{mgr};
- EventManager & event_mgr = EventManager::get_instance();
- InputSystem input_sys{mgr};
- AnimatorSystem asys{mgr};
- GameObject camera_obj = mgr.new_object("", "", vec2{1000, 1000}, 0, 1);
- camera_obj.add_component<Camera>(Color::WHITE, ivec2{1080, 720}, vec2{2000, 2000}, 1.0f);
+ Mediator mediator;
+ ComponentManager mgr{mediator};
+ RenderSystem sys{mediator};
+ EventManager event_mgr{mediator};
+ InputSystem input_sys{mediator};
+ SDLContext sdl_context{mediator};
+ GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1);
+ auto & camera = obj.add_component<Camera>(ivec2{500, 500}, vec2{500, 500},
+ Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
- GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
- auto s2 = Texture("asset/texture/test_ap43.png");
- bool button_clicked = false;
- auto & sprite2 = button_obj.add_component<Sprite>(
- s2, Color::GREEN, Sprite::FlipSettings{false, false}, 2, 1, 100);
- std::function<void()> on_click = [&]() { std::cout << "button clicked" << std::endl; };
- std::function<void()> on_enter = [&]() { std::cout << "enter" << std::endl; };
- std::function<void()> on_exit = [&]() { std::cout << "exit" << std::endl; };
- auto & button
- = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click, false);
- button.on_mouse_enter = on_enter;
- button.on_mouse_exit = on_exit;
- button.is_toggle = true;
- button.active = true;
+ // GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
+ // auto s2 = Texture("asset/texture/test_ap43.png");
+ // bool button_clicked = false;
+ // auto & sprite2 = button_obj.add_component<Sprite>(
+ // s2, Color::GREEN, Sprite::FlipSettings{false, false}, 2, 1, 100);
+ // std::function<void()> on_click = [&]() { std::cout << "button clicked" << std::endl; };
+ // std::function<void()> on_enter = [&]() { std::cout << "enter" << std::endl; };
+ // std::function<void()> on_exit = [&]() { std::cout << "exit" << std::endl; };
+ // auto & button
+ // = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click, false);
+ // button.on_mouse_enter = on_enter;
+ // button.on_mouse_exit = on_exit;
+ // button.is_toggle = true;
+ // button.active = true;
auto start = std::chrono::steady_clock::now();
while (true) {
+ const keyboard_state_t& keyboard_state = sdl_context.get_keyboard_state();
input_sys.update();
sys.update();
- asys.update();
event_mgr.dispatch_events();
SDL_Delay(30);
}
diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp
index a707444..f974d0c 100644
--- a/src/test/InputTest.cpp
+++ b/src/test/InputTest.cpp
@@ -260,3 +260,4 @@ TEST_F(InputTest, testButtonHover) {
event_manager.dispatch_events();
EXPECT_TRUE(button.hover);
}
+