10 #include <window_manager.hpp>
12 #include <SDL_image.h>
14 static bool singleton_initialized =
false;
21 singleton_initialized =
false;
22 SDL_FreeSurface(icon);
23 SDL_DestroyWindow(window);
34 : gMessage_bus(gMessageBus), gTask_manager(gTask_manager) {
36 if (singleton_initialized) {
37 throw std::runtime_error(
"The window manager cannot be initialized more than once!");
39 singleton_initialized =
true;
42 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
43 fprintf(stderr,
"Failed to initialize SDL: %s\n", SDL_GetError());
46 if (IMG_Init(IMG_INIT_PNG | IMG_INIT_WEBP) < 0) {
47 fprintf(stderr,
"Failed to initialize SDL_IMG: %s\n", IMG_GetError());
53 SDL_GetDisplayMode(0, 0, &DM);
54 width =
static_cast<int16_t
>(DM.w);
55 height =
static_cast<int16_t
>(DM.h);
56 window = SDL_CreateWindow(window_name.c_str(), 0, 0, width, height, SDL_WINDOW_OPENGL);
58 "Current screen resolution is " + std::to_string(width) +
"x" + std::to_string(height))));
59 uint32_t screen_width_height = width | (height << 16U);
60 gMessage_bus.
send_msg(
new message(REAL_SCREEN_COORDINATES, screen_width_height));
63 icon = IMG_Load(
"levels/icon.png");
64 SDL_SetWindowIcon(window, icon);
66 gMessage_bus.
subscribe(SET_FULLSCREEN, &msg_sub);
67 gMessage_bus.
subscribe(SET_WINDOW_RESOLUTION, &msg_sub);
75 void window_manager::update_task(
void *mngr) {
77 self->handle_messages();
84 void window_manager::handle_messages() {
86 while (temp !=
nullptr) {
87 switch (temp->msg_name) {
88 case SET_FULLSCREEN: {
89 auto arg =
static_cast<bool>(temp->base_data0);
94 case SET_WINDOW_BRIGHTNESS: {
95 auto arg = *
static_cast<float *
>(temp->
get_data());
98 new message(LOG_SUCCESS, make_data<std::string>(
"Brightness set to: " + std::to_string(arg))));
101 case SET_WINDOW_RESOLUTION: {
102 auto data = temp->base_data0;
103 int16_t new_width = data & 0x0000ffffU;
104 int16_t new_height = (data >> 16U) & 0x0000ffffU;
105 if (new_width != width && new_height != height) {
106 uint32_t screen_width_height = new_width | (new_height << 16U);
107 gMessage_bus.
send_msg(
new message(REAL_SCREEN_COORDINATES, screen_width_height));
108 SDL_SetWindowSize(window, new_width, new_height);
111 SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED,
112 SDL_WINDOWPOS_CENTERED);
134 void window_manager::set_fullscreen(
bool arg) {
136 if (arg && !(SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN)) {
137 SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
138 SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
139 }
else if (!arg && (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN)) {
140 SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
141 SDL_SetWindowFullscreen(window, 0);
142 SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
143 SDL_SetWindowFullscreen(window, 0);
144 SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
147 if(arg && !(SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN)) {
148 SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
150 else if(!arg && (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN)){
151 SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
152 SDL_SetWindowFullscreen(window, 0);
153 SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
163 void window_manager::set_brightness(
float arg) {
164 SDL_SetWindowBrightness(this->window, arg);
An object representing a task to be run by the task manager.
The central messaging system of the engine. All subsystem make extensive use of it.
void subscribe(uint8_t msg, subscriber *sub)
void send_msg(message *msg)
A message object passed around in the message bus. Holds anything created with make_data<>().
message * get_next_message()
The Task Manager handles all things multi-threaded in the engine.
void start_task_lockfree(ST::task *arg)
This object is responsible for managing the window.
SDL_Window * get_window()
window_manager(message_bus &gMessageBus, task_manager &gTask_manager, const std::string &window_name)