src/context.c

changeset 294
4df350dac84f
parent 290
2eb3813562e7
equal deleted inserted replaced
293:24b0f47f619c 294:4df350dac84f
32 #include <SDL3_ttf/SDL_ttf.h> 32 #include <SDL3_ttf/SDL_ttf.h>
33 33
34 #include <GL/glew.h> 34 #include <GL/glew.h>
35 35
36 #include <time.h> 36 #include <time.h>
37 #include <assert.h>
37 38
38 AscContext asc_context; 39 AscContext asc_context;
39 40
40 static uint64_t asc_nanos(void) { 41 static uint64_t asc_nanos(void) {
41 struct timespec ts; 42 struct timespec ts;
117 asc_dprintf("Window access attempted without active window"); 118 asc_dprintf("Window access attempted without active window");
118 abort(); 119 abort();
119 } 120 }
120 } 121 }
121 122
123 static AscWindow * asc_window_find(Uint32 sdl_id) {
124 for (unsigned i = 0 ; i < ASC_MAX_WINDOWS ; i++) {
125 if (asc_context.windows[i].id == sdl_id) {
126 return asc_context.windows + i;
127 }
128 }
129 return NULL;
130 }
131
122 static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) { 132 static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) {
123 unsigned int i = asc_window_index(id); 133 AscWindow *window = asc_window_find(id);
124 if (i < ASC_MAX_WINDOWS) { 134 assert(window != NULL);
125 asc_context.windows[i].resized = true; 135 window->resized = true;
126 asc_context.windows[i].dimensions.width = (unsigned) width; 136 window->rect.size.width = (unsigned) width;
127 asc_context.windows[i].dimensions.height = (unsigned) height; 137 window->rect.size.height = (unsigned) height;
128 } 138 }
139
140 static void asc_event_window_moved(Uint32 id, Sint32 x, Sint32 y) {
141 AscWindow *window = asc_window_find(id);
142 assert(window != NULL);
143 window->moved = true;
144 window->rect.pos.x = x;
145 window->rect.pos.y = y;
129 } 146 }
130 147
131 bool asc_loop_next(void) { 148 bool asc_loop_next(void) {
132 // reset mouse motion 149 // reset mouse motion
133 asc_context.input.mouse_xrel = 0; 150 asc_context.input.mouse_xrel = 0;
143 while (SDL_PollEvent(&event)) { 160 while (SDL_PollEvent(&event)) {
144 switch (event.type) { 161 switch (event.type) {
145 case SDL_EVENT_QUIT: 162 case SDL_EVENT_QUIT:
146 asc_set_flag(asc_context.flags, ASC_FLAG_QUIT); 163 asc_set_flag(asc_context.flags, ASC_FLAG_QUIT);
147 break; 164 break;
165 case SDL_EVENT_WINDOW_MOVED: {
166 asc_event_window_moved(
167 event.window.windowID,
168 event.window.data1,
169 event.window.data2
170 );
171 break;
172 }
148 case SDL_EVENT_WINDOW_RESIZED: { 173 case SDL_EVENT_WINDOW_RESIZED: {
149 asc_event_window_resized( 174 asc_event_window_resized(
150 event.window.windowID, 175 event.window.windowID,
151 event.window.data1, 176 event.window.data1,
152 event.window.data2 177 event.window.data2
153 ); 178 );
154 break; 179 break;
155 } 180 }
156 case SDL_EVENT_WINDOW_FOCUS_GAINED: { 181 case SDL_EVENT_WINDOW_FOCUS_GAINED: {
157 unsigned int idx = asc_window_index(event.window.windowID); 182 asc_window_find(event.window.windowID)->focused = true;
158 asc_context.windows[idx].focused = true;
159 break; 183 break;
160 } 184 }
161 case SDL_EVENT_WINDOW_FOCUS_LOST: { 185 case SDL_EVENT_WINDOW_FOCUS_LOST: {
162 unsigned int idx = asc_window_index(event.window.windowID); 186 asc_window_find(event.window.windowID)->focused = false;
163 asc_context.windows[idx].focused = false;
164 break; 187 break;
165 } 188 }
166 case SDL_EVENT_MOUSE_MOTION: { 189 case SDL_EVENT_MOUSE_MOTION: {
167 // accumulate relative motion 190 // accumulate relative motion
168 asc_context.input.mouse_xrel += event.motion.xrel; 191 asc_context.input.mouse_xrel += event.motion.xrel;

mercurial