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