Bläddra i källkod

Add initial SDL keyboard event logic

Patrick-Christopher Mattulat 4 dagar sedan
förälder
incheckning
596ea59ecb

+ 1 - 0
cmake/ls-atlantis-window.cmake

@@ -9,6 +9,7 @@
 ################################
 
 set(ATLANTIS_WINDOW_SOURCES
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/window/SdlKeyEventManager.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/window/SdlApi.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/window/SdlWindowApi.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/window/SdlWindowApiEventManager.cpp

+ 3 - 1
include/interface/IWindowApiEventManager.hpp

@@ -7,6 +7,8 @@
 #define LS_ATLANTIS_ENGINE_INTERFACE_WINDOW_API_EVENT_MANAGER_HPP
 
 #include <Export.hpp>
+#include <interface/IKeyboard.hpp>
+#include <memory>
 
 namespace ls::atlantis::interfaces
 {
@@ -17,7 +19,7 @@ namespace ls::atlantis::interfaces
       IWindowApiEventManager();
       virtual ~IWindowApiEventManager();
 
-      virtual void manage() = 0;
+      virtual void manage(const ::std::shared_ptr<ls::atlantis::interfaces::IKeyboard> &_keyboard) = 0;
   };
 }
 

+ 28 - 0
include/window/SdlKeyEventManager.hpp

@@ -0,0 +1,28 @@
+/*
+ * author: Patrick-Christopher Mattulat
+ * e-mail: webmaster@lynarstudios.com
+ */
+
+#ifndef LS_ATLANTIS_ENGINE_WINDOW_SDL_KEY_EVENT_MANAGER_HPP
+#define LS_ATLANTIS_ENGINE_WINDOW_SDL_KEY_EVENT_MANAGER_HPP
+
+#include <Export.hpp>
+#include <SDL3/SDL_events.h>
+#include <interface/IKeyboard.hpp>
+#include <memory>
+
+namespace ls::atlantis::window
+{
+  class LS_ATLANTIS_DYNAMIC_GOAL SdlKeyEventManager
+  {
+    public:
+
+      SdlKeyEventManager();
+      ~SdlKeyEventManager();
+
+      static void manageKeyDownEvent(const ::std::shared_ptr<ls::atlantis::interfaces::IKeyboard> &_keyboard, const ::std::shared_ptr<SDL_Event> &_event);
+      static void manageKeyUpEvent(const ::std::shared_ptr<ls::atlantis::interfaces::IKeyboard> &_keyboard, const ::std::shared_ptr<SDL_Event> &_event);
+  };
+}
+
+#endif

+ 6 - 1
include/window/SdlWindowApiEventManager.hpp

@@ -7,6 +7,7 @@
 #define LS_ATLANTIS_ENGINE_WINDOW_SDL_WINDOW_API_EVENT_MANAGER_HPP
 
 #include <Export.hpp>
+#include <SDL3/SDL.h>
 #include <interface/IWindowApiEventManager.hpp>
 
 namespace ls::atlantis::window
@@ -18,7 +19,11 @@ namespace ls::atlantis::window
       SdlWindowApiEventManager();
       ~SdlWindowApiEventManager() override;
 
-      void manage() override;
+      void manage(const ::std::shared_ptr<ls::atlantis::interfaces::IKeyboard> &_keyboard) override;
+
+    private:
+
+      ::std::shared_ptr<SDL_Event> sdlEvent{};
   };
 }
 

+ 5 - 1
output/BasicSdlWindowExample.cpp

@@ -32,11 +32,15 @@ int main()
   {
     engine.beginFrame();
 
-    if (engine.getData()->getKeyboard()->isFreed(KeyboardKeys::ESCAPE))
+    // customized game logic
+
+    if (engine.getData()->getKeyboard()->isPressed(KeyboardKeys::ESCAPE))
     {
       engine.quit();
     }
 
+    // draw and ending frame
+
     engine.endFrame();
   }
 

+ 1 - 1
source/cycle/Engine.cpp

@@ -31,7 +31,7 @@ Engine::~Engine() = default;
 
 void Engine::beginFrame() const
 {
-  this->data->getWindowApiEventManager()->manage();
+  this->data->getWindowApiEventManager()->manage(this->data->getKeyboard());
 }
 
 void Engine::endFrame() const

+ 1 - 0
source/cycle/EngineRuntimeData.cpp

@@ -6,6 +6,7 @@
 #include <cycle/EngineRuntimeData.hpp>
 #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
 
