]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - vid_sdl.c
934295d8e23eb01930750defb2ef088922cb8290
[xonotic/darkplaces.git] / vid_sdl.c
1 /*
2 Copyright (C) 2003  T. Joseph Carter
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19 #undef WIN32_LEAN_AND_MEAN  //hush a warning, SDL.h redefines this
20 #include <SDL.h>
21 #include <stdio.h>
22
23 #include "quakedef.h"
24 #include "image.h"
25 #include "dpsoftrast.h"
26
27 #ifndef __IPHONEOS__
28 #ifdef MACOSX
29 #include <Carbon/Carbon.h>
30 #include <IOKit/hidsystem/IOHIDLib.h>
31 #include <IOKit/hidsystem/IOHIDParameter.h>
32 #include <IOKit/hidsystem/event_status_driver.h>
33 static cvar_t apple_mouse_noaccel = {CVAR_SAVE, "apple_mouse_noaccel", "1", "disables mouse acceleration while DarkPlaces is active"};
34 static qboolean vid_usingnoaccel;
35 static double originalMouseSpeed = -1.0;
36 io_connect_t IN_GetIOHandle(void)
37 {
38         io_connect_t iohandle = MACH_PORT_NULL;
39         kern_return_t status;
40         io_service_t iohidsystem = MACH_PORT_NULL;
41         mach_port_t masterport;
42
43         status = IOMasterPort(MACH_PORT_NULL, &masterport);
44         if(status != KERN_SUCCESS)
45                 return 0;
46
47         iohidsystem = IORegistryEntryFromPath(masterport, kIOServicePlane ":/IOResources/IOHIDSystem");
48         if(!iohidsystem)
49                 return 0;
50
51         status = IOServiceOpen(iohidsystem, mach_task_self(), kIOHIDParamConnectType, &iohandle);
52         IOObjectRelease(iohidsystem);
53
54         return iohandle;
55 }
56 #endif
57 #endif
58
59 #ifdef WIN32
60 #define SDL_R_RESTART
61 #endif
62
63 // Tell startup code that we have a client
64 int cl_available = true;
65
66 qboolean vid_supportrefreshrate = false;
67
68 static qboolean vid_usingmouse = false;
69 static qboolean vid_usingmouse_relativeworks = false; // SDL2 workaround for unimplemented RelativeMouse mode
70 static qboolean vid_usinghidecursor = false;
71 static qboolean vid_hasfocus = false;
72 static qboolean vid_isfullscreen;
73 #if SDL_MAJOR_VERSION != 1
74 static qboolean vid_usingvsync = false;
75 #endif
76 static SDL_Joystick *vid_sdljoystick = NULL;
77 // GAME_STEELSTORM specific
78 static cvar_t *steelstorm_showing_map = NULL; // detect but do not create the cvar
79 static cvar_t *steelstorm_showing_mousecursor = NULL; // detect but do not create the cvar
80
81 static int win_half_width = 50;
82 static int win_half_height = 50;
83 static int video_bpp;
84
85 #if SDL_MAJOR_VERSION == 1
86 static SDL_Surface *screen;
87 static int video_flags;
88 #else
89 static SDL_GLContext *context;
90 static SDL_Window *window;
91 static int window_flags;
92 #endif
93 static SDL_Surface *vid_softsurface;
94 static vid_mode_t desktop_mode;
95
96 /////////////////////////
97 // Input handling
98 ////
99 //TODO: Add error checking
100
101 #ifndef SDLK_PERCENT
102 #define SDLK_PERCENT '%'
103 #if SDL_MAJOR_VERSION == 1
104 #define SDLK_PRINTSCREEN SDLK_PRINT
105 #define SDLK_SCROLLLOCK SDLK_SCROLLOCK
106 #define SDLK_NUMLOCKCLEAR SDLK_NUMLOCK
107 #define SDLK_KP_1 SDLK_KP1
108 #define SDLK_KP_2 SDLK_KP2
109 #define SDLK_KP_3 SDLK_KP3
110 #define SDLK_KP_4 SDLK_KP4
111 #define SDLK_KP_5 SDLK_KP5
112 #define SDLK_KP_6 SDLK_KP6
113 #define SDLK_KP_7 SDLK_KP7
114 #define SDLK_KP_8 SDLK_KP8
115 #define SDLK_KP_9 SDLK_KP9
116 #define SDLK_KP_0 SDLK_KP0
117 #endif
118 #endif
119
120 static int MapKey( unsigned int sdlkey )
121 {
122         switch(sdlkey)
123         {
124         default: return 0;
125 //      case SDLK_UNKNOWN:            return K_UNKNOWN;
126         case SDLK_RETURN:             return K_ENTER;
127         case SDLK_ESCAPE:             return K_ESCAPE;
128         case SDLK_BACKSPACE:          return K_BACKSPACE;
129         case SDLK_TAB:                return K_TAB;
130         case SDLK_SPACE:              return K_SPACE;
131         case SDLK_EXCLAIM:            return '!';
132         case SDLK_QUOTEDBL:           return '"';
133         case SDLK_HASH:               return '#';
134         case SDLK_PERCENT:            return '%';
135         case SDLK_DOLLAR:             return '$';
136         case SDLK_AMPERSAND:          return '&';
137         case SDLK_QUOTE:              return '\'';
138         case SDLK_LEFTPAREN:          return '(';
139         case SDLK_RIGHTPAREN:         return ')';
140         case SDLK_ASTERISK:           return '*';
141         case SDLK_PLUS:               return '+';
142         case SDLK_COMMA:              return ',';
143         case SDLK_MINUS:              return '-';
144         case SDLK_PERIOD:             return '.';
145         case SDLK_SLASH:              return '/';
146         case SDLK_0:                  return '0';
147         case SDLK_1:                  return '1';
148         case SDLK_2:                  return '2';
149         case SDLK_3:                  return '3';
150         case SDLK_4:                  return '4';
151         case SDLK_5:                  return '5';
152         case SDLK_6:                  return '6';
153         case SDLK_7:                  return '7';
154         case SDLK_8:                  return '8';
155         case SDLK_9:                  return '9';
156         case SDLK_COLON:              return ':';
157         case SDLK_SEMICOLON:          return ';';
158         case SDLK_LESS:               return '<';
159         case SDLK_EQUALS:             return '=';
160         case SDLK_GREATER:            return '>';
161         case SDLK_QUESTION:           return '?';
162         case SDLK_AT:                 return '@';
163         case SDLK_LEFTBRACKET:        return '[';
164         case SDLK_BACKSLASH:          return '\\';
165         case SDLK_RIGHTBRACKET:       return ']';
166         case SDLK_CARET:              return '^';
167         case SDLK_UNDERSCORE:         return '_';
168         case SDLK_BACKQUOTE:          return '`';
169         case SDLK_a:                  return 'a';
170         case SDLK_b:                  return 'b';
171         case SDLK_c:                  return 'c';
172         case SDLK_d:                  return 'd';
173         case SDLK_e:                  return 'e';
174         case SDLK_f:                  return 'f';
175         case SDLK_g:                  return 'g';
176         case SDLK_h:                  return 'h';
177         case SDLK_i:                  return 'i';
178         case SDLK_j:                  return 'j';
179         case SDLK_k:                  return 'k';
180         case SDLK_l:                  return 'l';
181         case SDLK_m:                  return 'm';
182         case SDLK_n:                  return 'n';
183         case SDLK_o:                  return 'o';
184         case SDLK_p:                  return 'p';
185         case SDLK_q:                  return 'q';
186         case SDLK_r:                  return 'r';
187         case SDLK_s:                  return 's';
188         case SDLK_t:                  return 't';
189         case SDLK_u:                  return 'u';
190         case SDLK_v:                  return 'v';
191         case SDLK_w:                  return 'w';
192         case SDLK_x:                  return 'x';
193         case SDLK_y:                  return 'y';
194         case SDLK_z:                  return 'z';
195         case SDLK_CAPSLOCK:           return K_CAPSLOCK;
196         case SDLK_F1:                 return K_F1;
197         case SDLK_F2:                 return K_F2;
198         case SDLK_F3:                 return K_F3;
199         case SDLK_F4:                 return K_F4;
200         case SDLK_F5:                 return K_F5;
201         case SDLK_F6:                 return K_F6;
202         case SDLK_F7:                 return K_F7;
203         case SDLK_F8:                 return K_F8;
204         case SDLK_F9:                 return K_F9;
205         case SDLK_F10:                return K_F10;
206         case SDLK_F11:                return K_F11;
207         case SDLK_F12:                return K_F12;
208         case SDLK_PRINTSCREEN:        return K_PRINTSCREEN;
209         case SDLK_SCROLLLOCK:         return K_SCROLLOCK;
210         case SDLK_PAUSE:              return K_PAUSE;
211         case SDLK_INSERT:             return K_INS;
212         case SDLK_HOME:               return K_HOME;
213         case SDLK_PAGEUP:             return K_PGUP;
214 #ifdef __IPHONEOS__
215         case SDLK_DELETE:             return K_BACKSPACE;
216 #else
217         case SDLK_DELETE:             return K_DEL;
218 #endif
219         case SDLK_END:                return K_END;
220         case SDLK_PAGEDOWN:           return K_PGDN;
221         case SDLK_RIGHT:              return K_RIGHTARROW;
222         case SDLK_LEFT:               return K_LEFTARROW;
223         case SDLK_DOWN:               return K_DOWNARROW;
224         case SDLK_UP:                 return K_UPARROW;
225         case SDLK_NUMLOCKCLEAR:       return K_NUMLOCK;
226         case SDLK_KP_DIVIDE:          return K_KP_DIVIDE;
227         case SDLK_KP_MULTIPLY:        return K_KP_MULTIPLY;
228         case SDLK_KP_MINUS:           return K_KP_MINUS;
229         case SDLK_KP_PLUS:            return K_KP_PLUS;
230         case SDLK_KP_ENTER:           return K_KP_ENTER;
231         case SDLK_KP_1:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_1 : K_END);
232         case SDLK_KP_2:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_2 : K_DOWNARROW);
233         case SDLK_KP_3:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_3 : K_PGDN);
234         case SDLK_KP_4:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_4 : K_LEFTARROW);
235         case SDLK_KP_5:               return K_KP_5;
236         case SDLK_KP_6:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_6 : K_RIGHTARROW);
237         case SDLK_KP_7:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_7 : K_HOME);
238         case SDLK_KP_8:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_8 : K_UPARROW);
239         case SDLK_KP_9:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_9 : K_PGUP);
240         case SDLK_KP_0:               return ((SDL_GetModState() & KMOD_NUM) ? K_KP_0 : K_INS);
241         case SDLK_KP_PERIOD:          return ((SDL_GetModState() & KMOD_NUM) ? K_KP_PERIOD : K_DEL);
242 //      case SDLK_APPLICATION:        return K_APPLICATION;
243 //      case SDLK_POWER:              return K_POWER;
244         case SDLK_KP_EQUALS:          return K_KP_EQUALS;
245 //      case SDLK_F13:                return K_F13;
246 //      case SDLK_F14:                return K_F14;
247 //      case SDLK_F15:                return K_F15;
248 //      case SDLK_F16:                return K_F16;
249 //      case SDLK_F17:                return K_F17;
250 //      case SDLK_F18:                return K_F18;
251 //      case SDLK_F19:                return K_F19;
252 //      case SDLK_F20:                return K_F20;
253 //      case SDLK_F21:                return K_F21;
254 //      case SDLK_F22:                return K_F22;
255 //      case SDLK_F23:                return K_F23;
256 //      case SDLK_F24:                return K_F24;
257 //      case SDLK_EXECUTE:            return K_EXECUTE;
258 //      case SDLK_HELP:               return K_HELP;
259 //      case SDLK_MENU:               return K_MENU;
260 //      case SDLK_SELECT:             return K_SELECT;
261 //      case SDLK_STOP:               return K_STOP;
262 //      case SDLK_AGAIN:              return K_AGAIN;
263 //      case SDLK_UNDO:               return K_UNDO;
264 //      case SDLK_CUT:                return K_CUT;
265 //      case SDLK_COPY:               return K_COPY;
266 //      case SDLK_PASTE:              return K_PASTE;
267 //      case SDLK_FIND:               return K_FIND;
268 //      case SDLK_MUTE:               return K_MUTE;
269 //      case SDLK_VOLUMEUP:           return K_VOLUMEUP;
270 //      case SDLK_VOLUMEDOWN:         return K_VOLUMEDOWN;
271 //      case SDLK_KP_COMMA:           return K_KP_COMMA;
272 //      case SDLK_KP_EQUALSAS400:     return K_KP_EQUALSAS400;
273 //      case SDLK_ALTERASE:           return K_ALTERASE;
274 //      case SDLK_SYSREQ:             return K_SYSREQ;
275 //      case SDLK_CANCEL:             return K_CANCEL;
276 //      case SDLK_CLEAR:              return K_CLEAR;
277 //      case SDLK_PRIOR:              return K_PRIOR;
278 //      case SDLK_RETURN2:            return K_RETURN2;
279 //      case SDLK_SEPARATOR:          return K_SEPARATOR;
280 //      case SDLK_OUT:                return K_OUT;
281 //      case SDLK_OPER:               return K_OPER;
282 //      case SDLK_CLEARAGAIN:         return K_CLEARAGAIN;
283 //      case SDLK_CRSEL:              return K_CRSEL;
284 //      case SDLK_EXSEL:              return K_EXSEL;
285 //      case SDLK_KP_00:              return K_KP_00;
286 //      case SDLK_KP_000:             return K_KP_000;
287 //      case SDLK_THOUSANDSSEPARATOR: return K_THOUSANDSSEPARATOR;
288 //      case SDLK_DECIMALSEPARATOR:   return K_DECIMALSEPARATOR;
289 //      case SDLK_CURRENCYUNIT:       return K_CURRENCYUNIT;
290 //      case SDLK_CURRENCYSUBUNIT:    return K_CURRENCYSUBUNIT;
291 //      case SDLK_KP_LEFTPAREN:       return K_KP_LEFTPAREN;
292 //      case SDLK_KP_RIGHTPAREN:      return K_KP_RIGHTPAREN;
293 //      case SDLK_KP_LEFTBRACE:       return K_KP_LEFTBRACE;
294 //      case SDLK_KP_RIGHTBRACE:      return K_KP_RIGHTBRACE;
295 //      case SDLK_KP_TAB:             return K_KP_TAB;
296 //      case SDLK_KP_BACKSPACE:       return K_KP_BACKSPACE;
297 //      case SDLK_KP_A:               return K_KP_A;
298 //      case SDLK_KP_B:               return K_KP_B;
299 //      case SDLK_KP_C:               return K_KP_C;
300 //      case SDLK_KP_D:               return K_KP_D;
301 //      case SDLK_KP_E:               return K_KP_E;
302 //      case SDLK_KP_F:               return K_KP_F;
303 //      case SDLK_KP_XOR:             return K_KP_XOR;
304 //      case SDLK_KP_POWER:           return K_KP_POWER;
305 //      case SDLK_KP_PERCENT:         return K_KP_PERCENT;
306 //      case SDLK_KP_LESS:            return K_KP_LESS;
307 //      case SDLK_KP_GREATER:         return K_KP_GREATER;
308 //      case SDLK_KP_AMPERSAND:       return K_KP_AMPERSAND;
309 //      case SDLK_KP_DBLAMPERSAND:    return K_KP_DBLAMPERSAND;
310 //      case SDLK_KP_VERTICALBAR:     return K_KP_VERTICALBAR;
311 //      case SDLK_KP_DBLVERTICALBAR:  return K_KP_DBLVERTICALBAR;
312 //      case SDLK_KP_COLON:           return K_KP_COLON;
313 //      case SDLK_KP_HASH:            return K_KP_HASH;
314 //      case SDLK_KP_SPACE:           return K_KP_SPACE;
315 //      case SDLK_KP_AT:              return K_KP_AT;
316 //      case SDLK_KP_EXCLAM:          return K_KP_EXCLAM;
317 //      case SDLK_KP_MEMSTORE:        return K_KP_MEMSTORE;
318 //      case SDLK_KP_MEMRECALL:       return K_KP_MEMRECALL;
319 //      case SDLK_KP_MEMCLEAR:        return K_KP_MEMCLEAR;
320 //      case SDLK_KP_MEMADD:          return K_KP_MEMADD;
321 //      case SDLK_KP_MEMSUBTRACT:     return K_KP_MEMSUBTRACT;
322 //      case SDLK_KP_MEMMULTIPLY:     return K_KP_MEMMULTIPLY;
323 //      case SDLK_KP_MEMDIVIDE:       return K_KP_MEMDIVIDE;
324 //      case SDLK_KP_PLUSMINUS:       return K_KP_PLUSMINUS;
325 //      case SDLK_KP_CLEAR:           return K_KP_CLEAR;
326 //      case SDLK_KP_CLEARENTRY:      return K_KP_CLEARENTRY;
327 //      case SDLK_KP_BINARY:          return K_KP_BINARY;
328 //      case SDLK_KP_OCTAL:           return K_KP_OCTAL;
329 //      case SDLK_KP_DECIMAL:         return K_KP_DECIMAL;
330 //      case SDLK_KP_HEXADECIMAL:     return K_KP_HEXADECIMAL;
331         case SDLK_LCTRL:              return K_CTRL;
332         case SDLK_LSHIFT:             return K_SHIFT;
333         case SDLK_LALT:               return K_ALT;
334 //      case SDLK_LGUI:               return K_LGUI;
335         case SDLK_RCTRL:              return K_CTRL;
336         case SDLK_RSHIFT:             return K_SHIFT;
337         case SDLK_RALT:               return K_ALT;
338 //      case SDLK_RGUI:               return K_RGUI;
339 //      case SDLK_MODE:               return K_MODE;
340 #if SDL_MAJOR_VERSION != 1
341 //      case SDLK_AUDIONEXT:          return K_AUDIONEXT;
342 //      case SDLK_AUDIOPREV:          return K_AUDIOPREV;
343 //      case SDLK_AUDIOSTOP:          return K_AUDIOSTOP;
344 //      case SDLK_AUDIOPLAY:          return K_AUDIOPLAY;
345 //      case SDLK_AUDIOMUTE:          return K_AUDIOMUTE;
346 //      case SDLK_MEDIASELECT:        return K_MEDIASELECT;
347 //      case SDLK_WWW:                return K_WWW;
348 //      case SDLK_MAIL:               return K_MAIL;
349 //      case SDLK_CALCULATOR:         return K_CALCULATOR;
350 //      case SDLK_COMPUTER:           return K_COMPUTER;
351 //      case SDLK_AC_SEARCH:          return K_AC_SEARCH; // Android button
352 //      case SDLK_AC_HOME:            return K_AC_HOME; // Android button
353         case SDLK_AC_BACK:            return K_ESCAPE; // Android button
354 //      case SDLK_AC_FORWARD:         return K_AC_FORWARD; // Android button
355 //      case SDLK_AC_STOP:            return K_AC_STOP; // Android button
356 //      case SDLK_AC_REFRESH:         return K_AC_REFRESH; // Android button
357 //      case SDLK_AC_BOOKMARKS:       return K_AC_BOOKMARKS; // Android button
358 //      case SDLK_BRIGHTNESSDOWN:     return K_BRIGHTNESSDOWN;
359 //      case SDLK_BRIGHTNESSUP:       return K_BRIGHTNESSUP;
360 //      case SDLK_DISPLAYSWITCH:      return K_DISPLAYSWITCH;
361 //      case SDLK_KBDILLUMTOGGLE:     return K_KBDILLUMTOGGLE;
362 //      case SDLK_KBDILLUMDOWN:       return K_KBDILLUMDOWN;
363 //      case SDLK_KBDILLUMUP:         return K_KBDILLUMUP;
364 //      case SDLK_EJECT:              return K_EJECT;
365 //      case SDLK_SLEEP:              return K_SLEEP;
366 #endif
367         }
368 }
369
370 qboolean VID_HasScreenKeyboardSupport(void)
371 {
372 #if SDL_MAJOR_VERSION != 1
373         return SDL_HasScreenKeyboardSupport() != SDL_FALSE;
374 #else
375         return false;
376 #endif
377 }
378
379 void VID_ShowKeyboard(qboolean show)
380 {
381 #if SDL_MAJOR_VERSION != 1
382         if (!SDL_HasScreenKeyboardSupport())
383                 return;
384
385         if (show)
386         {
387                 if (!SDL_IsTextInputActive())
388                         SDL_StartTextInput();
389         }
390         else
391         {
392                 if (SDL_IsTextInputActive())
393                         SDL_StopTextInput();
394         }
395 #endif
396 }
397
398 qboolean VID_ShowingKeyboard(void)
399 {
400 #if SDL_MAJOR_VERSION != 1
401         return SDL_IsTextInputActive() != 0;
402 #else
403         return false;
404 #endif
405 }
406
407 void VID_SetMouse(qboolean fullscreengrab, qboolean relative, qboolean hidecursor)
408 {
409 #ifndef DP_MOBILETOUCH
410 #ifdef MACOSX
411         if(relative)
412                 if(vid_usingmouse && (vid_usingnoaccel != !!apple_mouse_noaccel.integer))
413                         VID_SetMouse(false, false, false); // ungrab first!
414 #endif
415         if (vid_usingmouse != relative)
416         {
417                 vid_usingmouse = relative;
418                 cl_ignoremousemoves = 2;
419 #if SDL_MAJOR_VERSION == 1
420                 SDL_WM_GrabInput( relative ? SDL_GRAB_ON : SDL_GRAB_OFF );
421 #else
422                 vid_usingmouse_relativeworks = SDL_SetRelativeMouseMode(relative ? SDL_TRUE : SDL_FALSE) == 0;
423 //              Con_Printf("VID_SetMouse(%i, %i, %i) relativeworks = %i\n", (int)fullscreengrab, (int)relative, (int)hidecursor, (int)vid_usingmouse_relativeworks);
424 #endif
425 #ifdef MACOSX
426                 if(relative)
427                 {
428                         // Save the status of mouse acceleration
429                         originalMouseSpeed = -1.0; // in case of error
430                         if(apple_mouse_noaccel.integer)
431                         {
432                                 io_connect_t mouseDev = IN_GetIOHandle();
433                                 if(mouseDev != 0)
434                                 {
435                                         if(IOHIDGetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), &originalMouseSpeed) == kIOReturnSuccess)
436                                         {
437                                                 Con_DPrintf("previous mouse acceleration: %f\n", originalMouseSpeed);
438                                                 if(IOHIDSetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), -1.0) != kIOReturnSuccess)
439                                                 {
440                                                         Con_Print("Could not disable mouse acceleration (failed at IOHIDSetAccelerationWithKey).\n");
441                                                         Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
442                                                 }
443                                         }
444                                         else
445                                         {
446                                                 Con_Print("Could not disable mouse acceleration (failed at IOHIDGetAccelerationWithKey).\n");
447                                                 Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
448                                         }
449                                         IOServiceClose(mouseDev);
450                                 }
451                                 else
452                                 {
453                                         Con_Print("Could not disable mouse acceleration (failed at IO_GetIOHandle).\n");
454                                         Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
455                                 }
456                         }
457
458                         vid_usingnoaccel = !!apple_mouse_noaccel.integer;
459                 }
460                 else
461                 {
462                         if(originalMouseSpeed != -1.0)
463                         {
464                                 io_connect_t mouseDev = IN_GetIOHandle();
465                                 if(mouseDev != 0)
466                                 {
467                                         Con_DPrintf("restoring mouse acceleration to: %f\n", originalMouseSpeed);
468                                         if(IOHIDSetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), originalMouseSpeed) != kIOReturnSuccess)
469                                                 Con_Print("Could not re-enable mouse acceleration (failed at IOHIDSetAccelerationWithKey).\n");
470                                         IOServiceClose(mouseDev);
471                                 }
472                                 else
473                                         Con_Print("Could not re-enable mouse acceleration (failed at IO_GetIOHandle).\n");
474                         }
475                 }
476 #endif
477         }
478         if (vid_usinghidecursor != hidecursor)
479         {
480                 vid_usinghidecursor = hidecursor;
481                 SDL_ShowCursor( hidecursor ? SDL_DISABLE : SDL_ENABLE);
482         }
483 #endif
484 }
485
486 // multitouch[10][] represents the mouse pointer
487 // multitouch[][0]: finger active
488 // multitouch[][1]: Y
489 // multitouch[][2]: Y
490 // X and Y coordinates are 0-1.
491 #define MAXFINGERS 11
492 float multitouch[MAXFINGERS][3];
493
494 // this one stores how many areas this finger has touched
495 int multitouchs[MAXFINGERS];
496
497 // modified heavily by ELUAN
498 static qboolean VID_TouchscreenArea(int corner, float px, float py, float pwidth, float pheight, const char *icon, float textheight, const char *text, float *resultmove, qboolean *resultbutton, keynum_t key, const char *typedtext, float deadzone, float oversizepixels_x, float oversizepixels_y, qboolean iamexclusive)
499 {
500         int finger;
501         float fx, fy, fwidth, fheight;
502         float overfx, overfy, overfwidth, overfheight;
503         float rel[3];
504         float sqsum;
505         qboolean button = false;
506         VectorClear(rel);
507         if (pwidth > 0 && pheight > 0)
508         {
509                 if (corner & 1) px += vid_conwidth.value;
510                 if (corner & 2) py += vid_conheight.value;
511                 if (corner & 4) px += vid_conwidth.value * 0.5f;
512                 if (corner & 8) py += vid_conheight.value * 0.5f;
513                 if (corner & 16) {px *= vid_conwidth.value * (1.0f / 640.0f);py *= vid_conheight.value * (1.0f / 480.0f);pwidth *= vid_conwidth.value * (1.0f / 640.0f);pheight *= vid_conheight.value * (1.0f / 480.0f);}
514                 fx = px / vid_conwidth.value;
515                 fy = py / vid_conheight.value;
516                 fwidth = pwidth / vid_conwidth.value;
517                 fheight = pheight / vid_conheight.value;
518
519                 // try to prevent oversizepixels_* from interfering with the iamexclusive cvar by not letting we start controlling from too far of the actual touch area (areas without resultbuttons should NEVER have the oversizepixels_* parameters set to anything other than 0)
520                 if (resultbutton)
521                         if (!(*resultbutton))
522                         {
523                                 oversizepixels_x *= 0.2;
524                                 oversizepixels_y *= 0.2;
525                         }
526
527                 oversizepixels_x /= vid_conwidth.value;
528                 oversizepixels_y /= vid_conheight.value;
529
530                 overfx = fx - oversizepixels_x;
531                 overfy = fy - oversizepixels_y;
532                 overfwidth = fwidth + 2*oversizepixels_x;
533                 overfheight = fheight + 2*oversizepixels_y;
534
535                 for (finger = 0;finger < MAXFINGERS;finger++)
536                 {
537                         if (multitouchs[finger] && iamexclusive) // for this to work correctly, you must call touch areas in order of highest to lowest priority
538                                 continue;
539
540                         if (multitouch[finger][0] && multitouch[finger][1] >= overfx && multitouch[finger][2] >= overfy && multitouch[finger][1] < overfx + overfwidth && multitouch[finger][2] < overfy + overfheight)
541                         {
542                                 multitouchs[finger]++;
543
544                                 rel[0] = bound(-1, (multitouch[finger][1] - (fx + 0.5f * fwidth)) * (2.0f / fwidth), 1);
545                                 rel[1] = bound(-1, (multitouch[finger][2] - (fy + 0.5f * fheight)) * (2.0f / fheight), 1);
546                                 rel[2] = 0;
547
548                                 sqsum = rel[0]*rel[0] + rel[1]*rel[1];
549                                 // 2d deadzone
550                                 if (sqsum < deadzone*deadzone)
551                                 {
552                                         rel[0] = 0;
553                                         rel[1] = 0;
554                                 }
555                                 else if (sqsum > 1)
556                                 {
557                                         // ignore the third component
558                                         Vector2Normalize2(rel, rel);
559                                 }
560                                 button = true;
561                                 break;
562                         }
563                 }
564                 if (scr_numtouchscreenareas < 128)
565                 {
566                         scr_touchscreenareas[scr_numtouchscreenareas].pic = icon;
567                         scr_touchscreenareas[scr_numtouchscreenareas].text = text;
568                         scr_touchscreenareas[scr_numtouchscreenareas].textheight = textheight;
569                         scr_touchscreenareas[scr_numtouchscreenareas].rect[0] = px;
570                         scr_touchscreenareas[scr_numtouchscreenareas].rect[1] = py;
571                         scr_touchscreenareas[scr_numtouchscreenareas].rect[2] = pwidth;
572                         scr_touchscreenareas[scr_numtouchscreenareas].rect[3] = pheight;
573                         scr_touchscreenareas[scr_numtouchscreenareas].active = button;
574                         // the pics may have alpha too.
575                         scr_touchscreenareas[scr_numtouchscreenareas].activealpha = 1.f;
576                         scr_touchscreenareas[scr_numtouchscreenareas].inactivealpha = 0.95f;
577                         scr_numtouchscreenareas++;
578                 }
579         }
580         if (resultmove)
581         {
582                 if (button)
583                         VectorCopy(rel, resultmove);
584                 else
585                         VectorClear(resultmove);
586         }
587         if (resultbutton)
588         {
589                 if (*resultbutton != button)
590                 {
591                         if ((int)key > 0)
592                                 Key_Event(key, 0, button);
593                         if (typedtext && typedtext[0] && !*resultbutton)
594                         {
595                                 // FIXME: implement UTF8 support - nothing actually specifies a UTF8 string here yet, but should support it...
596                                 int i;
597                                 for (i = 0;typedtext[i];i++)
598                                 {
599                                         Key_Event(K_TEXT, typedtext[i], true);
600                                         Key_Event(K_TEXT, typedtext[i], false);
601                                 }
602                         }
603                 }
604                 *resultbutton = button;
605         }
606         return button;
607 }
608
609 // ELUAN:
610 // not reentrant, but we only need one mouse cursor anyway...
611 static void VID_TouchscreenCursor(float px, float py, float pwidth, float pheight, qboolean *resultbutton, keynum_t key)
612 {
613         int finger;
614         float fx, fy, fwidth, fheight;
615         qboolean button = false;
616         static int cursorfinger = -1;
617         static int cursorfreemovement = false;
618         static int canclick = false;
619         static int clickxy[2];
620         static int relclickxy[2];
621         static double clickrealtime = 0;
622
623         if (steelstorm_showing_mousecursor && steelstorm_showing_mousecursor->integer)
624         if (pwidth > 0 && pheight > 0)
625         {
626                 fx = px / vid_conwidth.value;
627                 fy = py / vid_conheight.value;
628                 fwidth = pwidth / vid_conwidth.value;
629                 fheight = pheight / vid_conheight.value;
630                 for (finger = 0;finger < MAXFINGERS;finger++)
631                 {
632                         if (multitouch[finger][0] && multitouch[finger][1] >= fx && multitouch[finger][2] >= fy && multitouch[finger][1] < fx + fwidth && multitouch[finger][2] < fy + fheight)
633                         {
634                                 if (cursorfinger == -1)
635                                 {
636                                         clickxy[0] =  multitouch[finger][1] * vid_width.value - 0.5f * pwidth;
637                                         clickxy[1] =  multitouch[finger][2] * vid_height.value - 0.5f * pheight;
638                                         relclickxy[0] =  (multitouch[finger][1] - fx) * vid_width.value - 0.5f * pwidth;
639                                         relclickxy[1] =  (multitouch[finger][2] - fy) * vid_height.value - 0.5f * pheight;
640                                 }
641                                 cursorfinger = finger;
642                                 button = true;
643                                 canclick = true;
644                                 cursorfreemovement = false;
645                                 break;
646                         }
647                 }
648                 if (scr_numtouchscreenareas < 128)
649                 {
650                         if (clickrealtime + 1 > realtime)
651                         {
652                                 scr_touchscreenareas[scr_numtouchscreenareas].pic = "gfx/gui/touch_puck_cur_click.tga";
653                         }
654                         else if (button)
655                         {
656                                 scr_touchscreenareas[scr_numtouchscreenareas].pic = "gfx/gui/touch_puck_cur_touch.tga";
657                         }
658                         else
659                         {
660                                 switch ((int)realtime * 10 % 20)
661                                 {
662                                 case 0:
663                                         scr_touchscreenareas[scr_numtouchscreenareas].pic = "gfx/gui/touch_puck_cur_touch.tga";
664                                         break;
665                                 default:
666                                         scr_touchscreenareas[scr_numtouchscreenareas].pic = "gfx/gui/touch_puck_cur_idle.tga";
667                                 }
668                         }
669                         scr_touchscreenareas[scr_numtouchscreenareas].text = "";
670                         scr_touchscreenareas[scr_numtouchscreenareas].textheight = 0;
671                         scr_touchscreenareas[scr_numtouchscreenareas].rect[0] = px;
672                         scr_touchscreenareas[scr_numtouchscreenareas].rect[1] = py;
673                         scr_touchscreenareas[scr_numtouchscreenareas].rect[2] = pwidth;
674                         scr_touchscreenareas[scr_numtouchscreenareas].rect[3] = pheight;
675                         scr_touchscreenareas[scr_numtouchscreenareas].active = button;
676                         scr_touchscreenareas[scr_numtouchscreenareas].activealpha = 1.0f;
677                         scr_touchscreenareas[scr_numtouchscreenareas].inactivealpha = 1.0f;
678                         scr_numtouchscreenareas++;
679                 }
680         }
681
682         if (cursorfinger != -1)
683         {
684                 if (multitouch[cursorfinger][0])
685                 {
686                         if (multitouch[cursorfinger][1] * vid_width.value - 0.5f * pwidth < clickxy[0] - 1 ||
687                                 multitouch[cursorfinger][1] * vid_width.value - 0.5f * pwidth > clickxy[0] + 1 ||
688                                 multitouch[cursorfinger][2] * vid_height.value - 0.5f * pheight< clickxy[1] - 1 ||
689                                 multitouch[cursorfinger][2] * vid_height.value - 0.5f * pheight> clickxy[1] + 1) // finger drifted more than the allowed amount
690                         {
691                                 cursorfreemovement = true;
692                         }
693                         if (cursorfreemovement)
694                         {
695                                 // in_windowmouse_x* is in screen resolution coordinates, not console resolution
696                                 in_windowmouse_x = multitouch[cursorfinger][1] * vid_width.value - 0.5f * pwidth - relclickxy[0];
697                                 in_windowmouse_y = multitouch[cursorfinger][2] * vid_height.value - 0.5f * pheight - relclickxy[1];
698                         }
699                 }
700                 else
701                 {
702                         cursorfinger = -1;
703                 }
704         }
705
706         if (resultbutton)
707         {
708                 if (/**resultbutton != button && */(int)key > 0)
709                 {
710                         if (!button && !cursorfreemovement && canclick)
711                         {
712                                 Key_Event(key, 0, true);
713                                 canclick = false;
714                                 clickrealtime = realtime;
715                         }
716
717                         // SS:BR can't qc can't cope with presses and releases on the same frame
718                         if (clickrealtime && clickrealtime + 0.1 < realtime)
719                         {
720                                 Key_Event(key, 0, false);
721                                 clickrealtime = 0;
722                         }
723                 }
724
725                 *resultbutton = button;
726         }
727 }
728
729 void VID_BuildJoyState(vid_joystate_t *joystate)
730 {
731         VID_Shared_BuildJoyState_Begin(joystate);
732
733         if (vid_sdljoystick)
734         {
735                 SDL_Joystick *joy = vid_sdljoystick;
736                 int j;
737                 int numaxes;
738                 int numbuttons;
739                 numaxes = SDL_JoystickNumAxes(joy);
740                 for (j = 0;j < numaxes;j++)
741                         joystate->axis[j] = SDL_JoystickGetAxis(joy, j) * (1.0f / 32767.0f);
742                 numbuttons = SDL_JoystickNumButtons(joy);
743                 for (j = 0;j < numbuttons;j++)
744                         joystate->button[j] = SDL_JoystickGetButton(joy, j);
745         }
746
747         VID_Shared_BuildJoyState_Finish(joystate);
748 }
749
750 // clear every touch screen area, except the one with button[skip]
751 #define Vid_ClearAllTouchscreenAreas(skip) \
752         if (skip != 0) \
753                 VID_TouchscreenCursor(0, 0, 0, 0, &buttons[0], K_MOUSE1); \
754         if (skip != 1) \
755                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, move, &buttons[1], K_MOUSE4, NULL, 0, 0, 0, false); \
756         if (skip != 2) \
757                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, aim,  &buttons[2], K_MOUSE5, NULL, 0, 0, 0, false); \
758         if (skip != 3) \
759                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[3], K_SHIFT, NULL, 0, 0, 0, false); \
760         if (skip != 4) \
761                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[4], K_MOUSE2, NULL, 0, 0, 0, false); \
762         if (skip != 9) \
763                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[9], K_MOUSE3, NULL, 0, 0, 0, false); \
764         if (skip != 10) \
765                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[10], (keynum_t)'m', NULL, 0, 0, 0, false); \
766         if (skip != 11) \
767                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[11], (keynum_t)'b', NULL, 0, 0, 0, false); \
768         if (skip != 12) \
769                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[12], (keynum_t)'q', NULL, 0, 0, 0, false); \
770         if (skip != 13) \
771                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[13], (keynum_t)'`', NULL, 0, 0, 0, false); \
772         if (skip != 14) \
773                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[14], K_ESCAPE, NULL, 0, 0, 0, false); \
774         if (skip != 15) \
775                 VID_TouchscreenArea( 0,  0,  0,  0,  0, NULL                         , 0.0f, NULL, NULL, &buttons[15], K_SPACE, NULL, 0, 0, 0, false); \
776
777 /////////////////////
778 // Movement handling
779 ////
780
781 static void IN_Move_TouchScreen_SteelStorm(void)
782 {
783         // ELUAN
784         int i, numfingers;
785         float xscale, yscale;
786         float move[3], aim[3];
787         static qboolean oldbuttons[128];
788         static qboolean buttons[128];
789         static keydest_t oldkeydest;
790         keydest_t keydest = (key_consoleactive & KEY_CONSOLEACTIVE_USER) ? key_console : key_dest;
791         memcpy(oldbuttons, buttons, sizeof(oldbuttons));
792         memset(multitouchs, 0, sizeof(multitouchs));
793
794         for (i = 0, numfingers = 0; i < MAXFINGERS - 1; i++)
795                 if (multitouch[i][0])
796                         numfingers++;
797
798         /*
799         Enable this to use a mouse as a touch device (it may conflict with the iamexclusive parameter if a finger is also reported as a mouse at the same location
800         if (numfingers == 1)
801         {
802                 multitouch[MAXFINGERS-1][0] = SDL_GetMouseState(&x, &y) ? 11 : 0;
803                 multitouch[MAXFINGERS-1][1] = (float)x / vid.width;
804                 multitouch[MAXFINGERS-1][2] = (float)y / vid.height;
805         }
806         else
807         {
808                 // disable it so it doesn't get stuck, because SDL seems to stop updating it if there are more than 1 finger on screen
809                 multitouch[MAXFINGERS-1][0] = 0;
810         }*/
811
812         if (oldkeydest != keydest)
813         {
814                 switch(keydest)
815                 {
816                 case key_game: VID_ShowKeyboard(false);break;
817                 case key_console: VID_ShowKeyboard(true);break;
818                 case key_message: VID_ShowKeyboard(true);break;
819                 default: break; /* qc extensions control the other cases */
820                 }
821         }
822         oldkeydest = keydest;
823         // TODO: make touchscreen areas controlled by a config file or the VMs. THIS IS A MESS!
824         // TODO: can't just clear buttons[] when entering a new keydest, some keys would remain pressed
825         // SS:BR menuqc has many peculiarities, including that it can't accept more than one command per frame and pressing and releasing on the same frame
826
827         // Tuned for the SGS3, use it's value as a base. CLEAN THIS.
828         xscale = vid_touchscreen_density.value / 2.0f;
829         yscale = vid_touchscreen_density.value / 2.0f;
830         switch(keydest)
831         {
832         case key_console:
833                 Vid_ClearAllTouchscreenAreas(14);
834                 VID_TouchscreenArea( 0,   0, 160,  64,  64, "gfx/gui/touch_menu_button.tga"         , 0.0f, NULL, NULL, &buttons[14], K_ESCAPE, NULL, 0, 0, 0, false);
835                 break;
836         case key_game:
837                 if (steelstorm_showing_map && steelstorm_showing_map->integer) // FIXME: another hack to be removed when touchscreen areas go to QC
838                 {
839                         VID_TouchscreenArea( 0,   0,   0, vid_conwidth.value, vid_conheight.value, NULL                         , 0.0f, NULL, NULL, &buttons[10], (keynum_t)'m', NULL, 0, 0, 0, false);
840                         Vid_ClearAllTouchscreenAreas(10);
841                 }
842                 else if (steelstorm_showing_mousecursor && steelstorm_showing_mousecursor->integer)
843                 {
844                         // in_windowmouse_x* is in screen resolution coordinates, not console resolution
845                         VID_TouchscreenCursor((float)in_windowmouse_x/vid_width.value*vid_conwidth.value, (float)in_windowmouse_y/vid_height.value*vid_conheight.value, 192*xscale, 192*yscale, &buttons[0], K_MOUSE1);
846                         Vid_ClearAllTouchscreenAreas(0);
847                 }
848                 else
849                 {
850                         VID_TouchscreenCursor(0, 0, 0, 0, &buttons[0], K_MOUSE1);
851
852                         VID_TouchscreenArea( 2,16*xscale,-240*yscale, 224*xscale, 224*yscale, "gfx/gui/touch_l_thumb_dpad.tga", 0.0f, NULL, move, &buttons[1], (keynum_t)0, NULL, 0.15, 112*xscale, 112*yscale, false);
853
854                         VID_TouchscreenArea( 3,-240*xscale,-160*yscale, 224*xscale, 128*yscale, "gfx/gui/touch_r_thumb_turn_n_shoot.tga"    , 0.0f, NULL, NULL,  0, (keynum_t)0, NULL, 0, 56*xscale, 0, false);
855                         VID_TouchscreenArea( 3,-240*xscale,-256*yscale, 224*xscale, 224*yscale, NULL    , 0.0f, NULL, aim,  &buttons[2], (keynum_t)0, NULL, 0.2, 56*xscale, 0, false);
856
857                         VID_TouchscreenArea( 2, (vid_conwidth.value / 2) - 128,-80,  256,  80, NULL, 0.0f, NULL, NULL, &buttons[3], K_SHIFT, NULL, 0, 0, 0, true);
858
859                         VID_TouchscreenArea( 3,-240*xscale,-256*yscale, 224*xscale,  64*yscale, "gfx/gui/touch_secondary_slide.tga", 0.0f, NULL, NULL, &buttons[4], K_MOUSE2, NULL, 0, 56*xscale, 0, false);
860                         VID_TouchscreenArea( 3,-240*xscale,-256*yscale, 224*xscale,  160*yscale, NULL , 0.0f, NULL, NULL, &buttons[9], K_MOUSE3, NULL, 0.2, 56*xscale, 0, false);
861
862                         VID_TouchscreenArea( 1,-100,   0, 100, 100, NULL                         , 0.0f, NULL, NULL, &buttons[10], (keynum_t)'m', NULL, 0, 0, 0, true);
863                         VID_TouchscreenArea( 1,-100, 120, 100, 100, NULL                         , 0.0f, NULL, NULL, &buttons[11], (keynum_t)'b', NULL, 0, 0, 0, true);
864                         VID_TouchscreenArea( 0,   0,   0,  64,  64, NULL                         , 0.0f, NULL, NULL, &buttons[12], (keynum_t)'q', NULL, 0, 0, 0, true);
865                         if (developer.integer)
866                                 VID_TouchscreenArea( 0,   0,  96,  64,  64, NULL                         , 0.0f, NULL, NULL, &buttons[13], (keynum_t)'`', NULL, 0, 0, 0, true);
867                         else
868                                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[13], (keynum_t)'`', NULL, 0, 0, 0, false);
869                         VID_TouchscreenArea( 0,   0, 160,  64,  64, "gfx/gui/touch_menu_button.tga"         , 0.0f, NULL, NULL, &buttons[14], K_ESCAPE, NULL, 0, 0, 0, true);
870                         switch(cl.activeweapon)
871                         {
872                         case 14:
873                                 VID_TouchscreenArea( 2,  16*xscale,-320*yscale, 224*xscale, 64*yscale, "gfx/gui/touch_booster.tga" , 0.0f, NULL, NULL, &buttons[15], K_SPACE, NULL, 0, 0, 0, true);
874                                 break;
875                         case 12:
876                                 VID_TouchscreenArea( 2,  16*xscale,-320*yscale, 224*xscale, 64*yscale, "gfx/gui/touch_shockwave.tga" , 0.0f, NULL, NULL, &buttons[15], K_SPACE, NULL, 0, 0, 0, true);
877                                 break;
878                         default:
879                                 VID_TouchscreenArea( 0,  0,  0,  0,  0, NULL , 0.0f, NULL, NULL, &buttons[15], K_SPACE, NULL, 0, 0, 0, false);
880                         }
881                 }
882                 break;
883         default:
884                 if (!steelstorm_showing_mousecursor || !steelstorm_showing_mousecursor->integer)
885                 {
886                         Vid_ClearAllTouchscreenAreas(14);
887                         // this way we can skip cutscenes
888                         VID_TouchscreenArea( 0,   0,   0, vid_conwidth.value, vid_conheight.value, NULL                         , 0.0f, NULL, NULL, &buttons[14], K_ESCAPE, NULL, 0, 0, 0, false);
889                 }
890                 else
891                 {
892                         // in_windowmouse_x* is in screen resolution coordinates, not console resolution
893                         VID_TouchscreenCursor((float)in_windowmouse_x/vid_width.value*vid_conwidth.value, (float)in_windowmouse_y/vid_height.value*vid_conheight.value, 192*xscale, 192*yscale, &buttons[0], K_MOUSE1);
894                         Vid_ClearAllTouchscreenAreas(0);
895                 }
896                 break;
897         }
898
899         if (VID_ShowingKeyboard() && (float)in_windowmouse_y > vid_height.value / 2 - 10)
900                 in_windowmouse_y = 128;
901
902         cl.cmd.forwardmove -= move[1] * cl_forwardspeed.value;
903         cl.cmd.sidemove += move[0] * cl_sidespeed.value;
904         cl.viewangles[0] += aim[1] * cl_pitchspeed.value * cl.realframetime;
905         cl.viewangles[1] -= aim[0] * cl_yawspeed.value * cl.realframetime;
906 }
907
908 static void IN_Move_TouchScreen_Quake(void)
909 {
910         int x, y;
911         float move[3], aim[3], click[3];
912         static qboolean oldbuttons[128];
913         static qboolean buttons[128];
914         keydest_t keydest = (key_consoleactive & KEY_CONSOLEACTIVE_USER) ? key_console : key_dest;
915         memcpy(oldbuttons, buttons, sizeof(oldbuttons));
916         memset(multitouchs, 0, sizeof(multitouchs));
917
918         // simple quake controls
919         multitouch[MAXFINGERS-1][0] = SDL_GetMouseState(&x, &y);
920         multitouch[MAXFINGERS-1][1] = x * 32768 / vid.width;
921         multitouch[MAXFINGERS-1][2] = y * 32768 / vid.height;
922
923         // top of screen is toggleconsole and K_ESCAPE
924         switch(keydest)
925         {
926         case key_console:
927                 VID_TouchscreenArea( 0,   0,   0,  64,  64, NULL                         , 0.0f, NULL, NULL, &buttons[13], (keynum_t)'`', NULL, 0, 0, 0, true);
928                 VID_TouchscreenArea( 0,  64,   0,  64,  64, "gfx/touch_menu.tga"         , 0.0f, NULL, NULL, &buttons[14], K_ESCAPE, NULL, 0, 0, 0, true);
929                 if (!VID_ShowingKeyboard())
930                 {
931                         // user entered a command, close the console now
932                         Con_ToggleConsole_f();
933                 }
934                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[15], (keynum_t)0, NULL, 0, 0, 0, true);
935                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, move, &buttons[0], K_MOUSE4, NULL, 0, 0, 0, true);
936                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, aim,  &buttons[1], K_MOUSE5, NULL, 0, 0, 0, true);
937                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, click,&buttons[2], K_MOUSE1, NULL, 0, 0, 0, true);
938                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[3], K_SPACE, NULL, 0, 0, 0, true);
939                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[4], K_MOUSE2, NULL, 0, 0, 0, true);
940                 break;
941         case key_game:
942                 VID_TouchscreenArea( 0,   0,   0,  64,  64, NULL                         , 0.0f, NULL, NULL, &buttons[13], (keynum_t)'`', NULL, 0, 0, 0, true);
943                 VID_TouchscreenArea( 0,  64,   0,  64,  64, "gfx/touch_menu.tga"         , 0.0f, NULL, NULL, &buttons[14], K_ESCAPE, NULL, 0, 0, 0, true);
944                 VID_TouchscreenArea( 2,   0,-128, 128, 128, "gfx/touch_movebutton.tga"   , 0.0f, NULL, move, &buttons[0], K_MOUSE4, NULL, 0, 0, 0, true);
945                 VID_TouchscreenArea( 3,-128,-128, 128, 128, "gfx/touch_aimbutton.tga"    , 0.0f, NULL, aim,  &buttons[1], K_MOUSE5, NULL, 0, 0, 0, true);
946                 VID_TouchscreenArea( 2,   0,-160,  64,  32, "gfx/touch_jumpbutton.tga"   , 0.0f, NULL, NULL, &buttons[3], K_SPACE, NULL, 0, 0, 0, true);
947                 VID_TouchscreenArea( 3,-128,-160,  64,  32, "gfx/touch_attackbutton.tga" , 0.0f, NULL, NULL, &buttons[2], K_MOUSE1, NULL, 0, 0, 0, true);
948                 VID_TouchscreenArea( 3, -64,-160,  64,  32, "gfx/touch_attack2button.tga", 0.0f, NULL, NULL, &buttons[4], K_MOUSE2, NULL, 0, 0, 0, true);
949                 buttons[15] = false;
950                 break;
951         default:
952                 VID_TouchscreenArea( 0,   0,   0,  64,  64, NULL                         , 0.0f, NULL, NULL, &buttons[13], (keynum_t)'`', NULL, 0, 0, 0, true);
953                 VID_TouchscreenArea( 0,  64,   0,  64,  64, "gfx/touch_menu.tga"         , 0.0f, NULL, NULL, &buttons[14], K_ESCAPE, NULL, 0, 0, 0, true);
954                 // in menus, an icon in the corner activates keyboard
955                 VID_TouchscreenArea( 2,   0, -32,  32,  32, "gfx/touch_keyboard.tga"     , 0.0f, NULL, NULL, &buttons[15], (keynum_t)0, NULL, 0, 0, 0, true);
956                 if (buttons[15])
957                         VID_ShowKeyboard(true);
958                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, move, &buttons[0], K_MOUSE4, NULL, 0, 0, 0, true);
959                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, aim,  &buttons[1], K_MOUSE5, NULL, 0, 0, 0, true);
960                 VID_TouchscreenArea(16, -320,-480,640, 960, NULL                         , 0.0f, NULL, click,&buttons[2], K_MOUSE1, NULL, 0, 0, 0, true);
961                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[3], K_SPACE, NULL, 0, 0, 0, true);
962                 VID_TouchscreenArea( 0,   0,   0,   0,   0, NULL                         , 0.0f, NULL, NULL, &buttons[4], K_MOUSE2, NULL, 0, 0, 0, true);
963                 if (buttons[2])
964                 {
965                         in_windowmouse_x = x;
966                         in_windowmouse_y = y;
967                 }
968                 break;
969         }
970
971         cl.cmd.forwardmove -= move[1] * cl_forwardspeed.value;
972         cl.cmd.sidemove += move[0] * cl_sidespeed.value;
973         cl.viewangles[0] += aim[1] * cl_pitchspeed.value * cl.realframetime;
974         cl.viewangles[1] -= aim[0] * cl_yawspeed.value * cl.realframetime;
975 }
976
977 void IN_Move( void )
978 {
979         static int old_x = 0, old_y = 0;
980         static int stuck = 0;
981         int x, y;
982         vid_joystate_t joystate;
983
984         scr_numtouchscreenareas = 0;
985
986         if (vid_touchscreen.integer)
987         {
988                 switch(gamemode)
989                 {
990                 case GAME_STEELSTORM:
991                         IN_Move_TouchScreen_SteelStorm();
992                         break;
993                 default:
994                         IN_Move_TouchScreen_Quake();
995                         break;
996                 }
997         }
998         else
999         {
1000                 if (vid_usingmouse)
1001                 {
1002                         if (vid_stick_mouse.integer || !vid_usingmouse_relativeworks)
1003                         {
1004                                 // have the mouse stuck in the middle, example use: prevent expose effect of beryl during the game when not using
1005                                 // window grabbing. --blub
1006         
1007                                 // we need 2 frames to initialize the center position
1008                                 if(!stuck)
1009                                 {
1010 #if SDL_MAJOR_VERSION == 1
1011                                         SDL_WarpMouse(win_half_width, win_half_height);
1012 #else
1013                                         SDL_WarpMouseInWindow(window, win_half_width, win_half_height);
1014 #endif
1015                                         SDL_GetMouseState(&x, &y);
1016                                         SDL_GetRelativeMouseState(&x, &y);
1017                                         ++stuck;
1018                                 } else {
1019                                         SDL_GetRelativeMouseState(&x, &y);
1020                                         in_mouse_x = x + old_x;
1021                                         in_mouse_y = y + old_y;
1022                                         SDL_GetMouseState(&x, &y);
1023                                         old_x = x - win_half_width;
1024                                         old_y = y - win_half_height;
1025 #if SDL_MAJOR_VERSION == 1
1026                                         SDL_WarpMouse(win_half_width, win_half_height);
1027 #else
1028                                         SDL_WarpMouseInWindow(window, win_half_width, win_half_height);
1029 #endif
1030                                 }
1031                         } else {
1032                                 SDL_GetRelativeMouseState( &x, &y );
1033                                 in_mouse_x = x;
1034                                 in_mouse_y = y;
1035                         }
1036                 }
1037
1038                 SDL_GetMouseState(&x, &y);
1039                 in_windowmouse_x = x;
1040                 in_windowmouse_y = y;
1041         }
1042
1043         VID_BuildJoyState(&joystate);
1044         VID_ApplyJoyState(&joystate);
1045 }
1046
1047 /////////////////////
1048 // Message Handling
1049 ////
1050
1051 #ifdef SDL_R_RESTART
1052 static qboolean sdl_needs_restart;
1053 static void sdl_start(void)
1054 {
1055 }
1056 static void sdl_shutdown(void)
1057 {
1058         sdl_needs_restart = false;
1059 }
1060 static void sdl_newmap(void)
1061 {
1062 }
1063 #endif
1064
1065 static keynum_t buttonremap[] =
1066 {
1067         K_MOUSE1,
1068         K_MOUSE3,
1069         K_MOUSE2,
1070 #if SDL_MAJOR_VERSION == 1
1071         // TODO Find out how SDL maps these buttons. It looks like we should
1072         // still include these for sdl2? At least the button indexes don't
1073         // differ between SDL1 and SDL2 for me, thus this array should stay the
1074         // same (in X11 button order).
1075         K_MWHEELUP,
1076         K_MWHEELDOWN,
1077 #endif
1078         K_MOUSE4,
1079         K_MOUSE5,
1080         K_MOUSE6,
1081         K_MOUSE7,
1082         K_MOUSE8,
1083         K_MOUSE9,
1084         K_MOUSE10,
1085         K_MOUSE11,
1086         K_MOUSE12,
1087         K_MOUSE13,
1088         K_MOUSE14,
1089         K_MOUSE15,
1090         K_MOUSE16,
1091 };
1092
1093 #if SDL_MAJOR_VERSION == 1
1094 // SDL
1095 void Sys_SendKeyEvents( void )
1096 {
1097         static qboolean sound_active = true;
1098         int keycode;
1099         SDL_Event event;
1100
1101         VID_EnableJoystick(true);
1102
1103         while( SDL_PollEvent( &event ) )
1104                 switch( event.type ) {
1105                         case SDL_QUIT:
1106                                 Sys_Quit(0);
1107                                 break;
1108                         case SDL_KEYDOWN:
1109                         case SDL_KEYUP:
1110                                 keycode = MapKey(event.key.keysym.sym);
1111                                 if (!VID_JoyBlockEmulatedKeys(keycode))
1112                                 {
1113                                         if(keycode == K_NUMLOCK || keycode == K_CAPSLOCK)
1114                                         {
1115                                                 // simulate down followed by up
1116                                                 Key_Event(keycode, event.key.keysym.unicode, true);
1117                                                 Key_Event(keycode, event.key.keysym.unicode, false);
1118                                                 break;
1119                                         }
1120                                         Key_Event(keycode, event.key.keysym.unicode, (event.key.state == SDL_PRESSED));
1121                                 }
1122                                 break;
1123                         case SDL_ACTIVEEVENT:
1124                                 if( event.active.state & SDL_APPACTIVE )
1125                                 {
1126                                         if( event.active.gain )
1127                                                 vid_hidden = false;
1128                                         else
1129                                                 vid_hidden = true;
1130                                 }
1131                                 break;
1132                         case SDL_MOUSEBUTTONDOWN:
1133                         case SDL_MOUSEBUTTONUP:
1134                                 if (!vid_touchscreen.integer)
1135                                 if (event.button.button > 0 && event.button.button <= ARRAY_SIZE(buttonremap))
1136                                         Key_Event( buttonremap[event.button.button - 1], 0, event.button.state == SDL_PRESSED );
1137                                 break;
1138                         case SDL_JOYBUTTONDOWN:
1139                         case SDL_JOYBUTTONUP:
1140                         case SDL_JOYAXISMOTION:
1141                         case SDL_JOYBALLMOTION:
1142                         case SDL_JOYHATMOTION:
1143                                 break;
1144                         case SDL_VIDEOEXPOSE:
1145                                 break;
1146                         case SDL_VIDEORESIZE:
1147                                 if(vid_resizable.integer < 2 || vid_isfullscreen)
1148                                 {
1149                                         vid.width = event.resize.w;
1150                                         vid.height = event.resize.h;
1151                                         if (!vid_isfullscreen)
1152                                                 screen = SDL_SetVideoMode(vid.width, vid.height, video_bpp, video_flags);
1153                                         if (vid_softsurface)
1154                                         {
1155                                                 SDL_FreeSurface(vid_softsurface);
1156                                                 vid_softsurface = SDL_CreateRGBSurface(SDL_SWSURFACE, vid.width, vid.height, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
1157                                                 SDL_SetAlpha(vid_softsurface, 0, 255);
1158                                                 vid.softpixels = (unsigned int *)vid_softsurface->pixels;
1159                                                 if (vid.softdepthpixels)
1160                                                         free(vid.softdepthpixels);
1161                                                 vid.softdepthpixels = (unsigned int*)calloc(1, vid.width * vid.height * 4);
1162                                         }
1163 #ifdef SDL_R_RESTART
1164                                         // better not call R_Modules_Restart from here directly, as this may wreak havoc...
1165                                         // so, let's better queue it for next frame
1166                                         if(!sdl_needs_restart)
1167                                         {
1168                                                 Cbuf_AddText("\nr_restart\n");
1169                                                 sdl_needs_restart = true;
1170                                         }
1171 #endif
1172                                 }
1173                                 break;
1174 #if SDL_MAJOR_VERSION != 1
1175                         case SDL_TEXTEDITING:
1176                                 break;
1177                         case SDL_TEXTINPUT:
1178                                 break;
1179 #endif
1180                         case SDL_MOUSEMOTION:
1181                                 break;
1182                         default:
1183                                 Con_DPrintf("Received unrecognized SDL_Event type 0x%x\n", event.type);
1184                                 break;
1185                 }
1186
1187         // enable/disable sound on focus gain/loss
1188         if ((!vid_hidden && vid_activewindow) || !snd_mutewhenidle.integer)
1189         {
1190                 if (!sound_active)
1191                 {
1192                         S_UnblockSound ();
1193                         sound_active = true;
1194                 }
1195         }
1196         else
1197         {
1198                 if (sound_active)
1199                 {
1200                         S_BlockSound ();
1201                         sound_active = false;
1202                 }
1203         }
1204 }
1205
1206 #else
1207
1208 //#define DEBUGSDLEVENTS
1209
1210 // SDL2
1211 void Sys_SendKeyEvents( void )
1212 {
1213         static qboolean sound_active = true;
1214         int keycode;
1215         int i;
1216         int j;
1217         int unicode;
1218         SDL_Event event;
1219
1220         VID_EnableJoystick(true);
1221
1222         while( SDL_PollEvent( &event ) )
1223                 switch( event.type ) {
1224                         case SDL_QUIT:
1225 #ifdef DEBUGSDLEVENTS
1226                                 Con_DPrintf("SDL_Event: SDL_QUIT\n");
1227 #endif
1228                                 Sys_Quit(0);
1229                                 break;
1230                         case SDL_KEYDOWN:
1231                         case SDL_KEYUP:
1232 #ifdef DEBUGSDLEVENTS
1233                                 if (event.type == SDL_KEYDOWN)
1234                                         Con_DPrintf("SDL_Event: SDL_KEYDOWN %i\n", event.key.keysym.sym);
1235                                 else
1236                                         Con_DPrintf("SDL_Event: SDL_KEYUP %i\n", event.key.keysym.sym);
1237 #endif
1238                                 keycode = MapKey(event.key.keysym.sym);
1239                                 if (!VID_JoyBlockEmulatedKeys(keycode))
1240                                         Key_Event(keycode, 0, (event.key.state == SDL_PRESSED));
1241                                 break;
1242                         case SDL_MOUSEBUTTONDOWN:
1243                         case SDL_MOUSEBUTTONUP:
1244 #ifdef DEBUGSDLEVENTS
1245                                 if (event.type == SDL_MOUSEBUTTONDOWN)
1246                                         Con_DPrintf("SDL_Event: SDL_MOUSEBUTTONDOWN\n");
1247                                 else
1248                                         Con_DPrintf("SDL_Event: SDL_MOUSEBUTTONUP\n");
1249 #endif
1250                                 if (!vid_touchscreen.integer)
1251                                 if (event.button.button > 0 && event.button.button <= ARRAY_SIZE(buttonremap))
1252                                         Key_Event( buttonremap[event.button.button - 1], 0, event.button.state == SDL_PRESSED );
1253                                 break;
1254                         case SDL_MOUSEWHEEL:
1255                                 // TODO support wheel x direction.
1256                                 i = event.wheel.y;
1257                                 while (i > 0) {
1258                                         --i;
1259                                         Key_Event( K_MWHEELUP, 0, true );
1260                                         Key_Event( K_MWHEELUP, 0, false );
1261                                 }
1262                                 while (i < 0) {
1263                                         ++i;
1264                                         Key_Event( K_MWHEELDOWN, 0, true );
1265                                         Key_Event( K_MWHEELDOWN, 0, false );
1266                                 }
1267                                 break;
1268                         case SDL_JOYBUTTONDOWN:
1269                         case SDL_JOYBUTTONUP:
1270                         case SDL_JOYAXISMOTION:
1271                         case SDL_JOYBALLMOTION:
1272                         case SDL_JOYHATMOTION:
1273 #ifdef DEBUGSDLEVENTS
1274                                 Con_DPrintf("SDL_Event: SDL_JOY*\n");
1275 #endif
1276                                 break;
1277                         case SDL_WINDOWEVENT:
1278 #ifdef DEBUGSDLEVENTS
1279                                 Con_DPrintf("SDL_Event: SDL_WINDOWEVENT %i\n", (int)event.window.event);
1280 #endif
1281                                 //if (event.window.windowID == window) // how to compare?
1282                                 {
1283                                         switch(event.window.event)
1284                                         {
1285                                         case SDL_WINDOWEVENT_SHOWN:
1286                                                 vid_hidden = false;
1287                                                 break;
1288                                         case  SDL_WINDOWEVENT_HIDDEN:
1289                                                 vid_hidden = true;
1290                                                 break;
1291                                         case SDL_WINDOWEVENT_EXPOSED:
1292 #ifdef DEBUGSDLEVENTS
1293                                                 Con_DPrintf("SDL_Event: SDL_WINDOWEVENT_EXPOSED\n");
1294 #endif
1295                                                 break;
1296                                         case SDL_WINDOWEVENT_MOVED:
1297                                                 break;
1298                                         case SDL_WINDOWEVENT_RESIZED:
1299                                                 if(vid_resizable.integer < 2)
1300                                                 {
1301                                                         vid.width = event.window.data1;
1302                                                         vid.height = event.window.data2;
1303                                                         if (vid_softsurface)
1304                                                         {
1305                                                                 SDL_FreeSurface(vid_softsurface);
1306                                                                 vid_softsurface = SDL_CreateRGBSurface(SDL_SWSURFACE, vid.width, vid.height, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
1307                                                                 SDL_SetSurfaceBlendMode(vid_softsurface, SDL_BLENDMODE_NONE);
1308                                                                 vid.softpixels = (unsigned int *)vid_softsurface->pixels;
1309                                                                 if (vid.softdepthpixels)
1310                                                                         free(vid.softdepthpixels);
1311                                                                 vid.softdepthpixels = (unsigned int*)calloc(1, vid.width * vid.height * 4);
1312                                                         }
1313 #ifdef SDL_R_RESTART
1314                                                         // better not call R_Modules_Restart from here directly, as this may wreak havoc...
1315                                                         // so, let's better queue it for next frame
1316                                                         if(!sdl_needs_restart)
1317                                                         {
1318                                                                 Cbuf_AddText("\nr_restart\n");
1319                                                                 sdl_needs_restart = true;
1320                                                         }
1321 #endif
1322                                                 }
1323                                                 break;
1324                                         case SDL_WINDOWEVENT_MINIMIZED:
1325                                                 break;
1326                                         case SDL_WINDOWEVENT_MAXIMIZED:
1327                                                 break;
1328                                         case SDL_WINDOWEVENT_RESTORED:
1329                                                 break;
1330                                         case SDL_WINDOWEVENT_ENTER:
1331                                                 break;
1332                                         case SDL_WINDOWEVENT_LEAVE:
1333                                                 break;
1334                                         case SDL_WINDOWEVENT_FOCUS_GAINED:
1335                                                 vid_hasfocus = true;
1336                                                 break;
1337                                         case SDL_WINDOWEVENT_FOCUS_LOST:
1338                                                 vid_hasfocus = false;
1339                                                 break;
1340                                         case SDL_WINDOWEVENT_CLOSE:
1341                                                 Sys_Quit(0);
1342                                                 break;
1343                                         }
1344                                 }
1345                                 break;
1346                         case SDL_TEXTEDITING:
1347 #ifdef DEBUGSDLEVENTS
1348                                 Con_DPrintf("SDL_Event: SDL_TEXTEDITING - composition = %s, cursor = %d, selection lenght = %d\n", event.edit.text, event.edit.start, event.edit.length);
1349 #endif
1350                                 // FIXME!  this is where composition gets supported
1351                                 break;
1352                         case SDL_TEXTINPUT:
1353 #ifdef DEBUGSDLEVENTS
1354                                 Con_DPrintf("SDL_Event: SDL_TEXTINPUT - text: %s\n", event.text.text);
1355 #endif
1356                                 // we have some characters to parse
1357                                 {
1358                                         unicode = 0;
1359                                         for (i = 0;event.text.text[i];)
1360                                         {
1361                                                 unicode = event.text.text[i++];
1362                                                 if (unicode & 0x80)
1363                                                 {
1364                                                         // UTF-8 character
1365                                                         // strip high bits (we could count these to validate character length but we don't)
1366                                                         for (j = 0x80;unicode & j;j >>= 1)
1367                                                                 unicode ^= j;
1368                                                         for (;(event.text.text[i] & 0xC0) == 0x80;i++)
1369                                                                 unicode = (unicode << 6) | (event.text.text[i] & 0x3F);
1370                                                         // low characters are invalid and could be bad, so replace them
1371                                                         if (unicode < 0x80)
1372                                                                 unicode = '?'; // we could use 0xFFFD instead, the unicode substitute character
1373                                                 }
1374                                                 //Con_DPrintf("SDL_TEXTINPUT: K_TEXT %i \n", unicode);
1375
1376                                                 Key_Event(K_TEXT, unicode, true);
1377                                                 Key_Event(K_TEXT, unicode, false);
1378                                         }
1379                                 }
1380                                 break;
1381                         case SDL_MOUSEMOTION:
1382                                 break;
1383                         case SDL_FINGERDOWN:
1384 #ifdef DEBUGSDLEVENTS
1385                                 Con_DPrintf("SDL_FINGERDOWN for finger %i\n", (int)event.tfinger.fingerId);
1386 #endif
1387                                 for (i = 0;i < MAXFINGERS-1;i++)
1388                                 {
1389                                         if (!multitouch[i][0])
1390                                         {
1391                                                 multitouch[i][0] = event.tfinger.fingerId + 1;
1392                                                 multitouch[i][1] = event.tfinger.x;
1393                                                 multitouch[i][2] = event.tfinger.y;
1394                                                 // TODO: use event.tfinger.pressure?
1395                                                 break;
1396                                         }
1397                                 }
1398                                 if (i == MAXFINGERS-1)
1399                                         Con_DPrintf("Too many fingers at once!\n");
1400                                 break;
1401                         case SDL_FINGERUP:
1402 #ifdef DEBUGSDLEVENTS
1403                                 Con_DPrintf("SDL_FINGERUP for finger %i\n", (int)event.tfinger.fingerId);
1404 #endif
1405                                 for (i = 0;i < MAXFINGERS-1;i++)
1406                                 {
1407                                         if (multitouch[i][0] == event.tfinger.fingerId + 1)
1408                                         {
1409                                                 multitouch[i][0] = 0;
1410                                                 break;
1411                                         }
1412                                 }
1413                                 if (i == MAXFINGERS-1)
1414                                         Con_DPrintf("No SDL_FINGERDOWN event matches this SDL_FINGERMOTION event\n");
1415                                 break;
1416                         case SDL_FINGERMOTION:
1417 #ifdef DEBUGSDLEVENTS
1418                                 Con_DPrintf("SDL_FINGERMOTION for finger %i\n", (int)event.tfinger.fingerId);
1419 #endif
1420                                 for (i = 0;i < MAXFINGERS-1;i++)
1421                                 {
1422                                         if (multitouch[i][0] == event.tfinger.fingerId + 1)
1423                                         {
1424                                                 multitouch[i][1] = event.tfinger.x;
1425                                                 multitouch[i][2] = event.tfinger.y;
1426                                                 break;
1427                                         }
1428                                 }
1429                                 if (i == MAXFINGERS-1)
1430                                         Con_DPrintf("No SDL_FINGERDOWN event matches this SDL_FINGERMOTION event\n");
1431                                 break;
1432                         default:
1433 #ifdef DEBUGSDLEVENTS
1434                                 Con_DPrintf("Received unrecognized SDL_Event type 0x%x\n", event.type);
1435 #endif
1436                                 break;
1437                 }
1438
1439         // enable/disable sound on focus gain/loss
1440         if ((!vid_hidden && vid_activewindow) || !snd_mutewhenidle.integer)
1441         {
1442                 if (!sound_active)
1443                 {
1444                         S_UnblockSound ();
1445                         sound_active = true;
1446                 }
1447         }
1448         else
1449         {
1450                 if (sound_active)
1451                 {
1452                         S_BlockSound ();
1453                         sound_active = false;
1454                 }
1455         }
1456 }
1457 #endif
1458
1459 /////////////////
1460 // Video system
1461 ////
1462
1463 #ifdef USE_GLES2
1464 #ifndef qglClear
1465 #ifdef __IPHONEOS__
1466 #include <OpenGLES/ES2/gl.h>
1467 #else
1468 #include <SDL_opengles.h>
1469 #endif
1470
1471 //#define PRECALL //Con_Printf("GLCALL %s:%i\n", __FILE__, __LINE__)
1472 #define PRECALL
1473 #define POSTCALL
1474 GLboolean wrapglIsBuffer(GLuint buffer) {PRECALL;return glIsBuffer(buffer);POSTCALL;}
1475 GLboolean wrapglIsEnabled(GLenum cap) {PRECALL;return glIsEnabled(cap);POSTCALL;}
1476 GLboolean wrapglIsFramebuffer(GLuint framebuffer) {PRECALL;return glIsFramebuffer(framebuffer);POSTCALL;}
1477 //GLboolean wrapglIsQuery(GLuint qid) {PRECALL;return glIsQuery(qid);POSTCALL;}
1478 GLboolean wrapglIsRenderbuffer(GLuint renderbuffer) {PRECALL;return glIsRenderbuffer(renderbuffer);POSTCALL;}
1479 //GLboolean wrapglUnmapBuffer(GLenum target) {PRECALL;return glUnmapBuffer(target);POSTCALL;}
1480 GLenum wrapglCheckFramebufferStatus(GLenum target) {PRECALL;return glCheckFramebufferStatus(target);POSTCALL;}
1481 GLenum wrapglGetError(void) {PRECALL;return glGetError();POSTCALL;}
1482 GLuint wrapglCreateProgram(void) {PRECALL;return glCreateProgram();POSTCALL;}
1483 GLuint wrapglCreateShader(GLenum shaderType) {PRECALL;return glCreateShader(shaderType);POSTCALL;}
1484 //GLuint wrapglGetHandle(GLenum pname) {PRECALL;return glGetHandle(pname);POSTCALL;}
1485 GLint wrapglGetAttribLocation(GLuint programObj, const GLchar *name) {PRECALL;return glGetAttribLocation(programObj, name);POSTCALL;}
1486 GLint wrapglGetUniformLocation(GLuint programObj, const GLchar *name) {PRECALL;return glGetUniformLocation(programObj, name);POSTCALL;}
1487 //GLvoid* wrapglMapBuffer(GLenum target, GLenum access) {PRECALL;return glMapBuffer(target, access);POSTCALL;}
1488 const GLubyte* wrapglGetString(GLenum name) {PRECALL;return (const GLubyte*)glGetString(name);POSTCALL;}
1489 void wrapglActiveStencilFace(GLenum e) {PRECALL;Con_Printf("glActiveStencilFace(e)\n");POSTCALL;}
1490 void wrapglActiveTexture(GLenum e) {PRECALL;glActiveTexture(e);POSTCALL;}
1491 void wrapglAlphaFunc(GLenum func, GLclampf ref) {PRECALL;Con_Printf("glAlphaFunc(func, ref)\n");POSTCALL;}
1492 void wrapglArrayElement(GLint i) {PRECALL;Con_Printf("glArrayElement(i)\n");POSTCALL;}
1493 void wrapglAttachShader(GLuint containerObj, GLuint obj) {PRECALL;glAttachShader(containerObj, obj);POSTCALL;}
1494 //void wrapglBegin(GLenum mode) {PRECALL;Con_Printf("glBegin(mode)\n");POSTCALL;}
1495 //void wrapglBeginQuery(GLenum target, GLuint qid) {PRECALL;glBeginQuery(target, qid);POSTCALL;}
1496 void wrapglBindAttribLocation(GLuint programObj, GLuint index, const GLchar *name) {PRECALL;glBindAttribLocation(programObj, index, name);POSTCALL;}
1497 //void wrapglBindFragDataLocation(GLuint programObj, GLuint index, const GLchar *name) {PRECALL;glBindFragDataLocation(programObj, index, name);POSTCALL;}
1498 void wrapglBindBuffer(GLenum target, GLuint buffer) {PRECALL;glBindBuffer(target, buffer);POSTCALL;}
1499 void wrapglBindFramebuffer(GLenum target, GLuint framebuffer) {PRECALL;glBindFramebuffer(target, framebuffer);POSTCALL;}
1500 void wrapglBindRenderbuffer(GLenum target, GLuint renderbuffer) {PRECALL;glBindRenderbuffer(target, renderbuffer);POSTCALL;}
1501 void wrapglBindTexture(GLenum target, GLuint texture) {PRECALL;glBindTexture(target, texture);POSTCALL;}
1502 void wrapglBlendEquation(GLenum e) {PRECALL;glBlendEquation(e);POSTCALL;}
1503 void wrapglBlendFunc(GLenum sfactor, GLenum dfactor) {PRECALL;glBlendFunc(sfactor, dfactor);POSTCALL;}
1504 void wrapglBufferData(GLenum target, GLsizeiptrARB size, const GLvoid *data, GLenum usage) {PRECALL;glBufferData(target, size, data, usage);POSTCALL;}
1505 void wrapglBufferSubData(GLenum target, GLintptrARB offset, GLsizeiptrARB size, const GLvoid *data) {PRECALL;glBufferSubData(target, offset, size, data);POSTCALL;}
1506 void wrapglClear(GLbitfield mask) {PRECALL;glClear(mask);POSTCALL;}
1507 void wrapglClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) {PRECALL;glClearColor(red, green, blue, alpha);POSTCALL;}
1508 void wrapglClearDepth(GLclampd depth) {PRECALL;/*Con_Printf("glClearDepth(%f)\n", depth);glClearDepthf((float)depth);*/POSTCALL;}
1509 void wrapglClearStencil(GLint s) {PRECALL;glClearStencil(s);POSTCALL;}
1510 void wrapglClientActiveTexture(GLenum target) {PRECALL;Con_Printf("glClientActiveTexture(target)\n");POSTCALL;}
1511 void wrapglColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) {PRECALL;Con_Printf("glColor4f(red, green, blue, alpha)\n");POSTCALL;}
1512 void wrapglColor4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha) {PRECALL;Con_Printf("glColor4ub(red, green, blue, alpha)\n");POSTCALL;}
1513 void wrapglColorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) {PRECALL;glColorMask(red, green, blue, alpha);POSTCALL;}
1514 void wrapglColorPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr) {PRECALL;Con_Printf("glColorPointer(size, type, stride, ptr)\n");POSTCALL;}
1515 void wrapglCompileShader(GLuint shaderObj) {PRECALL;glCompileShader(shaderObj);POSTCALL;}
1516 void wrapglCompressedTexImage2D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border,  GLsizei imageSize, const void *data) {PRECALL;glCompressedTexImage2D(target, level, internalformat, width, height, border, imageSize, data);POSTCALL;}
1517 void wrapglCompressedTexImage3D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const void *data) {PRECALL;Con_Printf("glCompressedTexImage3D(target, level, internalformat, width, height, depth, border, imageSize, data)\n");POSTCALL;}
1518 void wrapglCompressedTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *data) {PRECALL;glCompressedTexSubImage2D(target, level, xoffset, yoffset, width, height, format, imageSize, data);POSTCALL;}
1519 void wrapglCompressedTexSubImage3D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLsizei imageSize, const void *data) {PRECALL;Con_Printf("glCompressedTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, imageSize, data)\n");POSTCALL;}
1520 void wrapglCopyTexImage2D(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border) {PRECALL;glCopyTexImage2D(target, level, internalformat, x, y, width, height, border);POSTCALL;}
1521 void wrapglCopyTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height) {PRECALL;glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height);POSTCALL;}
1522 void wrapglCopyTexSubImage3D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height) {PRECALL;Con_Printf("glCopyTexSubImage3D(target, level, xoffset, yoffset, zoffset, x, y, width, height)\n");POSTCALL;}
1523 void wrapglCullFace(GLenum mode) {PRECALL;glCullFace(mode);POSTCALL;}
1524 void wrapglDeleteBuffers(GLsizei n, const GLuint *buffers) {PRECALL;glDeleteBuffers(n, buffers);POSTCALL;}
1525 void wrapglDeleteFramebuffers(GLsizei n, const GLuint *framebuffers) {PRECALL;glDeleteFramebuffers(n, framebuffers);POSTCALL;}
1526 void wrapglDeleteShader(GLuint obj) {PRECALL;glDeleteShader(obj);POSTCALL;}
1527 void wrapglDeleteProgram(GLuint obj) {PRECALL;glDeleteProgram(obj);POSTCALL;}
1528 //void wrapglDeleteQueries(GLsizei n, const GLuint *ids) {PRECALL;glDeleteQueries(n, ids);POSTCALL;}
1529 void wrapglDeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers) {PRECALL;glDeleteRenderbuffers(n, renderbuffers);POSTCALL;}
1530 void wrapglDeleteTextures(GLsizei n, const GLuint *textures) {PRECALL;glDeleteTextures(n, textures);POSTCALL;}
1531 void wrapglDepthFunc(GLenum func) {PRECALL;glDepthFunc(func);POSTCALL;}
1532 void wrapglDepthMask(GLboolean flag) {PRECALL;glDepthMask(flag);POSTCALL;}
1533 //void wrapglDepthRange(GLclampd near_val, GLclampd far_val) {PRECALL;glDepthRangef((float)near_val, (float)far_val);POSTCALL;}
1534 void wrapglDepthRangef(GLclampf near_val, GLclampf far_val) {PRECALL;glDepthRangef(near_val, far_val);POSTCALL;}
1535 void wrapglDetachShader(GLuint containerObj, GLuint attachedObj) {PRECALL;glDetachShader(containerObj, attachedObj);POSTCALL;}
1536 void wrapglDisable(GLenum cap) {PRECALL;glDisable(cap);POSTCALL;}
1537 void wrapglDisableClientState(GLenum cap) {PRECALL;Con_Printf("glDisableClientState(cap)\n");POSTCALL;}
1538 void wrapglDisableVertexAttribArray(GLuint index) {PRECALL;glDisableVertexAttribArray(index);POSTCALL;}
1539 void wrapglDrawArrays(GLenum mode, GLint first, GLsizei count) {PRECALL;glDrawArrays(mode, first, count);POSTCALL;}
1540 void wrapglDrawBuffer(GLenum mode) {PRECALL;Con_Printf("glDrawBuffer(mode)\n");POSTCALL;}
1541 void wrapglDrawBuffers(GLsizei n, const GLenum *bufs) {PRECALL;Con_Printf("glDrawBuffers(n, bufs)\n");POSTCALL;}
1542 void wrapglDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices) {PRECALL;glDrawElements(mode, count, type, indices);POSTCALL;}
1543 //void wrapglDrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices) {PRECALL;glDrawRangeElements(mode, start, end, count, type, indices);POSTCALL;}
1544 //void wrapglDrawRangeElementsEXT(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices) {PRECALL;glDrawRangeElements(mode, start, end, count, type, indices);POSTCALL;}
1545 void wrapglEnable(GLenum cap) {PRECALL;glEnable(cap);POSTCALL;}
1546 void wrapglEnableClientState(GLenum cap) {PRECALL;Con_Printf("glEnableClientState(cap)\n");POSTCALL;}
1547 void wrapglEnableVertexAttribArray(GLuint index) {PRECALL;glEnableVertexAttribArray(index);POSTCALL;}
1548 //void wrapglEnd(void) {PRECALL;Con_Printf("glEnd()\n");POSTCALL;}
1549 //void wrapglEndQuery(GLenum target) {PRECALL;glEndQuery(target);POSTCALL;}
1550 void wrapglFinish(void) {PRECALL;glFinish();POSTCALL;}
1551 void wrapglFlush(void) {PRECALL;glFlush();POSTCALL;}
1552 void wrapglFramebufferRenderbuffer(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer) {PRECALL;glFramebufferRenderbuffer(target, attachment, renderbuffertarget, renderbuffer);POSTCALL;}
1553 void wrapglFramebufferTexture2D(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level) {PRECALL;glFramebufferTexture2D(target, attachment, textarget, texture, level);POSTCALL;}
1554 void wrapglFramebufferTexture3D(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint zoffset) {PRECALL;Con_Printf("glFramebufferTexture3D()\n");POSTCALL;}
1555 void wrapglGenBuffers(GLsizei n, GLuint *buffers) {PRECALL;glGenBuffers(n, buffers);POSTCALL;}
1556 void wrapglGenFramebuffers(GLsizei n, GLuint *framebuffers) {PRECALL;glGenFramebuffers(n, framebuffers);POSTCALL;}
1557 //void wrapglGenQueries(GLsizei n, GLuint *ids) {PRECALL;glGenQueries(n, ids);POSTCALL;}
1558 void wrapglGenRenderbuffers(GLsizei n, GLuint *renderbuffers) {PRECALL;glGenRenderbuffers(n, renderbuffers);POSTCALL;}
1559 void wrapglGenTextures(GLsizei n, GLuint *textures) {PRECALL;glGenTextures(n, textures);POSTCALL;}
1560 void wrapglGenerateMipmap(GLenum target) {PRECALL;glGenerateMipmap(target);POSTCALL;}
1561 void wrapglGetActiveAttrib(GLuint programObj, GLuint index, GLsizei maxLength, GLsizei *length, GLint *size, GLenum *type, GLchar *name) {PRECALL;glGetActiveAttrib(programObj, index, maxLength, length, size, type, name);POSTCALL;}
1562 void wrapglGetActiveUniform(GLuint programObj, GLuint index, GLsizei maxLength, GLsizei *length, GLint *size, GLenum *type, GLchar *name) {PRECALL;glGetActiveUniform(programObj, index, maxLength, length, size, type, name);POSTCALL;}
1563 void wrapglGetAttachedShaders(GLuint containerObj, GLsizei maxCount, GLsizei *count, GLuint *obj) {PRECALL;glGetAttachedShaders(containerObj, maxCount, count, obj);POSTCALL;}
1564 void wrapglGetBooleanv(GLenum pname, GLboolean *params) {PRECALL;glGetBooleanv(pname, params);POSTCALL;}
1565 void wrapglGetCompressedTexImage(GLenum target, GLint lod, void *img) {PRECALL;Con_Printf("glGetCompressedTexImage(target, lod, img)\n");POSTCALL;}
1566 void wrapglGetDoublev(GLenum pname, GLdouble *params) {PRECALL;Con_Printf("glGetDoublev(pname, params)\n");POSTCALL;}
1567 void wrapglGetFloatv(GLenum pname, GLfloat *params) {PRECALL;glGetFloatv(pname, params);POSTCALL;}
1568 void wrapglGetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment, GLenum pname, GLint *params) {PRECALL;glGetFramebufferAttachmentParameteriv(target, attachment, pname, params);POSTCALL;}
1569 void wrapglGetShaderInfoLog(GLuint obj, GLsizei maxLength, GLsizei *length, GLchar *infoLog) {PRECALL;glGetShaderInfoLog(obj, maxLength, length, infoLog);POSTCALL;}
1570 void wrapglGetProgramInfoLog(GLuint obj, GLsizei maxLength, GLsizei *length, GLchar *infoLog) {PRECALL;glGetProgramInfoLog(obj, maxLength, length, infoLog);POSTCALL;}
1571 void wrapglGetIntegerv(GLenum pname, GLint *params) {PRECALL;glGetIntegerv(pname, params);POSTCALL;}
1572 void wrapglGetShaderiv(GLuint obj, GLenum pname, GLint *params) {PRECALL;glGetShaderiv(obj, pname, params);POSTCALL;}
1573 void wrapglGetProgramiv(GLuint obj, GLenum pname, GLint *params) {PRECALL;glGetProgramiv(obj, pname, params);POSTCALL;}
1574 //void wrapglGetQueryObjectiv(GLuint qid, GLenum pname, GLint *params) {PRECALL;glGetQueryObjectiv(qid, pname, params);POSTCALL;}
1575 //void wrapglGetQueryObjectuiv(GLuint qid, GLenum pname, GLuint *params) {PRECALL;glGetQueryObjectuiv(qid, pname, params);POSTCALL;}
1576 //void wrapglGetQueryiv(GLenum target, GLenum pname, GLint *params) {PRECALL;glGetQueryiv(target, pname, params);POSTCALL;}
1577 void wrapglGetRenderbufferParameteriv(GLenum target, GLenum pname, GLint *params) {PRECALL;glGetRenderbufferParameteriv(target, pname, params);POSTCALL;}
1578 void wrapglGetShaderSource(GLuint obj, GLsizei maxLength, GLsizei *length, GLchar *source) {PRECALL;glGetShaderSource(obj, maxLength, length, source);POSTCALL;}
1579 void wrapglGetTexImage(GLenum target, GLint level, GLenum format, GLenum type, GLvoid *pixels) {PRECALL;Con_Printf("glGetTexImage(target, level, format, type, pixels)\n");POSTCALL;}
1580 void wrapglGetTexLevelParameterfv(GLenum target, GLint level, GLenum pname, GLfloat *params) {PRECALL;Con_Printf("glGetTexLevelParameterfv(target, level, pname, params)\n");POSTCALL;}
1581 void wrapglGetTexLevelParameteriv(GLenum target, GLint level, GLenum pname, GLint *params) {PRECALL;Con_Printf("glGetTexLevelParameteriv(target, level, pname, params)\n");POSTCALL;}
1582 void wrapglGetTexParameterfv(GLenum target, GLenum pname, GLfloat *params) {PRECALL;glGetTexParameterfv(target, pname, params);POSTCALL;}
1583 void wrapglGetTexParameteriv(GLenum target, GLenum pname, GLint *params) {PRECALL;glGetTexParameteriv(target, pname, params);POSTCALL;}
1584 void wrapglGetUniformfv(GLuint programObj, GLint location, GLfloat *params) {PRECALL;glGetUniformfv(programObj, location, params);POSTCALL;}
1585 void wrapglGetUniformiv(GLuint programObj, GLint location, GLint *params) {PRECALL;glGetUniformiv(programObj, location, params);POSTCALL;}
1586 void wrapglHint(GLenum target, GLenum mode) {PRECALL;glHint(target, mode);POSTCALL;}
1587 void wrapglLineWidth(GLfloat width) {PRECALL;glLineWidth(width);POSTCALL;}
1588 void wrapglLinkProgram(GLuint programObj) {PRECALL;glLinkProgram(programObj);POSTCALL;}
1589 void wrapglLoadIdentity(void) {PRECALL;Con_Printf("glLoadIdentity()\n");POSTCALL;}
1590 void wrapglLoadMatrixf(const GLfloat *m) {PRECALL;Con_Printf("glLoadMatrixf(m)\n");POSTCALL;}
1591 void wrapglMatrixMode(GLenum mode) {PRECALL;Con_Printf("glMatrixMode(mode)\n");POSTCALL;}
1592 void wrapglMultiTexCoord1f(GLenum target, GLfloat s) {PRECALL;Con_Printf("glMultiTexCoord1f(target, s)\n");POSTCALL;}
1593 void wrapglMultiTexCoord2f(GLenum target, GLfloat s, GLfloat t) {PRECALL;Con_Printf("glMultiTexCoord2f(target, s, t)\n");POSTCALL;}
1594 void wrapglMultiTexCoord3f(GLenum target, GLfloat s, GLfloat t, GLfloat r) {PRECALL;Con_Printf("glMultiTexCoord3f(target, s, t, r)\n");POSTCALL;}
1595 void wrapglMultiTexCoord4f(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q) {PRECALL;Con_Printf("glMultiTexCoord4f(target, s, t, r, q)\n");POSTCALL;}
1596 void wrapglNormalPointer(GLenum type, GLsizei stride, const GLvoid *ptr) {PRECALL;Con_Printf("glNormalPointer(type, stride, ptr)\n");POSTCALL;}
1597 void wrapglPixelStorei(GLenum pname, GLint param) {PRECALL;glPixelStorei(pname, param);POSTCALL;}
1598 void wrapglPointSize(GLfloat size) {PRECALL;Con_Printf("glPointSize(size)\n");POSTCALL;}
1599 //void wrapglPolygonMode(GLenum face, GLenum mode) {PRECALL;Con_Printf("glPolygonMode(face, mode)\n");POSTCALL;}
1600 void wrapglPolygonOffset(GLfloat factor, GLfloat units) {PRECALL;glPolygonOffset(factor, units);POSTCALL;}
1601 void wrapglPolygonStipple(const GLubyte *mask) {PRECALL;Con_Printf("glPolygonStipple(mask)\n");POSTCALL;}
1602 void wrapglReadBuffer(GLenum mode) {PRECALL;Con_Printf("glReadBuffer(mode)\n");POSTCALL;}
1603 void wrapglReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels) {PRECALL;glReadPixels(x, y, width, height, format, type, pixels);POSTCALL;}
1604 void wrapglRenderbufferStorage(GLenum target, GLenum internalformat, GLsizei width, GLsizei height) {PRECALL;glRenderbufferStorage(target, internalformat, width, height);POSTCALL;}
1605 void wrapglScissor(GLint x, GLint y, GLsizei width, GLsizei height) {PRECALL;glScissor(x, y, width, height);POSTCALL;}
1606 void wrapglShaderSource(GLuint shaderObj, GLsizei count, const GLchar **string, const GLint *length) {PRECALL;glShaderSource(shaderObj, count, string, length);POSTCALL;}
1607 void wrapglStencilFunc(GLenum func, GLint ref, GLuint mask) {PRECALL;glStencilFunc(func, ref, mask);POSTCALL;}
1608 void wrapglStencilFuncSeparate(GLenum func1, GLenum func2, GLint ref, GLuint mask) {PRECALL;Con_Printf("glStencilFuncSeparate(func1, func2, ref, mask)\n");POSTCALL;}
1609 void wrapglStencilMask(GLuint mask) {PRECALL;glStencilMask(mask);POSTCALL;}
1610 void wrapglStencilOp(GLenum fail, GLenum zfail, GLenum zpass) {PRECALL;glStencilOp(fail, zfail, zpass);POSTCALL;}
1611 void wrapglStencilOpSeparate(GLenum e1, GLenum e2, GLenum e3, GLenum e4) {PRECALL;Con_Printf("glStencilOpSeparate(e1, e2, e3, e4)\n");POSTCALL;}
1612 void wrapglTexCoord1f(GLfloat s) {PRECALL;Con_Printf("glTexCoord1f(s)\n");POSTCALL;}
1613 void wrapglTexCoord2f(GLfloat s, GLfloat t) {PRECALL;Con_Printf("glTexCoord2f(s, t)\n");POSTCALL;}
1614 void wrapglTexCoord3f(GLfloat s, GLfloat t, GLfloat r) {PRECALL;Con_Printf("glTexCoord3f(s, t, r)\n");POSTCALL;}
1615 void wrapglTexCoord4f(GLfloat s, GLfloat t, GLfloat r, GLfloat q) {PRECALL;Con_Printf("glTexCoord4f(s, t, r, q)\n");POSTCALL;}
1616 void wrapglTexCoordPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr) {PRECALL;Con_Printf("glTexCoordPointer(size, type, stride, ptr)\n");POSTCALL;}
1617 void wrapglTexEnvf(GLenum target, GLenum pname, GLfloat param) {PRECALL;Con_Printf("glTexEnvf(target, pname, param)\n");POSTCALL;}
1618 void wrapglTexEnvfv(GLenum target, GLenum pname, const GLfloat *params) {PRECALL;Con_Printf("glTexEnvfv(target, pname, params)\n");POSTCALL;}
1619 void wrapglTexEnvi(GLenum target, GLenum pname, GLint param) {PRECALL;Con_Printf("glTexEnvi(target, pname, param)\n");POSTCALL;}
1620 void wrapglTexImage2D(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels) {PRECALL;glTexImage2D(target, level, internalFormat, width, height, border, format, type, pixels);POSTCALL;}
1621 void wrapglTexImage3D(GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels) {PRECALL;Con_Printf("glTexImage3D(target, level, internalformat, width, height, depth, border, format, type, pixels)\n");POSTCALL;}
1622 void wrapglTexParameterf(GLenum target, GLenum pname, GLfloat param) {PRECALL;glTexParameterf(target, pname, param);POSTCALL;}
1623 void wrapglTexParameterfv(GLenum target, GLenum pname, GLfloat *params) {PRECALL;glTexParameterfv(target, pname, params);POSTCALL;}
1624 void wrapglTexParameteri(GLenum target, GLenum pname, GLint param) {PRECALL;glTexParameteri(target, pname, param);POSTCALL;}
1625 void wrapglTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels) {PRECALL;glTexSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels);POSTCALL;}
1626 void wrapglTexSubImage3D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels) {PRECALL;Con_Printf("glTexSubImage3D(target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels)\n");POSTCALL;}
1627 void wrapglUniform1f(GLint location, GLfloat v0) {PRECALL;glUniform1f(location, v0);POSTCALL;}
1628 void wrapglUniform1fv(GLint location, GLsizei count, const GLfloat *value) {PRECALL;glUniform1fv(location, count, value);POSTCALL;}
1629 void wrapglUniform1i(GLint location, GLint v0) {PRECALL;glUniform1i(location, v0);POSTCALL;}
1630 void wrapglUniform1iv(GLint location, GLsizei count, const GLint *value) {PRECALL;glUniform1iv(location, count, value);POSTCALL;}
1631 void wrapglUniform2f(GLint location, GLfloat v0, GLfloat v1) {PRECALL;glUniform2f(location, v0, v1);POSTCALL;}
1632 void wrapglUniform2fv(GLint location, GLsizei count, const GLfloat *value) {PRECALL;glUniform2fv(location, count, value);POSTCALL;}
1633 void wrapglUniform2i(GLint location, GLint v0, GLint v1) {PRECALL;glUniform2i(location, v0, v1);POSTCALL;}
1634 void wrapglUniform2iv(GLint location, GLsizei count, const GLint *value) {PRECALL;glUniform2iv(location, count, value);POSTCALL;}
1635 void wrapglUniform3f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2) {PRECALL;glUniform3f(location, v0, v1, v2);POSTCALL;}
1636 void wrapglUniform3fv(GLint location, GLsizei count, const GLfloat *value) {PRECALL;glUniform3fv(location, count, value);POSTCALL;}
1637 void wrapglUniform3i(GLint location, GLint v0, GLint v1, GLint v2) {PRECALL;glUniform3i(location, v0, v1, v2);POSTCALL;}
1638 void wrapglUniform3iv(GLint location, GLsizei count, const GLint *value) {PRECALL;glUniform3iv(location, count, value);POSTCALL;}
1639 void wrapglUniform4f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) {PRECALL;glUniform4f(location, v0, v1, v2, v3);POSTCALL;}
1640 void wrapglUniform4fv(GLint location, GLsizei count, const GLfloat *value) {PRECALL;glUniform4fv(location, count, value);POSTCALL;}
1641 void wrapglUniform4i(GLint location, GLint v0, GLint v1, GLint v2, GLint v3) {PRECALL;glUniform4i(location, v0, v1, v2, v3);POSTCALL;}
1642 void wrapglUniform4iv(GLint location, GLsizei count, const GLint *value) {PRECALL;glUniform4iv(location, count, value);POSTCALL;}
1643 void wrapglUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) {PRECALL;glUniformMatrix2fv(location, count, transpose, value);POSTCALL;}
1644 void wrapglUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) {PRECALL;glUniformMatrix3fv(location, count, transpose, value);POSTCALL;}
1645 void wrapglUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) {PRECALL;glUniformMatrix4fv(location, count, transpose, value);POSTCALL;}
1646 void wrapglUseProgram(GLuint programObj) {PRECALL;glUseProgram(programObj);POSTCALL;}
1647 void wrapglValidateProgram(GLuint programObj) {PRECALL;glValidateProgram(programObj);POSTCALL;}
1648 void wrapglVertex2f(GLfloat x, GLfloat y) {PRECALL;Con_Printf("glVertex2f(x, y)\n");POSTCALL;}
1649 void wrapglVertex3f(GLfloat x, GLfloat y, GLfloat z) {PRECALL;Con_Printf("glVertex3f(x, y, z)\n");POSTCALL;}
1650 void wrapglVertex4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w) {PRECALL;Con_Printf("glVertex4f(x, y, z, w)\n");POSTCALL;}
1651 void wrapglVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer) {PRECALL;glVertexAttribPointer(index, size, type, normalized, stride, pointer);POSTCALL;}
1652 void wrapglVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr) {PRECALL;Con_Printf("glVertexPointer(size, type, stride, ptr)\n");POSTCALL;}
1653 void wrapglViewport(GLint x, GLint y, GLsizei width, GLsizei height) {PRECALL;glViewport(x, y, width, height);POSTCALL;}
1654 void wrapglVertexAttrib1f(GLuint index, GLfloat v0) {PRECALL;glVertexAttrib1f(index, v0);POSTCALL;}
1655 //void wrapglVertexAttrib1s(GLuint index, GLshort v0) {PRECALL;glVertexAttrib1s(index, v0);POSTCALL;}
1656 //void wrapglVertexAttrib1d(GLuint index, GLdouble v0) {PRECALL;glVertexAttrib1d(index, v0);POSTCALL;}
1657 void wrapglVertexAttrib2f(GLuint index, GLfloat v0, GLfloat v1) {PRECALL;glVertexAttrib2f(index, v0, v1);POSTCALL;}
1658 //void wrapglVertexAttrib2s(GLuint index, GLshort v0, GLshort v1) {PRECALL;glVertexAttrib2s(index, v0, v1);POSTCALL;}
1659 //void wrapglVertexAttrib2d(GLuint index, GLdouble v0, GLdouble v1) {PRECALL;glVertexAttrib2d(index, v0, v1);POSTCALL;}
1660 void wrapglVertexAttrib3f(GLuint index, GLfloat v0, GLfloat v1, GLfloat v2) {PRECALL;glVertexAttrib3f(index, v0, v1, v2);POSTCALL;}
1661 //void wrapglVertexAttrib3s(GLuint index, GLshort v0, GLshort v1, GLshort v2) {PRECALL;glVertexAttrib3s(index, v0, v1, v2);POSTCALL;}
1662 //void wrapglVertexAttrib3d(GLuint index, GLdouble v0, GLdouble v1, GLdouble v2) {PRECALL;glVertexAttrib3d(index, v0, v1, v2);POSTCALL;}
1663 void wrapglVertexAttrib4f(GLuint index, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) {PRECALL;glVertexAttrib4f(index, v0, v1, v2, v3);POSTCALL;}
1664 //void wrapglVertexAttrib4s(GLuint index, GLshort v0, GLshort v1, GLshort v2, GLshort v3) {PRECALL;glVertexAttrib4s(index, v0, v1, v2, v3);POSTCALL;}
1665 //void wrapglVertexAttrib4d(GLuint index, GLdouble v0, GLdouble v1, GLdouble v2, GLdouble v3) {PRECALL;glVertexAttrib4d(index, v0, v1, v2, v3);POSTCALL;}
1666 //void wrapglVertexAttrib4Nub(GLuint index, GLubyte x, GLubyte y, GLubyte z, GLubyte w) {PRECALL;glVertexAttrib4Nub(index, x, y, z, w);POSTCALL;}
1667 void wrapglVertexAttrib1fv(GLuint index, const GLfloat *v) {PRECALL;glVertexAttrib1fv(index, v);POSTCALL;}
1668 //void wrapglVertexAttrib1sv(GLuint index, const GLshort *v) {PRECALL;glVertexAttrib1sv(index, v);POSTCALL;}
1669 //void wrapglVertexAttrib1dv(GLuint index, const GLdouble *v) {PRECALL;glVertexAttrib1dv(index, v);POSTCALL;}
1670 void wrapglVertexAttrib2fv(GLuint index, const GLfloat *v) {PRECALL;glVertexAttrib2fv(index, v);POSTCALL;}
1671 //void wrapglVertexAttrib2sv(GLuint index, const GLshort *v) {PRECALL;glVertexAttrib2sv(index, v);POSTCALL;}
1672 //void wrapglVertexAttrib2dv(GLuint index, const GLdouble *v) {PRECALL;glVertexAttrib2dv(index, v);POSTCALL;}
1673 void wrapglVertexAttrib3fv(GLuint index, const GLfloat *v) {PRECALL;glVertexAttrib3fv(index, v);POSTCALL;}
1674 //void wrapglVertexAttrib3sv(GLuint index, const GLshort *v) {PRECALL;glVertexAttrib3sv(index, v);POSTCALL;}
1675 //void wrapglVertexAttrib3dv(GLuint index, const GLdouble *v) {PRECALL;glVertexAttrib3dv(index, v);POSTCALL;}
1676 void wrapglVertexAttrib4fv(GLuint index, const GLfloat *v) {PRECALL;glVertexAttrib4fv(index, v);POSTCALL;}
1677 //void wrapglVertexAttrib4sv(GLuint index, const GLshort *v) {PRECALL;glVertexAttrib4sv(index, v);POSTCALL;}
1678 //void wrapglVertexAttrib4dv(GLuint index, const GLdouble *v) {PRECALL;glVertexAttrib4dv(index, v);POSTCALL;}
1679 //void wrapglVertexAttrib4iv(GLuint index, const GLint *v) {PRECALL;glVertexAttrib4iv(index, v);POSTCALL;}
1680 //void wrapglVertexAttrib4bv(GLuint index, const GLbyte *v) {PRECALL;glVertexAttrib4bv(index, v);POSTCALL;}
1681 //void wrapglVertexAttrib4ubv(GLuint index, const GLubyte *v) {PRECALL;glVertexAttrib4ubv(index, v);POSTCALL;}
1682 //void wrapglVertexAttrib4usv(GLuint index, const GLushort *v) {PRECALL;glVertexAttrib4usv(index, GLushort v);POSTCALL;}
1683 //void wrapglVertexAttrib4uiv(GLuint index, const GLuint *v) {PRECALL;glVertexAttrib4uiv(index, v);POSTCALL;}
1684 //void wrapglVertexAttrib4Nbv(GLuint index, const GLbyte *v) {PRECALL;glVertexAttrib4Nbv(index, v);POSTCALL;}
1685 //void wrapglVertexAttrib4Nsv(GLuint index, const GLshort *v) {PRECALL;glVertexAttrib4Nsv(index, v);POSTCALL;}
1686 //void wrapglVertexAttrib4Niv(GLuint index, const GLint *v) {PRECALL;glVertexAttrib4Niv(index, v);POSTCALL;}
1687 //void wrapglVertexAttrib4Nubv(GLuint index, const GLubyte *v) {PRECALL;glVertexAttrib4Nubv(index, v);POSTCALL;}
1688 //void wrapglVertexAttrib4Nusv(GLuint index, const GLushort *v) {PRECALL;glVertexAttrib4Nusv(index, GLushort v);POSTCALL;}
1689 //void wrapglVertexAttrib4Nuiv(GLuint index, const GLuint *v) {PRECALL;glVertexAttrib4Nuiv(index, v);POSTCALL;}
1690 //void wrapglGetVertexAttribdv(GLuint index, GLenum pname, GLdouble *params) {PRECALL;glGetVertexAttribdv(index, pname, params);POSTCALL;}
1691 void wrapglGetVertexAttribfv(GLuint index, GLenum pname, GLfloat *params) {PRECALL;glGetVertexAttribfv(index, pname, params);POSTCALL;}
1692 void wrapglGetVertexAttribiv(GLuint index, GLenum pname, GLint *params) {PRECALL;glGetVertexAttribiv(index, pname, params);POSTCALL;}
1693 void wrapglGetVertexAttribPointerv(GLuint index, GLenum pname, GLvoid **pointer) {PRECALL;glGetVertexAttribPointerv(index, pname, pointer);POSTCALL;}
1694 #endif
1695
1696 #if SDL_MAJOR_VERSION == 1
1697 #define SDL_GL_ExtensionSupported(x) (strstr(gl_extensions, x) || strstr(gl_platformextensions, x))
1698 #endif
1699
1700 void GLES_Init(void)
1701 {
1702 #ifndef qglClear
1703         qglIsBufferARB = wrapglIsBuffer;
1704         qglIsEnabled = wrapglIsEnabled;
1705         qglIsFramebufferEXT = wrapglIsFramebuffer;
1706 //      qglIsQueryARB = wrapglIsQuery;
1707         qglIsRenderbufferEXT = wrapglIsRenderbuffer;
1708 //      qglUnmapBufferARB = wrapglUnmapBuffer;
1709         qglCheckFramebufferStatus = wrapglCheckFramebufferStatus;
1710         qglGetError = wrapglGetError;
1711         qglCreateProgram = wrapglCreateProgram;
1712         qglCreateShader = wrapglCreateShader;
1713 //      qglGetHandleARB = wrapglGetHandle;
1714         qglGetAttribLocation = wrapglGetAttribLocation;
1715         qglGetUniformLocation = wrapglGetUniformLocation;
1716 //      qglMapBufferARB = wrapglMapBuffer;
1717         qglGetString = wrapglGetString;
1718 //      qglActiveStencilFaceEXT = wrapglActiveStencilFace;
1719         qglActiveTexture = wrapglActiveTexture;
1720         qglAlphaFunc = wrapglAlphaFunc;
1721         qglArrayElement = wrapglArrayElement;
1722         qglAttachShader = wrapglAttachShader;
1723 //      qglBegin = wrapglBegin;
1724 //      qglBeginQueryARB = wrapglBeginQuery;
1725         qglBindAttribLocation = wrapglBindAttribLocation;
1726 //      qglBindFragDataLocation = wrapglBindFragDataLocation;
1727         qglBindBufferARB = wrapglBindBuffer;
1728         qglBindFramebuffer = wrapglBindFramebuffer;
1729         qglBindRenderbuffer = wrapglBindRenderbuffer;
1730         qglBindTexture = wrapglBindTexture;
1731         qglBlendEquationEXT = wrapglBlendEquation;
1732         qglBlendFunc = wrapglBlendFunc;
1733         qglBufferDataARB = wrapglBufferData;
1734         qglBufferSubDataARB = wrapglBufferSubData;
1735         qglClear = wrapglClear;
1736         qglClearColor = wrapglClearColor;
1737         qglClearDepth = wrapglClearDepth;
1738         qglClearStencil = wrapglClearStencil;
1739         qglClientActiveTexture = wrapglClientActiveTexture;
1740         qglColor4f = wrapglColor4f;
1741         qglColor4ub = wrapglColor4ub;
1742         qglColorMask = wrapglColorMask;
1743         qglColorPointer = wrapglColorPointer;
1744         qglCompileShader = wrapglCompileShader;
1745         qglCompressedTexImage2DARB = wrapglCompressedTexImage2D;
1746         qglCompressedTexImage3DARB = wrapglCompressedTexImage3D;
1747         qglCompressedTexSubImage2DARB = wrapglCompressedTexSubImage2D;
1748         qglCompressedTexSubImage3DARB = wrapglCompressedTexSubImage3D;
1749         qglCopyTexImage2D = wrapglCopyTexImage2D;
1750         qglCopyTexSubImage2D = wrapglCopyTexSubImage2D;
1751         qglCopyTexSubImage3D = wrapglCopyTexSubImage3D;
1752         qglCullFace = wrapglCullFace;
1753         qglDeleteBuffersARB = wrapglDeleteBuffers;
1754         qglDeleteFramebuffers = wrapglDeleteFramebuffers;
1755         qglDeleteProgram = wrapglDeleteProgram;
1756         qglDeleteShader = wrapglDeleteShader;
1757 //      qglDeleteQueriesARB = wrapglDeleteQueries;
1758         qglDeleteRenderbuffers = wrapglDeleteRenderbuffers;
1759         qglDeleteTextures = wrapglDeleteTextures;
1760         qglDepthFunc = wrapglDepthFunc;
1761         qglDepthMask = wrapglDepthMask;
1762         qglDepthRangef = wrapglDepthRangef;
1763         qglDetachShader = wrapglDetachShader;
1764         qglDisable = wrapglDisable;
1765         qglDisableClientState = wrapglDisableClientState;
1766         qglDisableVertexAttribArray = wrapglDisableVertexAttribArray;
1767         qglDrawArrays = wrapglDrawArrays;
1768 //      qglDrawBuffer = wrapglDrawBuffer;
1769 //      qglDrawBuffersARB = wrapglDrawBuffers;
1770         qglDrawElements = wrapglDrawElements;
1771 //      qglDrawRangeElements = wrapglDrawRangeElements;
1772         qglEnable = wrapglEnable;
1773         qglEnableClientState = wrapglEnableClientState;
1774         qglEnableVertexAttribArray = wrapglEnableVertexAttribArray;
1775 //      qglEnd = wrapglEnd;
1776 //      qglEndQueryARB = wrapglEndQuery;
1777         qglFinish = wrapglFinish;
1778         qglFlush = wrapglFlush;
1779         qglFramebufferRenderbufferEXT = wrapglFramebufferRenderbuffer;
1780         qglFramebufferTexture2DEXT = wrapglFramebufferTexture2D;
1781         qglFramebufferTexture3DEXT = wrapglFramebufferTexture3D;
1782         qglGenBuffersARB = wrapglGenBuffers;
1783         qglGenFramebuffers = wrapglGenFramebuffers;
1784 //      qglGenQueriesARB = wrapglGenQueries;
1785         qglGenRenderbuffers = wrapglGenRenderbuffers;
1786         qglGenTextures = wrapglGenTextures;
1787         qglGenerateMipmapEXT = wrapglGenerateMipmap;
1788         qglGetActiveAttrib = wrapglGetActiveAttrib;
1789         qglGetActiveUniform = wrapglGetActiveUniform;
1790         qglGetAttachedShaders = wrapglGetAttachedShaders;
1791         qglGetBooleanv = wrapglGetBooleanv;
1792 //      qglGetCompressedTexImageARB = wrapglGetCompressedTexImage;
1793         qglGetDoublev = wrapglGetDoublev;
1794         qglGetFloatv = wrapglGetFloatv;
1795         qglGetFramebufferAttachmentParameterivEXT = wrapglGetFramebufferAttachmentParameteriv;
1796         qglGetProgramInfoLog = wrapglGetProgramInfoLog;
1797         qglGetShaderInfoLog = wrapglGetShaderInfoLog;
1798         qglGetIntegerv = wrapglGetIntegerv;
1799         qglGetShaderiv = wrapglGetShaderiv;
1800         qglGetProgramiv = wrapglGetProgramiv;
1801 //      qglGetQueryObjectivARB = wrapglGetQueryObjectiv;
1802 //      qglGetQueryObjectuivARB = wrapglGetQueryObjectuiv;
1803 //      qglGetQueryivARB = wrapglGetQueryiv;
1804         qglGetRenderbufferParameterivEXT = wrapglGetRenderbufferParameteriv;
1805         qglGetShaderSource = wrapglGetShaderSource;
1806         qglGetTexImage = wrapglGetTexImage;
1807         qglGetTexLevelParameterfv = wrapglGetTexLevelParameterfv;
1808         qglGetTexLevelParameteriv = wrapglGetTexLevelParameteriv;
1809         qglGetTexParameterfv = wrapglGetTexParameterfv;
1810         qglGetTexParameteriv = wrapglGetTexParameteriv;
1811         qglGetUniformfv = wrapglGetUniformfv;
1812         qglGetUniformiv = wrapglGetUniformiv;
1813         qglHint = wrapglHint;
1814         qglLineWidth = wrapglLineWidth;
1815         qglLinkProgram = wrapglLinkProgram;
1816         qglLoadIdentity = wrapglLoadIdentity;
1817         qglLoadMatrixf = wrapglLoadMatrixf;
1818         qglMatrixMode = wrapglMatrixMode;
1819         qglMultiTexCoord1f = wrapglMultiTexCoord1f;
1820         qglMultiTexCoord2f = wrapglMultiTexCoord2f;
1821         qglMultiTexCoord3f = wrapglMultiTexCoord3f;
1822         qglMultiTexCoord4f = wrapglMultiTexCoord4f;
1823         qglNormalPointer = wrapglNormalPointer;
1824         qglPixelStorei = wrapglPixelStorei;
1825         qglPointSize = wrapglPointSize;
1826 //      qglPolygonMode = wrapglPolygonMode;
1827         qglPolygonOffset = wrapglPolygonOffset;
1828 //      qglPolygonStipple = wrapglPolygonStipple;
1829         qglReadBuffer = wrapglReadBuffer;
1830         qglReadPixels = wrapglReadPixels;
1831         qglRenderbufferStorage = wrapglRenderbufferStorage;
1832         qglScissor = wrapglScissor;
1833         qglShaderSource = wrapglShaderSource;
1834         qglStencilFunc = wrapglStencilFunc;
1835         qglStencilFuncSeparate = wrapglStencilFuncSeparate;
1836         qglStencilMask = wrapglStencilMask;
1837         qglStencilOp = wrapglStencilOp;
1838         qglStencilOpSeparate = wrapglStencilOpSeparate;
1839         qglTexCoord1f = wrapglTexCoord1f;
1840         qglTexCoord2f = wrapglTexCoord2f;
1841         qglTexCoord3f = wrapglTexCoord3f;
1842         qglTexCoord4f = wrapglTexCoord4f;
1843         qglTexCoordPointer = wrapglTexCoordPointer;
1844         qglTexEnvf = wrapglTexEnvf;
1845         qglTexEnvfv = wrapglTexEnvfv;
1846         qglTexEnvi = wrapglTexEnvi;
1847         qglTexImage2D = wrapglTexImage2D;
1848         qglTexImage3D = wrapglTexImage3D;
1849         qglTexParameterf = wrapglTexParameterf;
1850         qglTexParameterfv = wrapglTexParameterfv;
1851         qglTexParameteri = wrapglTexParameteri;
1852         qglTexSubImage2D = wrapglTexSubImage2D;
1853         qglTexSubImage3D = wrapglTexSubImage3D;
1854         qglUniform1f = wrapglUniform1f;
1855         qglUniform1fv = wrapglUniform1fv;
1856         qglUniform1i = wrapglUniform1i;
1857         qglUniform1iv = wrapglUniform1iv;
1858         qglUniform2f = wrapglUniform2f;
1859         qglUniform2fv = wrapglUniform2fv;
1860         qglUniform2i = wrapglUniform2i;
1861         qglUniform2iv = wrapglUniform2iv;
1862         qglUniform3f = wrapglUniform3f;
1863         qglUniform3fv = wrapglUniform3fv;
1864         qglUniform3i = wrapglUniform3i;
1865         qglUniform3iv = wrapglUniform3iv;
1866         qglUniform4f = wrapglUniform4f;
1867         qglUniform4fv = wrapglUniform4fv;
1868         qglUniform4i = wrapglUniform4i;
1869         qglUniform4iv = wrapglUniform4iv;
1870         qglUniformMatrix2fv = wrapglUniformMatrix2fv;
1871         qglUniformMatrix3fv = wrapglUniformMatrix3fv;
1872         qglUniformMatrix4fv = wrapglUniformMatrix4fv;
1873         qglUseProgram = wrapglUseProgram;
1874         qglValidateProgram = wrapglValidateProgram;
1875         qglVertex2f = wrapglVertex2f;
1876         qglVertex3f = wrapglVertex3f;
1877         qglVertex4f = wrapglVertex4f;
1878         qglVertexAttribPointer = wrapglVertexAttribPointer;
1879         qglVertexPointer = wrapglVertexPointer;
1880         qglViewport = wrapglViewport;
1881         qglVertexAttrib1f = wrapglVertexAttrib1f;
1882 //      qglVertexAttrib1s = wrapglVertexAttrib1s;
1883 //      qglVertexAttrib1d = wrapglVertexAttrib1d;
1884         qglVertexAttrib2f = wrapglVertexAttrib2f;
1885 //      qglVertexAttrib2s = wrapglVertexAttrib2s;
1886 //      qglVertexAttrib2d = wrapglVertexAttrib2d;
1887         qglVertexAttrib3f = wrapglVertexAttrib3f;
1888 //      qglVertexAttrib3s = wrapglVertexAttrib3s;
1889 //      qglVertexAttrib3d = wrapglVertexAttrib3d;
1890         qglVertexAttrib4f = wrapglVertexAttrib4f;
1891 //      qglVertexAttrib4s = wrapglVertexAttrib4s;
1892 //      qglVertexAttrib4d = wrapglVertexAttrib4d;
1893 //      qglVertexAttrib4Nub = wrapglVertexAttrib4Nub;
1894         qglVertexAttrib1fv = wrapglVertexAttrib1fv;
1895 //      qglVertexAttrib1sv = wrapglVertexAttrib1sv;
1896 //      qglVertexAttrib1dv = wrapglVertexAttrib1dv;
1897         qglVertexAttrib2fv = wrapglVertexAttrib2fv;
1898 //      qglVertexAttrib2sv = wrapglVertexAttrib2sv;
1899 //      qglVertexAttrib2dv = wrapglVertexAttrib2dv;
1900         qglVertexAttrib3fv = wrapglVertexAttrib3fv;
1901 //      qglVertexAttrib3sv = wrapglVertexAttrib3sv;
1902 //      qglVertexAttrib3dv = wrapglVertexAttrib3dv;
1903         qglVertexAttrib4fv = wrapglVertexAttrib4fv;
1904 //      qglVertexAttrib4sv = wrapglVertexAttrib4sv;
1905 //      qglVertexAttrib4dv = wrapglVertexAttrib4dv;
1906 //      qglVertexAttrib4iv = wrapglVertexAttrib4iv;
1907 //      qglVertexAttrib4bv = wrapglVertexAttrib4bv;
1908 //      qglVertexAttrib4ubv = wrapglVertexAttrib4ubv;
1909 //      qglVertexAttrib4usv = wrapglVertexAttrib4usv;
1910 //      qglVertexAttrib4uiv = wrapglVertexAttrib4uiv;
1911 //      qglVertexAttrib4Nbv = wrapglVertexAttrib4Nbv;
1912 //      qglVertexAttrib4Nsv = wrapglVertexAttrib4Nsv;
1913 //      qglVertexAttrib4Niv = wrapglVertexAttrib4Niv;
1914 //      qglVertexAttrib4Nubv = wrapglVertexAttrib4Nubv;
1915 //      qglVertexAttrib4Nusv = wrapglVertexAttrib4Nusv;
1916 //      qglVertexAttrib4Nuiv = wrapglVertexAttrib4Nuiv;
1917 //      qglGetVertexAttribdv = wrapglGetVertexAttribdv;
1918         qglGetVertexAttribfv = wrapglGetVertexAttribfv;
1919         qglGetVertexAttribiv = wrapglGetVertexAttribiv;
1920         qglGetVertexAttribPointerv = wrapglGetVertexAttribPointerv;
1921 #endif
1922
1923         gl_renderer = (const char *)qglGetString(GL_RENDERER);
1924         gl_vendor = (const char *)qglGetString(GL_VENDOR);
1925         gl_version = (const char *)qglGetString(GL_VERSION);
1926         gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
1927         
1928         if (!gl_extensions)
1929                 gl_extensions = "";
1930         if (!gl_platformextensions)
1931                 gl_platformextensions = "";
1932         
1933         Con_Printf("GL_VENDOR: %s\n", gl_vendor);
1934         Con_Printf("GL_RENDERER: %s\n", gl_renderer);
1935         Con_Printf("GL_VERSION: %s\n", gl_version);
1936         Con_DPrintf("GL_EXTENSIONS: %s\n", gl_extensions);
1937         Con_DPrintf("%s_EXTENSIONS: %s\n", gl_platform, gl_platformextensions);
1938         
1939         // LordHavoc: report supported extensions
1940         Con_DPrintf("\nQuakeC extensions for server and client: %s\nQuakeC extensions for menu: %s\n", vm_sv_extensions, vm_m_extensions );
1941
1942         // GLES devices in general do not like GL_BGRA, so use GL_RGBA
1943         vid.forcetextype = TEXTYPE_RGBA;
1944         
1945         vid.support.gl20shaders = true;
1946         vid.support.amd_texture_texture4 = false;
1947         vid.support.arb_depth_texture = SDL_GL_ExtensionSupported("GL_OES_depth_texture") != 0; // renderbuffer used anyway on gles2?
1948         vid.support.arb_draw_buffers = false;
1949         vid.support.arb_multitexture = false;
1950         vid.support.arb_occlusion_query = false;
1951         vid.support.arb_query_buffer_object = false;
1952         vid.support.arb_shadow = false;
1953         vid.support.arb_texture_compression = false; // different (vendor-specific) formats than on desktop OpenGL...
1954         vid.support.arb_texture_cube_map = SDL_GL_ExtensionSupported("GL_OES_texture_cube_map") != 0;
1955         vid.support.arb_texture_env_combine = false;
1956         vid.support.arb_texture_gather = false;
1957         vid.support.arb_texture_non_power_of_two = strstr(gl_extensions, "GL_OES_texture_npot") != NULL;
1958         vid.support.arb_vertex_buffer_object = true; // GLES2 core
1959         vid.support.ati_separate_stencil = false;
1960         vid.support.ext_blend_minmax = false;
1961         vid.support.ext_blend_subtract = true; // GLES2 core
1962         vid.support.ext_blend_func_separate = true; // GLES2 core
1963         vid.support.ext_draw_range_elements = false;
1964
1965         /*      ELUAN:
1966                 Note: "In OS 2.1, the functions in GL_OES_framebuffer_object were not usable from the Java API.
1967                 Calling them just threw an exception. Android developer relations confirmed that they forgot to implement these. (yeah...)
1968                 It's apparently been fixed in 2.2, though I haven't tested."
1969         */
1970         vid.support.ext_framebuffer_object = false;//true;
1971
1972         vid.support.ext_packed_depth_stencil = false;
1973         vid.support.ext_stencil_two_side = false;
1974         vid.support.ext_texture_3d = SDL_GL_ExtensionSupported("GL_OES_texture_3D") != 0;
1975         vid.support.ext_texture_compression_s3tc = SDL_GL_ExtensionSupported("GL_EXT_texture_compression_s3tc") != 0;
1976         vid.support.ext_texture_edge_clamp = true; // GLES2 core
1977         vid.support.ext_texture_filter_anisotropic = false; // probably don't want to use it...
1978         vid.support.ext_texture_srgb = false;
1979
1980         // NOTE: On some devices, a value of 512 gives better FPS than the maximum.
1981         qglGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint*)&vid.maxtexturesize_2d);
1982
1983 #ifdef GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT
1984         if (vid.support.ext_texture_filter_anisotropic)
1985                 qglGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, (GLint*)&vid.max_anisotropy);
1986 #endif
1987         if (vid.support.arb_texture_cube_map)
1988                 qglGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, (GLint*)&vid.maxtexturesize_cubemap);
1989 #ifdef GL_MAX_3D_TEXTURE_SIZE
1990         if (vid.support.ext_texture_3d)
1991                 qglGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, (GLint*)&vid.maxtexturesize_3d);
1992 #endif
1993         Con_Printf("GL_MAX_CUBE_MAP_TEXTURE_SIZE = %i\n", vid.maxtexturesize_cubemap);
1994         Con_Printf("GL_MAX_3D_TEXTURE_SIZE = %i\n", vid.maxtexturesize_3d);
1995         {
1996 #define GL_ALPHA_BITS                           0x0D55
1997 #define GL_RED_BITS                             0x0D52
1998 #define GL_GREEN_BITS                           0x0D53
1999 #define GL_BLUE_BITS                            0x0D54
2000 #define GL_DEPTH_BITS                           0x0D56
2001 #define GL_STENCIL_BITS                         0x0D57
2002                 int fb_r = -1, fb_g = -1, fb_b = -1, fb_a = -1, fb_d = -1, fb_s = -1;
2003                 qglGetIntegerv(GL_RED_BITS    , &fb_r);
2004                 qglGetIntegerv(GL_GREEN_BITS  , &fb_g);
2005                 qglGetIntegerv(GL_BLUE_BITS   , &fb_b);
2006                 qglGetIntegerv(GL_ALPHA_BITS  , &fb_a);
2007                 qglGetIntegerv(GL_DEPTH_BITS  , &fb_d);
2008                 qglGetIntegerv(GL_STENCIL_BITS, &fb_s);
2009                 Con_Printf("Framebuffer depth is R%iG%iB%iA%iD%iS%i\n", fb_r, fb_g, fb_b, fb_a, fb_d, fb_s);
2010         }
2011
2012         // verify that cubemap textures are really supported
2013         if (vid.support.arb_texture_cube_map && vid.maxtexturesize_cubemap < 256)
2014                 vid.support.arb_texture_cube_map = false;
2015         
2016         // verify that 3d textures are really supported
2017         if (vid.support.ext_texture_3d && vid.maxtexturesize_3d < 32)
2018         {
2019                 vid.support.ext_texture_3d = false;
2020                 Con_Printf("GL_OES_texture_3d reported bogus GL_MAX_3D_TEXTURE_SIZE, disabled\n");
2021         }
2022
2023         vid.texunits = 4;
2024         vid.teximageunits = 8;
2025         vid.texarrayunits = 5;
2026         //qglGetIntegerv(GL_MAX_TEXTURE_UNITS, (GLint*)&vid.texunits);
2027         qglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, (GLint*)&vid.teximageunits);CHECKGLERROR
2028         //qglGetIntegerv(GL_MAX_TEXTURE_COORDS, (GLint*)&vid.texarrayunits);CHECKGLERROR
2029         vid.texunits = bound(1, vid.texunits, MAX_TEXTUREUNITS);
2030         vid.teximageunits = bound(1, vid.teximageunits, MAX_TEXTUREUNITS);
2031         vid.texarrayunits = bound(1, vid.texarrayunits, MAX_TEXTUREUNITS);
2032         Con_DPrintf("Using GLES2.0 rendering path - %i texture matrix, %i texture images, %i texcoords%s\n", vid.texunits, vid.teximageunits, vid.texarrayunits, vid.support.ext_framebuffer_object ? ", shadowmapping supported" : "");
2033         vid.renderpath = RENDERPATH_GLES2;
2034         vid.useinterleavedarrays = false;
2035         vid.sRGBcapable2D = false;
2036         vid.sRGBcapable3D = false;
2037
2038         // VorteX: set other info (maybe place them in VID_InitMode?)
2039         extern cvar_t gl_info_vendor;
2040         extern cvar_t gl_info_renderer;
2041         extern cvar_t gl_info_version;
2042         extern cvar_t gl_info_platform;
2043         extern cvar_t gl_info_driver;
2044         Cvar_SetQuick(&gl_info_vendor, gl_vendor);
2045         Cvar_SetQuick(&gl_info_renderer, gl_renderer);
2046         Cvar_SetQuick(&gl_info_version, gl_version);
2047         Cvar_SetQuick(&gl_info_platform, gl_platform ? gl_platform : "");
2048         Cvar_SetQuick(&gl_info_driver, gl_driver);
2049 }
2050 #endif
2051
2052 void *GL_GetProcAddress(const char *name)
2053 {
2054         void *p = NULL;
2055         p = SDL_GL_GetProcAddress(name);
2056         return p;
2057 }
2058
2059 static qboolean vid_sdl_initjoysticksystem = false;
2060
2061 void VID_Init (void)
2062 {
2063 #ifndef __IPHONEOS__
2064 #ifdef MACOSX
2065         Cvar_RegisterVariable(&apple_mouse_noaccel);
2066 #endif
2067 #endif
2068 #ifdef DP_MOBILETOUCH
2069         Cvar_SetValueQuick(&vid_touchscreen, 1);
2070 #endif
2071
2072 #ifdef SDL_R_RESTART
2073         R_RegisterModule("SDL", sdl_start, sdl_shutdown, sdl_newmap, NULL, NULL);
2074 #endif
2075
2076         if (SDL_Init(SDL_INIT_VIDEO) < 0)
2077                 Sys_Error ("Failed to init SDL video subsystem: %s", SDL_GetError());
2078         vid_sdl_initjoysticksystem = SDL_InitSubSystem(SDL_INIT_JOYSTICK) >= 0;
2079         if (vid_sdl_initjoysticksystem)
2080                 Con_Printf("Failed to init SDL joystick subsystem: %s\n", SDL_GetError());
2081         vid_isfullscreen = false;
2082 }
2083
2084 static int vid_sdljoystickindex = -1;
2085 void VID_EnableJoystick(qboolean enable)
2086 {
2087         int index = joy_enable.integer > 0 ? joy_index.integer : -1;
2088         int numsdljoysticks;
2089         qboolean success = false;
2090         int sharedcount = 0;
2091         int sdlindex = -1;
2092         sharedcount = VID_Shared_SetJoystick(index);
2093         if (index >= 0 && index < sharedcount)
2094                 success = true;
2095         sdlindex = index - sharedcount;
2096
2097         numsdljoysticks = SDL_NumJoysticks();
2098         if (sdlindex < 0 || sdlindex >= numsdljoysticks)
2099                 sdlindex = -1;
2100
2101         // update cvar containing count of XInput joysticks + SDL joysticks
2102         if (joy_detected.integer != sharedcount + numsdljoysticks)
2103                 Cvar_SetValueQuick(&joy_detected, sharedcount + numsdljoysticks);
2104
2105         if (vid_sdljoystickindex != sdlindex)
2106         {
2107                 vid_sdljoystickindex = sdlindex;
2108                 // close SDL joystick if active
2109                 if (vid_sdljoystick)
2110                         SDL_JoystickClose(vid_sdljoystick);
2111                 vid_sdljoystick = NULL;
2112                 if (sdlindex >= 0)
2113                 {
2114                         vid_sdljoystick = SDL_JoystickOpen(sdlindex);
2115                         if (vid_sdljoystick)
2116                         {
2117 #if SDL_MAJOR_VERSION == 1
2118                                 const char *joystickname = SDL_JoystickName(sdlindex);
2119 #else
2120                                 const char *joystickname = SDL_JoystickName(vid_sdljoystick);
2121 #endif
2122                                 Con_Printf("Joystick %i opened (SDL_Joystick %i is \"%s\" with %i axes, %i buttons, %i balls)\n", index, sdlindex, joystickname, (int)SDL_JoystickNumAxes(vid_sdljoystick), (int)SDL_JoystickNumButtons(vid_sdljoystick), (int)SDL_JoystickNumBalls(vid_sdljoystick));
2123                         }
2124                         else
2125                         {
2126                                 Con_Printf("Joystick %i failed (SDL_JoystickOpen(%i) returned: %s)\n", index, sdlindex, SDL_GetError());
2127                                 sdlindex = -1;
2128                         }
2129                 }
2130         }
2131
2132         if (sdlindex >= 0)
2133                 success = true;
2134
2135         if (joy_active.integer != (success ? 1 : 0))
2136                 Cvar_SetValueQuick(&joy_active, success ? 1 : 0);
2137 }
2138
2139 #if SDL_MAJOR_VERSION == 1
2140 // set the icon (we dont use SDL here since it would be too much a PITA)
2141 #ifdef WIN32
2142 #include "resource.h"
2143 #include <SDL_syswm.h>
2144 static SDL_Surface *VID_WrapSDL_SetVideoMode(int screenwidth, int screenheight, int screenbpp, int screenflags)
2145 {
2146         SDL_Surface *screen = NULL;
2147         SDL_SysWMinfo info;
2148         HICON icon;
2149         SDL_WM_SetCaption( gamename, NULL );
2150         screen = SDL_SetVideoMode(screenwidth, screenheight, screenbpp, screenflags);
2151         if (screen)
2152         {
2153                 // get the HWND handle
2154                 SDL_VERSION( &info.version );
2155                 if (SDL_GetWMInfo(&info))
2156                 {
2157                         icon = LoadIcon( GetModuleHandle( NULL ), MAKEINTRESOURCE( IDI_ICON1 ) );
2158 #ifndef _W64 //If Windows 64bit data types don't exist
2159 #ifndef SetClassLongPtr
2160 #define SetClassLongPtr SetClassLong
2161 #endif
2162 #ifndef GCLP_HICON
2163 #define GCLP_HICON GCL_HICON
2164 #endif
2165 #ifndef LONG_PTR
2166 #define LONG_PTR LONG
2167 #endif
2168 #endif
2169                         SetClassLongPtr( info.window, GCLP_HICON, (LONG_PTR)icon );
2170                 }
2171         }
2172         return screen;
2173 }
2174 #elif defined(MACOSX)
2175 static SDL_Surface *VID_WrapSDL_SetVideoMode(int screenwidth, int screenheight, int screenbpp, int screenflags)
2176 {
2177         SDL_Surface *screen = NULL;
2178         SDL_WM_SetCaption( gamename, NULL );
2179         screen = SDL_SetVideoMode(screenwidth, screenheight, screenbpp, screenflags);
2180         // we don't use SDL_WM_SetIcon here because the icon in the .app should be used
2181         return screen;
2182 }
2183 #else
2184 // Adding the OS independent XPM version --blub
2185 #include "darkplaces.xpm"
2186 #include "nexuiz.xpm"
2187 #if SDL_MAJOR_VERSION == 1
2188 #if SDL_VIDEO_DRIVER_X11 && !SDL_VIDEO_DRIVER_QUARTZ
2189 #include <SDL_syswm.h>
2190 #endif
2191 #endif
2192 static SDL_Surface *icon = NULL;
2193 static SDL_Surface *VID_WrapSDL_SetVideoMode(int screenwidth, int screenheight, int screenbpp, int screenflags)
2194 {
2195         /*
2196          * Somewhat restricted XPM reader. Only supports XPMs saved by GIMP 2.4 at
2197          * default settings with less than 91 colors and transparency.
2198          */
2199
2200         int width, height, colors, isize, i, j;
2201         int thenone = -1;
2202         static SDL_Color palette[256];
2203         unsigned short palenc[256]; // store color id by char
2204         char *xpm;
2205         char **idata, *data;
2206         const SDL_version *version;
2207         SDL_Surface *screen = NULL;
2208
2209         if (icon)
2210                 SDL_FreeSurface(icon);
2211         icon = NULL;
2212         version = SDL_Linked_Version();
2213         // only use non-XPM icon support in SDL v1.3 and higher
2214         // SDL v1.2 does not support "smooth" transparency, and thus is better
2215         // off the xpm way
2216         if(version->major >= 2 || (version->major == 1 && version->minor >= 3))
2217         {
2218                 data = (char *) loadimagepixelsbgra("darkplaces-icon", false, false, false, NULL);
2219                 if(data)
2220                 {
2221                         unsigned int red = 0x00FF0000;
2222                         unsigned int green = 0x0000FF00;
2223                         unsigned int blue = 0x000000FF;
2224                         unsigned int alpha = 0xFF000000;
2225                         width = image_width;
2226                         height = image_height;
2227
2228                         // reallocate with malloc, as this is in tempmempool (do not want)
2229                         xpm = data;
2230                         data = (char *) malloc(width * height * 4);
2231                         memcpy(data, xpm, width * height * 4);
2232                         Mem_Free(xpm);
2233                         xpm = NULL;
2234
2235                         icon = SDL_CreateRGBSurface(SDL_SRCALPHA, width, height, 32, LittleLong(red), LittleLong(green), LittleLong(blue), LittleLong(alpha));
2236
2237                         if (icon)
2238                                 icon->pixels = data;
2239                         else
2240                         {
2241                                 Con_Printf(     "Failed to create surface for the window Icon!\n"
2242                                                 "%s\n", SDL_GetError());
2243                                 free(data);
2244                         }
2245                 }
2246         }
2247
2248         // we only get here if non-XPM icon was missing, or SDL version is not
2249         // sufficient for transparent non-XPM icons
2250         if(!icon)
2251         {
2252                 xpm = (char *) FS_LoadFile("darkplaces-icon.xpm", tempmempool, false, NULL);
2253                 idata = NULL;
2254                 if(xpm)
2255                         idata = XPM_DecodeString(xpm);
2256                 if(!idata)
2257                         idata = ENGINE_ICON;
2258                 if(xpm)
2259                         Mem_Free(xpm);
2260
2261                 data = idata[0];
2262
2263                 if(sscanf(data, "%i %i %i %i", &width, &height, &colors, &isize) == 4)
2264                 {
2265                         if(isize == 1)
2266                         {
2267                                 for(i = 0; i < colors; ++i)
2268                                 {
2269                                         unsigned int r, g, b;
2270                                         char idx;
2271
2272                                         if(sscanf(idata[i+1], "%c c #%02x%02x%02x", &idx, &r, &g, &b) != 4)
2273                                         {
2274                                                 char foo[2];
2275                                                 if(sscanf(idata[i+1], "%c c Non%1[e]", &idx, foo) != 2) // I take the DailyWTF credit for this. --div0
2276                                                         break;
2277                                                 else
2278                                                 {
2279                                                         palette[i].r = 255; // color key
2280                                                         palette[i].g = 0;
2281                                                         palette[i].b = 255;
2282                                                         thenone = i; // weeeee
2283                                                         palenc[(unsigned char) idx] = i;
2284                                                 }
2285                                         }
2286                                         else
2287                                         {
2288                                                 palette[i].r = r - (r == 255 && g == 0 && b == 255); // change 255/0/255 pink to 254/0/255 for color key
2289                                                 palette[i].g = g;
2290                                                 palette[i].b = b;
2291                                                 palenc[(unsigned char) idx] = i;
2292                                         }
2293                                 }
2294
2295                                 if (i == colors)
2296                                 {
2297                                         // allocate the image data
2298                                         data = (char*) malloc(width*height);
2299
2300                                         for(j = 0; j < height; ++j)
2301                                         {
2302                                                 for(i = 0; i < width; ++i)
2303                                                 {
2304                                                         // casting to the safest possible datatypes ^^
2305                                                         data[j * width + i] = palenc[((unsigned char*)idata[colors+j+1])[i]];
2306                                                 }
2307                                         }
2308
2309                                         if(icon != NULL)
2310                                         {
2311                                                 // SDL_FreeSurface should free the data too
2312                                                 // but for completeness' sake...
2313                                                 if(icon->flags & SDL_PREALLOC)
2314                                                 {
2315                                                         free(icon->pixels);
2316                                                         icon->pixels = NULL; // safety
2317                                                 }
2318                                                 SDL_FreeSurface(icon);
2319                                         }
2320
2321                                         icon = SDL_CreateRGBSurface(SDL_SRCCOLORKEY, width, height, 8, 0,0,0,0);// rmask, gmask, bmask, amask); no mask needed
2322                                         // 8 bit surfaces get an empty palette allocated according to the docs
2323                                         // so it's a palette image for sure :) no endian check necessary for the mask
2324
2325                                         if(icon)
2326                                         {
2327                                                 icon->pixels = data;
2328                                                 SDL_SetPalette(icon, SDL_PHYSPAL|SDL_LOGPAL, palette, 0, colors);
2329                                                 SDL_SetColorKey(icon, SDL_SRCCOLORKEY, thenone);
2330                                         }
2331                                         else
2332                                         {
2333                                                 Con_Printf(     "Failed to create surface for the window Icon!\n"
2334                                                                 "%s\n", SDL_GetError());
2335                                                 free(data);
2336                                         }
2337                                 }
2338                                 else
2339                                 {
2340                                         Con_Printf("This XPM's palette looks odd. Can't continue.\n");
2341                                 }
2342                         }
2343                         else
2344                         {
2345                                 // NOTE: Only 1-char colornames are supported
2346                                 Con_Printf("This XPM's palette is either huge or idiotically unoptimized. It's key size is %i\n", isize);
2347                         }
2348                 }
2349                 else
2350                 {
2351                         // NOTE: Only 1-char colornames are supported
2352                         Con_Printf("Sorry, but this does not even look similar to an XPM.\n");
2353                 }
2354         }
2355
2356         if (icon)
2357                 SDL_WM_SetIcon(icon, NULL);
2358
2359         SDL_WM_SetCaption( gamename, NULL );
2360         screen = SDL_SetVideoMode(screenwidth, screenheight, screenbpp, screenflags);
2361
2362 #if SDL_MAJOR_VERSION == 1
2363 // LordHavoc: info.info.x11.lock_func and accompanying code do not seem to compile with SDL 1.3
2364 #if SDL_VIDEO_DRIVER_X11 && !SDL_VIDEO_DRIVER_QUARTZ
2365
2366         version = SDL_Linked_Version();
2367         // only use non-XPM icon support in SDL v1.3 and higher
2368         // SDL v1.2 does not support "smooth" transparency, and thus is better
2369         // off the xpm way
2370         if(screen && (!(version->major >= 2 || (version->major == 1 && version->minor >= 3))))
2371         {
2372                 // in this case, we did not set the good icon yet
2373                 SDL_SysWMinfo info;
2374                 SDL_VERSION(&info.version);
2375                 if(SDL_GetWMInfo(&info) == 1 && info.subsystem == SDL_SYSWM_X11)
2376                 {
2377                         data = (char *) loadimagepixelsbgra("darkplaces-icon", false, false, false, NULL);
2378                         if(data)
2379                         {
2380                                 // use _NET_WM_ICON too
2381                                 static long netwm_icon[MAX_NETWM_ICON];
2382                                 int pos = 0;
2383                                 int i = 1;
2384                                 char vabuf[1024];
2385
2386                                 while(data)
2387                                 {
2388                                         if(pos + 2 * image_width * image_height < MAX_NETWM_ICON)
2389                                         {
2390                                                 netwm_icon[pos++] = image_width;
2391                                                 netwm_icon[pos++] = image_height;
2392                                                 for(i = 0; i < image_height; ++i)
2393                                                         for(j = 0; j < image_width; ++j)
2394                                                                 netwm_icon[pos++] = BuffLittleLong((unsigned char *) &data[(i*image_width+j)*4]);
2395                                         }
2396                                         else
2397                                         {
2398                                                 Con_Printf("Skipping NETWM icon #%d because there is no space left\n", i);
2399                                         }
2400                                         ++i;
2401                                         Mem_Free(data);
2402                                         data = (char *) loadimagepixelsbgra(va(vabuf, sizeof(vabuf), "darkplaces-icon%d", i), false, false, false, NULL);
2403                                 }
2404
2405                                 info.info.x11.lock_func();
2406                                 {
2407                                         Atom net_wm_icon = XInternAtom(info.info.x11.display, "_NET_WM_ICON", false);
2408                                         XChangeProperty(info.info.x11.display, info.info.x11.wmwindow, net_wm_icon, XA_CARDINAL, 32, PropModeReplace, (const unsigned char *) netwm_icon, pos);
2409                                 }
2410                                 info.info.x11.unlock_func();
2411                         }
2412                 }
2413         }
2414 #endif
2415 #endif
2416         return screen;
2417 }
2418
2419 #endif
2420 #endif
2421
2422 static void VID_OutputVersion(void)
2423 {
2424         SDL_version version;
2425 #if SDL_MAJOR_VERSION == 1
2426         version = *SDL_Linked_Version();
2427 #else
2428         SDL_GetVersion(&version);
2429 #endif
2430         Con_Printf(     "Linked against SDL version %d.%d.%d\n"
2431                                         "Using SDL library version %d.%d.%d\n",
2432                                         SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL,
2433                                         version.major, version.minor, version.patch );
2434 }
2435
2436 static qboolean VID_InitModeGL(viddef_mode_t *mode)
2437 {
2438 #if SDL_MAJOR_VERSION == 1
2439         static int notfirstvideomode = false;
2440         int flags = SDL_OPENGL;
2441 #else
2442         int windowflags = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL;
2443 #endif
2444 #ifndef USE_GLES2
2445         int i;
2446         const char *drivername;
2447 #endif
2448
2449         win_half_width = mode->width>>1;
2450         win_half_height = mode->height>>1;
2451
2452         if(vid_resizable.integer)
2453 #if SDL_MAJOR_VERSION == 1
2454                 flags |= SDL_RESIZABLE;
2455 #else
2456                 windowflags |= SDL_WINDOW_RESIZABLE;
2457 #endif
2458
2459         VID_OutputVersion();
2460
2461 #if SDL_MAJOR_VERSION == 1
2462         /*
2463         SDL 1.2 Hack
2464                 We cant switch from one OpenGL video mode to another.
2465                 Thus we first switch to some stupid 2D mode and then back to OpenGL.
2466         */
2467         if (notfirstvideomode)
2468                 SDL_SetVideoMode( 0, 0, 0, 0 );
2469         notfirstvideomode = true;
2470 #endif
2471
2472 #ifndef USE_GLES2
2473         // SDL usually knows best
2474         drivername = NULL;
2475
2476 // COMMANDLINEOPTION: SDL GL: -gl_driver <drivername> selects a GL driver library, default is whatever SDL recommends, useful only for 3dfxogl.dll/3dfxvgl.dll or fxmesa or similar, if you don't know what this is for, you don't need it
2477         i = COM_CheckParm("-gl_driver");
2478         if (i && i < com_argc - 1)
2479                 drivername = com_argv[i + 1];
2480         if (SDL_GL_LoadLibrary(drivername) < 0)
2481         {
2482                 Con_Printf("Unable to load GL driver \"%s\": %s\n", drivername, SDL_GetError());
2483                 return false;
2484         }
2485 #endif
2486
2487 #ifdef DP_MOBILETOUCH
2488         // mobile platforms are always fullscreen, we'll get the resolution after opening the window
2489         mode->fullscreen = true;
2490         // hide the menu with SDL_WINDOW_BORDERLESS
2491         windowflags |= SDL_WINDOW_FULLSCREEN | SDL_WINDOW_BORDERLESS;
2492 #endif
2493 #ifndef USE_GLES2
2494         if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL)
2495         {
2496                 VID_Shutdown();
2497                 Con_Print("Required OpenGL function glGetString not found\n");
2498                 return false;
2499         }
2500 #endif
2501
2502         // Knghtbrd: should do platform-specific extension string function here
2503
2504         vid_isfullscreen = false;
2505 #if SDL_MAJOR_VERSION == 1
2506         {
2507                 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
2508                 desktop_mode.width = vi->current_w;
2509                 desktop_mode.height = vi->current_h;
2510                 desktop_mode.bpp = vi->vfmt->BitsPerPixel;
2511                 desktop_mode.pixelheight_num = 1;
2512                 desktop_mode.pixelheight_denom = 1; // SDL does not provide this
2513                 if (mode->fullscreen) {
2514                         if (vid_desktopfullscreen.integer)
2515                         {
2516                                 mode->width = vi->current_w;
2517                                 mode->height = vi->current_h;
2518                                 mode->bitsperpixel = vi->vfmt->BitsPerPixel;
2519                         }
2520                         flags |= SDL_FULLSCREEN;
2521                         vid_isfullscreen = true;
2522                 }
2523         }
2524 #else
2525         {
2526                 if (mode->fullscreen) {
2527                         if (vid_desktopfullscreen.integer)
2528                                 windowflags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
2529                         else
2530                                 windowflags |= SDL_WINDOW_FULLSCREEN;
2531                         vid_isfullscreen = true;
2532                 }
2533         }
2534 #endif
2535         //flags |= SDL_HWSURFACE;
2536
2537         SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
2538         if (mode->bitsperpixel >= 32)
2539         {
2540                 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 8);
2541                 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 8);
2542                 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 8);
2543                 SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 8);
2544                 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 24);
2545                 SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 8);
2546         }
2547         else
2548         {
2549                 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5);
2550                 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5);
2551                 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5);
2552                 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
2553         }
2554         if (mode->stereobuffer)
2555                 SDL_GL_SetAttribute (SDL_GL_STEREO, 1);
2556         if (mode->samples > 1)
2557         {
2558                 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1);
2559                 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, mode->samples);
2560         }
2561
2562 #if SDL_MAJOR_VERSION == 1
2563         if (vid_vsync.integer)
2564                 SDL_GL_SetAttribute (SDL_GL_SWAP_CONTROL, 1);
2565         else
2566                 SDL_GL_SetAttribute (SDL_GL_SWAP_CONTROL, 0);
2567 #else
2568 #ifdef USE_GLES2
2569         SDL_GL_SetAttribute (SDL_GL_CONTEXT_MAJOR_VERSION, 2);
2570         SDL_GL_SetAttribute (SDL_GL_CONTEXT_MINOR_VERSION, 0);
2571         SDL_GL_SetAttribute (SDL_GL_RETAINED_BACKING, 1);
2572 #endif
2573 #endif
2574
2575         video_bpp = mode->bitsperpixel;
2576 #if SDL_MAJOR_VERSION == 1
2577         video_flags = flags;
2578         screen = VID_WrapSDL_SetVideoMode(mode->width, mode->height, mode->bitsperpixel, flags);
2579         if (screen == NULL)
2580         {
2581                 Con_Printf("Failed to set video mode to %ix%i: %s\n", mode->width, mode->height, SDL_GetError());
2582                 VID_Shutdown();
2583                 return false;
2584         }
2585         mode->width = screen->w;
2586         mode->height = screen->h;
2587 #else
2588         window_flags = windowflags;
2589         window = SDL_CreateWindow(gamename, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, mode->width, mode->height, windowflags);
2590         if (window == NULL)
2591         {
2592                 Con_Printf("Failed to set video mode to %ix%i: %s\n", mode->width, mode->height, SDL_GetError());
2593                 VID_Shutdown();
2594                 return false;
2595         }
2596         SDL_GetWindowSize(window, &mode->width, &mode->height);
2597         context = SDL_GL_CreateContext(window);
2598         if (context == NULL)
2599         {
2600                 Con_Printf("Failed to initialize OpenGL context: %s\n", SDL_GetError());
2601                 VID_Shutdown();
2602                 return false;
2603         }
2604 #endif
2605
2606         vid_softsurface = NULL;
2607         vid.softpixels = NULL;
2608
2609 #if SDL_MAJOR_VERSION == 1
2610         // init keyboard
2611         SDL_EnableUNICODE( SDL_ENABLE );
2612         // enable key repeat since everyone expects it
2613         SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2614 #endif
2615
2616 #if SDL_MAJOR_VERSION != 1
2617         SDL_GL_SetSwapInterval(vid_vsync.integer != 0);
2618         vid_usingvsync = (vid_vsync.integer != 0);
2619 #endif
2620
2621         gl_platform = "SDL";
2622         gl_platformextensions = "";
2623
2624 #ifdef USE_GLES2
2625         GLES_Init();
2626 #else
2627         GL_Init();
2628 #endif
2629
2630         vid_hidden = false;
2631         vid_activewindow = false;
2632         vid_hasfocus = true;
2633         vid_usingmouse = false;
2634         vid_usinghidecursor = false;
2635                 
2636 #if SDL_MAJOR_VERSION == 1
2637         SDL_WM_GrabInput(SDL_GRAB_OFF);
2638 #endif
2639         return true;
2640 }
2641
2642 extern cvar_t gl_info_extensions;
2643 extern cvar_t gl_info_vendor;
2644 extern cvar_t gl_info_renderer;
2645 extern cvar_t gl_info_version;
2646 extern cvar_t gl_info_platform;
2647 extern cvar_t gl_info_driver;
2648
2649 static qboolean VID_InitModeSoft(viddef_mode_t *mode)
2650 {
2651 #if SDL_MAJOR_VERSION == 1
2652         int flags = SDL_HWSURFACE;
2653         if(!COM_CheckParm("-noasyncblit")) flags |= SDL_ASYNCBLIT;
2654 #else
2655         int windowflags = SDL_WINDOW_SHOWN;
2656 #endif
2657
2658         win_half_width = mode->width>>1;
2659         win_half_height = mode->height>>1;
2660
2661         if(vid_resizable.integer)
2662 #if SDL_MAJOR_VERSION == 1
2663                 flags |= SDL_RESIZABLE;
2664 #else
2665                 windowflags |= SDL_WINDOW_RESIZABLE;
2666 #endif
2667
2668         VID_OutputVersion();
2669
2670         vid_isfullscreen = false;
2671         if (mode->fullscreen) {
2672 #if SDL_MAJOR_VERSION == 1
2673                 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
2674                 mode->width = vi->current_w;
2675                 mode->height = vi->current_h;
2676                 mode->bitsperpixel = vi->vfmt->BitsPerPixel;
2677                 flags |= SDL_FULLSCREEN;
2678 #else
2679                 if (vid_desktopfullscreen.integer)
2680                         windowflags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
2681                 else
2682                         windowflags |= SDL_WINDOW_FULLSCREEN;
2683 #endif
2684                 vid_isfullscreen = true;
2685         }
2686
2687         video_bpp = mode->bitsperpixel;
2688 #if SDL_MAJOR_VERSION == 1
2689         video_flags = flags;
2690         screen = VID_WrapSDL_SetVideoMode(mode->width, mode->height, mode->bitsperpixel, flags);
2691         if (screen == NULL)
2692         {
2693                 Con_Printf("Failed to set video mode to %ix%i: %s\n", mode->width, mode->height, SDL_GetError());
2694                 VID_Shutdown();
2695                 return false;
2696         }
2697         mode->width = screen->w;
2698         mode->height = screen->h;
2699 #else
2700         window_flags = windowflags;
2701         window = SDL_CreateWindow(gamename, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, mode->width, mode->height, windowflags);
2702         if (window == NULL)
2703         {
2704                 Con_Printf("Failed to set video mode to %ix%i: %s\n", mode->width, mode->height, SDL_GetError());
2705                 VID_Shutdown();
2706                 return false;
2707         }
2708         SDL_GetWindowSize(window, &mode->width, &mode->height);
2709 #endif
2710
2711         // create a framebuffer using our specific color format, we let the SDL blit function convert it in VID_Finish
2712         vid_softsurface = SDL_CreateRGBSurface(SDL_SWSURFACE, mode->width, mode->height, 32, 0x00FF0000, 0x0000FF00, 0x00000000FF, 0xFF000000);
2713         if (vid_softsurface == NULL)
2714         {
2715                 Con_Printf("Failed to setup software rasterizer framebuffer %ix%ix32bpp: %s\n", mode->width, mode->height, SDL_GetError());
2716                 VID_Shutdown();
2717                 return false;
2718         }
2719 #if SDL_MAJOR_VERSION == 1
2720         SDL_SetAlpha(vid_softsurface, 0, 255);
2721 #else
2722         SDL_SetSurfaceBlendMode(vid_softsurface, SDL_BLENDMODE_NONE);
2723 #endif
2724
2725         vid.softpixels = (unsigned int *)vid_softsurface->pixels;
2726         vid.softdepthpixels = (unsigned int *)calloc(1, mode->width * mode->height * 4);
2727         if (DPSOFTRAST_Init(mode->width, mode->height, vid_soft_threads.integer, vid_soft_interlace.integer, (unsigned int *)vid_softsurface->pixels, (unsigned int *)vid.softdepthpixels) < 0)
2728         {
2729                 Con_Printf("Failed to initialize software rasterizer\n");
2730                 VID_Shutdown();
2731                 return false;
2732         }
2733
2734 #if SDL_MAJOR_VERSION == 1
2735         // init keyboard
2736         SDL_EnableUNICODE( SDL_ENABLE );
2737         // enable key repeat since everyone expects it
2738         SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
2739 #endif
2740
2741         VID_Soft_SharedSetup();
2742
2743         vid_hidden = false;
2744         vid_activewindow = false;
2745         vid_hasfocus = true;
2746         vid_usingmouse = false;
2747         vid_usinghidecursor = false;
2748
2749 #if SDL_MAJOR_VERSION == 1
2750         SDL_WM_GrabInput(SDL_GRAB_OFF);
2751 #endif
2752         return true;
2753 }
2754
2755 qboolean VID_InitMode(viddef_mode_t *mode)
2756 {
2757         // GAME_STEELSTORM specific
2758         steelstorm_showing_map = Cvar_FindVar("steelstorm_showing_map");
2759         steelstorm_showing_mousecursor = Cvar_FindVar("steelstorm_showing_mousecursor");
2760
2761         if (!SDL_WasInit(SDL_INIT_VIDEO) && SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
2762                 Sys_Error ("Failed to init SDL video subsystem: %s", SDL_GetError());
2763
2764 #if SDL_MAJOR_VERSION != 1
2765         Cvar_SetValueQuick(&vid_touchscreen_supportshowkeyboard, SDL_HasScreenKeyboardSupport() ? 1 : 0);
2766 #endif
2767 #ifdef SSE_POSSIBLE
2768         if (vid_soft.integer)
2769                 return VID_InitModeSoft(mode);
2770         else
2771 #endif
2772                 return VID_InitModeGL(mode);
2773 }
2774
2775 void VID_Shutdown (void)
2776 {
2777         VID_EnableJoystick(false);
2778         VID_SetMouse(false, false, false);
2779         VID_RestoreSystemGamma();
2780
2781 #if SDL_MAJOR_VERSION == 1
2782 #ifndef WIN32
2783 #ifndef MACOSX
2784         if (icon)
2785                 SDL_FreeSurface(icon);
2786         icon = NULL;
2787 #endif
2788 #endif
2789 #endif
2790
2791         if (vid_softsurface)
2792                 SDL_FreeSurface(vid_softsurface);
2793         vid_softsurface = NULL;
2794         vid.softpixels = NULL;
2795         if (vid.softdepthpixels)
2796                 free(vid.softdepthpixels);
2797         vid.softdepthpixels = NULL;
2798
2799 #if SDL_MAJOR_VERSION != 1
2800         SDL_DestroyWindow(window);
2801         window = NULL;
2802 #endif
2803
2804         SDL_QuitSubSystem(SDL_INIT_VIDEO);
2805
2806         gl_driver[0] = 0;
2807         gl_extensions = "";
2808         gl_platform = "";
2809         gl_platformextensions = "";
2810 }
2811
2812 int VID_SetGamma (unsigned short *ramps, int rampsize)
2813 {
2814 #if SDL_MAJOR_VERSION == 1
2815         return !SDL_SetGammaRamp (ramps, ramps + rampsize, ramps + rampsize*2);
2816 #else
2817         return !SDL_SetWindowGammaRamp (window, ramps, ramps + rampsize, ramps + rampsize*2);
2818 #endif
2819 }
2820
2821 int VID_GetGamma (unsigned short *ramps, int rampsize)
2822 {
2823 #if SDL_MAJOR_VERSION == 1
2824         return !SDL_GetGammaRamp (ramps, ramps + rampsize, ramps + rampsize*2);
2825 #else
2826         return !SDL_GetWindowGammaRamp (window, ramps, ramps + rampsize, ramps + rampsize*2);
2827 #endif
2828 }
2829
2830 void VID_Finish (void)
2831 {
2832 #if SDL_MAJOR_VERSION == 1
2833         Uint8 appstate;
2834
2835         //react on appstate changes
2836         appstate = SDL_GetAppState();
2837
2838         vid_hidden = !(appstate & SDL_APPACTIVE);
2839         vid_hasfocus = (appstate & SDL_APPINPUTFOCUS) != 0;
2840 #endif
2841         vid_activewindow = !vid_hidden && vid_hasfocus;
2842
2843         VID_UpdateGamma(false, 256);
2844
2845         if (!vid_hidden)
2846         {
2847                 switch(vid.renderpath)
2848                 {
2849                 case RENDERPATH_GL11:
2850                 case RENDERPATH_GL13:
2851                 case RENDERPATH_GL20:
2852                 case RENDERPATH_GLES1:
2853                 case RENDERPATH_GLES2:
2854                         CHECKGLERROR
2855                         if (r_speeds.integer == 2 || gl_finish.integer)
2856                                 GL_Finish();
2857
2858 #if SDL_MAJOR_VERSION != 1
2859 {
2860         qboolean vid_usevsync;
2861         vid_usevsync = (vid_vsync.integer && !cls.timedemo);
2862         if (vid_usingvsync != vid_usevsync)
2863         {
2864                 if (SDL_GL_SetSwapInterval(vid_usevsync != 0) >= 0)
2865                         Con_DPrintf("Vsync %s\n", vid_usevsync ? "activated" : "deactivated");
2866                 else
2867                         Con_DPrintf("ERROR: can't %s vsync\n", vid_usevsync ? "activate" : "deactivate");
2868         }
2869 }
2870 #endif
2871 #if SDL_MAJOR_VERSION == 1
2872                         SDL_GL_SwapBuffers();
2873 #else
2874                         SDL_GL_SwapWindow(window);
2875 #endif
2876                         break;
2877                 case RENDERPATH_SOFT:
2878                         DPSOFTRAST_Finish();
2879 #if SDL_MAJOR_VERSION == 1
2880 //              if (!r_test.integer)
2881                 {
2882                         SDL_BlitSurface(vid_softsurface, NULL, screen, NULL);
2883                         SDL_Flip(screen);
2884                 }
2885 #else
2886                         {
2887                                 SDL_Surface *screen = SDL_GetWindowSurface(window);
2888                                 SDL_BlitSurface(vid_softsurface, NULL, screen, NULL);
2889                                 SDL_UpdateWindowSurface(window);
2890                         }
2891 #endif
2892                         break;
2893                 case RENDERPATH_D3D9:
2894                 case RENDERPATH_D3D10:
2895                 case RENDERPATH_D3D11:
2896                         if (r_speeds.integer == 2 || gl_finish.integer)
2897                                 GL_Finish();
2898                         break;
2899                 }
2900         }
2901 }
2902
2903 vid_mode_t *VID_GetDesktopMode(void)
2904 {
2905 #if SDL_MAJOR_VERSION != 1
2906         SDL_DisplayMode mode;
2907         int bpp;
2908         Uint32 rmask, gmask, bmask, amask;
2909         SDL_GetDesktopDisplayMode(0, &mode);
2910         SDL_PixelFormatEnumToMasks(mode.format, &bpp, &rmask, &gmask, &bmask, &amask);
2911         desktop_mode.width = mode.w;
2912         desktop_mode.height = mode.h;
2913         desktop_mode.bpp = bpp;
2914         desktop_mode.refreshrate = mode.refresh_rate;
2915         desktop_mode.pixelheight_num = 1;
2916         desktop_mode.pixelheight_denom = 1; // SDL does not provide this
2917         // TODO check whether this actually works, or whether we do still need
2918         // a read-window-size-after-entering-desktop-fullscreen hack for
2919         // multiscreen setups.
2920 #endif
2921         return &desktop_mode;
2922 }
2923
2924 size_t VID_ListModes(vid_mode_t *modes, size_t maxcount)
2925 {
2926         size_t k = 0;
2927 #if SDL_MAJOR_VERSION == 1
2928         SDL_Rect **vidmodes;
2929         int bpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
2930 #ifdef WIN64
2931         SDL_Rect **ENDRECT = (SDL_Rect**)-1LL;
2932 #else
2933         SDL_Rect **ENDRECT = (SDL_Rect**)-1;
2934 #endif
2935
2936         for(vidmodes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE); vidmodes && vidmodes != ENDRECT && *vidmodes; ++vidmodes)
2937         {
2938                 if(k >= maxcount)
2939                         break;
2940                 modes[k].width = (*vidmodes)->w;
2941                 modes[k].height = (*vidmodes)->h;
2942                 modes[k].bpp = bpp;
2943                 modes[k].refreshrate = 60; // no support for refresh rate in SDL
2944                 modes[k].pixelheight_num = 1;
2945                 modes[k].pixelheight_denom = 1; // SDL does not provide this
2946                 ++k;
2947         }
2948 #else
2949         int modenum;
2950         int nummodes = SDL_GetNumDisplayModes(0);
2951         SDL_DisplayMode mode;
2952         for (modenum = 0;modenum < nummodes;modenum++)
2953         {
2954                 if (k >= maxcount)
2955                         break;
2956                 if (SDL_GetDisplayMode(0, modenum, &mode))
2957                         continue;
2958                 modes[k].width = mode.w;
2959                 modes[k].height = mode.h;
2960                 // FIXME bpp?
2961                 modes[k].refreshrate = mode.refresh_rate;
2962                 modes[k].pixelheight_num = 1;
2963                 modes[k].pixelheight_denom = 1; // SDL does not provide this
2964                 k++;
2965         }
2966 #endif
2967         return k;
2968 }