72 NULL, |
71 NULL, |
73 CX_BUFFER_AUTO_EXTEND |
72 CX_BUFFER_AUTO_EXTEND |
74 ); |
73 ); |
75 |
74 |
76 // initialize SDL |
75 // initialize SDL |
77 const int supported_img_flags = IMG_INIT_PNG | IMG_INIT_JPG; |
76 if (!SDL_Init(SDL_INIT_VIDEO)) { |
78 if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
|
79 asc_error("Failed to initialize SDL: %s", SDL_GetError()); |
77 asc_error("Failed to initialize SDL: %s", SDL_GetError()); |
80 } else if (TTF_Init() < 0) { |
78 } else if (!TTF_Init()) { |
81 asc_error("Failed to initialize SDL_ttf: %s", TTF_GetError()); |
79 asc_error("Failed to initialize SDL_ttf: %s", SDL_GetError()); |
82 } else if (IMG_Init(supported_img_flags) != supported_img_flags) { |
|
83 asc_error("Failed to initialize SDL_img: %s", IMG_GetError()); |
|
84 } |
80 } |
85 SDL_ClearError(); |
81 SDL_ClearError(); |
86 asc_context.total_nanos = asc_nanos(); |
82 asc_context.total_nanos = asc_nanos(); |
87 asc_set_flag(asc_context.flags, ASC_FLAG_INITIALIZED); |
83 asc_set_flag(asc_context.flags, ASC_FLAG_INITIALIZED); |
88 asc_dprintf("Ascension context initialized."); |
84 asc_dprintf("Ascension context initialized."); |
139 // reset mouse motion |
134 // reset mouse motion |
140 asc_context.input.mouse_xrel = 0; |
135 asc_context.input.mouse_xrel = 0; |
141 asc_context.input.mouse_yrel = 0; |
136 asc_context.input.mouse_yrel = 0; |
142 |
137 |
143 // reset key flags |
138 // reset key flags |
144 for (unsigned int i = 0 ; i < SDL_NUM_SCANCODES ; i++) { |
139 for (unsigned int i = 0 ; i < SDL_SCANCODE_COUNT ; i++) { |
145 asc_clear_flag(asc_context.input.keys[i], ASC_KEY_RELEASE_FLAG|ASC_KEY_PRESS_FLAG); |
140 asc_clear_flag(asc_context.input.keys[i], ASC_KEY_RELEASE_FLAG|ASC_KEY_PRESS_FLAG); |
146 } |
141 } |
147 |
142 |
148 // dispatch SDL events |
143 // dispatch SDL events |
149 SDL_Event event; |
144 SDL_Event event; |
150 while (SDL_PollEvent(&event)) { |
145 while (SDL_PollEvent(&event)) { |
151 switch (event.type) { |
146 switch (event.type) { |
152 case SDL_QUIT: |
147 case SDL_EVENT_QUIT: |
153 asc_set_flag(asc_context.flags, ASC_FLAG_QUIT); |
148 asc_set_flag(asc_context.flags, ASC_FLAG_QUIT); |
154 break; |
149 break; |
155 case SDL_WINDOWEVENT: { |
150 case SDL_EVENT_WINDOW_RESIZED: { |
156 if (event.window.event == SDL_WINDOWEVENT_RESIZED) { |
151 asc_event_window_resized( |
157 asc_event_window_resized( |
152 event.window.windowID, |
158 event.window.windowID, |
153 event.window.data1, |
159 event.window.data1, |
154 event.window.data2 |
160 event.window.data2 |
155 ); |
161 ); |
156 break; |
162 } else if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) { |
157 } |
163 unsigned int idx = asc_window_index(event.window.windowID); |
158 case SDL_EVENT_WINDOW_FOCUS_GAINED: { |
164 asc_context.windows[idx].focused = true; |
159 unsigned int idx = asc_window_index(event.window.windowID); |
165 } else if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) { |
160 asc_context.windows[idx].focused = true; |
166 unsigned int idx = asc_window_index(event.window.windowID); |
161 break; |
167 asc_context.windows[idx].focused = false; |
162 } |
168 } |
163 case SDL_EVENT_WINDOW_FOCUS_LOST: { |
169 break; |
164 unsigned int idx = asc_window_index(event.window.windowID); |
170 } |
165 asc_context.windows[idx].focused = false; |
171 case SDL_MOUSEMOTION: { |
166 break; |
|
167 } |
|
168 case SDL_EVENT_MOUSE_MOTION: { |
172 // accumulate relative motion |
169 // accumulate relative motion |
173 asc_context.input.mouse_xrel += event.motion.xrel; |
170 asc_context.input.mouse_xrel += event.motion.xrel; |
174 asc_context.input.mouse_yrel += event.motion.yrel; |
171 asc_context.input.mouse_yrel += event.motion.yrel; |
175 // update absolute position |
172 // update absolute position |
176 asc_context.input.mouse_x = event.motion.x; |
173 asc_context.input.mouse_x = event.motion.x; |
178 // update which window the mouse was seen in |
175 // update which window the mouse was seen in |
179 asc_context.input.mouse_window = |
176 asc_context.input.mouse_window = |
180 asc_window_index(event.motion.windowID); |
177 asc_window_index(event.motion.windowID); |
181 break; |
178 break; |
182 } |
179 } |
183 case SDL_KEYDOWN: |
180 case SDL_EVENT_KEY_DOWN: |
184 // we only set the down and press flags if the key is not already known to be down |
181 // we only set the down and press flags if the key is not already known to be down |
185 if (asc_key_up(event.key.keysym.scancode)) { |
182 if (asc_key_up(event.key.scancode)) { |
186 asc_context.input.keys[event.key.keysym.scancode] = ASC_KEY_DOWN_FLAG|ASC_KEY_PRESS_FLAG; |
183 asc_context.input.keys[event.key.scancode] = ASC_KEY_DOWN_FLAG|ASC_KEY_PRESS_FLAG; |
187 } |
184 } |
188 break; |
185 break; |
189 case SDL_KEYUP: |
186 case SDL_EVENT_KEY_UP: |
190 // we can directly set the release flag - it will be cleared next round |
187 // we can directly set the release flag - it will be cleared next round |
191 asc_context.input.keys[event.key.keysym.scancode] = ASC_KEY_RELEASE_FLAG; |
188 asc_context.input.keys[event.key.scancode] = ASC_KEY_RELEASE_FLAG; |
192 break; |
189 break; |
193 } |
190 } |
194 } |
191 } |
195 |
192 |
196 // compute frame time |
193 // compute frame time |