+
 using ls::atlantis::cycle::EngineRuntimeData;
 using ls::atlantis::interfaces::IKeyboard;
 using ls::atlantis::interfaces::IWindowApi;

+ 33 - 0
source/window/SdlKeyEventManager.cpp

@@ -0,0 +1,33 @@
+/*
+ * author: Patrick-Christopher Mattulat
+ * e-mail: webmaster@lynarstudios.com
+ */
+
+#include <glossary/KeyboardKeys.hpp>
+#include <interface/IKeyboard.hpp>
+#include <window/SdlKeyEventManager.hpp>
+
+using ls::atlantis::glossary::KeyboardKeys;
+using ls::atlantis::interfaces::IKeyboard;
+using ls::atlantis::window::SdlKeyEventManager;
+using ::std::shared_ptr;
+
+SdlKeyEventManager::SdlKeyEventManager() = default;
+
+SdlKeyEventManager::~SdlKeyEventManager() = default;
+
+void SdlKeyEventManager::manageKeyDownEvent(const shared_ptr<IKeyboard> &_keyboard, const shared_ptr<SDL_Event> &_event)
+{
+  if (_event->key.key == SDLK_ESCAPE)
+  {
+    _keyboard->updateKey(KeyboardKeys::ESCAPE, _keyboard->isFreed(KeyboardKeys::ESCAPE), true);
+  }
+}
+
+void SdlKeyEventManager::manageKeyUpEvent(const shared_ptr<IKeyboard> &_keyboard, const shared_ptr<SDL_Event> &_event)
+{
+  if (_event->key.key == SDLK_ESCAPE)
+  {
+    _keyboard->updateKey(KeyboardKeys::ESCAPE, true, _keyboard->isPressed(KeyboardKeys::ESCAPE));
+  }
+}

+ 31 - 3
source/window/SdlWindowApiEventManager.cpp

@@ -3,13 +3,41 @@
  * e-mail: webmaster@lynarstudios.com
  */
 
+#include <interface/IKeyboard.hpp>
+#include <window/SdlKeyEventManager.hpp>
 #include <window/SdlWindowApiEventManager.hpp>
 
+using ls::atlantis::interfaces::IKeyboard;
+using ls::atlantis::window::SdlKeyEventManager;
 using ls::atlantis::window::SdlWindowApiEventManager;
+using ::std::make_shared;
+using ::std::shared_ptr;
 
-SdlWindowApiEventManager::SdlWindowApiEventManager() = default;
+SdlWindowApiEventManager::SdlWindowApiEventManager()
+{
+  this->sdlEvent = make_shared<SDL_Event>();
+}
 
 SdlWindowApiEventManager::~SdlWindowApiEventManager() = default;
 
-void SdlWindowApiEventManager::manage()
-{}
+void SdlWindowApiEventManager::manage(const shared_ptr<IKeyboard> &_keyboard)
+{
+  while (SDL_PollEvent(this->sdlEvent.get()))
+  {
+    switch (this->sdlEvent->type)
+    {
+      case SDL_EVENT_KEY_DOWN:
+      {
+        SdlKeyEventManager::manageKeyDownEvent(_keyboard, this->sdlEvent);
+      } break;
+      case SDL_EVENT_KEY_UP:
+      {
+        SdlKeyEventManager::manageKeyUpEvent(_keyboard, this->sdlEvent);
+      } break;
+      default:
+      {
+        // other use cases not supported, yet
+      }
+    }
+  }
+}

+ 6 - 2
test/cycle/mock/WindowApiEventManagerMock.cpp

@@ -6,10 +6,14 @@
 #include <test/cycle/mock/WindowApiEventManagerMock.hpp>
 
 using ls::atlantis::cycle::test::WindowApiEventManagerMock;
+using ls::atlantis::interfaces::IKeyboard;
+using ::std::shared_ptr;
 
 WindowApiEventManagerMock::WindowApiEventManagerMock() = default;
 
 WindowApiEventManagerMock::~WindowApiEventManagerMock() = default;
 
-void WindowApiEventManagerMock::manage()
-{}
+void WindowApiEventManagerMock::manage(const shared_ptr<IKeyboard> &_keyboard)
+{
+  // for mock not needed
+}

+ 1 - 1
test/cycle/mock/WindowApiEventManagerMock.hpp

@@ -17,7 +17,7 @@ namespace ls::atlantis::cycle::test
       WindowApiEventManagerMock();
       ~WindowApiEventManagerMock() override;
 
-      void manage() override;
+      void manage(const ::std::shared_ptr<ls::atlantis::interfaces::IKeyboard> &_keyboard) override;
   };
 }