summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/command.cpp27
-rw-r--r--src/command.h32
-rw-r--r--src/game.cpp23
-rw-r--r--src/game.h19
-rw-r--r--src/keyHandler.cpp29
-rw-r--r--src/keyHandler.h21
-rw-r--r--src/main.cpp22
-rw-r--r--src/window.cpp32
-rw-r--r--src/window.h16
9 files changed, 189 insertions, 32 deletions
diff --git a/src/command.cpp b/src/command.cpp
new file mode 100644
index 0000000..ab134e9
--- /dev/null
+++ b/src/command.cpp
@@ -0,0 +1,27 @@
+#include "command.h"
+#include "game.h"
+
+Command::~Command(){}
+
+Nop::~Nop(){}
+void Nop::execute( GameState &game ){
+}
+
+// Window commands
+WindowClose::~WindowClose(){}
+void WindowClose::execute( GameState &game ){
+ game.close();
+}
+
+WindowFocus::~WindowFocus(){}
+void WindowFocus::execute( GameState &game ){
+ game.resume();
+}
+
+WindowUnFocus::~WindowUnFocus(){}
+void WindowUnFocus::execute( GameState &game ){
+ game.pause();
+}
+
+// Keyboard commands
+
diff --git a/src/command.h b/src/command.h
new file mode 100644
index 0000000..5af7517
--- /dev/null
+++ b/src/command.h
@@ -0,0 +1,32 @@
+#ifndef COMMAND_H
+#define COMMAND_H
+#include "game.h"
+
+class Command{
+ public:
+ virtual ~Command();
+ virtual void execute( GameState &state ) =0;
+};
+
+class Nop: public Command {
+ ~Nop();
+ void execute( GameState &state ) override;
+};
+
+// Window commands
+class WindowClose: public Command {
+ ~WindowClose();
+ void execute( GameState &state ) override;
+};
+class WindowFocus: public Command {
+ ~WindowFocus();
+ void execute( GameState &state ) override;
+};
+class WindowUnFocus: public Command {
+ ~WindowUnFocus();
+ void execute( GameState &state ) override;
+};
+
+// Keyboard commands
+
+#endif
diff --git a/src/game.cpp b/src/game.cpp
new file mode 100644
index 0000000..aae7e7f
--- /dev/null
+++ b/src/game.cpp
@@ -0,0 +1,23 @@
+#include "game.h"
+
+GameState::GameState():
+ closed_ {false},
+ paused_ {false} {
+}
+
+void GameState::close(){
+ closed_ = true;
+}
+void GameState::pause(){
+ paused_ = true;
+}
+void GameState::resume(){
+ paused_ = false;
+}
+
+bool GameState::isPaused(){
+ return paused_;
+}
+bool GameState::isClosed(){
+ return closed_;
+}
diff --git a/src/game.h b/src/game.h
new file mode 100644
index 0000000..118d642
--- /dev/null
+++ b/src/game.h
@@ -0,0 +1,19 @@
+#ifndef GAME_H
+#define GAME_H
+
+class GameState{
+
+ private:
+ bool closed_;
+ bool paused_;
+
+ public:
+ GameState();
+ void close();
+ void resume();
+ void pause();
+ bool isClosed();
+ bool isPaused();
+};
+
+#endif
diff --git a/src/keyHandler.cpp b/src/keyHandler.cpp
new file mode 100644
index 0000000..e188992
--- /dev/null
+++ b/src/keyHandler.cpp
@@ -0,0 +1,29 @@
+#include "keyHandler.h"
+
+KeyHandler::KeyHandler(){
+ nop = new Nop;
+}
+KeyHandler::~KeyHandler(){
+ delete nop;
+}
+
+Command* KeyHandler::handleEvent( SDL_Event e ){
+ switch( e.type ){
+ case SDL_KEYDOWN:
+ return pressed( e.key.keysym.sym, e.key.keysym.mod );
+ case SDL_KEYUP:
+ return released( e.key.keysym.sym, e.key.keysym.mod );
+ default:
+ return nop;
+ }
+}
+
+Command* KeyHandler::pressed( SDL_Keycode k, Uint16 mod ){
+ // TODO
+ return nop;
+}
+
+Command* KeyHandler::released( SDL_Keycode k, Uint16 mod ){
+ // TODO
+ return nop;
+}
diff --git a/src/keyHandler.h b/src/keyHandler.h
new file mode 100644
index 0000000..5c2152c
--- /dev/null
+++ b/src/keyHandler.h
@@ -0,0 +1,21 @@
+#ifndef KEYHANDLER_H
+#define KEYHANDLER_H
+#include<SDL2/SDL.h>
+#include "command.h"
+
+class KeyHandler{
+
+ public:
+ KeyHandler();
+ ~KeyHandler();
+ Command* handleEvent( SDL_Event e );
+ Command* pressed( SDL_Keycode k, Uint16 mod = 0);
+ Command* released( SDL_Keycode k, Uint16 mod = 0);
+
+ // Modifiers: https://wiki.libsdl.org/SDL_Keymod
+ // Keycodes: https://wiki.libsdl.org/SDL_Keycode
+ private:
+ Command* nop;
+};
+
+#endif
diff --git a/src/main.cpp b/src/main.cpp
index 422fdc3..d059b75 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -5,6 +5,7 @@
#include "renderer.h"
#include "window.h"
#include "timer.h"
+#include "keyHandler.h"
#include <chrono>
#include <thread>
@@ -96,26 +97,23 @@ main( int argc, char* args[] ) {
const Uint32 LOOP_TIME = 20L; // milliseconds
Timer timer;
timer.start();
+
+ GameState game;
+ KeyHandler keys;
+
while( true )
{
// Handle events
SDL_Event e;
while( SDL_PollEvent( &e ) != 0 )
{
- //User requests quit
- window.handleEvent(e);
- if( e.type == SDL_QUIT ) {
- return 0;
- } else if (e.type == SDL_KEYDOWN){
- switch( e.key.keysym.sym ){
- case 'q':
- return 0;
- }
- }
+ // Maybe add commands in a queue or a better defined stream?
+ window.handleEvent(e)->execute(game);
+ keys.handleEvent(e)->execute(game);
}
- if(window.closed()) return 0;
- if(!window.focused()) goto next;
+ if(game.isClosed()) return 0;
+ if(game.isPaused()) goto next;
// GAME LOOP:
diff --git a/src/window.cpp b/src/window.cpp
index 39cbc3d..cb25e94 100644
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -8,9 +8,19 @@ Window::Window( const char* title, int w, int h, int x, int y )
{
printf( "Window could not be created: %s\n", SDL_GetError() );
}
+
+ close = new WindowClose();
+ focus = new WindowFocus();
+ unfocus = new WindowUnFocus();
+ nop = new Nop();
+
}
Window::~Window(){
+ delete close;
+ delete focus;
+ delete unfocus;
+ delete nop;
SDL_DestroyWindow( window_ );
}
@@ -28,18 +38,16 @@ void Window::toggleFullscreen(){
fullscreen_ = !fullscreen_;
}
-void Window::handleEvent(SDL_Event e){
+Command* Window::handleEvent(SDL_Event e){
switch( e.window.event ){
case SDL_WINDOWEVENT_CLOSE:
- closed_ = true;
- printf("CLOSE!");
- break;
+ return close;
case SDL_WINDOWEVENT_FOCUS_GAINED:
- focused_ = true;
- break;
+ return focus;
case SDL_WINDOWEVENT_FOCUS_LOST:
- focused_ = false;
- break;
+ return unfocus;
+ default:
+ return nop;
}
}
@@ -48,11 +56,3 @@ Point Window::getSize(){
SDL_GetWindowSize(window_, &size.x, &size.y);
return size;
}
-
-bool Window::focused(){
- return focused_;
-}
-
-bool Window::closed(){
- return closed_;
-}
diff --git a/src/window.h b/src/window.h
index efa2165..ee4c146 100644
--- a/src/window.h
+++ b/src/window.h
@@ -1,3 +1,6 @@
+#ifndef WINDOW_H
+#define WINDOW_H
+#include "command.h"
#include<SDL2/SDL.h>
struct Point{
@@ -14,14 +17,19 @@ class Window{
int y = SDL_WINDOWPOS_UNDEFINED );
~Window();
SDL_Window* window();
- void handleEvent( SDL_Event e );
+ Command* handleEvent( SDL_Event e );
void toggleFullscreen();
Point getSize();
- bool closed();
- bool focused();
private:
SDL_Window* window_;
bool fullscreen_;
- bool focused_;
bool closed_;
+ bool focused_;
+
+ Command* close;
+ Command* unfocus;
+ Command* focus;
+ Command* nop;
};
+
+#endif