Browse Source

Add basic game loop implementation

Patrick-Christopher Mattulat 5 ngày trước cách đây
mục cha
commit
7fcf8ad1a8

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

@@ -13,6 +13,7 @@ set(ATLANTIS_INTERFACE_SOURCES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/interface/IKeyboard.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/interface/ISdlApi.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/interface/IWindowApi.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/interface/IWindowApiEventManager.cpp
 )
 
 ################################

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

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

+ 4 - 0
include/cycle/Engine.hpp

@@ -21,8 +21,12 @@ namespace ls::atlantis::cycle
       explicit Engine(const ls::atlantis::cycle::EngineParameter &_parameter);
       ~Engine();
 
+      void beginFrame();
+      void endFrame() const;
       [[nodiscard]] ::std::shared_ptr<ls::atlantis::cycle::EngineRuntimeData> getData() const;
       [[nodiscard]] ::std::shared_ptr<ls::atlantis::interfaces::AStatusCode> init() const;
+      [[nodiscard]] bool isRunning() const;
+      void quit() const;
 
     private:
 

+ 3 - 0
include/cycle/EngineRuntimeData.hpp

@@ -21,12 +21,15 @@ namespace ls::atlantis::cycle
       ~EngineRuntimeData();
 
       [[nodiscard]] ::std::shared_ptr<ls::atlantis::interfaces::IKeyboard> getKeyboard() const;
+      [[nodiscard]] bool getIsUp() const;
       [[nodiscard]] ::std::shared_ptr<ls::atlantis::interfaces::IWindowApi> getWindowApi() const;
+      void setIsUp(bool _isUp);
       void setKeyboard(const ::std::shared_ptr<ls::atlantis::interfaces::IKeyboard> &_keyboard);
       void setWindowApi(const ::std::shared_ptr<ls::atlantis::interfaces::IWindowApi> &_windowApi);
 
     private:
 
+      bool isUp{};
       ::std::shared_ptr<ls::atlantis::interfaces::IKeyboard> keyboard{};
       ::std::shared_ptr<ls::atlantis::interfaces::IWindowApi> windowApi{};
   };

+ 24 - 0
include/interface/IWindowApiEventManager.hpp

@@ -0,0 +1,24 @@
+/*
+ * author: Patrick-Christopher Mattulat
+ * e-mail: webmaster@lynarstudios.com
+ */
+
+#ifndef LS_ATLANTIS_ENGINE_INTERFACE_WINDOW_API_EVENT_MANAGER_HPP
+#define LS_ATLANTIS_ENGINE_INTERFACE_WINDOW_API_EVENT_MANAGER_HPP
+
+#include <Export.hpp>
+
+namespace ls::atlantis::interfaces
+{
+  class LS_ATLANTIS_DYNAMIC_GOAL IWindowApiEventManager
+  {
+    public:
+
+      IWindowApiEventManager();
+      virtual ~IWindowApiEventManager();
+
+      virtual void manage() = 0;
+  };
+}
+
+#endif

+ 25 - 0
include/window/SdlWindowApiEventManager.hpp

@@ -0,0 +1,25 @@
+/*
+ * author: Patrick-Christopher Mattulat
+ * e-mail: webmaster@lynarstudios.com
+ */
+
+#ifndef LS_ATLANTIS_ENGINE_WINDOW_SDL_WINDOW_API_EVENT_MANAGER_HPP
+#define LS_ATLANTIS_ENGINE_WINDOW_SDL_WINDOW_API_EVENT_MANAGER_HPP
+
+#include <Export.hpp>
+#include <interface/IWindowApiEventManager.hpp>
+
+namespace ls::atlantis::window
+{
+  class LS_ATLANTIS_DYNAMIC_GOAL SdlWindowApiEventManager : public ls::atlantis::interfaces::IWindowApiEventManager
+  {
+    public:
+
+      SdlWindowApiEventManager();
+      ~SdlWindowApiEventManager() override;
+
+      void manage() override;
+  };
+}
+
+#endif

+ 14 - 0
output/BasicSdlWindowExample.cpp

