1.1 --- a/src/window.c Thu Apr 18 21:53:53 2024 +0200 1.2 +++ b/src/window.c Thu Apr 18 22:53:55 2024 +0200 1.3 @@ -43,16 +43,16 @@ 1.4 settings->title = "Ascended Window"; 1.5 } 1.6 1.7 -AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) { 1.8 +void asc_window_initialize(unsigned int index, AscWindowSettings const *settings) { 1.9 if (index >= ASC_MAX_WINDOWS) { 1.10 asc_error("Maximum number of windows exceeded."); 1.11 - return NULL; 1.12 + return; 1.13 } 1.14 AscWindow *window = &asc_context.windows[index]; 1.15 if (window->id > 0) { 1.16 asc_error("Cannot create window - slot already occupied."); 1.17 asc_dprintf("Tried to create window with index %u twice", index); 1.18 - return NULL; 1.19 + return; 1.20 } 1.21 if (window->ui != NULL) { 1.22 asc_dprintf("Window with index %u has a dangling UI pointer", index); 1.23 @@ -72,7 +72,7 @@ 1.24 ); 1.25 if (window->window == NULL) { 1.26 asc_error(SDL_GetError()); 1.27 - return NULL; 1.28 + return; 1.29 } 1.30 1.31 window->id = SDL_GetWindowID(window->window); 1.32 @@ -85,27 +85,31 @@ 1.33 if (asc_gl_context_initialize(&window->glctx, window->window, &settings->glsettings)) { 1.34 window->ui = asc_scene_node_empty(); 1.35 asc_dprintf("Window %u initialized", window->id); 1.36 - asc_window_active = window; 1.37 - return window; 1.38 + asc_context.active_window = index; 1.39 } else { 1.40 asc_dprintf("Creating GL context failed for window %u", window->id); 1.41 // cleanup on error 1.42 SDL_DestroyWindow(window->window); 1.43 window->window = NULL; 1.44 window->id = 0; 1.45 - return NULL; 1.46 } 1.47 } 1.48 1.49 -void asc_window_destroy(AscWindow* window) { 1.50 +void asc_window_destroy(unsigned int index) { 1.51 // safeguard 1.52 - if (window->id == 0) return; 1.53 + if (asc_context.windows[index].id == 0) return; 1.54 1.55 // this window cannot be active anymore 1.56 - if (asc_window_active == window) { 1.57 - asc_window_active = NULL; 1.58 + if (asc_context.active_window == index) { 1.59 + asc_context.active_window = ASC_MAX_WINDOWS; 1.60 } 1.61 1.62 + // pointer to the window 1.63 + AscWindow *window = &asc_context.windows[index]; 1.64 + 1.65 + // for releasing OpenGL resources, we need to make the context current 1.66 + asc_gl_context_activate(&window->glctx); 1.67 + 1.68 // destroy all scenes 1.69 asc_scene_node_free(window->ui); 1.70 window->ui = NULL; 1.71 @@ -119,8 +123,8 @@ 1.72 } 1.73 1.74 // if another window was active, make the other context current again 1.75 - if (asc_window_active != NULL) { 1.76 - asc_gl_context_activate(&asc_window_active->glctx); 1.77 + if (asc_context.active_window < ASC_MAX_WINDOWS) { 1.78 + asc_gl_context_activate(&asc_active_window->glctx); 1.79 } 1.80 1.81 // clean the data 1.82 @@ -128,15 +132,19 @@ 1.83 memset(window, 0, sizeof(AscWindow)); 1.84 } 1.85 1.86 -void asc_window_sync(AscWindow* window) { 1.87 - AscWindow *active_window = asc_window_active; 1.88 - if (window != active_window) { 1.89 - asc_window_activate(window); 1.90 +void asc_window_sync(unsigned int index) { 1.91 + // necessary safeguard 1.92 + if (asc_context.windows[index].id == 0) return; 1.93 + 1.94 + // active the window that shall be synced temporarily 1.95 + unsigned int active_index = asc_context.active_window; 1.96 + if (index != active_index) { 1.97 + asc_window_activate(index); 1.98 } 1.99 1.100 // Clear the color buffer for the window frame 1.101 - int window_width = window->dimensions.width; 1.102 - int window_height = window->dimensions.height; 1.103 + int window_width = asc_active_window->dimensions.width; 1.104 + int window_height = asc_active_window->dimensions.height; 1.105 glViewport(0, 0, window_width, window_height); 1.106 glClear(GL_COLOR_BUFFER_BIT); 1.107 asc_recti viewport = {0, 0, window_width, window_height}; 1.108 @@ -144,20 +152,20 @@ 1.109 // Draw the UI 1.110 AscCamera ui_camera; 1.111 asc_camera_ortho(&ui_camera, viewport); 1.112 - asc_scene_draw(window->ui, viewport, &ui_camera); 1.113 + asc_scene_draw(asc_active_window->ui, viewport, &ui_camera); 1.114 1.115 // Swap Buffers 1.116 - SDL_GL_SwapWindow(window->window); 1.117 + SDL_GL_SwapWindow(asc_active_window->window); 1.118 1.119 // Clear Flags 1.120 - window->resized = false; 1.121 + asc_active_window->resized = false; 1.122 1.123 - if (window != active_window) { 1.124 - asc_window_activate(active_window); 1.125 + if (index != active_index) { 1.126 + asc_window_activate(active_index); 1.127 } 1.128 } 1.129 1.130 -void asc_window_activate(AscWindow *window) { 1.131 - asc_gl_context_activate(&window->glctx); 1.132 - asc_window_active = (AscWindow *)window; 1.133 +void asc_window_activate(unsigned int index) { 1.134 + asc_context.active_window = index; 1.135 + asc_gl_context_activate(&asc_active_window->glctx); 1.136 }