src/window.c

changeset 47
44457f6cb0a2
parent 44
b3da4096c607
child 64
f18dc427f86f
equal deleted inserted replaced
46:d3285aed65b3 47:44457f6cb0a2
41 settings->glsettings.gl_major_version = 4; 41 settings->glsettings.gl_major_version = 4;
42 settings->glsettings.gl_minor_version = 0; 42 settings->glsettings.gl_minor_version = 0;
43 settings->title = "Ascended Window"; 43 settings->title = "Ascended Window";
44 } 44 }
45 45
46 static void asc_window_init_scenes(AscWindow *window) {
47 asc_scene_init(&window->ui);
48 asc_camera_ortho(&window->ui.cameras[0], (asc_recti){
49 0, 0, window->dimensions
50 });
51 }
52
53 AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) { 46 AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) {
54 if (index >= ASC_MAX_WINDOWS) { 47 if (index >= ASC_MAX_WINDOWS) {
55 asc_error("Maximum number of windows exceeded."); 48 asc_error("Maximum number of windows exceeded.");
56 return NULL; 49 return NULL;
57 } 50 }
58 AscWindow *window = &asc_context.windows[index]; 51 AscWindow *window = &asc_context.windows[index];
59 if (window->id > 0) { 52 if (window->id > 0) {
60 asc_error("Cannot create window - slot already occupied."); 53 asc_error("Cannot create window - slot already occupied.");
61 asc_dprintf("Tried to create window with index %u", index); 54 asc_dprintf("Tried to create window with index %u twice", index);
62 return NULL; 55 return NULL;
56 }
57 if (window->ui != NULL) {
58 asc_dprintf("Window with index %u has a dangling UI pointer", index);
59 asc_scene_node_free(window->ui);
63 } 60 }
64 61
65 Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; 62 Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
66 flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE; 63 flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE;
67 64
84 &window->dimensions.height 81 &window->dimensions.height
85 ); 82 );
86 window->resized = true; // count initial sizing as resize 83 window->resized = true; // count initial sizing as resize
87 84
88 if (asc_gl_context_initialize(&window->glctx, window->window, &settings->glsettings)) { 85 if (asc_gl_context_initialize(&window->glctx, window->window, &settings->glsettings)) {
86 window->ui = asc_scene_node_empty();
89 asc_dprintf("Window %u initialized", window->id); 87 asc_dprintf("Window %u initialized", window->id);
90 asc_context.active_window = window; 88 asc_context.active_window = window;
91 asc_window_init_scenes(window);
92 return window; 89 return window;
93 } else { 90 } else {
94 asc_dprintf("Creating GL context failed for window %u", window->id); 91 asc_dprintf("Creating GL context failed for window %u", window->id);
95 // cleanup on error 92 // cleanup on error
96 SDL_DestroyWindow(window->window); 93 SDL_DestroyWindow(window->window);
108 if (asc_context.active_window == window) { 105 if (asc_context.active_window == window) {
109 asc_context.active_window = NULL; 106 asc_context.active_window = NULL;
110 } 107 }
111 108
112 // destroy all scenes 109 // destroy all scenes
113 asc_scene_destroy(&window->ui); 110 asc_scene_node_free(window->ui);
111 window->ui = NULL;
114 112
115 // release context related data 113 // release context related data
116 asc_gl_context_destroy(&window->glctx); 114 asc_gl_context_destroy(&window->glctx);
117 115
118 // destroy window 116 // destroy window
134 AscWindow *active_window = asc_context.active_window; 132 AscWindow *active_window = asc_context.active_window;
135 if (window != active_window) { 133 if (window != active_window) {
136 asc_window_activate(window); 134 asc_window_activate(window);
137 } 135 }
138 136
137 // Clear the color buffer for the window frame
138 int window_width = window->dimensions.width;
139 int window_height = window->dimensions.height;
140 glViewport(0, 0, window_width, window_height);
141 glClear(GL_COLOR_BUFFER_BIT);
142 asc_recti viewport = {0, 0, window_width, window_height};
143
139 // Draw the UI 144 // Draw the UI
140 asc_scene_draw(&window->ui); 145 AscCamera ui_camera;
146 asc_camera_ortho(&ui_camera, viewport);
147 asc_scene_draw(window->ui, viewport, &ui_camera);
141 148
142 // Swap Buffers 149 // Swap Buffers
143 SDL_GL_SwapWindow(window->window); 150 SDL_GL_SwapWindow(window->window);
144 151
145 // Clear Flags 152 // Clear Flags

mercurial