@@ -5,11 +5,13 @@
 
 #include <cycle/Engine.hpp>
 #include <cycle/EngineParameter.hpp>
+#include <glossary/KeyboardKeys.hpp>
 #include <glossary/StatusCodes.hpp>
 #include <glossary/WindowApiTypes.hpp>
 
 using ls::atlantis::cycle::Engine;
 using ls::atlantis::cycle::EngineParameter;
+using ls::atlantis::glossary::KeyboardKeys;
 using ls::atlantis::glossary::StatusCodes;
 using ls::atlantis::glossary::WindowApiTypes;
 using ls::atlantis::interfaces::AStatusCode;
@@ -27,5 +29,17 @@ int main()
   Engine engine{parameter};
   const shared_ptr<AStatusCode> statusCode = engine.init();
 
+  while (engine.isRunning())
+  {
+    engine.beginFrame();
+
+    if (engine.getData()->getKeyboard()->isFreed(KeyboardKeys::ESCAPE))
+    {
+      engine.quit();
+    }
+
+    engine.endFrame();
+  }
+
   return statusCode->getCode() == StatusCodes::OK ? EXIT_SUCCESS : EXIT_FAILURE;
 }

+ 23 - 1
source/cycle/Engine.cpp

@@ -21,10 +21,22 @@ using ::std::shared_ptr;
 Engine::Engine(const EngineParameter &_parameter)
   : data(make_shared<EngineRuntimeData>()),
     parameter(_parameter)
-{}
+{
+  this->data->setIsUp(true);
+}
 
 Engine::~Engine() = default;
 
+void Engine::beginFrame()
+{
+
+}
+
+void Engine::endFrame() const
+{
+  this->data->getKeyboard()->reset();
+}
+
 shared_ptr<EngineRuntimeData> Engine::getData() const
 {
   return this->data;
@@ -42,6 +54,16 @@ shared_ptr<AStatusCode> Engine::init() const
   return result;
 }
 
+bool Engine::isRunning() const
+{
+  return this->data->getIsUp();
+}
+
+void Engine::quit() const
+{
+  this->data->setIsUp(false);
+}
+
 shared_ptr<AStatusCode> Engine::_initKeyboard() const
 {
   const auto keyboard = KeyboardFactory::build(this->parameter.getWindowApiType());

+ 10 - 0
source/cycle/EngineRuntimeData.cpp

@@ -21,11 +21,21 @@ shared_ptr<IKeyboard> EngineRuntimeData::getKeyboard() const
   return this->keyboard;
 }
 
+bool EngineRuntimeData::getIsUp() const
+{
+  return this->isUp;
+}
+
 shared_ptr<IWindowApi> EngineRuntimeData::getWindowApi() const
 {
   return this->windowApi;
 }
 
+void EngineRuntimeData::setIsUp(const bool _isUp)
+{
+  this->isUp = _isUp;
+}
+
 void EngineRuntimeData::setKeyboard(const shared_ptr<IKeyboard> &_keyboard)
 {
   NullPointerArgumentEvaluator(_keyboard).evaluate();

+ 12 - 0
source/interface/IWindowApiEventManager.cpp

@@ -0,0 +1,12 @@
+/*
+ * author: Patrick-Christopher Mattulat
+ * e-mail: webmaster@lynarstudios.com
+ */
+
+#include <interface/IWindowApiEventManager.hpp>
+
+using ls::atlantis::interfaces::IWindowApiEventManager;
+
+IWindowApiEventManager::IWindowApiEventManager() = default;
+
+IWindowApiEventManager::~IWindowApiEventManager() = default;

+ 15 - 0
source/window/SdlWindowApiEventManager.cpp

@@ -0,0 +1,15 @@
+/*
+ * author: Patrick-Christopher Mattulat
+ * e-mail: webmaster@lynarstudios.com
+ */
+
+#include <window/SdlWindowApiEventManager.hpp>
+
+using ls::atlantis::window::SdlWindowApiEventManager;
+
+SdlWindowApiEventManager::SdlWindowApiEventManager() = default;
+
+SdlWindowApiEventManager::~SdlWindowApiEventManager() = default;
+
+void SdlWindowApiEventManager::manage()
+{}