25 * POSSIBILITY OF SUCH DAMAGE. |
25 * POSSIBILITY OF SUCH DAMAGE. |
26 */ |
26 */ |
27 |
27 |
28 #include "ascension/window.h" |
28 #include "ascension/window.h" |
29 #include "ascension/context.h" |
29 #include "ascension/context.h" |
30 #include "ascension/error.h" |
|
31 #include "ascension/utils.h" |
|
32 |
30 |
33 #include <GL/glew.h> |
31 #include <GL/glew.h> |
34 |
32 |
35 void asc_window_settings_init_defaults(AscWindowSettings* settings) { |
33 void asc_window_settings_init_defaults(AscWindowSettings* settings) { |
36 settings->dimensions.width = 800; |
34 settings->dimensions.width = 800; |
50 } |
48 } |
51 AscWindow *window = &asc_context.windows[index]; |
49 AscWindow *window = &asc_context.windows[index]; |
52 if (window->id > 0) { |
50 if (window->id > 0) { |
53 asc_error("Cannot create window - slot %u already occupied.", index); |
51 asc_error("Cannot create window - slot %u already occupied.", index); |
54 return; |
52 return; |
55 } |
|
56 if (window->ui != NULL) { |
|
57 asc_wprintf("Window with index %u has a dangling UI pointer", index); |
|
58 asc_scene_node_free(window->ui); |
|
59 } |
53 } |
60 |
54 |
61 Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; |
55 Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; |
62 flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE; |
56 flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE; |
63 |
57 |
80 &window->dimensions.height |
74 &window->dimensions.height |
81 ); |
75 ); |
82 window->resized = true; // count initial sizing as resize |
76 window->resized = true; // count initial sizing as resize |
83 |
77 |
84 if (asc_gl_context_initialize(&window->glctx, window->window, &settings->glsettings)) { |
78 if (asc_gl_context_initialize(&window->glctx, window->window, &settings->glsettings)) { |
85 window->ui = asc_scene_node_empty(); |
79 asc_scene_init(&window->ui); |
86 asc_dprintf("Window %u initialized", window->id); |
80 asc_dprintf("Window %u initialized", window->id); |
87 asc_context.active_window = index; |
81 asc_context.active_window = index; |
88 } else { |
82 } else { |
89 asc_error("Creating GL context failed for window %u", window->id); |
83 asc_error("Creating GL context failed for window %u", window->id); |
90 // cleanup on error |
84 // cleanup on error |
108 |
102 |
109 // for releasing OpenGL resources, we need to make the context current |
103 // for releasing OpenGL resources, we need to make the context current |
110 asc_gl_context_activate(&window->glctx); |
104 asc_gl_context_activate(&window->glctx); |
111 |
105 |
112 // destroy all scenes |
106 // destroy all scenes |
113 asc_scene_node_free(window->ui); |
107 asc_scene_destroy(&window->ui); |
114 window->ui = NULL; |
|
115 |
108 |
116 // release context related data |
109 // release context related data |
117 asc_gl_context_destroy(&window->glctx); |
110 asc_gl_context_destroy(&window->glctx); |
118 |
111 |
119 // destroy window |
112 // destroy window |
144 // Clear the color buffer for the window frame |
137 // Clear the color buffer for the window frame |
145 int window_width = asc_active_window->dimensions.width; |
138 int window_width = asc_active_window->dimensions.width; |
146 int window_height = asc_active_window->dimensions.height; |
139 int window_height = asc_active_window->dimensions.height; |
147 glViewport(0, 0, window_width, window_height); |
140 glViewport(0, 0, window_width, window_height); |
148 glClear(GL_COLOR_BUFFER_BIT); |
141 glClear(GL_COLOR_BUFFER_BIT); |
149 asc_recti viewport = {0, 0, window_width, window_height}; |
|
150 |
142 |
151 // Draw the UI |
143 // Draw the UI |
152 AscCamera ui_camera; |
144 AscScene *ui = &asc_active_window->ui; |
153 asc_camera_ortho(&ui_camera, viewport); |
145 asc_recti viewport = {0, 0, window_width, window_height}; |
154 asc_scene_draw(asc_active_window->ui, viewport, &ui_camera); |
146 asc_camera_ortho(&ui->camera, viewport); |
|
147 asc_scene_draw(ui, viewport); |
155 |
148 |
156 // Swap Buffers |
149 // Swap Buffers |
157 SDL_GL_SwapWindow(asc_active_window->window); |
150 SDL_GL_SwapWindow(asc_active_window->window); |
158 |
151 |
159 // Clear Flags |
152 // Clear Flags |