pass object bound functions to delegate

This commit is contained in:
Frank DeMarco 2019-05-03 22:16:56 -04:00
parent eb35aedeef
commit 845d3bb883
12 changed files with 74 additions and 32 deletions

View File

@ -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);

View File

@ -7,6 +7,7 @@
#include <cstdlib>
#include <algorithm>
#include <string>
#include <functional>
#include <SDL_image.h>
#include "sdl2-gfx/SDL2_gfxPrimitives.h"

View File

@ -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) \

View File

@ -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()

View File

@ -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);

View File

@ -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);
}
}
}

View File

@ -3,24 +3,21 @@
#include <map>
#include <list>
#include <functional>
#include "Node.hpp"
#include "SDL.h"
struct Subscriber
{
Node *instance;
void (Node::*callback)(SDL_Event*);
};
typedef std::function<void(SDL_Event*)> subscriber;
struct Delegate : Node
{
std::map<SDL_EventType, std::list<Subscriber>> subscribers;
std::map<SDL_EventType, std::list<subscriber>> subscribers;
Delegate(Node*);
void add_subscriber(Node*, void (Node::*)(SDL_Event*), SDL_EventType);
void add_subscriber(subscriber, SDL_EventType);
void dispatch();
};

View File

@ -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)
{

View File

@ -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"; }

View File

@ -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)

View File

@ -1,17 +1,51 @@
#ifndef Input_h_
#define Input_h_
#include <map>
#include <functional>
#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<std::string, unsigned int> 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<KeyCombination> key_map;
Input(Node*);
void respond(SDL_Event*);
std::string get_class_name() { return "Input"; }
void load_key_map();
void add_to_key_map();
};

View File

@ -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"; };
};