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

View File

@ -34,6 +34,7 @@
#include "Location.hpp" #include "Location.hpp"
#include "Sprite.hpp" #include "Sprite.hpp"
#include "Input.hpp" #include "Input.hpp"
#include "Delegate.hpp"
struct Mushroom : Sprite 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 $(GLEW_DIR)%.o: $(GLEW_DIR)%.c $(GLEW_DIR)%.h
$(CC_LINUX) $(CFLAGS) $< -o $@ $(CC_LINUX) $(CFLAGS) $< -o $@
$(SFW_SRC_DIR)Sprite.o: $(addprefix $(SFW_SRC_DIR),Node.hpp Game.hpp Location.hpp) $(SFW_SRC_DIR)Sprite.o: $(addprefix $(SFW_SRC_DIR),Game.hpp Location.hpp)
$(SFW_SRC_DIR)Game.o: $(addprefix $(SFW_SRC_DIR),Node.hpp Sprite.hpp Configuration.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)Node.o: $(addprefix $(SFW_SRC_DIR),Game.hpp Configuration.hpp)
$(SFW_SRC_DIR)Configuration.o: $(SFW_SRC_DIR)Node.hpp $(SFW_SRC_DIR)%.o: $(addprefix $(SFW_SRC_DIR),%.cpp %.hpp Node.cpp)
$(SFW_SRC_DIR)Input.o: $(SFW_SRC_DIR)Node.hpp
$(SFW_SRC_DIR)%.o: $(addprefix $(SFW_SRC_DIR),%.cpp %.hpp)
$(CPPC_LINUX) $(CPP_FLAGS) $(SDL_FLAGS) $< -o $@ $(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) 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 $@ $(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) $(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 $(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() Game::Game()
{ {
delegate->add_subscriber(this, &Node::respond, SDL_QUIT);
std::cout << "GLEW " << glewGetString(GLEW_VERSION) << std::endl; std::cout << "GLEW " << glewGetString(GLEW_VERSION) << std::endl;
putenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN=0"); putenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN=0");
putenv("SDL_VIDEO_CENTERED=1"); putenv("SDL_VIDEO_CENTERED=1");
@ -129,16 +130,13 @@ void Game::run()
last_frame_length = ticks - last_frame_timestamp; last_frame_length = ticks - last_frame_timestamp;
frame_time_overflow = last_frame_length + frame_time_overflow - frame_length; frame_time_overflow = last_frame_length + frame_time_overflow - frame_length;
last_frame_timestamp = ticks; last_frame_timestamp = ticks;
delegate->dispatch();
update(); update();
} }
SDL_Delay(15); SDL_Delay(15);
} }
} }
// void Game::update()
// {
// }
void Game::flag_to_end() void Game::flag_to_end()
{ {
done = true; done = true;
@ -154,6 +152,14 @@ void Game::set_framerate(int fps)
frame_length = 1000.0 / framerate; frame_length = 1000.0 / framerate;
} }
void Game::respond(SDL_Event *event)
{
if (event->type == SDL_QUIT)
{
flag_to_end();
}
}
void Game::quit() void Game::quit()
{ {
if (glcontext != NULL) if (glcontext != NULL)

View File

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

View File

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

View File

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