event dispatch

This commit is contained in:
Frank DeMarco 2019-05-03 02:09:48 -04:00
parent cf695b3d10
commit eb35aedeef
12 changed files with 218 additions and 108 deletions

View File

@ -343,103 +343,103 @@ struct Demo : Game
void update()
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
flag_to_end();
}
else if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_F9)
{
capture_screen(window);
}
else if (event.key.keysym.sym == SDLK_F10)
{
if (not is_recording)
{
start_recording(&is_recording);
}
else
{
end_recording(frames, &is_recording);
}
}
else if (event.key.keysym.sym == SDLK_F11)
{
if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN)
{
SDL_SetWindowFullscreen(window, 0);
}
else
{
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
}
}
else if (SDL_GetModState() & KMOD_CTRL)
{
if (event.key.keysym.sym == SDLK_f)
{
show_framerate = not show_framerate;
}
else if (event.key.keysym.sym == SDLK_UP)
{
set_framerate(framerate + 1);
}
else if (event.key.keysym.sym == SDLK_DOWN)
{
set_framerate(framerate - 1);
}
}
else if (event.key.keysym.sym == SDLK_UP)
{
up_active = true;
}
else if (event.key.keysym.sym == SDLK_RIGHT)
{
right_active = true;
}
else if (event.key.keysym.sym == SDLK_DOWN)
{
down_active = true;
}
else if (event.key.keysym.sym == SDLK_LEFT)
{
left_active = true;
}
else if (event.key.keysym.sym == SDLK_SPACE)
{
if (is_gl_context)
{
load_sdl_context();
}
else
{
load_gl_context();
}
}
}
else if (event.type == SDL_KEYUP)
{
if (event.key.keysym.sym == SDLK_UP)
{
up_active = false;
}
else if (event.key.keysym.sym == SDLK_RIGHT)
{
right_active = false;
}
else if (event.key.keysym.sym == SDLK_DOWN)
{
down_active = false;
}
else if (event.key.keysym.sym == SDLK_LEFT)
{
left_active = false;
}
}
}
// while (SDL_PollEvent(&event))
// {
// if (event.type == SDL_QUIT)
// {
// flag_to_end();
// }
// else if (event.type == SDL_KEYDOWN)
// {
// if (event.key.keysym.sym == SDLK_F9)
// {
// capture_screen(window);
// }
// else if (event.key.keysym.sym == SDLK_F10)
// {
// if (not is_recording)
// {
// start_recording(&is_recording);
// }
// else
// {
// end_recording(frames, &is_recording);
// }
// }
// else if (event.key.keysym.sym == SDLK_F11)
// {
// if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN)
// {
// SDL_SetWindowFullscreen(window, 0);
// }
// else
// {
// SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
// }
// }
// else if (SDL_GetModState() & KMOD_CTRL)
// {
// if (event.key.keysym.sym == SDLK_f)
// {
// show_framerate = not show_framerate;
// }
// else if (event.key.keysym.sym == SDLK_UP)
// {
// set_framerate(framerate + 1);
// }
// else if (event.key.keysym.sym == SDLK_DOWN)
// {
// set_framerate(framerate - 1);
// }
// }
// else if (event.key.keysym.sym == SDLK_UP)
// {
// up_active = true;
// }
// else if (event.key.keysym.sym == SDLK_RIGHT)
// {
// right_active = true;
// }
// else if (event.key.keysym.sym == SDLK_DOWN)
// {
// down_active = true;
// }
// else if (event.key.keysym.sym == SDLK_LEFT)
// {
// left_active = true;
// }
// else if (event.key.keysym.sym == SDLK_SPACE)
// {
// if (is_gl_context)
// {
// load_sdl_context();
// }
// else
// {
// load_gl_context();
// }
// }
// }
// else if (event.type == SDL_KEYUP)
// {
// if (event.key.keysym.sym == SDLK_UP)
// {
// up_active = false;
// }
// else if (event.key.keysym.sym == SDLK_RIGHT)
// {
// right_active = false;
// }
// else if (event.key.keysym.sym == SDLK_DOWN)
// {
// down_active = false;
// }
// else if (event.key.keysym.sym == SDLK_LEFT)
// {
// left_active = false;
// }
// }
// }
if (is_recording and ticks - last_capture_timestamp + capture_time_overflow >
recording_capture_framerate)
{

View File

@ -34,6 +34,7 @@
#include "Location.hpp"
#include "Sprite.hpp"
#include "Input.hpp"
#include "Delegate.hpp"
struct Mushroom : Sprite
{

View File

@ -36,18 +36,16 @@ $(SDLGFX2_DIR)%.o: $(SDLGFX2_DIR)%.c $(SDLGFX2_DIR)%.h
$(GLEW_DIR)%.o: $(GLEW_DIR)%.c $(GLEW_DIR)%.h
$(CC_LINUX) $(CFLAGS) $< -o $@
$(SFW_SRC_DIR)Sprite.o: $(addprefix $(SFW_SRC_DIR),Node.hpp Game.hpp Location.hpp)
$(SFW_SRC_DIR)Game.o: $(addprefix $(SFW_SRC_DIR),Node.hpp Sprite.hpp Configuration.hpp)
$(SFW_SRC_DIR)Sprite.o: $(addprefix $(SFW_SRC_DIR),Game.hpp Location.hpp)
$(SFW_SRC_DIR)Game.o: $(addprefix $(SFW_SRC_DIR),Sprite.hpp Configuration.hpp Delegate.hpp)
$(SFW_SRC_DIR)Node.o: $(addprefix $(SFW_SRC_DIR),Game.hpp Configuration.hpp)
$(SFW_SRC_DIR)Configuration.o: $(SFW_SRC_DIR)Node.hpp
$(SFW_SRC_DIR)Input.o: $(SFW_SRC_DIR)Node.hpp
$(SFW_SRC_DIR)%.o: $(addprefix $(SFW_SRC_DIR),%.cpp %.hpp)
$(SFW_SRC_DIR)%.o: $(addprefix $(SFW_SRC_DIR),%.cpp %.hpp Node.cpp)
$(CPPC_LINUX) $(CPP_FLAGS) $(SDL_FLAGS) $< -o $@
Demo.o: Demo.cpp Demo.hpp $(addprefix $(SFW_SRC_DIR),Sprite.hpp Node.hpp Game.hpp Location.hpp Input.hpp)
$(CPPC_LINUX) $(CPP_FLAGS) $(SDL_FLAGS) $< -o $@
linux: Demo.o $(addprefix $(SFW_SRC_DIR),Sprite.o Node.o Game.o Location.o Configuration.o Input.o) \
linux: Demo.o $(addprefix $(SFW_SRC_DIR),Sprite.o Node.o Game.o Location.o Configuration.o Input.o Delegate.o) \
$(GLEW_DIR)glew.o $(addprefix $(SDLGFX2_DIR),SDL2_rotozoom.o SDL2_gfxPrimitives.o)
$(CPPC_LINUX) $(LFLAGS) -D__LINUX__ $^ -lGL -lSDL2_image -lSDL2_ttf -lSDL2_mixer -lstdc++fs -o demo

33
src/Delegate.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "Delegate.hpp"
Delegate::Delegate(Node *parent) : Node(parent) {}
void Delegate::add_subscriber(Node *instance, void (Node::*callback)(SDL_Event*), SDL_EventType type)
{
if (subscribers.count(type) == 0)
{
subscribers[type] = {};
}
Subscriber s = {instance, callback};
subscribers[type].push_back(s);
}
void Delegate::dispatch()
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
for (auto iter = subscribers.begin(); iter != subscribers.end(); iter++)
{
if (event.type == iter->first)
{
for (Subscriber subscriber : iter->second)
{
Node *n = subscriber.instance;
void (Node::*callback)(SDL_Event*) = subscriber.callback;
(n->*callback)(&event);
}
}
}
}
}

