libscgui/Application.hpp

54 lines
1.5 KiB
C++
Raw Normal View History

//
// Application.hpp
// libscgui
//
// Created by Bob Polis at 2020-10-23
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#ifndef _Application_H_
#define _Application_H_
#include <SDL2/SDL.h>
#include <map>
#include <vector>
namespace sc {
namespace gui {
using EventHandler = bool(*)(const SDL_Event&);
2020-10-25 19:03:34 +01:00
using RunLoopAction = void(*)();
class Application {
public:
static Application& instance() { return app; }
Application() = default;
~Application() = default;
// prohibit copying and moving
Application(const Application&) = delete;
Application& operator=(const Application&) = delete;
Application(Application&&) = delete;
Application& operator=(Application&&) = delete;
void run();
void add_event_handler(EventHandler handler, SDL_EventType event);
2020-10-25 19:03:34 +01:00
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;
2020-10-25 19:03:34 +01:00
std::vector<RunLoopAction> _actions;
bool handle_event(const SDL_Event& event);
};
// convenience function
Application& app() { return Application::instance(); }
}
}
#endif // _Application_H_