From c71635e5d6cff8edc0c14ab040cd044052c1f7f2 Mon Sep 17 00:00:00 2001 From: Frank DeMarco Date: Fri, 31 May 2019 21:53:39 -0400 Subject: [PATCH] keyup sends cancel to command subscribers --- demo/Demo.cpp | 36 ++++++++++++++++++------------------ demo/Makefile | 2 +- src/Delegate.cpp | 19 +++++++++++++++++-- src/Delegate.hpp | 4 +++- src/Input.cpp | 19 +++++++++++-------- src/Node.cpp | 2 +- src/Sprite.cpp | 1 + 7 files changed, 52 insertions(+), 31 deletions(-) diff --git a/demo/Demo.cpp b/demo/Demo.cpp index f00e376..a6894a3 100644 --- a/demo/Demo.cpp +++ b/demo/Demo.cpp @@ -129,8 +129,8 @@ struct Demo : Game Mix_PlayMusic(music, -1); load_gl_context(); delegate.subscribe(&Demo::respond, this); - Input* l = new Input(this); - delete l; + // Input* l = new Input(this); + // delete l; // input.print_branch(); // mushroom.print_branch(); // Input* i = new Input(this); @@ -424,22 +424,22 @@ struct Demo : Game roundedBoxColor(renderer, 300, 200, 500, 300, 10, 0x8f8fdfff); aacircleColor(renderer, 300, 200, 30, 0xffef3fff); int speed = 2; - if (up_active) - { - // grass.move(0, -speed); - } - if (right_active) - { - // grass.move(speed); - } - if (down_active) - { - // grass.move(0, speed); - } - if (left_active) - { - // grass.move(-speed, 0); - } + // if (up_active) + // { + // grass.move(0, -speed); + // } + // if (right_active) + // { + // grass.move(speed); + // } + // if (down_active) + // { + // grass.move(0, speed); + // } + // if (left_active) + // { + // grass.move(-speed, 0); + // } // grass.update(); // mushroom.update(); SDL_RenderPresent(renderer); diff --git a/demo/Makefile b/demo/Makefile index 151a1ff..db2222f 100644 --- a/demo/Makefile +++ b/demo/Makefile @@ -6,7 +6,7 @@ GLEW_DIR = $(SFW_LIB_DIR)glew/ CC_LINUX = clang-7 CPPC_LINUX = clang++-7 SDLCONFIG = /home/frank/local/sdl/bin/sdl2-config -CFLAGS = -Wall -O2 -c -I$(SFW_LIB_DIR) -I$(SFW_SRC_DIR) +CFLAGS = -Wall -O0 -c -I$(SFW_LIB_DIR) -I$(SFW_SRC_DIR) -g CPP_FLAGS = $(CFLAGS) --std=c++17 SDL_FLAGS = $(shell $(SDLCONFIG) --cflags) LFLAGS = $(shell $(SDLCONFIG) --libs) -lpthread diff --git a/src/Delegate.cpp b/src/Delegate.cpp index 4a0be9d..f23ef52 100644 --- a/src/Delegate.cpp +++ b/src/Delegate.cpp @@ -28,11 +28,26 @@ void Delegate::dispatch() } } } + if (event.type == command_event_type) + { + delete static_cast(event.user.data2); + } } } -bool Delegate::compare(SDL_Event& event, std::string command) +bool Delegate::compare(SDL_Event& event, std::string command, bool neutral, bool cancel) { return event.type == command_event_type and - *static_cast(event.user.data1) == command; + (command == "" or command == *static_cast(event.user.data1)) and + (neutral or (cancel == *static_cast(event.user.data2))); +} + +bool Delegate::compare_cancel(SDL_Event& event, std::string command) +{ + return compare(event, command, false, true); +} + +bool Delegate::compare_neutral(SDL_Event& event, std::string command) +{ + return compare(event, command, true); } diff --git a/src/Delegate.hpp b/src/Delegate.hpp index 195889f..b668896 100644 --- a/src/Delegate.hpp +++ b/src/Delegate.hpp @@ -27,7 +27,9 @@ struct Delegate : Node Delegate(Node*); void add_subscriber(Subscriber, int); void dispatch(); - bool compare(SDL_Event&, std::string); + bool compare(SDL_Event&, std::string = "", bool = false, bool = false); + bool compare_cancel(SDL_Event&, std::string = ""); + bool compare_neutral(SDL_Event&, std::string = ""); template void subscribe(void(T::*f)(SDL_Event&), T* o, int type = command_event_type) diff --git a/src/Input.cpp b/src/Input.cpp index 2e3f90a..7164b26 100644 --- a/src/Input.cpp +++ b/src/Input.cpp @@ -4,6 +4,7 @@ Input::Input(Node *parent) : Node(parent) { load_key_map(); get_delegate().subscribe(&Input::respond, this, SDL_KEYDOWN); + get_delegate().subscribe(&Input::respond, this, SDL_KEYUP); for (KeyCombination& combination : key_map) { print_key_combination(combination); @@ -74,21 +75,23 @@ void Input::add_to_key_map( void Input::respond(SDL_Event &event) { - if (event.type == SDL_KEYDOWN) - { + // if (event.type == SDL_KEYDOWN) + // { SDL_Keymod mod = SDL_GetModState(); - for (KeyCombination &combination : key_map) + for (KeyCombination& combination : key_map) { if (event.key.keysym.sym == combination.key and (not combination.ctrl || mod & KMOD_CTRL) and (not combination.shift || mod & KMOD_SHIFT) and (not combination.alt || mod & KMOD_ALT)) { - SDL_Event event; - event.type = Delegate::command_event_type; - event.user.data1 = &combination.command; - SDL_PushEvent(&event); + SDL_Event relay; + bool* cancel = new bool(event.type == SDL_KEYDOWN ? false : true); + relay.type = Delegate::command_event_type; + relay.user.data1 = &combination.command; + relay.user.data2 = cancel; + SDL_PushEvent(&relay); } } - } + // } } diff --git a/src/Node.cpp b/src/Node.cpp index 78bc695..175a27e 100644 --- a/src/Node.cpp +++ b/src/Node.cpp @@ -44,7 +44,7 @@ Display& Node::get_display() void Node::print_branch() { - Node *current = this; + Node* current = this; while (current != NULL) { std::cout << current->get_class_name() << " @ " << current; diff --git a/src/Sprite.cpp b/src/Sprite.cpp index e3755e2..0981372 100644 --- a/src/Sprite.cpp +++ b/src/Sprite.cpp @@ -1,4 +1,5 @@ #include "Sprite.hpp" +#include "Game.hpp" Sprite::Sprite(Node *parent) : Node(parent) {}