added run loop actions

This commit is contained in:
Bob Polis 2020-10-25 19:03:34 +01:00
parent 4e12340507
commit deddc2bbea
2 changed files with 17 additions and 1 deletions

View File

@ -8,6 +8,7 @@
#include "Application.hpp" #include "Application.hpp"
#include "Window.hpp" #include "Window.hpp"
#include <algorithm>
using namespace sc::gui; using namespace sc::gui;
@ -19,8 +20,11 @@ void Application::run() {
for (const Window& window : Window::windows()) { for (const Window& window : Window::windows()) {
window.update(); window.update();
} }
for (RunLoopAction action : _actions) {
action();
}
SDL_Event event; SDL_Event event;
if (SDL_WaitEvent(&event)) { if (SDL_PollEvent(&event)) {
switch (event.type) { switch (event.type) {
case SDL_QUIT: case SDL_QUIT:
quit = true; quit = true;
@ -55,3 +59,11 @@ bool Application::handle_event(const SDL_Event& event) {
} }
return quit; return quit;
} }
void Application::add_run_loop_action(RunLoopAction action) {
_actions.push_back(action);
}
void Application::remove_run_loop_action(RunLoopAction action) {
_actions.erase(std::remove(_actions.begin(), _actions.end(), action), _actions.end());
}

View File

@ -16,6 +16,7 @@
namespace sc { namespace sc {
namespace gui { namespace gui {
using EventHandler = bool(*)(const SDL_Event&); using EventHandler = bool(*)(const SDL_Event&);
using RunLoopAction = void(*)();
class Application { class Application {
public: public:
@ -32,11 +33,14 @@ namespace sc {
void run(); void run();
void add_event_handler(EventHandler handler, SDL_EventType event); void add_event_handler(EventHandler handler, SDL_EventType event);
void add_run_loop_action(RunLoopAction action);
void remove_run_loop_action(RunLoopAction action);
private: private:
static Application app; static Application app;
std::map<SDL_EventType, std::vector<EventHandler>> _event_handlers; std::map<SDL_EventType, std::vector<EventHandler>> _event_handlers;
std::vector<RunLoopAction> _actions;
bool handle_event(const SDL_Event& event); bool handle_event(const SDL_Event& event);
}; };