28
src/Delegate.hpp Normal file
View File

@ -0,0 +1,28 @@
#ifndef Delegate_h_
#define Delegate_h_
#include <map>
#include <list>
#include "Node.hpp"
#include "SDL.h"
struct Subscriber
{
Node *instance;
void (Node::*callback)(SDL_Event*);
};
struct Delegate : Node
{
std::map<SDL_EventType, std::list<Subscriber>> subscribers;
Delegate(Node*);
void add_subscriber(Node*, void (Node::*)(SDL_Event*), SDL_EventType);
void dispatch();
};
#endif

View File

@ -2,6 +2,7 @@
Game::Game()
{
delegate->add_subscriber(this, &Node::respond, SDL_QUIT);
std::cout << "GLEW " << glewGetString(GLEW_VERSION) << std::endl;
putenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN=0");
putenv("SDL_VIDEO_CENTERED=1");
@ -129,16 +130,13 @@ void Game::run()
last_frame_length = ticks - last_frame_timestamp;
frame_time_overflow = last_frame_length + frame_time_overflow - frame_length;
last_frame_timestamp = ticks;
delegate->dispatch();
update();
}
SDL_Delay(15);
}
}
// void Game::update()
// {
// }
void Game::flag_to_end()
{
done = true;
@ -154,6 +152,14 @@ void Game::set_framerate(int fps)
frame_length = 1000.0 / framerate;
}
void Game::respond(SDL_Event *event)
{
if (event->type == SDL_QUIT)
{
flag_to_end();
}
}
void Game::quit()
{
if (glcontext != NULL)

View File

@ -17,6 +17,7 @@
#include "Node.hpp"
#include "Configuration.hpp"
#include "Delegate.hpp"
struct Game : Node
{
@ -35,6 +36,7 @@ struct Game : Node
float frame_length = 1000.0 / framerate;
bool done = false, show_framerate = false, is_gl_context = true;
Configuration *configuration = new Configuration(this);
Delegate *delegate = new Delegate(this);
Game();
void print_error(std::string);
@ -44,8 +46,9 @@ struct Game : Node
void load_gl_context();
void run();
void flag_to_end();
virtual void update() = 0;
virtual void update() {};
void set_framerate(int);
void respond(SDL_Event*);
void quit();
std::string get_class_name() { return "Game"; }

11
src/Input.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "Input.hpp"
Input::Input(Node *parent) : Node(parent)
{
get_delegate()->add_subscriber(this, &Node::respond, SDL_KEYDOWN);
}
void Input::respond(SDL_Event *event)
{
std::cout << event->key.keysym.sym << std::endl;
}

18
src/Input.hpp Normal file
View File

@ -0,0 +1,18 @@
#ifndef Input_h_
#define Input_h_
#include "SDL.h"
#include "Node.hpp"
#include "Delegate.hpp"
struct Input : Node
{
Input(Node*);
void respond(SDL_Event*);
std::string get_class_name() { return "Input"; }
};
#endif

View File

@ -1,6 +1,8 @@
#ifndef Location_h_
#define Location_h_
#include <iostream>
#include <SDL.h>
#define GLM_ENABLE_EXPERIMENTAL

View File

@ -14,6 +14,11 @@ Configuration* Node::get_configuration()
return get_root()->configuration;
}
Delegate* Node::get_delegate()
{
return get_root()->delegate;
}
Game* Node::get_root()
{
Node *current = this;

View File

@ -3,10 +3,13 @@
#include <iostream>
#include "SDL.h"
#include "filesystem.hpp"
struct Game;
struct Configuration;
struct Delegate;
struct Node
{
@ -17,7 +20,9 @@ struct Node
Node(Node*);
Game *get_root();
Configuration* get_configuration();
Delegate* get_delegate();
void print_branch();
virtual void respond(SDL_Event*) {};
virtual std::string get_class_name() { return "Node"; };
};