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