libscgui/Application.cpp
2020-10-24 15:58:58 +02:00

58 lines
1.4 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"
using namespace sc::gui;
Application Application::app;
void Application::run() {
bool quit {false};
while (!quit) {
for (const Window& window : Window::windows()) {
window.update();
}
SDL_Event event;
if (SDL_WaitEvent(&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;
}