]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - vid_sdl.c
factor out thread interface from dpsoftrast
[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 shoud 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 static qboolean vid_usingmouse = false;
96 static qboolean vid_usinghidecursor = false;
97 static qboolean vid_isfullscreen;
98 #if !(SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION == 2)
99 static qboolean vid_usingvsync = false;
100 #endif
101 static int vid_numjoysticks = 0;
102 #define MAX_JOYSTICKS 8
103 static SDL_Joystick *vid_joysticks[MAX_JOYSTICKS];
104
105 static int win_half_width = 50;
106 static int win_half_height = 50;
107 static int video_bpp, video_flags;
108
109 static SDL_Surface *screen;
110 static SDL_Surface *vid_softsurface;
111
112 // joystick axes state
113 #define MAX_JOYSTICK_AXES       16
114 typedef struct
115 {
116         float oldmove;
117         float move;
118         double keytime;
119 }joy_axiscache_t;
120 static joy_axiscache_t joy_axescache[MAX_JOYSTICK_AXES];
121
122 /////////////////////////
123 // Input handling
124 ////
125 //TODO: Add joystick support
126 //TODO: Add error checking
127
128
129 //keysym to quake keysym mapping
130 #define tenoh   0,0,0,0,0, 0,0,0,0,0
131 #define fiftyoh tenoh, tenoh, tenoh, tenoh, tenoh
132 #define hundredoh fiftyoh, fiftyoh
133 static unsigned int tbl_sdltoquake[] =
134 {
135         0,0,0,0,                //SDLK_UNKNOWN          = 0,
136         0,0,0,0,                //SDLK_FIRST            = 0,
137         K_BACKSPACE,    //SDLK_BACKSPACE        = 8,
138         K_TAB,                  //SDLK_TAB                      = 9,
139         0,0,
140         0,                              //SDLK_CLEAR            = 12,
141         K_ENTER,                //SDLK_RETURN           = 13,
142     0,0,0,0,0,
143         K_PAUSE,                //SDLK_PAUSE            = 19,
144         0,0,0,0,0,0,0,
145         K_ESCAPE,               //SDLK_ESCAPE           = 27,
146         0,0,0,0,
147         K_SPACE,                //SDLK_SPACE            = 32,
148         '!',                    //SDLK_EXCLAIM          = 33,
149         '"',                    //SDLK_QUOTEDBL         = 34,
150         '#',                    //SDLK_HASH                     = 35,
151         '$',                    //SDLK_DOLLAR           = 36,
152         0,
153         '&',                    //SDLK_AMPERSAND        = 38,
154         '\'',                   //SDLK_QUOTE            = 39,
155         '(',                    //SDLK_LEFTPAREN        = 40,
156         ')',                    //SDLK_RIGHTPAREN       = 41,
157         '*',                    //SDLK_ASTERISK         = 42,
158         '+',                    //SDLK_PLUS                     = 43,
159         ',',                    //SDLK_COMMA            = 44,
160         '-',                    //SDLK_MINUS            = 45,
161         '.',                    //SDLK_PERIOD           = 46,
162         '/',                    //SDLK_SLASH            = 47,
163         '0',                    //SDLK_0                        = 48,
164         '1',                    //SDLK_1                        = 49,
165         '2',                    //SDLK_2                        = 50,
166         '3',                    //SDLK_3                        = 51,
167         '4',                    //SDLK_4                        = 52,
168         '5',                    //SDLK_5                        = 53,
169         '6',                    //SDLK_6                        = 54,
170         '7',                    //SDLK_7                        = 55,
171         '8',                    //SDLK_8                        = 56,
172         '9',                    //SDLK_9                        = 57,
173         ':',                    //SDLK_COLON            = 58,
174         ';',                    //SDLK_SEMICOLON        = 59,
175         '<',                    //SDLK_LESS                     = 60,
176         '=',                    //SDLK_EQUALS           = 61,
177         '>',                    //SDLK_GREATER          = 62,
178         '?',                    //SDLK_QUESTION         = 63,
179         '@',                    //SDLK_AT                       = 64,
180         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
181         '[',            //SDLK_LEFTBRACKET      = 91,
182         '\\',           //SDLK_BACKSLASH        = 92,
183         ']',            //SDLK_RIGHTBRACKET     = 93,
184         '^',            //SDLK_CARET            = 94,
185         '_',            //SDLK_UNDERSCORE       = 95,
186         '`',            //SDLK_BACKQUOTE        = 96,
187         'a',            //SDLK_a                        = 97,
188         'b',            //SDLK_b                        = 98,
189         'c',            //SDLK_c                        = 99,
190         'd',            //SDLK_d                        = 100,
191         'e',            //SDLK_e                        = 101,
192         'f',            //SDLK_f                        = 102,
193         'g',            //SDLK_g                        = 103,
194         'h',            //SDLK_h                        = 104,
195         'i',            //SDLK_i                        = 105,
196         'j',            //SDLK_j                        = 106,
197         'k',            //SDLK_k                        = 107,
198         'l',            //SDLK_l                        = 108,
199         'm',            //SDLK_m                        = 109,
200         'n',            //SDLK_n                        = 110,
201         'o',            //SDLK_o                        = 111,
202         'p',            //SDLK_p                        = 112,
203         'q',            //SDLK_q                        = 113,
204         'r',            //SDLK_r                        = 114,
205         's',            //SDLK_s                        = 115,
206         't',            //SDLK_t                        = 116,
207         'u',            //SDLK_u                        = 117,
208         'v',            //SDLK_v                        = 118,
209         'w',            //SDLK_w                        = 119,
210         'x',            //SDLK_x                        = 120,
211         'y',            //SDLK_y                        = 121,
212         'z',            //SDLK_z                        = 122,
213         0,0,0,0,
214         K_DEL,          //SDLK_DELETE           = 127,
215         hundredoh /*227*/, tenoh, tenoh, 0,0,0,0,0,0,0,0,
216         K_KP_0,         //SDLK_KP0              = 256,
217         K_KP_1,         //SDLK_KP1              = 257,
218         K_KP_2,         //SDLK_KP2              = 258,
219         K_KP_3,         //SDLK_KP3              = 259,
220         K_KP_4,         //SDLK_KP4              = 260,
221         K_KP_5,         //SDLK_KP5              = 261,
222         K_KP_6,         //SDLK_KP6              = 262,
223         K_KP_7,         //SDLK_KP7              = 263,
224         K_KP_8,         //SDLK_KP8              = 264,
225         K_KP_9,         //SDLK_KP9              = 265,
226         K_KP_PERIOD,//SDLK_KP_PERIOD    = 266,
227         K_KP_DIVIDE,//SDLK_KP_DIVIDE    = 267,
228         K_KP_MULTIPLY,//SDLK_KP_MULTIPLY= 268,
229         K_KP_MINUS,     //SDLK_KP_MINUS         = 269,
230         K_KP_PLUS,      //SDLK_KP_PLUS          = 270,
231         K_KP_ENTER,     //SDLK_KP_ENTER         = 271,
232         K_KP_EQUALS,//SDLK_KP_EQUALS    = 272,
233         K_UPARROW,      //SDLK_UP               = 273,
234         K_DOWNARROW,//SDLK_DOWN         = 274,
235         K_RIGHTARROW,//SDLK_RIGHT       = 275,
236         K_LEFTARROW,//SDLK_LEFT         = 276,
237         K_INS,          //SDLK_INSERT   = 277,
238         K_HOME,         //SDLK_HOME             = 278,
239         K_END,          //SDLK_END              = 279,
240         K_PGUP,         //SDLK_PAGEUP   = 280,
241         K_PGDN,         //SDLK_PAGEDOWN = 281,
242         K_F1,           //SDLK_F1               = 282,
243         K_F2,           //SDLK_F2               = 283,
244         K_F3,           //SDLK_F3               = 284,
245         K_F4,           //SDLK_F4               = 285,
246         K_F5,           //SDLK_F5               = 286,
247         K_F6,           //SDLK_F6               = 287,
248         K_F7,           //SDLK_F7               = 288,
249         K_F8,           //SDLK_F8               = 289,
250         K_F9,           //SDLK_F9               = 290,
251         K_F10,          //SDLK_F10              = 291,
252         K_F11,          //SDLK_F11              = 292,
253         K_F12,          //SDLK_F12              = 293,
254         0,                      //SDLK_F13              = 294,
255         0,                      //SDLK_F14              = 295,
256         0,                      //SDLK_F15              = 296,
257         0,0,0,
258         K_NUMLOCK,      //SDLK_NUMLOCK  = 300,
259         K_CAPSLOCK,     //SDLK_CAPSLOCK = 301,
260         K_SCROLLOCK,//SDLK_SCROLLOCK= 302,
261         K_SHIFT,        //SDLK_RSHIFT   = 303,
262         K_SHIFT,        //SDLK_LSHIFT   = 304,
263         K_CTRL,         //SDLK_RCTRL    = 305,
264         K_CTRL,         //SDLK_LCTRL    = 306,
265         K_ALT,          //SDLK_RALT             = 307,
266         K_ALT,          //SDLK_LALT             = 308,
267         0,                      //SDLK_RMETA    = 309,
268         0,                      //SDLK_LMETA    = 310,
269         0,                      //SDLK_LSUPER   = 311,          /* Left "Windows" key */
270         0,                      //SDLK_RSUPER   = 312,          /* Right "Windows" key */
271         K_ALT,                  //SDLK_MODE             = 313,          /* "Alt Gr" key */
272         0,                      //SDLK_COMPOSE  = 314,          /* Multi-key compose key */
273         0,                      //SDLK_HELP             = 315,
274         0,                      //SDLK_PRINT    = 316,
275         0,                      //SDLK_SYSREQ   = 317,
276         K_PAUSE,        //SDLK_BREAK    = 318,
277         0,                      //SDLK_MENU             = 319,
278         0,                      //SDLK_POWER    = 320,          /* Power Macintosh power key */
279         'e',            //SDLK_EURO             = 321,          /* Some european keyboards */
280         0                       //SDLK_UNDO             = 322,          /* Atari keyboard has Undo */
281 };
282 #undef tenoh
283 #undef fiftyoh
284 #undef hundredoh
285
286 static int MapKey( unsigned int sdlkey )
287 {
288         if( sdlkey > sizeof(tbl_sdltoquake)/ sizeof(int) )
289                 return 0;
290     return tbl_sdltoquake[ sdlkey ];
291 }
292
293 void VID_SetMouse(qboolean fullscreengrab, qboolean relative, qboolean hidecursor)
294 {
295 #ifndef __IPHONEOS__
296 #ifdef MACOSX
297         if(relative)
298                 if(vid_usingmouse && (vid_usingnoaccel != !!apple_mouse_noaccel.integer))
299                         VID_SetMouse(false, false, false); // ungrab first!
300 #endif
301 #endif
302         if (vid_usingmouse != relative)
303         {
304                 vid_usingmouse = relative;
305                 cl_ignoremousemoves = 2;
306                 SDL_WM_GrabInput( relative ? SDL_GRAB_ON : SDL_GRAB_OFF );
307 #ifndef __IPHONEOS__
308 #ifdef MACOSX
309                 if(relative)
310                 {
311                         // Save the status of mouse acceleration
312                         originalMouseSpeed = -1.0; // in case of error
313                         if(apple_mouse_noaccel.integer)
314                         {
315                                 io_connect_t mouseDev = IN_GetIOHandle();
316                                 if(mouseDev != 0)
317                                 {
318                                         if(IOHIDGetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), &originalMouseSpeed) == kIOReturnSuccess)
319                                         {
320                                                 Con_DPrintf("previous mouse acceleration: %f\n", originalMouseSpeed);
321                                                 if(IOHIDSetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), -1.0) != kIOReturnSuccess)
322                                                 {
323                                                         Con_Print("Could not disable mouse acceleration (failed at IOHIDSetAccelerationWithKey).\n");
324                                                         Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
325                                                 }
326                                         }
327                                         else
328                                         {
329                                                 Con_Print("Could not disable mouse acceleration (failed at IOHIDGetAccelerationWithKey).\n");
330                                                 Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
331                                         }
332                                         IOServiceClose(mouseDev);
333                                 }
334                                 else
335                                 {
336                                         Con_Print("Could not disable mouse acceleration (failed at IO_GetIOHandle).\n");
337                                         Cvar_SetValueQuick(&apple_mouse_noaccel, 0);
338                                 }
339                         }
340
341                         vid_usingnoaccel = !!apple_mouse_noaccel.integer;
342                 }
343                 else
344                 {
345                         if(originalMouseSpeed != -1.0)
346                         {
347                                 io_connect_t mouseDev = IN_GetIOHandle();
348                                 if(mouseDev != 0)
349                                 {
350                                         Con_DPrintf("restoring mouse acceleration to: %f\n", originalMouseSpeed);
351                                         if(IOHIDSetAccelerationWithKey(mouseDev, CFSTR(kIOHIDMouseAccelerationType), originalMouseSpeed) != kIOReturnSuccess)
352                                                 Con_Print("Could not re-enable mouse acceleration (failed at IOHIDSetAccelerationWithKey).\n");
353                                         IOServiceClose(mouseDev);
354                                 }
355                                 else
356                                         Con_Print("Could not re-enable mouse acceleration (failed at IO_GetIOHandle).\n");
357                         }
358                 }
359 #endif
360 #endif
361         }
362         if (vid_usinghidecursor != hidecursor)
363         {
364                 vid_usinghidecursor = hidecursor;
365                 SDL_ShowCursor( hidecursor ? SDL_DISABLE : SDL_ENABLE);
366         }
367 }
368
369 static double IN_JoystickGetAxis(SDL_Joystick *joy, int axis, double sensitivity, double deadzone)
370 {
371         double value;
372         if (axis < 0 || axis >= SDL_JoystickNumAxes(joy))
373                 return 0; // no such axis on this joystick
374         value = SDL_JoystickGetAxis(joy, axis) * (1.0 / 32767.0);
375         value = bound(-1, value, 1);
376         if (fabs(value) < deadzone)
377                 return 0; // within deadzone around center
378         return value * sensitivity;
379 }
380
381 /////////////////////
382 // Joystick axis keyevents
383 // a sort of hack emulating Arrow keys for joystick axises
384 // as some drives dont send such keyevents for them
385 // additionally we should block drivers that do send arrow keyevents to prevent double events
386 ////
387
388 static void IN_JoystickKeyeventForAxis(SDL_Joystick *joy, int axis, int key_pos, int key_neg)
389 {
390         double joytime;
391
392         if (axis < 0 || axis >= SDL_JoystickNumAxes(joy))
393                 return; // no such axis on this joystick
394
395         joytime = Sys_DoubleTime();
396         // no key event, continuous keydown event
397         if (joy_axescache[axis].move == joy_axescache[axis].oldmove)
398         {
399                 if (joy_axescache[axis].move != 0 && joytime > joy_axescache[axis].keytime)
400                 {
401                         //Con_Printf("joy %s %i %f\n", Key_KeynumToString((joy_axescache[axis].move > 0) ? key_pos : key_neg), 1, cl.time);
402                         Key_Event((joy_axescache[axis].move > 0) ? key_pos : key_neg, 0, 1);
403                         joy_axescache[axis].keytime = joytime + 0.5 / 20;
404                 }
405                 return;
406         }
407         // generate key up event
408         if (joy_axescache[axis].oldmove)
409         {
410                 //Con_Printf("joy %s %i %f\n", Key_KeynumToString((joy_axescache[axis].oldmove > 0) ? key_pos : key_neg), 1, cl.time);
411                 Key_Event((joy_axescache[axis].oldmove > 0) ? key_pos : key_neg, 0, 0);
412         }
413         // generate key down event
414         if (joy_axescache[axis].move)
415         {
416                 //Con_Printf("joy %s %i %f\n", Key_KeynumToString((joy_axescache[axis].move > 0) ? key_pos : key_neg), 1, cl.time);
417                 Key_Event((joy_axescache[axis].move > 0) ? key_pos : key_neg, 0, 1);
418                 joy_axescache[axis].keytime = joytime + 0.5;
419         }
420 }
421
422 static qboolean IN_JoystickBlockDoubledKeyEvents(int keycode)
423 {
424         if (!joy_axiskeyevents.integer)
425                 return false;
426
427         // block keyevent if it's going to be provided by joystick keyevent system
428         if (vid_numjoysticks && joy_enable.integer && joy_index.integer >= 0 && joy_index.integer < vid_numjoysticks)
429         {
430                 SDL_Joystick *joy = vid_joysticks[joy_index.integer];
431
432                 if (keycode == K_UPARROW || keycode == K_DOWNARROW)
433                         if (IN_JoystickGetAxis(joy, joy_axisforward.integer, 1, 0.01) || joy_axescache[joy_axisforward.integer].move || joy_axescache[joy_axisforward.integer].oldmove)
434                                 return true;
435                 if (keycode == K_RIGHTARROW || keycode == K_LEFTARROW)
436                         if (IN_JoystickGetAxis(joy, joy_axisside.integer, 1, 0.01) || joy_axescache[joy_axisside.integer].move || joy_axescache[joy_axisside.integer].oldmove)
437                                 return true;
438         }
439
440         return false;
441 }
442
443 /////////////////////
444 // Movement handling
445 ////
446
447 void IN_Move( void )
448 {
449         int j;
450         static int old_x = 0, old_y = 0;
451         static int stuck = 0;
452         int x, y, numaxes, numballs;
453
454         if (vid_usingmouse)
455         {
456                 if(vid_stick_mouse.integer)
457                 {
458                         // have the mouse stuck in the middle, example use: prevent expose effect of beryl during the game when not using
459                         // window grabbing. --blub
460
461                         // we need 2 frames to initialize the center position
462                         if(!stuck)
463                         {
464                                 SDL_WarpMouse(win_half_width, win_half_height);
465                                 SDL_GetMouseState(&x, &y);
466                                 SDL_GetRelativeMouseState(&x, &y);
467                                 ++stuck;
468                         } else {
469                                 SDL_GetRelativeMouseState(&x, &y);
470                                 in_mouse_x = x + old_x;
471                                 in_mouse_y = y + old_y;
472                                 SDL_GetMouseState(&x, &y);
473                                 old_x = x - win_half_width;
474                                 old_y = y - win_half_height;
475                                 SDL_WarpMouse(win_half_width, win_half_height);
476                         }
477                 } else {
478                         SDL_GetRelativeMouseState( &x, &y );
479                         in_mouse_x = x;
480                         in_mouse_y = y;
481                 }
482         }
483
484         SDL_GetMouseState(&x, &y);
485         in_windowmouse_x = x;
486         in_windowmouse_y = y;
487
488         if (vid_numjoysticks && joy_enable.integer && joy_index.integer >= 0 && joy_index.integer < vid_numjoysticks)
489         {
490                 SDL_Joystick *joy = vid_joysticks[joy_index.integer];
491
492                 // balls convert to mousemove
493                 numballs = SDL_JoystickNumBalls(joy);
494                 for (j = 0;j < numballs;j++)
495                 {
496                         SDL_JoystickGetBall(joy, j, &x, &y);
497                         in_mouse_x += x;
498                         in_mouse_y += y;
499                 }
500
501                 // axes
502                 cl.cmd.forwardmove += IN_JoystickGetAxis(joy, joy_axisforward.integer, joy_sensitivityforward.value, joy_deadzoneforward.value) * cl_forwardspeed.value;
503                 cl.cmd.sidemove    += IN_JoystickGetAxis(joy, joy_axisside.integer, joy_sensitivityside.value, joy_deadzoneside.value) * cl_sidespeed.value;
504                 cl.cmd.upmove      += IN_JoystickGetAxis(joy, joy_axisup.integer, joy_sensitivityup.value, joy_deadzoneup.value) * cl_upspeed.value;
505                 cl.viewangles[0]   += IN_JoystickGetAxis(joy, joy_axispitch.integer, joy_sensitivitypitch.value, joy_deadzonepitch.value) * cl.realframetime * cl_pitchspeed.value;
506                 cl.viewangles[1]   += IN_JoystickGetAxis(joy, joy_axisyaw.integer, joy_sensitivityyaw.value, joy_deadzoneyaw.value) * cl.realframetime * cl_yawspeed.value;
507                 //cl.viewangles[2]   += IN_JoystickGetAxis(joy, joy_axisroll.integer, joy_sensitivityroll.value, joy_deadzoneroll.value) * cl.realframetime * cl_rollspeed.value;
508         
509                 // cache state of axes to emulate button events for them
510                 numaxes = min(MAX_JOYSTICK_AXES, SDL_JoystickNumAxes(joy));
511                 for (j = 0; j < numaxes; j++)
512                 {
513                         joy_axescache[j].oldmove = joy_axescache[j].move;
514                         joy_axescache[j].move = IN_JoystickGetAxis(joy, j, 1, 0.01);
515                 }
516
517                 // run keyevents
518                 if (joy_axiskeyevents.integer)
519                 {
520                         IN_JoystickKeyeventForAxis(joy, joy_axisforward.integer, K_DOWNARROW, K_UPARROW);
521                         IN_JoystickKeyeventForAxis(joy, joy_axisside.integer, K_RIGHTARROW, K_LEFTARROW);
522                 }
523         }
524 }
525
526 /////////////////////
527 // Message Handling
528 ////
529
530 #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION == 2
531 static int Sys_EventFilter( SDL_Event *event )
532 {
533         //TODO: Add a quit query in linux, too - though linux user are more likely to know what they do
534         if (event->type == SDL_QUIT)
535         {
536 #ifdef WIN32
537                 if (MessageBox( NULL, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION ) == IDNO)
538                         return 0;
539 #endif
540         }
541         return 1;
542 }
543 #endif
544
545 #ifdef SDL_R_RESTART
546 static qboolean sdl_needs_restart;
547 static void sdl_start(void)
548 {
549 }
550 static void sdl_shutdown(void)
551 {
552         sdl_needs_restart = false;
553 }
554 static void sdl_newmap(void)
555 {
556 }
557 #endif
558
559 static keynum_t buttonremap[18] =
560 {
561         K_MOUSE1,
562         K_MOUSE3,
563         K_MOUSE2,
564         K_MWHEELUP,
565         K_MWHEELDOWN,
566         K_MOUSE4,
567         K_MOUSE5,
568         K_MOUSE6,
569         K_MOUSE7,
570         K_MOUSE8,
571         K_MOUSE9,
572         K_MOUSE10,
573         K_MOUSE11,
574         K_MOUSE12,
575         K_MOUSE13,
576         K_MOUSE14,
577         K_MOUSE15,
578         K_MOUSE16,
579 };
580
581 void Sys_SendKeyEvents( void )
582 {
583         static qboolean sound_active = true;
584         int keycode;
585         SDL_Event event;
586
587         while( SDL_PollEvent( &event ) )
588                 switch( event.type ) {
589                         case SDL_QUIT:
590 #if !(SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION == 2)
591 #ifdef WIN32
592                                 if (MessageBox( NULL, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION ) == IDNO)
593                                         return 0;
594 #endif
595 #endif
596                                 Sys_Quit(0);
597                                 break;
598                         case SDL_KEYDOWN:
599                         case SDL_KEYUP:
600                                 keycode = MapKey(event.key.keysym.sym);
601                                 if (!IN_JoystickBlockDoubledKeyEvents(keycode))
602                                         Key_Event(keycode, event.key.keysym.unicode, (event.key.state == SDL_PRESSED));
603                                 break;
604                         case SDL_ACTIVEEVENT:
605                                 if( event.active.state & SDL_APPACTIVE )
606                                 {
607                                         if( event.active.gain )
608                                                 vid_hidden = false;
609                                         else
610                                                 vid_hidden = true;
611                                 }
612                                 break;
613                         case SDL_MOUSEBUTTONDOWN:
614                         case SDL_MOUSEBUTTONUP:
615                                 if (event.button.button <= 18)
616                                         Key_Event( buttonremap[event.button.button - 1], 0, event.button.state == SDL_PRESSED );
617                                 break;
618                         case SDL_JOYBUTTONDOWN:
619                                 if (!joy_enable.integer)
620                                         break; // ignore down events if joystick has been disabled
621                         case SDL_JOYBUTTONUP:
622                                 if (event.jbutton.button < 48)
623                                         Key_Event( event.jbutton.button + (event.jbutton.button < 16 ? K_JOY1 : K_AUX1 - 16), 0, (event.jbutton.state == SDL_PRESSED) );
624                                 break;
625                         case SDL_VIDEORESIZE:
626                                 if(vid_resizable.integer < 2)
627                                 {
628                                         vid.width = event.resize.w;
629                                         vid.height = event.resize.h;
630                                         SDL_SetVideoMode(vid.width, vid.height, video_bpp, video_flags);
631                                         if (vid_softsurface)
632                                         {
633                                                 SDL_FreeSurface(vid_softsurface);
634                                                 vid_softsurface = SDL_CreateRGBSurface(SDL_SWSURFACE, vid.width, vid.height, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
635                                                 vid.softpixels = (unsigned int *)vid_softsurface->pixels;
636                                                 SDL_SetAlpha(vid_softsurface, 0, 255);
637                                                 if (vid.softdepthpixels)
638                                                         free(vid.softdepthpixels);
639                                                 vid.softdepthpixels = (unsigned int*)calloc(1, vid.width * vid.height * 4);
640                                         }
641 #ifdef SDL_R_RESTART
642                                         // better not call R_Modules_Restart from here directly, as this may wreak havoc...
643                                         // so, let's better queue it for next frame
644                                         if(!sdl_needs_restart)
645                                         {
646                                                 Cbuf_AddText("\nr_restart\n");
647                                                 sdl_needs_restart = true;
648                                         }
649 #endif
650                                 }
651                                 break;
652                 }
653
654         // enable/disable sound on focus gain/loss
655         if ((!vid_hidden && vid_activewindow) || !snd_mutewhenidle.integer)
656         {
657                 if (!sound_active)
658                 {
659                         S_UnblockSound ();
660                         sound_active = true;
661                 }
662         }
663         else
664         {
665                 if (sound_active)
666                 {
667                         S_BlockSound ();
668                         sound_active = false;
669                 }
670         }
671 }
672
673 /////////////////
674 // Video system
675 ////
676
677 #ifdef __IPHONEOS__
678 //#include <SDL_opengles.h>
679 #include <OpenGLES/ES2/gl.h>
680
681 GLboolean wrapglIsBuffer(GLuint buffer) {return glIsBuffer(buffer);}
682 GLboolean wrapglIsEnabled(GLenum cap) {return glIsEnabled(cap);}
683 GLboolean wrapglIsFramebuffer(GLuint framebuffer) {return glIsFramebuffer(framebuffer);}
684 //GLboolean wrapglIsQuery(GLuint qid) {return glIsQuery(qid);}
685 GLboolean wrapglIsRenderbuffer(GLuint renderbuffer) {return glIsRenderbuffer(renderbuffer);}
686 //GLboolean wrapglUnmapBuffer(GLenum target) {return glUnmapBuffer(target);}
687 GLenum wrapglCheckFramebufferStatus(GLenum target) {return glCheckFramebufferStatus(target);}
688 GLenum wrapglGetError(void) {return glGetError();}
689 GLuint wrapglCreateProgram(void) {return glCreateProgram();}
690 GLuint wrapglCreateShader(GLenum shaderType) {return glCreateShader(shaderType);}
691 //GLuint wrapglGetHandle(GLenum pname) {return glGetHandle(pname);}
692 GLint wrapglGetAttribLocation(GLuint programObj, const GLchar *name) {return glGetAttribLocation(programObj, name);}
693 GLint wrapglGetUniformLocation(GLuint programObj, const GLchar *name) {return glGetUniformLocation(programObj, name);}
694 //GLvoid* wrapglMapBuffer(GLenum target, GLenum access) {return glMapBuffer(target, access);}
695 const GLubyte* wrapglGetString(GLenum name) {return glGetString(name);}
696 void wrapglActiveStencilFace(GLenum e) {Con_Printf("glActiveStencilFace(e)\n");}
697 void wrapglActiveTexture(GLenum e) {glActiveTexture(e);}
698 void wrapglAlphaFunc(GLenum func, GLclampf ref) {Con_Printf("glAlphaFunc(func, ref)\n");}
699 void wrapglArrayElement(GLint i) {Con_Printf("glArrayElement(i)\n");}
700 void wrapglAttachShader(GLuint containerObj, GLuint obj) {glAttachShader(containerObj, obj);}
701 void wrapglBegin(GLenum mode) {Con_Printf("glBegin(mode)\n");}
702 //void wrapglBeginQuery(GLenum target, GLuint qid) {glBeginQuery(target, qid);}
703 void wrapglBindAttribLocation(GLuint programObj, GLuint index, const GLchar *name) {glBindAttribLocation(programObj, index, name);}
704 void wrapglBindBuffer(GLenum target, GLuint buffer) {glBindBuffer(target, buffer);}
705 void wrapglBindFramebuffer(GLenum target, GLuint framebuffer) {glBindFramebuffer(target, framebuffer);}
706 void wrapglBindRenderbuffer(GLenum target, GLuint renderbuffer) {glBindRenderbuffer(target, renderbuffer);}
707 void wrapglBindTexture(GLenum target, GLuint texture) {glBindTexture(target, texture);}
708 void wrapglBlendEquation(GLenum e) {glBlendEquation(e);}
709 void wrapglBlendFunc(GLenum sfactor, GLenum dfactor) {glBlendFunc(sfactor, dfactor);}
710 void wrapglBufferData(GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage) {glBufferData(target, size, data, usage);}
711 void wrapglBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid *data) {glBufferSubData(target, offset, size, data);}
712 void wrapglClear(GLbitfield mask) {glClear(mask);}
713 void wrapglClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) {glClearColor(red, green, blue, alpha);}
714 void wrapglClearDepth(GLclampd depth) {glClearDepthf((float)depth);}
715 void wrapglClearStencil(GLint s) {glClearStencil(s);}
716 void wrapglClientActiveTexture(GLenum target) {Con_Printf("glClientActiveTexture(target)\n");}
717 void wrapglColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) {Con_Printf("glColor4f(red, green, blue, alpha)\n");}
718 void wrapglColor4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha) {Con_Printf("glColor4ub(red, green, blue, alpha)\n");}
719 void wrapglColorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) {glColorMask(red, green, blue, alpha);}
720 void wrapglColorPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr) {Con_Printf("glColorPointer(size, type, stride, ptr)\n");}
721 void wrapglCompileShader(GLuint shaderObj) {glCompileShader(shaderObj);}
722 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);}
723 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");}
724 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);}
725 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");}
726 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);}
727 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);}
728 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");}
729 void wrapglCullFace(GLenum mode) {glCullFace(mode);}
730 void wrapglDeleteBuffers(GLsizei n, const GLuint *buffers) {glDeleteBuffers(n, buffers);}
731 void wrapglDeleteFramebuffers(GLsizei n, const GLuint *framebuffers) {glDeleteFramebuffers(n, framebuffers);}
732 void wrapglDeleteShader(GLuint obj) {glDeleteShader(obj);}
733 void wrapglDeleteProgram(GLuint obj) {glDeleteProgram(obj);}
734 //void wrapglDeleteQueries(GLsizei n, const GLuint *ids) {glDeleteQueries(n, ids);}
735 void wrapglDeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers) {glDeleteRenderbuffers(n, renderbuffers);}
736 void wrapglDeleteTextures(GLsizei n, const GLuint *textures) {glDeleteTextures(n, textures);}
737 void wrapglDepthFunc(GLenum func) {glDepthFunc(func);}
738 void wrapglDepthMask(GLboolean flag) {glDepthMask(flag);}
739 void wrapglDepthRange(GLclampd near_val, GLclampd far_val) {glDepthRangef((float)near_val, (float)far_val);}
740 void wrapglDetachShader(GLuint containerObj, GLuint attachedObj) {glDetachShader(containerObj, attachedObj);}
741 void wrapglDisable(GLenum cap) {glDisable(cap);}
742 void wrapglDisableClientState(GLenum cap) {Con_Printf("glDisableClientState(cap)\n");}
743 void wrapglDisableVertexAttribArray(GLuint index) {glDisableVertexAttribArray(index);}
744 void wrapglDrawArrays(GLenum mode, GLint first, GLsizei count) {glDrawArrays(mode, first, count);}
745 void wrapglDrawBuffer(GLenum mode) {Con_Printf("glDrawBuffer(mode)\n");}
746 void wrapglDrawBuffers(GLsizei n, const GLenum *bufs) {Con_Printf("glDrawBuffers(n, bufs)\n");}
747 void wrapglDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices) {glDrawElements(mode, count, type, indices);}
748 //void wrapglDrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices) {glDrawRangeElements(mode, start, end, count, type, indices);}
749 //void wrapglDrawRangeElementsEXT(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices) {glDrawRangeElements(mode, start, end, count, type, indices);}
750 void wrapglEnable(GLenum cap) {glEnable(cap);}
751 void wrapglEnableClientState(GLenum cap) {Con_Printf("glEnableClientState(cap)\n");}
752 void wrapglEnableVertexAttribArray(GLuint index) {glEnableVertexAttribArray(index);}
753 void wrapglEnd(void) {Con_Printf("glEnd()\n");}
754 //void wrapglEndQuery(GLenum target) {glEndQuery(target);}
755 void wrapglFinish(void) {glFinish();}
756 void wrapglFlush(void) {glFlush();}
757 void wrapglFramebufferRenderbuffer(GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer) {glFramebufferRenderbuffer(target, attachment, renderbuffertarget, renderbuffer);}
758 void wrapglFramebufferTexture2D(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level) {glFramebufferTexture2D(target, attachment, textarget, texture, level);}
759 void wrapglFramebufferTexture3D(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level, GLint zoffset) {Con_Printf("glFramebufferTexture3D()\n");}
760 void wrapglGenBuffers(GLsizei n, GLuint *buffers) {glGenBuffers(n, buffers);}
761 void wrapglGenFramebuffers(GLsizei n, GLuint *framebuffers) {glGenFramebuffers(n, framebuffers);}
762 //void wrapglGenQueries(GLsizei n, GLuint *ids) {glGenQueries(n, ids);}
763 void wrapglGenRenderbuffers(GLsizei n, GLuint *renderbuffers) {glGenRenderbuffers(n, renderbuffers);}
764 void wrapglGenTextures(GLsizei n, GLuint *textures) {glGenTextures(n, textures);}
765 void wrapglGenerateMipmap(GLenum target) {glGenerateMipmap(target);}
766 void wrapglGetActiveAttrib(GLuint programObj, GLuint index, GLsizei maxLength, GLsizei *length, GLint *size, GLenum *type, GLchar *name) {glGetActiveAttrib(programObj, index, maxLength, length, size, type, name);}
767 void wrapglGetActiveUniform(GLuint programObj, GLuint index, GLsizei maxLength, GLsizei *length, GLint *size, GLenum *type, GLchar *name) {glGetActiveUniform(programObj, index, maxLength, length, size, type, name);}
768 void wrapglGetAttachedShaders(GLuint containerObj, GLsizei maxCount, GLsizei *count, GLuint *obj) {glGetAttachedShaders(containerObj, maxCount, count, obj);}
769 void wrapglGetBooleanv(GLenum pname, GLboolean *params) {glGetBooleanv(pname, params);}
770 void wrapglGetCompressedTexImage(GLenum target, GLint lod, void *img) {Con_Printf("glGetCompressedTexImage(target, lod, img)\n");}
771 void wrapglGetDoublev(GLenum pname, GLdouble *params) {Con_Printf("glGetDoublev(pname, params)\n");}
772 void wrapglGetFloatv(GLenum pname, GLfloat *params) {glGetFloatv(pname, params);}
773 void wrapglGetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment, GLenum pname, GLint *params) {glGetFramebufferAttachmentParameteriv(target, attachment, pname, params);}
774 void wrapglGetShaderInfoLog(GLuint obj, GLsizei maxLength, GLsizei *length, GLchar *infoLog) {glGetShaderInfoLog(obj, maxLength, length, infoLog);}
775 void wrapglGetProgramInfoLog(GLuint obj, GLsizei maxLength, GLsizei *length, GLchar *infoLog) {glGetProgramInfoLog(obj, maxLength, length, infoLog);}
776 void wrapglGetIntegerv(GLenum pname, GLint *params) {glGetIntegerv(pname, params);}
777 void wrapglGetShaderiv(GLuint obj, GLenum pname, GLint *params) {glGetShaderiv(obj, pname, params);}
778 void wrapglGetProgramiv(GLuint obj, GLenum pname, GLint *params) {glGetProgramiv(obj, pname, params);}
779 //void wrapglGetQueryObjectiv(GLuint qid, GLenum pname, GLint *params) {glGetQueryObjectiv(qid, pname, params);}
780 //void wrapglGetQueryObjectuiv(GLuint qid, GLenum pname, GLuint *params) {glGetQueryObjectuiv(qid, pname, params);}
781 //void wrapglGetQueryiv(GLenum target, GLenum pname, GLint *params) {glGetQueryiv(target, pname, params);}
782 void wrapglGetRenderbufferParameteriv(GLenum target, GLenum pname, GLint *params) {glGetRenderbufferParameteriv(target, pname, params);}
783 void wrapglGetShaderSource(GLuint obj, GLsizei maxLength, GLsizei *length, GLchar *source) {glGetShaderSource(obj, maxLength, length, source);}
784 void wrapglGetTexImage(GLenum target, GLint level, GLenum format, GLenum type, GLvoid *pixels) {Con_Printf("glGetTexImage(target, level, format, type, pixels)\n");}
785 void wrapglGetTexLevelParameterfv(GLenum target, GLint level, GLenum pname, GLfloat *params) {Con_Printf("glGetTexLevelParameterfv(target, level, pname, params)\n");}
786 void wrapglGetTexLevelParameteriv(GLenum target, GLint level, GLenum pname, GLint *params) {Con_Printf("glGetTexLevelParameteriv(target, level, pname, params)\n");}
787 void wrapglGetTexParameterfv(GLenum target, GLenum pname, GLfloat *params) {glGetTexParameterfv(target, pname, params);}
788 void wrapglGetTexParameteriv(GLenum target, GLenum pname, GLint *params) {glGetTexParameteriv(target, pname, params);}
789 void wrapglGetUniformfv(GLuint programObj, GLint location, GLfloat *params) {glGetUniformfv(programObj, location, params);}
790 void wrapglGetUniformiv(GLuint programObj, GLint location, GLint *params) {glGetUniformiv(programObj, location, params);}
791 void wrapglHint(GLenum target, GLenum mode) {glHint(target, mode);}
792 void wrapglLineWidth(GLfloat width) {glLineWidth(width);}
793 void wrapglLinkProgram(GLuint programObj) {glLinkProgram(programObj);}
794 void wrapglLoadIdentity(void) {Con_Printf("glLoadIdentity()\n");}
795 void wrapglLoadMatrixf(const GLfloat *m) {Con_Printf("glLoadMatrixf(m)\n");}
796 void wrapglMatrixMode(GLenum mode) {Con_Printf("glMatrixMode(mode)\n");}
797 void wrapglMultiTexCoord1f(GLenum target, GLfloat s) {Con_Printf("glMultiTexCoord1f(target, s)\n");}
798 void wrapglMultiTexCoord2f(GLenum target, GLfloat s, GLfloat t) {Con_Printf("glMultiTexCoord2f(target, s, t)\n");}
799 void wrapglMultiTexCoord3f(GLenum target, GLfloat s, GLfloat t, GLfloat r) {Con_Printf("glMultiTexCoord3f(target, s, t, r)\n");}
800 void wrapglMultiTexCoord4f(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q) {Con_Printf("glMultiTexCoord4f(target, s, t, r, q)\n");}
801 void wrapglNormalPointer(GLenum type, GLsizei stride, const GLvoid *ptr) {Con_Printf("glNormalPointer(type, stride, ptr)\n");}
802 void wrapglPixelStorei(GLenum pname, GLint param) {glPixelStorei(pname, param);}
803 void wrapglPointSize(GLfloat size) {Con_Printf("glPointSize(size)\n");}
804 void wrapglPolygonMode(GLenum face, GLenum mode) {Con_Printf("glPolygonMode(face, mode)\n");}
805 void wrapglPolygonOffset(GLfloat factor, GLfloat units) {glPolygonOffset(factor, units);}
806 void wrapglPolygonStipple(const GLubyte *mask) {Con_Printf("glPolygonStipple(mask)\n");}
807 void wrapglReadBuffer(GLenum mode) {Con_Printf("glReadBuffer(mode)\n");}
808 void wrapglReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels) {glReadPixels(x, y, width, height, format, type, pixels);}
809 void wrapglRenderbufferStorage(GLenum target, GLenum internalformat, GLsizei width, GLsizei height) {glRenderbufferStorage(target, internalformat, width, height);}
810 void wrapglScissor(GLint x, GLint y, GLsizei width, GLsizei height) {glScissor(x, y, width, height);}
811 void wrapglShaderSource(GLuint shaderObj, GLsizei count, const GLchar **string, const GLint *length) {glShaderSource(shaderObj, count, string, length);}
812 void wrapglStencilFunc(GLenum func, GLint ref, GLuint mask) {glStencilFunc(func, ref, mask);}
813 void wrapglStencilFuncSeparate(GLenum func1, GLenum func2, GLint ref, GLuint mask) {Con_Printf("glStencilFuncSeparate(func1, func2, ref, mask)\n");}
814 void wrapglStencilMask(GLuint mask) {glStencilMask(mask);}
815 void wrapglStencilOp(GLenum fail, GLenum zfail, GLenum zpass) {glStencilOp(fail, zfail, zpass);}
816 void wrapglStencilOpSeparate(GLenum e1, GLenum e2, GLenum e3, GLenum e4) {Con_Printf("glStencilOpSeparate(e1, e2, e3, e4)\n");}
817 void wrapglTexCoord1f(GLfloat s) {Con_Printf("glTexCoord1f(s)\n");}
818 void wrapglTexCoord2f(GLfloat s, GLfloat t) {Con_Printf("glTexCoord2f(s, t)\n");}
819 void wrapglTexCoord3f(GLfloat s, GLfloat t, GLfloat r) {Con_Printf("glTexCoord3f(s, t, r)\n");}
820 void wrapglTexCoord4f(GLfloat s, GLfloat t, GLfloat r, GLfloat q) {Con_Printf("glTexCoord4f(s, t, r, q)\n");}
821 void wrapglTexCoordPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr) {Con_Printf("glTexCoordPointer(size, type, stride, ptr)\n");}
822 void wrapglTexEnvf(GLenum target, GLenum pname, GLfloat param) {Con_Printf("glTexEnvf(target, pname, param)\n");}
823 void wrapglTexEnvfv(GLenum target, GLenum pname, const GLfloat *params) {Con_Printf("glTexEnvfv(target, pname, params)\n");}
824 void wrapglTexEnvi(GLenum target, GLenum pname, GLint param) {Con_Printf("glTexEnvi(target, pname, param)\n");}
825 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);}
826 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");}
827 void wrapglTexParameterf(GLenum target, GLenum pname, GLfloat param) {glTexParameterf(target, pname, param);}
828 void wrapglTexParameterfv(GLenum target, GLenum pname, GLfloat *params) {glTexParameterfv(target, pname, params);}
829 void wrapglTexParameteri(GLenum target, GLenum pname, GLint param) {glTexParameteri(target, pname, param);}
830 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);}
831 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");}
832 void wrapglUniform1f(GLint location, GLfloat v0) {glUniform1f(location, v0);}
833 void wrapglUniform1fv(GLint location, GLsizei count, const GLfloat *value) {glUniform1fv(location, count, value);}
834 void wrapglUniform1i(GLint location, GLint v0) {glUniform1i(location, v0);}
835 void wrapglUniform1iv(GLint location, GLsizei count, const GLint *value) {glUniform1iv(location, count, value);}
836 void wrapglUniform2f(GLint location, GLfloat v0, GLfloat v1) {glUniform2f(location, v0, v1);}
837 void wrapglUniform2fv(GLint location, GLsizei count, const GLfloat *value) {glUniform2fv(location, count, value);}
838 void wrapglUniform2i(GLint location, GLint v0, GLint v1) {glUniform2i(location, v0, v1);}
839 void wrapglUniform2iv(GLint location, GLsizei count, const GLint *value) {glUniform2iv(location, count, value);}
840 void wrapglUniform3f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2) {glUniform3f(location, v0, v1, v2);}
841 void wrapglUniform3fv(GLint location, GLsizei count, const GLfloat *value) {glUniform3fv(location, count, value);}
842 void wrapglUniform3i(GLint location, GLint v0, GLint v1, GLint v2) {glUniform3i(location, v0, v1, v2);}
843 void wrapglUniform3iv(GLint location, GLsizei count, const GLint *value) {glUniform3iv(location, count, value);}
844 void wrapglUniform4f(GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) {glUniform4f(location, v0, v1, v2, v3);}
845 void wrapglUniform4fv(GLint location, GLsizei count, const GLfloat *value) {glUniform4fv(location, count, value);}
846 void wrapglUniform4i(GLint location, GLint v0, GLint v1, GLint v2, GLint v3) {glUniform4i(location, v0, v1, v2, v3);}
847 void wrapglUniform4iv(GLint location, GLsizei count, const GLint *value) {glUniform4iv(location, count, value);}
848 void wrapglUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) {glUniformMatrix2fv(location, count, transpose, value);}
849 void wrapglUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) {glUniformMatrix3fv(location, count, transpose, value);}
850 void wrapglUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) {glUniformMatrix4fv(location, count, transpose, value);}
851 void wrapglUseProgram(GLuint programObj) {glUseProgram(programObj);}
852 void wrapglValidateProgram(GLuint programObj) {glValidateProgram(programObj);}
853 void wrapglVertex2f(GLfloat x, GLfloat y) {Con_Printf("glVertex2f(x, y)\n");}
854 void wrapglVertex3f(GLfloat x, GLfloat y, GLfloat z) {Con_Printf("glVertex3f(x, y, z)\n");}
855 void wrapglVertex4f(GLfloat x, GLfloat y, GLfloat z, GLfloat w) {Con_Printf("glVertex4f(x, y, z, w)\n");}
856 void wrapglVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer) {glVertexAttribPointer(index, size, type, normalized, stride, pointer);}
857 void wrapglVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr) {Con_Printf("glVertexPointer(size, type, stride, ptr)\n");}
858 void wrapglViewport(GLint x, GLint y, GLsizei width, GLsizei height) {glViewport(x, y, width, height);}
859 void wrapglVertexAttrib1f(GLuint index, GLfloat v0) {glVertexAttrib1f(index, v0);}
860 //void wrapglVertexAttrib1s(GLuint index, GLshort v0) {glVertexAttrib1s(index, v0);}
861 //void wrapglVertexAttrib1d(GLuint index, GLdouble v0) {glVertexAttrib1d(index, v0);}
862 void wrapglVertexAttrib2f(GLuint index, GLfloat v0, GLfloat v1) {glVertexAttrib2f(index, v0, v1);}
863 //void wrapglVertexAttrib2s(GLuint index, GLshort v0, GLshort v1) {glVertexAttrib2s(index, v0, v1);}
864 //void wrapglVertexAttrib2d(GLuint index, GLdouble v0, GLdouble v1) {glVertexAttrib2d(index, v0, v1);}
865 void wrapglVertexAttrib3f(GLuint index, GLfloat v0, GLfloat v1, GLfloat v2) {glVertexAttrib3f(index, v0, v1, v2);}
866 //void wrapglVertexAttrib3s(GLuint index, GLshort v0, GLshort v1, GLshort v2) {glVertexAttrib3s(index, v0, v1, v2);}
867 //void wrapglVertexAttrib3d(GLuint index, GLdouble v0, GLdouble v1, GLdouble v2) {glVertexAttrib3d(index, v0, v1, v2);}
868 void wrapglVertexAttrib4f(GLuint index, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) {glVertexAttrib4f(index, v0, v1, v2, v3);}
869 //void wrapglVertexAttrib4s(GLuint index, GLshort v0, GLshort v1, GLshort v2, GLshort v3) {glVertexAttrib4s(index, v0, v1, v2, v3);}
870 //void wrapglVertexAttrib4d(GLuint index, GLdouble v0, GLdouble v1, GLdouble v2, GLdouble v3) {glVertexAttrib4d(index, v0, v1, v2, v3);}
871 //void wrapglVertexAttrib4Nub(GLuint index, GLubyte x, GLubyte y, GLubyte z, GLubyte w) {glVertexAttrib4Nub(index, x, y, z, w);}
872 void wrapglVertexAttrib1fv(GLuint index, const GLfloat *v) {glVertexAttrib1fv(index, v);}
873 //void wrapglVertexAttrib1sv(GLuint index, const GLshort *v) {glVertexAttrib1sv(index, v);}
874 //void wrapglVertexAttrib1dv(GLuint index, const GLdouble *v) {glVertexAttrib1dv(index, v);}
875 void wrapglVertexAttrib2fv(GLuint index, const GLfloat *v) {glVertexAttrib2fv(index, v);}
876 //void wrapglVertexAttrib2sv(GLuint index, const GLshort *v) {glVertexAttrib2sv(index, v);}
877 //void wrapglVertexAttrib2dv(GLuint index, const GLdouble *v) {glVertexAttrib2dv(index, v);}
878 void wrapglVertexAttrib3fv(GLuint index, const GLfloat *v) {glVertexAttrib3fv(index, v);}
879 //void wrapglVertexAttrib3sv(GLuint index, const GLshort *v) {glVertexAttrib3sv(index, v);}
880 //void wrapglVertexAttrib3dv(GLuint index, const GLdouble *v) {glVertexAttrib3dv(index, v);}
881 void wrapglVertexAttrib4fv(GLuint index, const GLfloat *v) {glVertexAttrib4fv(index, v);}
882 //void wrapglVertexAttrib4sv(GLuint index, const GLshort *v) {glVertexAttrib4sv(index, v);}
883 //void wrapglVertexAttrib4dv(GLuint index, const GLdouble *v) {glVertexAttrib4dv(index, v);}
884 //void wrapglVertexAttrib4iv(GLuint index, const GLint *v) {glVertexAttrib4iv(index, v);}
885 //void wrapglVertexAttrib4bv(GLuint index, const GLbyte *v) {glVertexAttrib4bv(index, v);}
886 //void wrapglVertexAttrib4ubv(GLuint index, const GLubyte *v) {glVertexAttrib4ubv(index, v);}
887 //void wrapglVertexAttrib4usv(GLuint index, const GLushort *v) {glVertexAttrib4usv(index, GLushort v);}
888 //void wrapglVertexAttrib4uiv(GLuint index, const GLuint *v) {glVertexAttrib4uiv(index, v);}
889 //void wrapglVertexAttrib4Nbv(GLuint index, const GLbyte *v) {glVertexAttrib4Nbv(index, v);}
890 //void wrapglVertexAttrib4Nsv(GLuint index, const GLshort *v) {glVertexAttrib4Nsv(index, v);}
891 //void wrapglVertexAttrib4Niv(GLuint index, const GLint *v) {glVertexAttrib4Niv(index, v);}
892 //void wrapglVertexAttrib4Nubv(GLuint index, const GLubyte *v) {glVertexAttrib4Nubv(index, v);}
893 //void wrapglVertexAttrib4Nusv(GLuint index, const GLushort *v) {glVertexAttrib4Nusv(index, GLushort v);}
894 //void wrapglVertexAttrib4Nuiv(GLuint index, const GLuint *v) {glVertexAttrib4Nuiv(index, v);}
895 //void wrapglGetVertexAttribdv(GLuint index, GLenum pname, GLdouble *params) {glGetVertexAttribdv(index, pname, params);}
896 void wrapglGetVertexAttribfv(GLuint index, GLenum pname, GLfloat *params) {glGetVertexAttribfv(index, pname, params);}
897 void wrapglGetVertexAttribiv(GLuint index, GLenum pname, GLint *params) {glGetVertexAttribiv(index, pname, params);}
898 void wrapglGetVertexAttribPointerv(GLuint index, GLenum pname, GLvoid **pointer) {glGetVertexAttribPointerv(index, pname, pointer);}
899
900 void GLES_Init(void)
901 {
902         qglIsBufferARB = wrapglIsBuffer;
903         qglIsEnabled = wrapglIsEnabled;
904         qglIsFramebufferEXT = wrapglIsFramebuffer;
905 //      qglIsQueryARB = wrapglIsQuery;
906         qglIsRenderbufferEXT = wrapglIsRenderbuffer;
907 //      qglUnmapBufferARB = wrapglUnmapBuffer;
908         qglCheckFramebufferStatusEXT = wrapglCheckFramebufferStatus;
909         qglGetError = wrapglGetError;
910         qglCreateProgram = wrapglCreateProgram;
911         qglCreateShader = wrapglCreateShader;
912 //      qglGetHandleARB = wrapglGetHandle;
913         qglGetAttribLocation = wrapglGetAttribLocation;
914         qglGetUniformLocation = wrapglGetUniformLocation;
915 //      qglMapBufferARB = wrapglMapBuffer;
916         qglGetString = wrapglGetString;
917 //      qglActiveStencilFaceEXT = wrapglActiveStencilFace;
918         qglActiveTexture = wrapglActiveTexture;
919         qglAlphaFunc = wrapglAlphaFunc;
920         qglArrayElement = wrapglArrayElement;
921         qglAttachShader = wrapglAttachShader;
922         qglBegin = wrapglBegin;
923 //      qglBeginQueryARB = wrapglBeginQuery;
924         qglBindAttribLocation = wrapglBindAttribLocation;
925         qglBindBufferARB = wrapglBindBuffer;
926         qglBindFramebufferEXT = wrapglBindFramebuffer;
927         qglBindRenderbufferEXT = wrapglBindRenderbuffer;
928         qglBindTexture = wrapglBindTexture;
929         qglBlendEquationEXT = wrapglBlendEquation;
930         qglBlendFunc = wrapglBlendFunc;
931         qglBufferDataARB = wrapglBufferData;
932         qglBufferSubDataARB = wrapglBufferSubData;
933         qglClear = wrapglClear;
934         qglClearColor = wrapglClearColor;
935         qglClearDepth = wrapglClearDepth;
936         qglClearStencil = wrapglClearStencil;
937         qglClientActiveTexture = wrapglClientActiveTexture;
938         qglColor4f = wrapglColor4f;
939         qglColor4ub = wrapglColor4ub;
940         qglColorMask = wrapglColorMask;
941         qglColorPointer = wrapglColorPointer;
942         qglCompileShader = wrapglCompileShader;
943         qglCompressedTexImage2DARB = wrapglCompressedTexImage2D;
944         qglCompressedTexImage3DARB = wrapglCompressedTexImage3D;
945         qglCompressedTexSubImage2DARB = wrapglCompressedTexSubImage2D;
946         qglCompressedTexSubImage3DARB = wrapglCompressedTexSubImage3D;
947         qglCopyTexImage2D = wrapglCopyTexImage2D;
948         qglCopyTexSubImage2D = wrapglCopyTexSubImage2D;
949         qglCopyTexSubImage3D = wrapglCopyTexSubImage3D;
950         qglCullFace = wrapglCullFace;
951         qglDeleteBuffersARB = wrapglDeleteBuffers;
952         qglDeleteFramebuffersEXT = wrapglDeleteFramebuffers;
953         qglDeleteProgram = wrapglDeleteProgram;
954         qglDeleteShader = wrapglDeleteShader;
955 //      qglDeleteQueriesARB = wrapglDeleteQueries;
956         qglDeleteRenderbuffersEXT = wrapglDeleteRenderbuffers;
957         qglDeleteTextures = wrapglDeleteTextures;
958         qglDepthFunc = wrapglDepthFunc;
959         qglDepthMask = wrapglDepthMask;
960         qglDepthRange = wrapglDepthRange;
961         qglDetachShader = wrapglDetachShader;
962         qglDisable = wrapglDisable;
963         qglDisableClientState = wrapglDisableClientState;
964         qglDisableVertexAttribArray = wrapglDisableVertexAttribArray;
965         qglDrawArrays = wrapglDrawArrays;
966 //      qglDrawBuffer = wrapglDrawBuffer;
967 //      qglDrawBuffersARB = wrapglDrawBuffers;
968         qglDrawElements = wrapglDrawElements;
969 //      qglDrawRangeElements = wrapglDrawRangeElements;
970         qglEnable = wrapglEnable;
971         qglEnableClientState = wrapglEnableClientState;
972         qglEnableVertexAttribArray = wrapglEnableVertexAttribArray;
973         qglEnd = wrapglEnd;
974 //      qglEndQueryARB = wrapglEndQuery;
975         qglFinish = wrapglFinish;
976         qglFlush = wrapglFlush;
977         qglFramebufferRenderbufferEXT = wrapglFramebufferRenderbuffer;
978         qglFramebufferTexture2DEXT = wrapglFramebufferTexture2D;
979         qglFramebufferTexture3DEXT = wrapglFramebufferTexture3D;
980         qglGenBuffersARB = wrapglGenBuffers;
981         qglGenFramebuffersEXT = wrapglGenFramebuffers;
982 //      qglGenQueriesARB = wrapglGenQueries;
983         qglGenRenderbuffersEXT = wrapglGenRenderbuffers;
984         qglGenTextures = wrapglGenTextures;
985         qglGenerateMipmapEXT = wrapglGenerateMipmap;
986         qglGetActiveAttrib = wrapglGetActiveAttrib;
987         qglGetActiveUniform = wrapglGetActiveUniform;
988         qglGetAttachedShaders = wrapglGetAttachedShaders;
989         qglGetBooleanv = wrapglGetBooleanv;
990         qglGetCompressedTexImageARB = wrapglGetCompressedTexImage;
991         qglGetDoublev = wrapglGetDoublev;
992         qglGetFloatv = wrapglGetFloatv;
993         qglGetFramebufferAttachmentParameterivEXT = wrapglGetFramebufferAttachmentParameteriv;
994         qglGetProgramInfoLog = wrapglGetProgramInfoLog;
995         qglGetShaderInfoLog = wrapglGetShaderInfoLog;
996         qglGetIntegerv = wrapglGetIntegerv;
997         qglGetShaderiv = wrapglGetShaderiv;
998         qglGetProgramiv = wrapglGetProgramiv;
999 //      qglGetQueryObjectivARB = wrapglGetQueryObjectiv;
1000 //      qglGetQueryObjectuivARB = wrapglGetQueryObjectuiv;
1001 //      qglGetQueryivARB = wrapglGetQueryiv;
1002         qglGetRenderbufferParameterivEXT = wrapglGetRenderbufferParameteriv;
1003         qglGetShaderSource = wrapglGetShaderSource;
1004         qglGetTexImage = wrapglGetTexImage;
1005         qglGetTexLevelParameterfv = wrapglGetTexLevelParameterfv;
1006         qglGetTexLevelParameteriv = wrapglGetTexLevelParameteriv;
1007         qglGetTexParameterfv = wrapglGetTexParameterfv;
1008         qglGetTexParameteriv = wrapglGetTexParameteriv;
1009         qglGetUniformfv = wrapglGetUniformfv;
1010         qglGetUniformiv = wrapglGetUniformiv;
1011         qglHint = wrapglHint;
1012         qglLineWidth = wrapglLineWidth;
1013         qglLinkProgram = wrapglLinkProgram;
1014         qglLoadIdentity = wrapglLoadIdentity;
1015         qglLoadMatrixf = wrapglLoadMatrixf;
1016         qglMatrixMode = wrapglMatrixMode;
1017         qglMultiTexCoord1f = wrapglMultiTexCoord1f;
1018         qglMultiTexCoord2f = wrapglMultiTexCoord2f;
1019         qglMultiTexCoord3f = wrapglMultiTexCoord3f;
1020         qglMultiTexCoord4f = wrapglMultiTexCoord4f;
1021         qglNormalPointer = wrapglNormalPointer;
1022         qglPixelStorei = wrapglPixelStorei;
1023         qglPointSize = wrapglPointSize;
1024         qglPolygonMode = wrapglPolygonMode;
1025         qglPolygonOffset = wrapglPolygonOffset;
1026         qglPolygonStipple = wrapglPolygonStipple;
1027         qglReadBuffer = wrapglReadBuffer;
1028         qglReadPixels = wrapglReadPixels;
1029         qglRenderbufferStorageEXT = wrapglRenderbufferStorage;
1030         qglScissor = wrapglScissor;
1031         qglShaderSource = wrapglShaderSource;
1032         qglStencilFunc = wrapglStencilFunc;
1033         qglStencilFuncSeparate = wrapglStencilFuncSeparate;
1034         qglStencilMask = wrapglStencilMask;
1035         qglStencilOp = wrapglStencilOp;
1036         qglStencilOpSeparate = wrapglStencilOpSeparate;
1037         qglTexCoord1f = wrapglTexCoord1f;
1038         qglTexCoord2f = wrapglTexCoord2f;
1039         qglTexCoord3f = wrapglTexCoord3f;
1040         qglTexCoord4f = wrapglTexCoord4f;
1041         qglTexCoordPointer = wrapglTexCoordPointer;
1042         qglTexEnvf = wrapglTexEnvf;
1043         qglTexEnvfv = wrapglTexEnvfv;
1044         qglTexEnvi = wrapglTexEnvi;
1045         qglTexImage2D = wrapglTexImage2D;
1046         qglTexImage3D = wrapglTexImage3D;
1047         qglTexParameterf = wrapglTexParameterf;
1048         qglTexParameterfv = wrapglTexParameterfv;
1049         qglTexParameteri = wrapglTexParameteri;
1050         qglTexSubImage2D = wrapglTexSubImage2D;
1051         qglTexSubImage3D = wrapglTexSubImage3D;
1052         qglUniform1f = wrapglUniform1f;
1053         qglUniform1fv = wrapglUniform1fv;
1054         qglUniform1i = wrapglUniform1i;
1055         qglUniform1iv = wrapglUniform1iv;
1056         qglUniform2f = wrapglUniform2f;
1057         qglUniform2fv = wrapglUniform2fv;
1058         qglUniform2i = wrapglUniform2i;
1059         qglUniform2iv = wrapglUniform2iv;
1060         qglUniform3f = wrapglUniform3f;
1061         qglUniform3fv = wrapglUniform3fv;
1062         qglUniform3i = wrapglUniform3i;
1063         qglUniform3iv = wrapglUniform3iv;
1064         qglUniform4f = wrapglUniform4f;
1065         qglUniform4fv = wrapglUniform4fv;
1066         qglUniform4i = wrapglUniform4i;
1067         qglUniform4iv = wrapglUniform4iv;
1068         qglUniformMatrix2fv = wrapglUniformMatrix2fv;
1069         qglUniformMatrix3fv = wrapglUniformMatrix3fv;
1070         qglUniformMatrix4fv = wrapglUniformMatrix4fv;
1071         qglUseProgram = wrapglUseProgram;
1072         qglValidateProgram = wrapglValidateProgram;
1073         qglVertex2f = wrapglVertex2f;
1074         qglVertex3f = wrapglVertex3f;
1075         qglVertex4f = wrapglVertex4f;
1076         qglVertexAttribPointer = wrapglVertexAttribPointer;
1077         qglVertexPointer = wrapglVertexPointer;
1078         qglViewport = wrapglViewport;
1079         qglVertexAttrib1f = wrapglVertexAttrib1f;
1080 //      qglVertexAttrib1s = wrapglVertexAttrib1s;
1081 //      qglVertexAttrib1d = wrapglVertexAttrib1d;
1082         qglVertexAttrib2f = wrapglVertexAttrib2f;
1083 //      qglVertexAttrib2s = wrapglVertexAttrib2s;
1084 //      qglVertexAttrib2d = wrapglVertexAttrib2d;
1085         qglVertexAttrib3f = wrapglVertexAttrib3f;
1086 //      qglVertexAttrib3s = wrapglVertexAttrib3s;
1087 //      qglVertexAttrib3d = wrapglVertexAttrib3d;
1088         qglVertexAttrib4f = wrapglVertexAttrib4f;
1089 //      qglVertexAttrib4s = wrapglVertexAttrib4s;
1090 //      qglVertexAttrib4d = wrapglVertexAttrib4d;
1091 //      qglVertexAttrib4Nub = wrapglVertexAttrib4Nub;
1092         qglVertexAttrib1fv = wrapglVertexAttrib1fv;
1093 //      qglVertexAttrib1sv = wrapglVertexAttrib1sv;
1094 //      qglVertexAttrib1dv = wrapglVertexAttrib1dv;
1095         qglVertexAttrib2fv = wrapglVertexAttrib2fv;
1096 //      qglVertexAttrib2sv = wrapglVertexAttrib2sv;
1097 //      qglVertexAttrib2dv = wrapglVertexAttrib2dv;
1098         qglVertexAttrib3fv = wrapglVertexAttrib3fv;
1099 //      qglVertexAttrib3sv = wrapglVertexAttrib3sv;
1100 //      qglVertexAttrib3dv = wrapglVertexAttrib3dv;
1101         qglVertexAttrib4fv = wrapglVertexAttrib4fv;
1102 //      qglVertexAttrib4sv = wrapglVertexAttrib4sv;
1103 //      qglVertexAttrib4dv = wrapglVertexAttrib4dv;
1104 //      qglVertexAttrib4iv = wrapglVertexAttrib4iv;
1105 //      qglVertexAttrib4bv = wrapglVertexAttrib4bv;
1106 //      qglVertexAttrib4ubv = wrapglVertexAttrib4ubv;
1107 //      qglVertexAttrib4usv = wrapglVertexAttrib4usv;
1108 //      qglVertexAttrib4uiv = wrapglVertexAttrib4uiv;
1109 //      qglVertexAttrib4Nbv = wrapglVertexAttrib4Nbv;
1110 //      qglVertexAttrib4Nsv = wrapglVertexAttrib4Nsv;
1111 //      qglVertexAttrib4Niv = wrapglVertexAttrib4Niv;
1112 //      qglVertexAttrib4Nubv = wrapglVertexAttrib4Nubv;
1113 //      qglVertexAttrib4Nusv = wrapglVertexAttrib4Nusv;
1114 //      qglVertexAttrib4Nuiv = wrapglVertexAttrib4Nuiv;
1115 //      qglGetVertexAttribdv = wrapglGetVertexAttribdv;
1116         qglGetVertexAttribfv = wrapglGetVertexAttribfv;
1117         qglGetVertexAttribiv = wrapglGetVertexAttribiv;
1118         qglGetVertexAttribPointerv = wrapglGetVertexAttribPointerv;
1119
1120         gl_renderer = (const char *)qglGetString(GL_RENDERER);
1121         gl_vendor = (const char *)qglGetString(GL_VENDOR);
1122         gl_version = (const char *)qglGetString(GL_VERSION);
1123         gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
1124         
1125         if (!gl_extensions)
1126                 gl_extensions = "";
1127         if (!gl_platformextensions)
1128                 gl_platformextensions = "";
1129         
1130         Con_Printf("GL_VENDOR: %s\n", gl_vendor);
1131         Con_Printf("GL_RENDERER: %s\n", gl_renderer);
1132         Con_Printf("GL_VERSION: %s\n", gl_version);
1133         Con_DPrintf("GL_EXTENSIONS: %s\n", gl_extensions);
1134         Con_DPrintf("%s_EXTENSIONS: %s\n", gl_platform, gl_platformextensions);
1135         
1136         // LordHavoc: report supported extensions
1137         Con_DPrintf("\nQuakeC extensions for server and client: %s\nQuakeC extensions for menu: %s\n", vm_sv_extensions, vm_m_extensions );
1138         
1139         vid.support.gl20shaders = true;
1140         vid.support.amd_texture_texture4 = false;
1141         vid.support.arb_depth_texture = false;
1142         vid.support.arb_draw_buffers = false;
1143         vid.support.arb_multitexture = false;
1144         vid.support.arb_occlusion_query = false;
1145         vid.support.arb_shadow = false;
1146         vid.support.arb_texture_compression = false; // different (vendor-specific) formats than on desktop OpenGL...
1147         vid.support.arb_texture_cube_map = true;
1148         vid.support.arb_texture_env_combine = false;
1149         vid.support.arb_texture_gather = false;
1150         vid.support.arb_texture_non_power_of_two = strstr(gl_extensions, "GL_OES_texture_npot") != NULL;
1151         vid.support.arb_vertex_buffer_object = true;
1152         vid.support.ati_separate_stencil = false;
1153         vid.support.ext_blend_minmax = false;
1154         vid.support.ext_blend_subtract = true;
1155         vid.support.ext_draw_range_elements = true;
1156         vid.support.ext_framebuffer_object = true;
1157         vid.support.ext_stencil_two_side = false;
1158         vid.support.ext_texture_3d = false;//SDL_GL_ExtensionSupported("GL_OES_texture_3D"); // iPhoneOS does not support 3D textures, odd...
1159         vid.support.ext_texture_compression_s3tc = false;
1160         vid.support.ext_texture_edge_clamp = true;
1161         vid.support.ext_texture_filter_anisotropic = false; // probably don't want to use it...
1162
1163         qglGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint*)&vid.maxtexturesize_2d);
1164         if (vid.support.ext_texture_filter_anisotropic)
1165                 qglGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, (GLint*)&vid.max_anisotropy);
1166         if (vid.support.arb_texture_cube_map)
1167                 qglGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB, (GLint*)&vid.maxtexturesize_cubemap);
1168         if (vid.support.ext_texture_3d)
1169                 qglGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, (GLint*)&vid.maxtexturesize_3d);
1170         Con_Printf("GL_MAX_CUBE_MAP_TEXTURE_SIZE = %i\n", vid.maxtexturesize_cubemap);
1171         Con_Printf("GL_MAX_3D_TEXTURE_SIZE = %i\n", vid.maxtexturesize_3d);
1172
1173         // verify that cubemap textures are really supported
1174         if (vid.support.arb_texture_cube_map && vid.maxtexturesize_cubemap < 256)
1175                 vid.support.arb_texture_cube_map = false;
1176         
1177         // verify that 3d textures are really supported
1178         if (vid.support.ext_texture_3d && vid.maxtexturesize_3d < 32)
1179         {
1180                 vid.support.ext_texture_3d = false;
1181                 Con_Printf("GL_OES_texture_3d reported bogus GL_MAX_3D_TEXTURE_SIZE, disabled\n");
1182         }
1183
1184         vid.texunits = 4;
1185         vid.teximageunits = 8;
1186         vid.texarrayunits = 5;
1187         vid.texunits = bound(1, vid.texunits, MAX_TEXTUREUNITS);
1188         vid.teximageunits = bound(1, vid.teximageunits, MAX_TEXTUREUNITS);
1189         vid.texarrayunits = bound(1, vid.texarrayunits, MAX_TEXTUREUNITS);
1190         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" : "");
1191         vid.renderpath = RENDERPATH_GLES2;
1192         vid.useinterleavedarrays = false;
1193
1194         // VorteX: set other info (maybe place them in VID_InitMode?)
1195         extern cvar_t gl_info_vendor;
1196         extern cvar_t gl_info_renderer;
1197         extern cvar_t gl_info_version;
1198         extern cvar_t gl_info_platform;
1199         extern cvar_t gl_info_driver;
1200         Cvar_SetQuick(&gl_info_vendor, gl_vendor);
1201         Cvar_SetQuick(&gl_info_renderer, gl_renderer);
1202         Cvar_SetQuick(&gl_info_version, gl_version);
1203         Cvar_SetQuick(&gl_info_platform, gl_platform ? gl_platform : "");
1204         Cvar_SetQuick(&gl_info_driver, gl_driver);
1205 }
1206 #endif
1207
1208 void *GL_GetProcAddress(const char *name)
1209 {
1210         void *p = NULL;
1211         p = SDL_GL_GetProcAddress(name);
1212         return p;
1213 }
1214
1215 #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION == 2
1216 static int Sys_EventFilter( SDL_Event *event );
1217 #endif
1218 static qboolean vid_sdl_initjoysticksystem = false;
1219
1220 void VID_Init (void)
1221 {
1222 #ifndef __IPHONEOS__
1223 #ifdef MACOSX
1224         Cvar_RegisterVariable(&apple_mouse_noaccel);
1225 #endif
1226 #endif
1227         Cvar_RegisterVariable(&vid_soft);
1228         Cvar_RegisterVariable(&vid_soft_threads);
1229         Cvar_RegisterVariable(&vid_soft_interlace);
1230         Cvar_RegisterVariable(&joy_detected);
1231         Cvar_RegisterVariable(&joy_enable);
1232         Cvar_RegisterVariable(&joy_index);
1233         Cvar_RegisterVariable(&joy_axisforward);
1234         Cvar_RegisterVariable(&joy_axisside);
1235         Cvar_RegisterVariable(&joy_axisup);
1236         Cvar_RegisterVariable(&joy_axispitch);
1237         Cvar_RegisterVariable(&joy_axisyaw);
1238         //Cvar_RegisterVariable(&joy_axisroll);
1239         Cvar_RegisterVariable(&joy_deadzoneforward);
1240         Cvar_RegisterVariable(&joy_deadzoneside);
1241         Cvar_RegisterVariable(&joy_deadzoneup);
1242         Cvar_RegisterVariable(&joy_deadzonepitch);
1243         Cvar_RegisterVariable(&joy_deadzoneyaw);
1244         //Cvar_RegisterVariable(&joy_deadzoneroll);
1245         Cvar_RegisterVariable(&joy_sensitivityforward);
1246         Cvar_RegisterVariable(&joy_sensitivityside);
1247         Cvar_RegisterVariable(&joy_sensitivityup);
1248         Cvar_RegisterVariable(&joy_sensitivitypitch);
1249         Cvar_RegisterVariable(&joy_sensitivityyaw);
1250         //Cvar_RegisterVariable(&joy_sensitivityroll);
1251         Cvar_RegisterVariable(&joy_axiskeyevents);
1252         
1253 #ifdef SDL_R_RESTART
1254         R_RegisterModule("SDL", sdl_start, sdl_shutdown, sdl_newmap, NULL, NULL);
1255 #endif
1256
1257         if (SDL_Init(SDL_INIT_VIDEO) < 0)
1258                 Sys_Error ("Failed to init SDL video subsystem: %s", SDL_GetError());
1259         vid_sdl_initjoysticksystem = SDL_InitSubSystem(SDL_INIT_JOYSTICK) >= 0;
1260         if (vid_sdl_initjoysticksystem)
1261                 Con_Printf("Failed to init SDL joystick subsystem: %s\n", SDL_GetError());
1262         vid_isfullscreen = false;
1263 }
1264
1265 // set the icon (we dont use SDL here since it would be too much a PITA)
1266 #ifdef WIN32
1267 #include "resource.h"
1268 #include <SDL_syswm.h>
1269 static void VID_SetCaption(void)
1270 {
1271     SDL_SysWMinfo       info;
1272         HICON                   icon;
1273
1274         // set the caption
1275         SDL_WM_SetCaption( gamename, NULL );
1276
1277         // get the HWND handle
1278     SDL_VERSION( &info.version );
1279         if( !SDL_GetWMInfo( &info ) )
1280                 return;
1281
1282         icon = LoadIcon( GetModuleHandle( NULL ), MAKEINTRESOURCE( IDI_ICON1 ) );
1283 #ifndef _W64 //If Windows 64bit data types don't exist
1284 #ifndef SetClassLongPtr
1285 #define SetClassLongPtr SetClassLong
1286 #endif
1287 #ifndef GCLP_HICON
1288 #define GCLP_HICON GCL_HICON
1289 #endif
1290 #ifndef LONG_PTR
1291 #define LONG_PTR LONG
1292 #endif
1293 #endif
1294         SetClassLongPtr( info.window, GCLP_HICON, (LONG_PTR)icon );
1295 }
1296 static void VID_SetIcon_Pre(void)
1297 {
1298 }
1299 static void VID_SetIcon_Post(void)
1300 {
1301 }
1302 #else
1303 // Adding the OS independent XPM version --blub
1304 #include "darkplaces.xpm"
1305 #include "nexuiz.xpm"
1306 static SDL_Surface *icon = NULL;
1307 static void VID_SetIcon_Pre(void)
1308 {
1309         /*
1310          * Somewhat restricted XPM reader. Only supports XPMs saved by GIMP 2.4 at
1311          * default settings with less than 91 colors and transparency.
1312          */
1313
1314         int width, height, colors, isize, i, j;
1315         int thenone = -1;
1316         static SDL_Color palette[256];
1317         unsigned short palenc[256]; // store color id by char
1318         char *xpm;
1319         char **idata, *data;
1320         const SDL_version *version;
1321
1322         version = SDL_Linked_Version();
1323         // only use non-XPM icon support in SDL v1.3 and higher
1324         // SDL v1.2 does not support "smooth" transparency, and thus is better
1325         // off the xpm way
1326         if(version->major >= 2 || (version->major == 1 && version->minor >= 3))
1327         {
1328                 data = (char *) loadimagepixelsbgra("darkplaces-icon", false, false, false, NULL);
1329                 if(data)
1330                 {
1331                         unsigned int red = 0x00FF0000;
1332                         unsigned int green = 0x0000FF00;
1333                         unsigned int blue = 0x000000FF;
1334                         unsigned int alpha = 0xFF000000;
1335                         width = image_width;
1336                         height = image_height;
1337
1338                         // reallocate with malloc, as this is in tempmempool (do not want)
1339                         xpm = data;
1340                         data = malloc(width * height * 4);
1341                         memcpy(data, xpm, width * height * 4);
1342                         Mem_Free(xpm);
1343                         xpm = NULL;
1344
1345                         icon = SDL_CreateRGBSurface(SDL_SRCALPHA, width, height, 32, LittleLong(red), LittleLong(green), LittleLong(blue), LittleLong(alpha));
1346
1347                         if(icon == NULL) {
1348                                 Con_Printf(     "Failed to create surface for the window Icon!\n"
1349                                                 "%s\n", SDL_GetError());
1350                                 free(data);
1351                                 return;
1352                         }
1353
1354                         icon->pixels = data;
1355                 }
1356         }
1357
1358         // we only get here if non-XPM icon was missing, or SDL version is not
1359         // sufficient for transparent non-XPM icons
1360         if(!icon)
1361         {
1362                 xpm = (char *) FS_LoadFile("darkplaces-icon.xpm", tempmempool, false, NULL);
1363                 idata = NULL;
1364                 if(xpm)
1365                         idata = XPM_DecodeString(xpm);
1366                 if(!idata)
1367                         idata = ENGINE_ICON;
1368                 if(xpm)
1369                         Mem_Free(xpm);
1370
1371                 data = idata[0];
1372
1373                 if(sscanf(data, "%i %i %i %i", &width, &height, &colors, &isize) != 4)
1374                 {
1375                         // NOTE: Only 1-char colornames are supported
1376                         Con_Printf("Sorry, but this does not even look similar to an XPM.\n");
1377                         return;
1378                 }
1379
1380                 if(isize != 1)
1381                 {
1382                         // NOTE: Only 1-char colornames are supported
1383                         Con_Printf("This XPM's palette is either huge or idiotically unoptimized. It's key size is %i\n", isize);
1384                         return;
1385                 }
1386
1387                 for(i = 0; i < colors; ++i)
1388                 {
1389                         unsigned int r, g, b;
1390                         char idx;
1391
1392                         if(sscanf(idata[i+1], "%c c #%02x%02x%02x", &idx, &r, &g, &b) != 4)
1393                         {
1394                                 char foo[2];
1395                                 if(sscanf(idata[i+1], "%c c Non%1[e]", &idx, foo) != 2) // I take the DailyWTF credit for this. --div0
1396                                 {
1397                                         Con_Printf("This XPM's palette looks odd. Can't continue.\n");
1398                                         return;
1399                                 }
1400                                 else
1401                                 {
1402                                         palette[i].r = 255; // color key
1403                                         palette[i].g = 0;
1404                                         palette[i].b = 255;
1405                                         thenone = i; // weeeee
1406                                 }
1407                         }
1408                         else
1409                         {
1410                                 palette[i].r = r - (r == 255 && g == 0 && b == 255); // change 255/0/255 pink to 254/0/255 for color key
1411                                 palette[i].g = g;
1412                                 palette[i].b = b;
1413                         }
1414
1415                         palenc[(unsigned char) idx] = i;
1416                 }
1417
1418                 // allocate the image data
1419                 data = (char*) malloc(width*height);
1420
1421                 for(j = 0; j < height; ++j)
1422                 {
1423                         for(i = 0; i < width; ++i)
1424                         {
1425                                 // casting to the safest possible datatypes ^^
1426                                 data[j * width + i] = palenc[((unsigned char*)idata[colors+j+1])[i]];
1427                         }
1428                 }
1429
1430                 if(icon != NULL)
1431                 {
1432                         // SDL_FreeSurface should free the data too
1433                         // but for completeness' sake...
1434                         if(icon->flags & SDL_PREALLOC)
1435                         {
1436                                 free(icon->pixels);
1437                                 icon->pixels = NULL; // safety
1438                         }
1439                         SDL_FreeSurface(icon);
1440                 }
1441
1442                 icon = SDL_CreateRGBSurface(SDL_SRCCOLORKEY, width, height, 8, 0,0,0,0);// rmask, gmask, bmask, amask); no mask needed
1443                 // 8 bit surfaces get an empty palette allocated according to the docs
1444                 // so it's a palette image for sure :) no endian check necessary for the mask
1445
1446                 if(icon == NULL) {
1447                         Con_Printf(     "Failed to create surface for the window Icon!\n"
1448                                         "%s\n", SDL_GetError());
1449                         free(data);
1450                         return;
1451                 }
1452
1453                 icon->pixels = data;
1454                 SDL_SetPalette(icon, SDL_PHYSPAL|SDL_LOGPAL, palette, 0, colors);
1455                 SDL_SetColorKey(icon, SDL_SRCCOLORKEY, thenone);
1456         }
1457
1458         SDL_WM_SetIcon(icon, NULL);
1459 }
1460 static void VID_SetIcon_Post(void)
1461 {
1462 #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION == 2
1463 // LordHavoc: info.info.x11.lock_func and accompanying code do not seem to compile with SDL 1.3
1464 #if SDL_VIDEO_DRIVER_X11 && !SDL_VIDEO_DRIVER_QUARTZ
1465         int j;
1466         char *data;
1467         const SDL_version *version;
1468
1469         version = SDL_Linked_Version();
1470         // only use non-XPM icon support in SDL v1.3 and higher
1471         // SDL v1.2 does not support "smooth" transparency, and thus is better
1472         // off the xpm way
1473         if(!(version->major >= 2 || (version->major == 1 && version->minor >= 3)))
1474         {
1475                 // in this case, we did not set the good icon yet
1476                 SDL_SysWMinfo info;
1477                 SDL_VERSION(&info.version);
1478                 if(SDL_GetWMInfo(&info) == 1 && info.subsystem == SDL_SYSWM_X11)
1479                 {
1480                         data = (char *) loadimagepixelsbgra("darkplaces-icon", false, false, false, NULL);
1481                         if(data)
1482                         {
1483                                 // use _NET_WM_ICON too
1484                                 static long netwm_icon[MAX_NETWM_ICON];
1485                                 int pos = 0;
1486                                 int i = 1;
1487
1488                                 while(data)
1489                                 {
1490                                         if(pos + 2 * image_width * image_height < MAX_NETWM_ICON)
1491                                         {
1492                                                 netwm_icon[pos++] = image_width;
1493                                                 netwm_icon[pos++] = image_height;
1494                                                 for(i = 0; i < image_height; ++i)
1495                                                         for(j = 0; j < image_width; ++j)
1496                                                                 netwm_icon[pos++] = BuffLittleLong((unsigned char *) &data[(i*image_width+j)*4]);
1497                                         }
1498                                         else
1499                                         {
1500                                                 Con_Printf("Skipping NETWM icon #%d because there is no space left\n", i);
1501                                         }
1502                                         ++i;
1503                                         Mem_Free(data);
1504                                         data = (char *) loadimagepixelsbgra(va("darkplaces-icon%d", i), false, false, false, NULL);
1505                                 }
1506
1507                                 info.info.x11.lock_func();
1508                                 {
1509                                         Atom net_wm_icon = XInternAtom(info.info.x11.display, "_NET_WM_ICON", false);
1510                                         XChangeProperty(info.info.x11.display, info.info.x11.wmwindow, net_wm_icon, XA_CARDINAL, 32, PropModeReplace, (const unsigned char *) netwm_icon, pos);
1511                                 }
1512                                 info.info.x11.unlock_func();
1513                         }
1514                 }
1515         }
1516 #endif
1517 #endif
1518 }
1519
1520
1521 static void VID_SetCaption(void)
1522 {
1523         SDL_WM_SetCaption( gamename, NULL );
1524 }
1525 #endif
1526
1527 static void VID_OutputVersion(void)
1528 {
1529         const SDL_version *version;
1530         version = SDL_Linked_Version();
1531         Con_Printf(     "Linked against SDL version %d.%d.%d\n"
1532                                         "Using SDL library version %d.%d.%d\n",
1533                                         SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL,
1534                                         version->major, version->minor, version->patch );
1535 }
1536
1537 qboolean VID_InitModeGL(viddef_mode_t *mode)
1538 {
1539         int i;
1540 // FIXME SDL_SetVideoMode
1541         static int notfirstvideomode = false;
1542         int flags = SDL_OPENGL;
1543         const char *drivername;
1544
1545         win_half_width = mode->width>>1;
1546         win_half_height = mode->height>>1;
1547
1548         if(vid_resizable.integer)
1549                 flags |= SDL_RESIZABLE;
1550
1551         VID_OutputVersion();
1552
1553         /*
1554         SDL Hack
1555                 We cant switch from one OpenGL video mode to another.
1556                 Thus we first switch to some stupid 2D mode and then back to OpenGL.
1557         */
1558         if (notfirstvideomode)
1559                 SDL_SetVideoMode( 0, 0, 0, 0 );
1560         notfirstvideomode = true;
1561
1562         // SDL usually knows best
1563         drivername = NULL;
1564
1565 // 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
1566         i = COM_CheckParm("-gl_driver");
1567         if (i && i < com_argc - 1)
1568                 drivername = com_argv[i + 1];
1569         if (SDL_GL_LoadLibrary(drivername) < 0)
1570         {
1571                 Con_Printf("Unable to load GL driver \"%s\": %s\n", drivername, SDL_GetError());
1572                 return false;
1573         }
1574
1575 #ifndef __IPHONEOS__
1576         if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL)
1577         {
1578                 VID_Shutdown();
1579                 Con_Print("Required OpenGL function glGetString not found\n");
1580                 return false;
1581         }
1582 #endif
1583
1584         // Knghtbrd: should do platform-specific extension string function here
1585
1586         vid_isfullscreen = false;
1587         if (mode->fullscreen) {
1588                 flags |= SDL_FULLSCREEN;
1589                 vid_isfullscreen = true;
1590         }
1591         //flags |= SDL_HWSURFACE;
1592
1593         SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
1594         if (mode->bitsperpixel >= 32)
1595         {
1596                 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 8);
1597                 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 8);
1598                 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 8);
1599                 SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 8);
1600                 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 24);
1601                 SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 8);
1602         }
1603         else
1604         {
1605                 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5);
1606                 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5);
1607                 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5);
1608                 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
1609         }
1610         if (mode->stereobuffer)
1611                 SDL_GL_SetAttribute (SDL_GL_STEREO, 1);
1612         if (mode->samples > 1)
1613         {
1614                 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1);
1615                 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, mode->samples);
1616         }
1617 #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION == 2
1618         if (vid_vsync.integer)
1619                 SDL_GL_SetAttribute (SDL_GL_SWAP_CONTROL, 1);
1620         else
1621                 SDL_GL_SetAttribute (SDL_GL_SWAP_CONTROL, 0);
1622 #else
1623         // TODO: SDL_GL_CONTEXT_MAJOR_VERSION, SDL_GL_CONTEXT_MINOR_VERSION
1624 #endif
1625
1626         video_bpp = mode->bitsperpixel;
1627         video_flags = flags;
1628         VID_SetIcon_Pre();
1629         screen = SDL_SetVideoMode(mode->width, mode->height, mode->bitsperpixel, flags);
1630         VID_SetIcon_Post();
1631
1632         if (screen == NULL)
1633         {
1634                 Con_Printf("Failed to set video mode to %ix%i: %s\n", mode->width, mode->height, SDL_GetError());
1635                 VID_Shutdown();
1636                 return false;
1637         }
1638
1639         vid_softsurface = NULL;
1640         vid.softpixels = NULL;
1641
1642         // set window title
1643         VID_SetCaption();
1644 #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION == 2
1645         // set up an event filter to ask confirmation on close button in WIN32
1646         SDL_SetEventFilter( (SDL_EventFilter) Sys_EventFilter );
1647 #endif
1648         // init keyboard
1649         SDL_EnableUNICODE( SDL_ENABLE );
1650         // enable key repeat since everyone expects it
1651         SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
1652
1653 #if !(SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION == 2)
1654         SDL_GL_SetSwapInterval(vid_vsync.integer != 0);
1655         vid_usingvsync = (vid_vsync.integer != 0);
1656 #endif
1657
1658         gl_platform = "SDL";
1659         gl_platformextensions = "";
1660
1661 #ifdef __IPHONEOS__
1662         GLES_Init();
1663 #else
1664         GL_Init();
1665 #endif
1666
1667         vid_numjoysticks = SDL_NumJoysticks();
1668         vid_numjoysticks = bound(0, vid_numjoysticks, MAX_JOYSTICKS);
1669         Cvar_SetValueQuick(&joy_detected, vid_numjoysticks);
1670         Con_Printf("%d SDL joystick(s) found:\n", vid_numjoysticks);
1671         memset(vid_joysticks, 0, sizeof(vid_joysticks));
1672         for (i = 0;i < vid_numjoysticks;i++)
1673         {
1674                 SDL_Joystick *joy;
1675                 joy = vid_joysticks[i] = SDL_JoystickOpen(i);
1676                 if (!joy)
1677                 {
1678                         Con_Printf("joystick #%i: open failed: %s\n", i, SDL_GetError());
1679                         continue;
1680                 }
1681                 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));
1682         }
1683
1684         vid_hidden = false;
1685         vid_activewindow = false;
1686         vid_usingmouse = false;
1687         vid_usinghidecursor = false;
1688
1689         SDL_WM_GrabInput(SDL_GRAB_OFF);
1690         return true;
1691 }
1692
1693 extern cvar_t gl_info_extensions;
1694 extern cvar_t gl_info_vendor;
1695 extern cvar_t gl_info_renderer;
1696 extern cvar_t gl_info_version;
1697 extern cvar_t gl_info_platform;
1698 extern cvar_t gl_info_driver;
1699
1700 qboolean VID_InitModeSoft(viddef_mode_t *mode)
1701 {
1702 // FIXME SDL_SetVideoMode
1703         int i;
1704         int flags = SDL_HWSURFACE;
1705
1706         win_half_width = mode->width>>1;
1707         win_half_height = mode->height>>1;
1708
1709         if(vid_resizable.integer)
1710                 flags |= SDL_RESIZABLE;
1711
1712         VID_OutputVersion();
1713
1714         vid_isfullscreen = false;
1715         if (mode->fullscreen) {
1716                 flags |= SDL_FULLSCREEN;
1717                 vid_isfullscreen = true;
1718         }
1719
1720         video_bpp = mode->bitsperpixel;
1721         video_flags = flags;
1722         VID_SetIcon_Pre();
1723         screen = SDL_SetVideoMode(mode->width, mode->height, mode->bitsperpixel, flags);
1724         VID_SetIcon_Post();
1725
1726         if (screen == NULL)
1727         {
1728                 Con_Printf("Failed to set video mode to %ix%i: %s\n", mode->width, mode->height, SDL_GetError());
1729                 VID_Shutdown();
1730                 return false;
1731         }
1732
1733         // create a framebuffer using our specific color format, we let the SDL blit function convert it in VID_Finish
1734         vid_softsurface = SDL_CreateRGBSurface(SDL_SWSURFACE, mode->width, mode->height, 32, 0x00FF0000, 0x0000FF00, 0x00000000FF, 0xFF000000);
1735         if (vid_softsurface == NULL)
1736         {
1737                 Con_Printf("Failed to setup software rasterizer framebuffer %ix%ix32bpp: %s\n", mode->width, mode->height, SDL_GetError());
1738                 VID_Shutdown();
1739                 return false;
1740         }
1741         SDL_SetAlpha(vid_softsurface, 0, 255);
1742
1743         vid.softpixels = (unsigned int *)vid_softsurface->pixels;
1744         vid.softdepthpixels = (unsigned int *)calloc(1, mode->width * mode->height * 4);
1745         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)
1746         {
1747                 Con_Printf("Failed to initialize software rasterizer\n");
1748                 VID_Shutdown();
1749                 return false;
1750         }
1751
1752         // set window title
1753         VID_SetCaption();
1754         // set up an event filter to ask confirmation on close button in WIN32
1755 #if SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION == 2
1756         SDL_SetEventFilter( (SDL_EventFilter) Sys_EventFilter );
1757 #endif
1758         // init keyboard
1759         SDL_EnableUNICODE( SDL_ENABLE );
1760         // enable key repeat since everyone expects it
1761         SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
1762
1763         gl_platform = "SDLSoft";
1764         gl_platformextensions = "";
1765
1766         gl_renderer = "DarkPlaces-Soft";
1767         gl_vendor = "Forest Hale";
1768         gl_version = "0.0";
1769         gl_extensions = "";
1770
1771         // clear the extension flags
1772         memset(&vid.support, 0, sizeof(vid.support));
1773         Cvar_SetQuick(&gl_info_extensions, "");
1774
1775         vid.forcevbo = false;
1776         vid.support.arb_depth_texture = true;
1777         vid.support.arb_draw_buffers = true;
1778         vid.support.arb_occlusion_query = true;
1779         vid.support.arb_shadow = true;
1780         //vid.support.arb_texture_compression = true;
1781         vid.support.arb_texture_cube_map = true;
1782         vid.support.arb_texture_non_power_of_two = false;
1783         vid.support.arb_vertex_buffer_object = true;
1784         vid.support.ext_blend_subtract = true;
1785         vid.support.ext_draw_range_elements = true;
1786         vid.support.ext_framebuffer_object = true;
1787         vid.support.ext_texture_3d = true;
1788         //vid.support.ext_texture_compression_s3tc = true;
1789         vid.support.ext_texture_filter_anisotropic = true;
1790         vid.support.ati_separate_stencil = true;
1791
1792         vid.maxtexturesize_2d = 16384;
1793         vid.maxtexturesize_3d = 512;
1794         vid.maxtexturesize_cubemap = 16384;
1795         vid.texunits = 4;
1796         vid.teximageunits = 32;
1797         vid.texarrayunits = 8;
1798         vid.max_anisotropy = 1;
1799         vid.maxdrawbuffers = 4;
1800
1801         vid.texunits = bound(4, vid.texunits, MAX_TEXTUREUNITS);
1802         vid.teximageunits = bound(16, vid.teximageunits, MAX_TEXTUREUNITS);
1803         vid.texarrayunits = bound(8, vid.texarrayunits, MAX_TEXTUREUNITS);
1804         Con_DPrintf("Using DarkPlaces Software Rasterizer rendering path\n");
1805         vid.renderpath = RENDERPATH_SOFT;
1806         vid.useinterleavedarrays = false;
1807
1808         Cvar_SetQuick(&gl_info_vendor, gl_vendor);
1809         Cvar_SetQuick(&gl_info_renderer, gl_renderer);
1810         Cvar_SetQuick(&gl_info_version, gl_version);
1811         Cvar_SetQuick(&gl_info_platform, gl_platform ? gl_platform : "");
1812         Cvar_SetQuick(&gl_info_driver, gl_driver);
1813
1814         // LordHavoc: report supported extensions
1815         Con_DPrintf("\nQuakeC extensions for server and client: %s\nQuakeC extensions for menu: %s\n", vm_sv_extensions, vm_m_extensions );
1816
1817         // clear to black (loading plaque will be seen over this)
1818         GL_Clear(GL_COLOR_BUFFER_BIT, NULL, 1.0f, 128);
1819
1820         vid_numjoysticks = SDL_NumJoysticks();
1821         vid_numjoysticks = bound(0, vid_numjoysticks, MAX_JOYSTICKS);
1822         Cvar_SetValueQuick(&joy_detected, vid_numjoysticks);
1823         Con_Printf("%d SDL joystick(s) found:\n", vid_numjoysticks);
1824         memset(vid_joysticks, 0, sizeof(vid_joysticks));
1825         for (i = 0;i < vid_numjoysticks;i++)
1826         {
1827                 SDL_Joystick *joy;
1828                 joy = vid_joysticks[i] = SDL_JoystickOpen(i);
1829                 if (!joy)
1830                 {
1831                         Con_Printf("joystick #%i: open failed: %s\n", i, SDL_GetError());
1832                         continue;
1833                 }
1834                 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));
1835         }
1836
1837         vid_hidden = false;
1838         vid_activewindow = false;
1839         vid_usingmouse = false;
1840         vid_usinghidecursor = false;
1841
1842         SDL_WM_GrabInput(SDL_GRAB_OFF);
1843         return true;
1844 }
1845
1846 qboolean VID_InitMode(viddef_mode_t *mode)
1847 {
1848         if (!SDL_WasInit(SDL_INIT_VIDEO) && SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
1849                 Sys_Error ("Failed to init SDL video subsystem: %s", SDL_GetError());
1850 #ifdef SSE2_PRESENT
1851         if (vid_soft.integer)
1852                 return VID_InitModeSoft(mode);
1853         else
1854 #endif
1855                 return VID_InitModeGL(mode);
1856 }
1857
1858 void VID_Shutdown (void)
1859 {
1860         VID_SetMouse(false, false, false);
1861         VID_RestoreSystemGamma();
1862
1863 #ifndef WIN32
1864         if (icon)
1865                 SDL_FreeSurface(icon);
1866         icon = NULL;
1867 #endif
1868
1869         if (vid_softsurface)
1870                 SDL_FreeSurface(vid_softsurface);
1871         vid_softsurface = NULL;
1872         vid.softpixels = NULL;
1873         if (vid.softdepthpixels)
1874                 free(vid.softdepthpixels);
1875         vid.softdepthpixels = NULL;
1876
1877         SDL_QuitSubSystem(SDL_INIT_VIDEO);
1878
1879         gl_driver[0] = 0;
1880         gl_extensions = "";
1881         gl_platform = "";
1882         gl_platformextensions = "";
1883 }
1884
1885 int VID_SetGamma (unsigned short *ramps, int rampsize)
1886 {
1887         return !SDL_SetGammaRamp (ramps, ramps + rampsize, ramps + rampsize*2);
1888 }
1889
1890 int VID_GetGamma (unsigned short *ramps, int rampsize)
1891 {
1892         return !SDL_GetGammaRamp (ramps, ramps + rampsize, ramps + rampsize*2);
1893 }
1894
1895 void VID_Finish (void)
1896 {
1897         Uint8 appstate;
1898
1899         //react on appstate changes
1900         appstate = SDL_GetAppState();
1901
1902         vid_hidden = !(appstate & SDL_APPACTIVE);
1903
1904         if( vid_hidden || !( appstate & SDL_APPMOUSEFOCUS ) || !( appstate & SDL_APPINPUTFOCUS ) )
1905                 vid_activewindow = false;
1906         else
1907                 vid_activewindow = true;
1908
1909         VID_UpdateGamma(false, 256);
1910
1911         if (!vid_hidden)
1912         {
1913                 switch(vid.renderpath)
1914                 {
1915                 case RENDERPATH_GL11:
1916                 case RENDERPATH_GL13:
1917                 case RENDERPATH_GL20:
1918                 case RENDERPATH_CGGL:
1919                 case RENDERPATH_GLES2:
1920                         CHECKGLERROR
1921                         if (r_speeds.integer == 2 || gl_finish.integer)
1922                         {
1923                                 qglFinish();CHECKGLERROR
1924                         }
1925 #if !(SDL_MAJOR_VERSION == 1 && SDL_MINOR_VERSION == 2)
1926 {
1927         qboolean vid_usevsync;
1928         vid_usevsync = (vid_vsync.integer && !cls.timedemo);
1929         if (vid_usingvsync != vid_usevsync)
1930         {
1931                 if (SDL_GL_SetSwapInterval(vid_usevsync != 0) >= 0)
1932                         Con_DPrintf("Vsync %s\n", vid_usevsync ? "activated" : "deactivated");
1933                 else
1934                         Con_DPrintf("ERROR: can't %s vsync\n", vid_usevsync ? "activate" : "deactivate");
1935         }
1936 }
1937 #endif
1938                         SDL_GL_SwapBuffers();
1939                         break;
1940                 case RENDERPATH_SOFT:
1941                         DPSOFTRAST_Finish();
1942                         SDL_BlitSurface(vid_softsurface, NULL, screen, NULL);
1943                         SDL_Flip(screen);
1944                         break;
1945                 case RENDERPATH_D3D9:
1946                 case RENDERPATH_D3D10:
1947                 case RENDERPATH_D3D11:
1948                         break;
1949                 }
1950         }
1951 }
1952
1953 size_t VID_ListModes(vid_mode_t *modes, size_t maxcount)
1954 {
1955         size_t k;
1956         SDL_Rect **vidmodes;
1957         int bpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
1958
1959         k = 0;
1960         for(vidmodes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE); *vidmodes; ++vidmodes)
1961         {
1962                 if(k >= maxcount)
1963                         break;
1964                 modes[k].width = (*vidmodes)->w;
1965                 modes[k].height = (*vidmodes)->h;
1966                 modes[k].bpp = bpp;
1967                 modes[k].refreshrate = 60; // no support for refresh rate in SDL
1968                 modes[k].pixelheight_num = 1;
1969                 modes[k].pixelheight_denom = 1; // SDL does not provide this
1970                 ++k;
1971         }
1972         return k;
1973 }