added run loop actions
This commit is contained in:
parent
4e12340507
commit
deddc2bbea
@ -8,6 +8,7 @@
|
||||
|
||||
#include "Application.hpp"
|
||||
#include "Window.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
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());
|
||||
}
|
||||
|
@ -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<SDL_EventType, std::vector<EventHandler>> _event_handlers;
|
||||
std::vector<RunLoopAction> _actions;
|
||||
|
||||
bool handle_event(const SDL_Event& event);
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user