diff --git a/App/.vscode/launch.json b/App/.vscode/launch.json new file mode 100644 index 0000000..10efcb2 --- /dev/null +++ b/App/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug", + "program": "${workspaceFolder}/", + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/App/App/CMakeLists.txt b/App/App/CMakeLists.txt index 784423c..62d8b9d 100644 --- a/App/App/CMakeLists.txt +++ b/App/App/CMakeLists.txt @@ -19,7 +19,6 @@ target_link_libraries(App PRIVATE SDL2::SDL2main OpenGL::GL ) -# target_link_libraries(App PUBLIC ) target_include_directories(App PUBLIC "${CMAKE_SOURCE_DIR}/App/include") target_include_directories(App PUBLIC "${CMAKE_SOURCE_DIR}/AppLib/include/AppLib") diff --git a/App/App/include/App.hh b/App/App/include/App.hh index d947af0..c6fa761 100644 --- a/App/App/include/App.hh +++ b/App/App/include/App.hh @@ -6,6 +6,7 @@ #include #include "Logger.hh" +#include "Controller.hh" using namespace Core; @@ -16,15 +17,18 @@ namespace App { private: std::shared_ptr m_Logger; + std::shared_ptr m_Controller; int m_ReturnCode; int m_WindowWidth; int m_WindowHeight; SDL_Window* m_Window; SDL_GLContext m_glContext; bool m_Quit; + bool m_VSync; public: App(/* args */); int getReturnCode() const; + void handleCommand(const Uint32 command); ~App(); }; } // namespace App diff --git a/App/App/include/Controller.hh b/App/App/include/Controller.hh new file mode 100644 index 0000000..b687aeb --- /dev/null +++ b/App/App/include/Controller.hh @@ -0,0 +1,18 @@ +#pragma once + +#include "SDL2/SDL.h" + +namespace App +{ +class Controller +{ +private: + SDL_Event* m_lastEvent; + Uint32 m_lastCommand; + +public: + Controller(); + Uint32 HandleEvent(SDL_Event *event); + ~Controller(); +}; +} // namespace App diff --git a/App/App/src/App.cpp b/App/App/src/App.cpp index b95be6e..61f3d00 100644 --- a/App/App/src/App.cpp +++ b/App/App/src/App.cpp @@ -10,10 +10,11 @@ namespace App { - App::App() : m_ReturnCode(0), m_WindowWidth(800), m_WindowHeight(600), m_Quit(false) + App::App() : m_ReturnCode(0), m_WindowWidth(800), m_WindowHeight(600), m_Quit(false), m_VSync(false) { std::cout << "Eternity Engine" << std::endl; m_Logger = std::make_shared(); + m_Controller = std::make_shared(); //Use OpenGL 3.1 core SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); @@ -55,20 +56,26 @@ namespace App { // Event loop exit flag m_Quit = false; - + Uint32 command = 0; // Event loop while(!m_Quit) { SDL_Event e; // Wait indefinitely for the next available event - SDL_WaitEvent(&e); + if(m_VSync) + SDL_PollEvent(&e); // Use this for vsync + else + SDL_WaitEvent(&e); // User requests quit if(e.type == SDL_QUIT) { m_Quit = true; } + + command = m_Controller->HandleEvent(&e); + handleCommand(command); // Set background color as cornflower blue glClearColor(0.39f, 0.58f, 0.93f, 1.f); @@ -87,12 +94,26 @@ namespace App { SDL_DestroyWindow(m_Window); SDL_Quit(); - m_Logger->~Logger(); + m_Logger.reset(); + m_Controller.reset(); } int App::getReturnCode() const { return m_ReturnCode; } + + void App::handleCommand(const Uint32 command) + { + switch (command) + { + case -1: + m_Quit = true; + break; + + default: + break; + } + } } // namespace App diff --git a/App/App/src/Controller.cpp b/App/App/src/Controller.cpp new file mode 100644 index 0000000..705dd0c --- /dev/null +++ b/App/App/src/Controller.cpp @@ -0,0 +1,40 @@ +#include "Controller.hh" + +namespace App +{ + +Controller::Controller() : m_lastCommand(0) +{ + m_lastEvent = nullptr; +} + +Controller::~Controller() +{ +} + +Uint32 Controller::HandleEvent(SDL_Event *event) +{ + m_lastEvent = event; + switch (m_lastEvent->type) + { + case SDL_KEYDOWN: + switch (m_lastEvent->key.keysym.scancode) + { + case SDL_SCANCODE_ESCAPE: + m_lastCommand = -1; + break; + + default: + break; + } + break; + + default: + m_lastCommand = 0; + break; + } + return m_lastCommand; +} + +} // namespace App +