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