libscgui/Window.hpp
2020-10-24 18:02:12 +02:00

61 lines
2.1 KiB
C++

//
// Window.hpp
// gui
//
// Created by Bob Polis at 2020-10-14
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#ifndef __WINDOW_H_
#define __WINDOW_H_
#include <SDL2/SDL.h>
#include <string>
#include <memory>
#include <stdexcept>
#include <vector>
#include <map>
namespace sc {
namespace gui {
using WindowEventHandler = bool(*)(const SDL_Event&, bool quit);
class Image;
class Window {
public:
static std::vector<Window>& windows() { return _windows; }
static bool handle_window_event(const SDL_Event& event);
static bool handle_global_event(const SDL_Event& event);
static void add_global_event_handler(WindowEventHandler handler, SDL_WindowEventID type);
static Window* from_sdl(Uint32 window_id);
static Window& new_window(const std::string& title);
Window(const std::string& title);
void add_event_handler(WindowEventHandler handler, SDL_WindowEventID type);
bool handle_event(const SDL_Event& event, bool quit);
SDL_Window* window() const { return _w.get(); }
SDL_Renderer* renderer() const { return _r.get(); }
SDL_Texture* texture() const { return _t.get(); }
void set_size(int w, int h);
void update() const;
void show_image(const Image& image);
private:
static std::vector<Window> _windows;
static std::map<SDL_WindowEventID, std::vector<WindowEventHandler>> _global_event_handlers;
std::string _title;
std::unique_ptr<SDL_Window, void(*)(SDL_Window*)> _w {nullptr, SDL_DestroyWindow};
std::unique_ptr<SDL_Renderer, void(*)(SDL_Renderer*)> _r {nullptr, SDL_DestroyRenderer};
std::unique_ptr<SDL_Texture, void(*)(SDL_Texture*)> _t {nullptr, SDL_DestroyTexture};
std::map<SDL_WindowEventID, std::vector<WindowEventHandler>> _event_handlers;
};
}
}
#endif // __WINDOW_H_