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