70 lines
1.8 KiB
C++
70 lines
1.8 KiB
C++
//
|
|
// Application.cpp
|
|
// libscgui
|
|
//
|
|
// Created by Bob Polis at 2020-10-23
|
|
// Copyright (c) 2020 SwiftCoder. All rights reserved.
|
|
//
|
|
|
|
#include "Application.hpp"
|
|
#include "Window.hpp"
|
|
#include <algorithm>
|
|
|
|
using namespace sc::gui;
|
|
|
|
Application Application::app;
|
|
|
|
void Application::run() {
|
|
bool quit {false};
|
|
while (!quit) {
|
|
for (const Window& window : Window::windows()) {
|
|
window.update();
|
|
}
|
|
for (RunLoopAction action : _actions) {
|
|
action();
|
|
}
|
|
SDL_Event event;
|
|
if (SDL_PollEvent(&event)) {
|
|
switch (event.type) {
|
|
case SDL_QUIT:
|
|
quit = true;
|
|
break;
|
|
case SDL_WINDOWEVENT:
|
|
quit = Window::handle_window_event(event);
|
|
break;
|
|
default:
|
|
quit = handle_event(event);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void Application::add_event_handler(EventHandler handler, SDL_EventType event) {
|
|
auto it = _event_handlers.find(event);
|
|
if (it != _event_handlers.end()) {
|
|
it->second.push_back(handler);
|
|
} else {
|
|
std::vector<EventHandler> handlers {handler};
|
|
_event_handlers.emplace(event, handlers);
|
|
}
|
|
}
|
|
|
|
bool Application::handle_event(const SDL_Event& event) {
|
|
bool quit {false};
|
|
auto it = _event_handlers.find(static_cast<SDL_EventType>(event.type));
|
|
if (it != _event_handlers.end()) {
|
|
for (EventHandler handler : it->second) {
|
|
quit |= handler(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());
|
|
}
|