App: Add controller with basic event handling
This commit is contained in:
parent
f8ac97ff7e
commit
a2e592e79a
16
App/.vscode/launch.json
vendored
Normal file
16
App/.vscode/launch.json
vendored
Normal file
@ -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}/<executable file>",
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -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")
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
#include <GL/gl.h>
|
||||
|
||||
#include "Logger.hh"
|
||||
#include "Controller.hh"
|
||||
|
||||
|
||||
using namespace Core;
|
||||
@ -16,15 +17,18 @@ namespace App
|
||||
{
|
||||
private:
|
||||
std::shared_ptr<Logger> m_Logger;
|
||||
std::shared_ptr<Controller> 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
|
||||
|
||||
18
App/App/include/Controller.hh
Normal file
18
App/App/include/Controller.hh
Normal file
@ -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
|
||||
@ -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<Logger>();
|
||||
m_Controller = std::make_shared<Controller>();
|
||||
|
||||
//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
|
||||
|
||||
40
App/App/src/Controller.cpp
Normal file
40
App/App/src/Controller.cpp
Normal file
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user