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