From 845d3bb883740c51c1fa12802804ebefd7f18f67 Mon Sep 17 00:00:00 2001 From: Frank DeMarco Date: Fri, 3 May 2019 22:16:56 -0400 Subject: [PATCH] pass object bound functions to delegate --- demo/Demo.cpp | 6 ++++-- demo/Demo.hpp | 1 + demo/Makefile | 10 +++++----- src/Configuration.cpp | 14 +++++++++----- src/Configuration.hpp | 3 +-- src/Delegate.cpp | 9 +++------ src/Delegate.hpp | 11 ++++------- src/Game.cpp | 4 ++-- src/Game.hpp | 2 +- src/Input.cpp | 11 ++++++++++- src/Input.hpp | 34 ++++++++++++++++++++++++++++++++++ src/Node.hpp | 1 - 12 files changed, 74 insertions(+), 32 deletions(-) diff --git a/demo/Demo.cpp b/demo/Demo.cpp index 9a35b4d..a1a002d 100644 --- a/demo/Demo.cpp +++ b/demo/Demo.cpp @@ -1,4 +1,6 @@ -// reset, pause, auto reset, analog d-pad, gamepad config, any key +// reset, pause, auto reset, analog d-pad, gamepad config, any key, confirm exit game + +// sweaty gamer hands oily snacks and bad hygiene #include "Demo.hpp" @@ -169,7 +171,7 @@ struct Demo : Game Mushroom *mushroom = new Mushroom(this); Sprite *grass = new Sprite(this, "resource/Field.png"); - Demo() : Game() + Demo() { Mix_Music *music = Mix_LoadMUS("resource/Field.mp3"); Mix_PlayMusic(music, -1); diff --git a/demo/Demo.hpp b/demo/Demo.hpp index 4b5d5a1..194c0f2 100644 --- a/demo/Demo.hpp +++ b/demo/Demo.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "sdl2-gfx/SDL2_gfxPrimitives.h" diff --git a/demo/Makefile b/demo/Makefile index 49e2915..7f1220c 100644 --- a/demo/Makefile +++ b/demo/Makefile @@ -36,13 +36,13 @@ $(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),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)%.o: $(addprefix $(SFW_SRC_DIR),%.cpp %.hpp Node.cpp) +$(SFW_SRC_DIR)Sprite.o: $(addprefix $(SFW_SRC_DIR),Game.*pp Location.*pp) +$(SFW_SRC_DIR)Game.o: $(addprefix $(SFW_SRC_DIR),Sprite.*pp Configuration.*pp Delegate.*pp) +$(SFW_SRC_DIR)Node.o: $(addprefix $(SFW_SRC_DIR),Game.*pp Configuration.*pp) +$(SFW_SRC_DIR)%.o: $(addprefix $(SFW_SRC_DIR),%.cpp %.hpp Node.*pp) $(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.*pp Node.*pp Game.*pp Location.*pp Input.*pp) $(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 Delegate.o) \ diff --git a/src/Configuration.cpp b/src/Configuration.cpp index 1778ae6..d99b3a5 100644 --- a/src/Configuration.cpp +++ b/src/Configuration.cpp @@ -17,6 +17,7 @@ void Configuration::set_defaults() {"record", {"CTRL", "SHIFT", "F10"}}, {"screenshot", "F9"} }; + config = sys_config; } void Configuration::load() @@ -27,12 +28,15 @@ void Configuration::load() void Configuration::load(fs::path path) { std::ifstream contents(path); - contents >> config; - config["blob"] = false; - config["slime"][1] = {420, 69}; - config["goo"] = "yum"; + contents >> game_config; + // config["blob"] = false; + // config["slime"][1] = {420, 69}; + // config["goo"] = "yum"; + // std::cout << std::setw(4) << config << std::endl; + // std::cout << std::setw(4) << sys_config << std::endl; + config.update(game_config); std::cout << std::setw(4) << config << std::endl; - std::cout << std::setw(4) << sys_config << std::endl; + std::cout << std::setw(4) << game_config << std::endl; } void Configuration::write() diff --git a/src/Configuration.hpp b/src/Configuration.hpp index 8275402..43d28bb 100644 --- a/src/Configuration.hpp +++ b/src/Configuration.hpp @@ -12,10 +12,9 @@ struct Configuration : Node { - nlohmann::json sys_config, config; + nlohmann::json sys_config, game_config, config; fs::path config_path; int tab_width = 4; - std::string class_name = "Configuration"; Configuration(Node*); Configuration(Node*, fs::path); diff --git a/src/Delegate.cpp b/src/Delegate.cpp index 0045587..1d7c71e 100644 --- a/src/Delegate.cpp +++ b/src/Delegate.cpp @@ -2,13 +2,12 @@ Delegate::Delegate(Node *parent) : Node(parent) {} -void Delegate::add_subscriber(Node *instance, void (Node::*callback)(SDL_Event*), SDL_EventType type) +void Delegate::add_subscriber(subscriber s, SDL_EventType type) { if (subscribers.count(type) == 0) { subscribers[type] = {}; } - Subscriber s = {instance, callback}; subscribers[type].push_back(s); } @@ -21,11 +20,9 @@ void Delegate::dispatch() { if (event.type == iter->first) { - for (Subscriber subscriber : iter->second) + for (subscriber s : iter->second) { - Node *n = subscriber.instance; - void (Node::*callback)(SDL_Event*) = subscriber.callback; - (n->*callback)(&event); + s(&event); } } } diff --git a/src/Delegate.hpp b/src/Delegate.hpp index 27c604f..b27806f 100644 --- a/src/Delegate.hpp +++ b/src/Delegate.hpp @@ -3,24 +3,21 @@ #include #include +#include #include "Node.hpp" #include "SDL.h" -struct Subscriber -{ - Node *instance; - void (Node::*callback)(SDL_Event*); -}; +typedef std::function subscriber; struct Delegate : Node { - std::map> subscribers; + std::map> subscribers; Delegate(Node*); - void add_subscriber(Node*, void (Node::*)(SDL_Event*), SDL_EventType); + void add_subscriber(subscriber, SDL_EventType); void dispatch(); }; diff --git a/src/Game.cpp b/src/Game.cpp index b82d3bf..b345438 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -2,7 +2,7 @@ Game::Game() { - delegate->add_subscriber(this, &Node::respond, SDL_QUIT); + delegate->add_subscriber(std::bind(&Game::handle_quit_event, this, std::placeholders::_1), SDL_QUIT); std::cout << "GLEW " << glewGetString(GLEW_VERSION) << std::endl; putenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN=0"); putenv("SDL_VIDEO_CENTERED=1"); @@ -152,7 +152,7 @@ void Game::set_framerate(int fps) frame_length = 1000.0 / framerate; } -void Game::respond(SDL_Event *event) +void Game::handle_quit_event(SDL_Event *event) { if (event->type == SDL_QUIT) { diff --git a/src/Game.hpp b/src/Game.hpp index 22a2448..5491ee2 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -48,7 +48,7 @@ struct Game : Node void flag_to_end(); virtual void update() {}; void set_framerate(int); - void respond(SDL_Event*); + void handle_quit_event(SDL_Event*); void quit(); std::string get_class_name() { return "Game"; } diff --git a/src/Input.cpp b/src/Input.cpp index 0b06095..1d27eb4 100644 --- a/src/Input.cpp +++ b/src/Input.cpp @@ -2,7 +2,16 @@ Input::Input(Node *parent) : Node(parent) { - get_delegate()->add_subscriber(this, &Node::respond, SDL_KEYDOWN); + load_key_map(); + get_delegate()->add_subscriber(std::bind(&Input::respond, this, std::placeholders::_1), SDL_KEYDOWN); +} + +void Input::load_key_map() +{ +} + +void Input::add_to_key_map() +{ } void Input::respond(SDL_Event *event) diff --git a/src/Input.hpp b/src/Input.hpp index 2d20416..ef5df49 100644 --- a/src/Input.hpp +++ b/src/Input.hpp @@ -1,17 +1,51 @@ #ifndef Input_h_ #define Input_h_ +#include +#include + #include "SDL.h" #include "Node.hpp" #include "Delegate.hpp" +struct KeyCombination +{ + bool alt; + bool ctrl; + bool shift; + unsigned int key; +}; + struct Input : Node { + + std::map key_ids + { + {"up", SDLK_UP}, + {"right", SDLK_RIGHT}, + {"down", SDLK_DOWN}, + {"left", SDLK_LEFT}, + {"f1", SDLK_F1}, + {"f2", SDLK_F2}, + {"f3", SDLK_F3}, + {"f4", SDLK_F4}, + {"f5", SDLK_F5}, + {"f6", SDLK_F6}, + {"f7", SDLK_F7}, + {"f8", SDLK_F8}, + {"f9", SDLK_F9}, + {"f10", SDLK_F10}, + {"f11", SDLK_F11}, + {"f12", SDLK_F11} + }; + std::list key_map; Input(Node*); void respond(SDL_Event*); std::string get_class_name() { return "Input"; } + void load_key_map(); + void add_to_key_map(); }; diff --git a/src/Node.hpp b/src/Node.hpp index cdb1194..458c643 100644 --- a/src/Node.hpp +++ b/src/Node.hpp @@ -22,7 +22,6 @@ struct Node Configuration* get_configuration(); Delegate* get_delegate(); void print_branch(); - virtual void respond(SDL_Event*) {}; virtual std::string get_class_name() { return "Node"; }; };