diff --git a/Application.cpp b/Application.cpp index 7993228..b591f2d 100644 --- a/Application.cpp +++ b/Application.cpp @@ -8,6 +8,7 @@ #include "Application.hpp" #include "Window.hpp" +#include using namespace sc::gui; @@ -19,8 +20,11 @@ void Application::run() { for (const Window& window : Window::windows()) { window.update(); } + for (RunLoopAction action : _actions) { + action(); + } SDL_Event event; - if (SDL_WaitEvent(&event)) { + if (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: quit = true; @@ -55,3 +59,11 @@ bool Application::handle_event(const SDL_Event& event) { } 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()); +} diff --git a/Application.hpp b/Application.hpp index 60ada5c..c5015a2 100644 --- a/Application.hpp +++ b/Application.hpp @@ -16,6 +16,7 @@ namespace sc { namespace gui { using EventHandler = bool(*)(const SDL_Event&); + using RunLoopAction = void(*)(); class Application { public: @@ -32,11 +33,14 @@ namespace sc { void run(); void add_event_handler(EventHandler handler, SDL_EventType event); + void add_run_loop_action(RunLoopAction action); + void remove_run_loop_action(RunLoopAction action); private: static Application app; std::map> _event_handlers; + std::vector _actions; bool handle_event(const SDL_Event& event); };