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