]> icculus.org git repositories - divverent/darkplaces.git/blob - vid_sdl.c
SDL: also use a non-XPM icon darkplaces-icon.tga but only if SDL is >= 1.3 (SDL 1...
[divverent/darkplaces.git] / vid_sdl.c
1 /*
2 Copyright (C) 2003  T. Joseph Carter
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19 #undef WIN32_LEAN_AND_MEAN  //hush a warning, SDL.h redefines this
20 #include <SDL.h>
21 #include <stdio.h>
22
23 #include "quakedef.h"
24 #include "image.h"
25
26 #ifdef WIN32
27 #define SDL_R_RESTART
28 #endif
29
30 // Tell startup code that we have a client
31 int cl_available = true;
32
33 qboolean vid_supportrefreshrate = false;
34
35 cvar_t joy_detected = {CVAR_READONLY, "joy_detected", "0", "number of joysticks detected by engine"};
36 cvar_t joy_enable = {CVAR_SAVE, "joy_enable", "0", "enables joystick support"};
37 cvar_t joy_index = {0, "joy_index", "0", "selects which joystick to use if you have multiple"};
38 cvar_t joy_axisforward = {0, "joy_axisforward", "1", "which joystick axis to query for forward/backward movement"};
39 cvar_t joy_axisside = {0, "joy_axisside", "0", "which joystick axis to query for right/left movement"};
40 cvar_t joy_axisup = {0, "joy_axisup", "-1", "which joystick axis to query for up/down movement"};
41 cvar_t joy_axispitch = {0, "joy_axispitch", "3", "which joystick axis to query for looking up/down"};
42 cvar_t joy_axisyaw = {0, "joy_axisyaw", "2", "which joystick axis to query for looking right/left"};
43 cvar_t joy_axisroll = {0, "joy_axisroll", "-1", "which joystick axis to query for tilting head right/left"};
44 cvar_t joy_deadzoneforward = {0, "joy_deadzoneforward", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
45 cvar_t joy_deadzoneside = {0, "joy_deadzoneside", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
46 cvar_t joy_deadzoneup = {0, "joy_deadzoneup", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
47 cvar_t joy_deadzonepitch = {0, "joy_deadzonepitch", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
48 cvar_t joy_deadzoneyaw = {0, "joy_deadzoneyaw", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
49 cvar_t joy_deadzoneroll = {0, "joy_deadzoneroll", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"};
50 cvar_t joy_sensitivityforward = {0, "joy_sensitivityforward", "-1", "movement multiplier"};
51 cvar_t joy_sensitivityside = {0, "joy_sensitivityside", "1", "movement multiplier"};
52 cvar_t joy_sensitivityup = {0, "joy_sensitivityup", "1", "movement multiplier"};
53 cvar_t joy_sensitivitypitch = {0, "joy_sensitivitypitch", "1", "movement multiplier"};
54 cvar_t joy_sensitivityyaw = {0, "joy_sensitivityyaw", "-1", "movement multiplier"};
55 cvar_t joy_sensitivityroll = {0, "joy_sensitivityroll", "1", "movement multiplier"};
56
57 static qboolean vid_usingmouse = false;
58 static qboolean vid_usinghidecursor = false;
59 static qboolean vid_isfullscreen;
60 static int vid_numjoysticks = 0;
61 #define MAX_JOYSTICKS 8
62 static SDL_Joystick *vid_joysticks[MAX_JOYSTICKS];
63
64 static int win_half_width = 50;
65 static int win_half_height = 50;
66 static int video_bpp, video_flags;
67
68 static SDL_Surface *screen;
69
70 /////////////////////////
71 // Input handling
72 ////
73 //TODO: Add joystick support
74 //TODO: Add error checking
75
76
77 //keysym to quake keysym mapping
78 #define tenoh   0,0,0,0,0, 0,0,0,0,0
79 #define fiftyoh tenoh, tenoh, tenoh, tenoh, tenoh
80 #define hundredoh fiftyoh, fiftyoh
81 static unsigned int tbl_sdltoquake[] =
82 {
83         0,0,0,0,                //SDLK_UNKNOWN          = 0,
84         0,0,0,0,                //SDLK_FIRST            = 0,
85         K_BACKSPACE,    //SDLK_BACKSPACE        = 8,
86         K_TAB,                  //SDLK_TAB                      = 9,
87         0,0,
88         0,                              //SDLK_CLEAR            = 12,
89         K_ENTER,                //SDLK_RETURN           = 13,
90     0,0,0,0,0,
91         K_PAUSE,                //SDLK_PAUSE            = 19,
92         0,0,0,0,0,0,0,
93         K_ESCAPE,               //SDLK_ESCAPE           = 27,
94         0,0,0,0,
95         K_SPACE,                //SDLK_SPACE            = 32,
96         '!',                    //SDLK_EXCLAIM          = 33,
97         '"',                    //SDLK_QUOTEDBL         = 34,
98         '#',                    //SDLK_HASH                     = 35,
99         '$',                    //SDLK_DOLLAR           = 36,
100         0,
101         '&',                    //SDLK_AMPERSAND        = 38,
102         '\'',                   //SDLK_QUOTE            = 39,
103         '(',                    //SDLK_LEFTPAREN        = 40,
104         ')',                    //SDLK_RIGHTPAREN       = 41,
105         '*',                    //SDLK_ASTERISK         = 42,
106         '+',                    //SDLK_PLUS                     = 43,
107         ',',                    //SDLK_COMMA            = 44,
108         '-',                    //SDLK_MINUS            = 45,
109         '.',                    //SDLK_PERIOD           = 46,
110         '/',                    //SDLK_SLASH            = 47,
111         '0',                    //SDLK_0                        = 48,
112         '1',                    //SDLK_1                        = 49,
113         '2',                    //SDLK_2                        = 50,
114         '3',                    //SDLK_3                        = 51,
115         '4',                    //SDLK_4                        = 52,
116         '5',                    //SDLK_5                        = 53,
117         '6',                    //SDLK_6                        = 54,
118         '7',                    //SDLK_7                        = 55,
119         '8',                    //SDLK_8                        = 56,
120         '9',                    //SDLK_9                        = 57,
121         ':',                    //SDLK_COLON            = 58,
122         ';',                    //SDLK_SEMICOLON        = 59,
123         '<',                    //SDLK_LESS                     = 60,
124         '=',                    //SDLK_EQUALS           = 61,
125         '>',                    //SDLK_GREATER          = 62,
126         '?',                    //SDLK_QUESTION         = 63,
127         '@',                    //SDLK_AT                       = 64,
128         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,
129         '[',            //SDLK_LEFTBRACKET      = 91,
130         '\\',           //SDLK_BACKSLASH        = 92,
131         ']',            //SDLK_RIGHTBRACKET     = 93,
132         '^',            //SDLK_CARET            = 94,
133         '_',            //SDLK_UNDERSCORE       = 95,
134         '`',            //SDLK_BACKQUOTE        = 96,
135         'a',            //SDLK_a                        = 97,
136         'b',            //SDLK_b                        = 98,
137         'c',            //SDLK_c                        = 99,
138         'd',            //SDLK_d                        = 100,
139         'e',            //SDLK_e                        = 101,
140         'f',            //SDLK_f                        = 102,
141         'g',            //SDLK_g                        = 103,
142         'h',            //SDLK_h                        = 104,
143         'i',            //SDLK_i                        = 105,
144         'j',            //SDLK_j                        = 106,
145         'k',            //SDLK_k                        = 107,
146         'l',            //SDLK_l                        = 108,
147         'm',            //SDLK_m                        = 109,
148         'n',            //SDLK_n                        = 110,
149         'o',            //SDLK_o                        = 111,
150         'p',            //SDLK_p                        = 112,
151         'q',            //SDLK_q                        = 113,
152         'r',            //SDLK_r                        = 114,
153         's',            //SDLK_s                        = 115,
154         't',            //SDLK_t                        = 116,
155         'u',            //SDLK_u                        = 117,
156         'v',            //SDLK_v                        = 118,
157         'w',            //SDLK_w                        = 119,
158         'x',            //SDLK_x                        = 120,
159         'y',            //SDLK_y                        = 121,
160         'z',            //SDLK_z                        = 122,
161         0,0,0,0,
162         K_DEL,          //SDLK_DELETE           = 127,
163         hundredoh /*227*/, tenoh, tenoh, 0,0,0,0,0,0,0,0,
164         K_KP_0,         //SDLK_KP0              = 256,
165         K_KP_1,         //SDLK_KP1              = 257,
166         K_KP_2,         //SDLK_KP2              = 258,
167         K_KP_3,         //SDLK_KP3              = 259,
168         K_KP_4,         //SDLK_KP4              = 260,
169         K_KP_5,         //SDLK_KP5              = 261,
170         K_KP_6,         //SDLK_KP6              = 262,
171         K_KP_7,         //SDLK_KP7              = 263,
172         K_KP_8,         //SDLK_KP8              = 264,
173         K_KP_9,         //SDLK_KP9              = 265,
174         K_KP_PERIOD,//SDLK_KP_PERIOD    = 266,
175         K_KP_DIVIDE,//SDLK_KP_DIVIDE    = 267,
176         K_KP_MULTIPLY,//SDLK_KP_MULTIPLY= 268,
177         K_KP_MINUS,     //SDLK_KP_MINUS         = 269,
178         K_KP_PLUS,      //SDLK_KP_PLUS          = 270,
179         K_KP_ENTER,     //SDLK_KP_ENTER         = 271,
180         K_KP_EQUALS,//SDLK_KP_EQUALS    = 272,
181         K_UPARROW,      //SDLK_UP               = 273,
182         K_DOWNARROW,//SDLK_DOWN         = 274,
183         K_RIGHTARROW,//SDLK_RIGHT       = 275,
184         K_LEFTARROW,//SDLK_LEFT         = 276,
185         K_INS,          //SDLK_INSERT   = 277,
186         K_HOME,         //SDLK_HOME             = 278,
187         K_END,          //SDLK_END              = 279,
188         K_PGUP,         //SDLK_PAGEUP   = 280,
189         K_PGDN,         //SDLK_PAGEDOWN = 281,
190         K_F1,           //SDLK_F1               = 282,
191         K_F2,           //SDLK_F2               = 283,
192         K_F3,           //SDLK_F3               = 284,
193         K_F4,           //SDLK_F4               = 285,
194         K_F5,           //SDLK_F5               = 286,
195         K_F6,           //SDLK_F6               = 287,
196         K_F7,           //SDLK_F7               = 288,
197         K_F8,           //SDLK_F8               = 289,
198         K_F9,           //SDLK_F9               = 290,
199         K_F10,          //SDLK_F10              = 291,
200         K_F11,          //SDLK_F11              = 292,
201         K_F12,          //SDLK_F12              = 293,
202         0,                      //SDLK_F13              = 294,
203         0,                      //SDLK_F14              = 295,
204         0,                      //SDLK_F15              = 296,
205         0,0,0,
206         K_NUMLOCK,      //SDLK_NUMLOCK  = 300,
207         K_CAPSLOCK,     //SDLK_CAPSLOCK = 301,
208         K_SCROLLOCK,//SDLK_SCROLLOCK= 302,
209         K_SHIFT,        //SDLK_RSHIFT   = 303,
210         K_SHIFT,        //SDLK_LSHIFT   = 304,
211         K_CTRL,         //SDLK_RCTRL    = 305,
212         K_CTRL,         //SDLK_LCTRL    = 306,
213         K_ALT,          //SDLK_RALT             = 307,
214         K_ALT,          //SDLK_LALT             = 308,
215         0,                      //SDLK_RMETA    = 309,
216         0,                      //SDLK_LMETA    = 310,
217         0,                      //SDLK_LSUPER   = 311,          /* Left "Windows" key */
218         0,                      //SDLK_RSUPER   = 312,          /* Right "Windows" key */
219         K_ALT,                  //SDLK_MODE             = 313,          /* "Alt Gr" key */
220         0,                      //SDLK_COMPOSE  = 314,          /* Multi-key compose key */
221         0,                      //SDLK_HELP             = 315,
222         0,                      //SDLK_PRINT    = 316,
223         0,                      //SDLK_SYSREQ   = 317,
224         K_PAUSE,        //SDLK_BREAK    = 318,
225         0,                      //SDLK_MENU             = 319,
226         0,                      //SDLK_POWER    = 320,          /* Power Macintosh power key */
227         'e',            //SDLK_EURO             = 321,          /* Some european keyboards */
228         0                       //SDLK_UNDO             = 322,          /* Atari keyboard has Undo */
229 };
230 #undef tenoh
231 #undef fiftyoh
232 #undef hundredoh
233
234 static int MapKey( unsigned int sdlkey )
235 {
236         if( sdlkey > sizeof(tbl_sdltoquake)/ sizeof(int) )
237                 return 0;
238     return tbl_sdltoquake[ sdlkey ];
239 }
240
241 void VID_SetMouse(qboolean fullscreengrab, qboolean relative, qboolean hidecursor)
242 {
243         if (vid_usingmouse != relative)
244         {
245                 vid_usingmouse = relative;
246                 cl_ignoremousemoves = 2;
247                 SDL_WM_GrabInput( relative ? SDL_GRAB_ON : SDL_GRAB_OFF );
248         }
249         if (vid_usinghidecursor != hidecursor)
250         {
251                 vid_usinghidecursor = hidecursor;
252                 SDL_ShowCursor( hidecursor ? SDL_DISABLE : SDL_ENABLE);
253         }
254 }
255
256 static double IN_JoystickGetAxis(SDL_Joystick *joy, int axis, double sensitivity, double deadzone)
257 {
258         double value;
259         if (axis < 0 || axis >= SDL_JoystickNumAxes(joy))
260                 return 0; // no such axis on this joystick
261         value = SDL_JoystickGetAxis(joy, axis) * (1.0 / 32767.0);
262         value = bound(-1, value, 1);
263         if (fabs(value) < deadzone)
264                 return 0; // within deadzone around center
265         return value * sensitivity;
266 }
267
268 void IN_Move( void )
269 {
270         int j;
271         static int old_x = 0, old_y = 0;
272         static int stuck = 0;
273         int x, y;
274         if (vid_usingmouse)
275         {
276                 if(vid_stick_mouse.integer)
277                 {
278                         // have the mouse stuck in the middle, example use: prevent expose effect of beryl during the game when not using
279                         // window grabbing. --blub
280
281                         // we need 2 frames to initialize the center position
282                         if(!stuck)
283                         {
284                                 SDL_WarpMouse(win_half_width, win_half_height);
285                                 SDL_GetMouseState(&x, &y);
286                                 SDL_GetRelativeMouseState(&x, &y);
287                                 ++stuck;
288                         } else {
289                                 SDL_GetRelativeMouseState(&x, &y);
290                                 in_mouse_x = x + old_x;
291                                 in_mouse_y = y + old_y;
292                                 SDL_GetMouseState(&x, &y);
293                                 old_x = x - win_half_width;
294                                 old_y = y - win_half_height;
295                                 SDL_WarpMouse(win_half_width, win_half_height);
296                         }
297                 } else {
298                         SDL_GetRelativeMouseState( &x, &y );
299                         in_mouse_x = x;
300                         in_mouse_y = y;
301                 }
302         }
303
304         SDL_GetMouseState(&x, &y);
305         in_windowmouse_x = x;
306         in_windowmouse_y = y;
307
308         if (vid_numjoysticks && joy_enable.integer && joy_index.integer >= 0 && joy_index.integer < vid_numjoysticks)
309         {
310                 SDL_Joystick *joy = vid_joysticks[joy_index.integer];
311                 int numballs = SDL_JoystickNumBalls(joy);
312                 for (j = 0;j < numballs;j++)
313                 {
314                         SDL_JoystickGetBall(joy, j, &x, &y);
315                         in_mouse_x += x;
316                         in_mouse_y += y;
317                 }
318                 cl.cmd.forwardmove += IN_JoystickGetAxis(joy, joy_axisforward.integer, joy_sensitivityforward.value, joy_deadzoneforward.value) * cl_forwardspeed.value;
319                 cl.cmd.sidemove    += IN_JoystickGetAxis(joy, joy_axisside.integer, joy_sensitivityside.value, joy_deadzoneside.value) * cl_sidespeed.value;
320                 cl.cmd.upmove      += IN_JoystickGetAxis(joy, joy_axisup.integer, joy_sensitivityup.value, joy_deadzoneup.value) * cl_upspeed.value;
321                 cl.viewangles[0]   += IN_JoystickGetAxis(joy, joy_axispitch.integer, joy_sensitivitypitch.value, joy_deadzonepitch.value) * cl.realframetime * cl_pitchspeed.value;
322                 cl.viewangles[1]   += IN_JoystickGetAxis(joy, joy_axisyaw.integer, joy_sensitivityyaw.value, joy_deadzoneyaw.value) * cl.realframetime * cl_yawspeed.value;
323                 //cl.viewangles[2]   += IN_JoystickGetAxis(joy, joy_axisroll.integer, joy_sensitivityroll.value, joy_deadzoneroll.value) * cl.realframetime * cl_rollspeed.value;
324         }
325 }
326
327 /////////////////////
328 // Message Handling
329 ////
330
331 static int Sys_EventFilter( SDL_Event *event )
332 {
333         //TODO: Add a quit query in linux, too - though linux user are more likely to know what they do
334         if (event->type == SDL_QUIT)
335         {
336 #ifdef WIN32
337                 if (MessageBox( NULL, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION ) == IDNO)
338                         return 0;
339 #endif
340         }
341         return 1;
342 }
343
344 #ifdef SDL_R_RESTART
345 static qboolean sdl_needs_restart;
346 static void sdl_start(void)
347 {
348 }
349 static void sdl_shutdown(void)
350 {
351         sdl_needs_restart = false;
352 }
353 static void sdl_newmap(void)
354 {
355 }
356 #endif
357
358 static keynum_t buttonremap[18] =
359 {
360         K_MOUSE1,
361         K_MOUSE3,
362         K_MOUSE2,
363         K_MWHEELUP,
364         K_MWHEELDOWN,
365         K_MOUSE4,
366         K_MOUSE5,
367         K_MOUSE6,
368         K_MOUSE7,
369         K_MOUSE8,
370         K_MOUSE9,
371         K_MOUSE10,
372         K_MOUSE11,
373         K_MOUSE12,
374         K_MOUSE13,
375         K_MOUSE14,
376         K_MOUSE15,
377         K_MOUSE16,
378 };
379
380 void Sys_SendKeyEvents( void )
381 {
382         static qboolean sound_active = true;
383         SDL_Event event;
384
385         while( SDL_PollEvent( &event ) )
386                 switch( event.type ) {
387                         case SDL_QUIT:
388                                 Sys_Quit(0);
389                                 break;
390                         case SDL_KEYDOWN:
391                         case SDL_KEYUP:
392                                 Key_Event( MapKey( event.key.keysym.sym ), event.key.keysym.unicode, (event.key.state == SDL_PRESSED) );
393                                 break;
394                         case SDL_ACTIVEEVENT:
395                                 if( event.active.state & SDL_APPACTIVE )
396                                 {
397                                         if( event.active.gain )
398                                                 vid_hidden = false;
399                                         else
400                                                 vid_hidden = true;
401                                 }
402                                 break;
403                         case SDL_MOUSEBUTTONDOWN:
404                         case SDL_MOUSEBUTTONUP:
405                                 if (event.button.button <= 18)
406                                         Key_Event( buttonremap[event.button.button - 1], 0, event.button.state == SDL_PRESSED );
407                                 break;
408                         case SDL_JOYBUTTONDOWN:
409                                 if (!joy_enable.integer)
410                                         break; // ignore down events if joystick has been disabled
411                         case SDL_JOYBUTTONUP:
412                                 if (event.jbutton.button < 48)
413                                         Key_Event( event.jbutton.button + (event.jbutton.button < 16 ? K_JOY1 : K_AUX1 - 16), 0, (event.jbutton.state == SDL_PRESSED) );
414                                 break;
415                         case SDL_VIDEORESIZE:
416                                 if(vid_resizable.integer < 2)
417                                 {
418                                         vid.width = event.resize.w;
419                                         vid.height = event.resize.h;
420                                         SDL_SetVideoMode(vid.width, vid.height, video_bpp, video_flags);
421 #ifdef SDL_R_RESTART
422                                         // better not call R_Modules_Restart from here directly, as this may wreak havoc...
423                                         // so, let's better queue it for next frame
424                                         if(!sdl_needs_restart)
425                                         {
426                                                 Cbuf_AddText("\nr_restart\n");
427                                                 sdl_needs_restart = true;
428                                         }
429 #endif
430                                 }
431                                 break;
432                 }
433
434         // enable/disable sound on focus gain/loss
435         if ((!vid_hidden && vid_activewindow) || !snd_mutewhenidle.integer)
436         {
437                 if (!sound_active)
438                 {
439                         S_UnblockSound ();
440                         sound_active = true;
441                 }
442         }
443         else
444         {
445                 if (sound_active)
446                 {
447                         S_BlockSound ();
448                         sound_active = false;
449                 }
450         }
451 }
452
453 /////////////////
454 // Video system
455 ////
456
457 void *GL_GetProcAddress(const char *name)
458 {
459         void *p = NULL;
460         p = SDL_GL_GetProcAddress(name);
461         return p;
462 }
463
464 static int Sys_EventFilter( SDL_Event *event );
465 static qboolean vid_sdl_initjoysticksystem = false;
466
467 void VID_Init (void)
468 {
469         Cvar_RegisterVariable(&joy_detected);
470         Cvar_RegisterVariable(&joy_enable);
471         Cvar_RegisterVariable(&joy_index);
472         Cvar_RegisterVariable(&joy_axisforward);
473         Cvar_RegisterVariable(&joy_axisside);
474         Cvar_RegisterVariable(&joy_axisup);
475         Cvar_RegisterVariable(&joy_axispitch);
476         Cvar_RegisterVariable(&joy_axisyaw);
477         //Cvar_RegisterVariable(&joy_axisroll);
478         Cvar_RegisterVariable(&joy_deadzoneforward);
479         Cvar_RegisterVariable(&joy_deadzoneside);
480         Cvar_RegisterVariable(&joy_deadzoneup);
481         Cvar_RegisterVariable(&joy_deadzonepitch);
482         Cvar_RegisterVariable(&joy_deadzoneyaw);
483         //Cvar_RegisterVariable(&joy_deadzoneroll);
484         Cvar_RegisterVariable(&joy_sensitivityforward);
485         Cvar_RegisterVariable(&joy_sensitivityside);
486         Cvar_RegisterVariable(&joy_sensitivityup);
487         Cvar_RegisterVariable(&joy_sensitivitypitch);
488         Cvar_RegisterVariable(&joy_sensitivityyaw);
489         //Cvar_RegisterVariable(&joy_sensitivityroll);
490         
491 #ifdef SDL_R_RESTART
492         R_RegisterModule("SDL", sdl_start, sdl_shutdown, sdl_newmap, NULL, NULL);
493 #endif
494
495         if (SDL_Init(SDL_INIT_VIDEO) < 0)
496                 Sys_Error ("Failed to init SDL video subsystem: %s", SDL_GetError());
497         vid_sdl_initjoysticksystem = SDL_Init(SDL_INIT_JOYSTICK) >= 0;
498         if (vid_sdl_initjoysticksystem)
499                 Con_Printf("Failed to init SDL joystick subsystem: %s\n", SDL_GetError());
500         vid_isfullscreen = false;
501 }
502
503 // set the icon (we dont use SDL here since it would be too much a PITA)
504 #ifdef WIN32
505 #include "resource.h"
506 #include <SDL_syswm.h>
507 static void VID_SetCaption(void)
508 {
509     SDL_SysWMinfo       info;
510         HICON                   icon;
511
512         // set the caption
513         SDL_WM_SetCaption( gamename, NULL );
514
515         // get the HWND handle
516     SDL_VERSION( &info.version );
517         if( !SDL_GetWMInfo( &info ) )
518                 return;
519
520         icon = LoadIcon( GetModuleHandle( NULL ), MAKEINTRESOURCE( IDI_ICON1 ) );
521 #ifndef _W64 //If Windows 64bit data types don't exist
522 #ifndef SetClassLongPtr
523 #define SetClassLongPtr SetClassLong
524 #endif
525 #ifndef GCLP_HICON
526 #define GCLP_HICON GCL_HICON
527 #endif
528 #ifndef LONG_PTR
529 #define LONG_PTR LONG
530 #endif
531 #endif
532         SetClassLongPtr( info.window, GCLP_HICON, (LONG_PTR)icon );
533 }
534 static void VID_SetIcon(void)
535 {
536 }
537 #else
538 // Adding the OS independent XPM version --blub
539 #include "darkplaces.xpm"
540 #include "nexuiz.xpm"
541 static SDL_Surface *icon = NULL;
542 static void VID_SetIcon(void)
543 {
544         /*
545          * Somewhat restricted XPM reader. Only supports XPMs saved by GIMP 2.4 at
546          * default settings with less than 91 colors and transparency.
547          */
548
549         int width, height, colors, isize, i, j;
550         int thenone = -1;
551         static SDL_Color palette[256];
552         unsigned short palenc[256]; // store color id by char
553         char *xpm;
554         char **idata, *data;
555         const SDL_version *version;
556
557         version = SDL_Linked_Version();
558         // only use non-XPM icon support in SDL v1.3 and higher
559         // SDL v1.2 does not support "smooth" transparency, and thus is better
560         // off the xpm way
561         if(version->major >= 2 || version->major == 1 && version->minor >= 3)
562         {
563                 data = (char *) loadimagepixelsbgra("darkplaces-icon", false, false, false, NULL);
564                 if(data)
565                 {
566                         unsigned int red = 0x00FF0000;
567                         unsigned int green = 0x0000FF00;
568                         unsigned int blue = 0x000000FF;
569                         unsigned int alpha = 0xFF000000;
570                         width = image_width;
571                         height = image_height;
572
573                         // reallocate with malloc, as this is in tempmempool (do not want)
574                         xpm = data;
575                         data = malloc(width * height * 4);
576                         memcpy(data, xpm, width * height * 4);
577                         Mem_Free(xpm);
578                         xpm = NULL;
579
580                         icon = SDL_CreateRGBSurface(SDL_SRCALPHA, width, height, 32, LittleLong(red), LittleLong(green), LittleLong(blue), LittleLong(alpha));
581
582                         if(icon == NULL) {
583                                 Con_Printf(     "Failed to create surface for the window Icon!\n"
584                                                 "%s\n", SDL_GetError());
585                                 free(data);
586                                 return;
587                         }
588
589                         icon->pixels = data;
590                 }
591         }
592
593         // we only get here if non-XPM icon was missing, or SDL version is not
594         // sufficient for transparent non-XPM icons
595         if(!icon)
596         {
597                 xpm = (char *) FS_LoadFile("darkplaces-icon.xpm", tempmempool, false, NULL);
598                 idata = NULL;
599                 if(xpm)
600                         idata = XPM_DecodeString(xpm);
601                 if(!idata)
602                         idata = ENGINE_ICON;
603                 if(xpm)
604                         Mem_Free(xpm);
605
606                 data = idata[0];
607
608                 if(sscanf(data, "%i %i %i %i", &width, &height, &colors, &isize) != 4)
609                 {
610                         // NOTE: Only 1-char colornames are supported
611                         Con_Printf("Sorry, but this does not even look similar to an XPM.\n");
612                         return;
613                 }
614
615                 if(isize != 1)
616                 {
617                         // NOTE: Only 1-char colornames are supported
618                         Con_Printf("This XPM's palette is either huge or idiotically unoptimized. It's key size is %i\n", isize);
619                         return;
620                 }
621
622                 for(i = 0; i < colors; ++i)
623                 {
624                         unsigned int r, g, b;
625                         char idx;
626
627                         if(sscanf(idata[i+1], "%c c #%02x%02x%02x", &idx, &r, &g, &b) != 4)
628                         {
629                                 char foo[2];
630                                 if(sscanf(idata[i+1], "%c c Non%1[e]", &idx, foo) != 2) // I take the DailyWTF credit for this. --div0
631                                 {
632                                         Con_Printf("This XPM's palette looks odd. Can't continue.\n");
633                                         return;
634                                 }
635                                 else
636                                 {
637                                         palette[i].r = 255; // color key
638                                         palette[i].g = 0;
639                                         palette[i].b = 255;
640                                         thenone = i; // weeeee
641                                 }
642                         }
643                         else
644                         {
645                                 palette[i].r = r - (r == 255 && g == 0 && b == 255); // change 255/0/255 pink to 254/0/255 for color key
646                                 palette[i].g = g;
647                                 palette[i].b = b;
648                         }
649
650                         palenc[(unsigned char) idx] = i;
651                 }
652
653                 // allocate the image data
654                 data = (char*) malloc(width*height);
655
656                 for(j = 0; j < height; ++j)
657                 {
658                         for(i = 0; i < width; ++i)
659                         {
660                                 // casting to the safest possible datatypes ^^
661                                 data[j * width + i] = palenc[((unsigned char*)idata[colors+j+1])[i]];
662                         }
663                 }
664
665                 if(icon != NULL)
666                 {
667                         // SDL_FreeSurface should free the data too
668                         // but for completeness' sake...
669                         if(icon->flags & SDL_PREALLOC)
670                         {
671                                 free(icon->pixels);
672                                 icon->pixels = NULL; // safety
673                         }
674                         SDL_FreeSurface(icon);
675                 }
676
677                 icon = SDL_CreateRGBSurface(SDL_SRCCOLORKEY, width, height, 8, 0,0,0,0);// rmask, gmask, bmask, amask); no mask needed
678                 // 8 bit surfaces get an empty palette allocated according to the docs
679                 // so it's a palette image for sure :) no endian check necessary for the mask
680
681                 if(icon == NULL) {
682                         Con_Printf(     "Failed to create surface for the window Icon!\n"
683                                         "%s\n", SDL_GetError());
684                         free(data);
685                         return;
686                 }
687
688                 icon->pixels = data;
689                 SDL_SetPalette(icon, SDL_PHYSPAL|SDL_LOGPAL, palette, 0, colors);
690                 SDL_SetColorKey(icon, SDL_SRCCOLORKEY, thenone);
691         }
692
693         SDL_WM_SetIcon(icon, NULL);
694 }
695
696
697 static void VID_SetCaption(void)
698 {
699         SDL_WM_SetCaption( gamename, NULL );
700 }
701 #endif
702
703 static void VID_OutputVersion(void)
704 {
705         const SDL_version *version;
706         version = SDL_Linked_Version();
707         Con_Printf(     "Linked against SDL version %d.%d.%d\n"
708                                         "Using SDL library version %d.%d.%d\n",
709                                         SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL,
710                                         version->major, version->minor, version->patch );
711 }
712
713 qboolean VID_InitMode(viddef_mode_t *mode)
714 {
715         int i;
716         static int notfirstvideomode = false;
717         int flags = SDL_OPENGL;
718         const char *drivername;
719
720         win_half_width = mode->width>>1;
721         win_half_height = mode->height>>1;
722
723         if(vid_resizable.integer)
724                 flags |= SDL_RESIZABLE;
725
726         VID_OutputVersion();
727
728         /*
729         SDL Hack
730                 We cant switch from one OpenGL video mode to another.
731                 Thus we first switch to some stupid 2D mode and then back to OpenGL.
732         */
733         if (notfirstvideomode)
734                 SDL_SetVideoMode( 0, 0, 0, 0 );
735         notfirstvideomode = true;
736
737         // SDL usually knows best
738         drivername = NULL;
739
740 // 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
741         i = COM_CheckParm("-gl_driver");
742         if (i && i < com_argc - 1)
743                 drivername = com_argv[i + 1];
744         if (SDL_GL_LoadLibrary(drivername) < 0)
745         {
746                 Con_Printf("Unable to load GL driver \"%s\": %s\n", drivername, SDL_GetError());
747                 return false;
748         }
749
750         if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL)
751         {
752                 VID_Shutdown();
753                 Con_Print("Required OpenGL function glGetString not found\n");
754                 return false;
755         }
756
757         // Knghtbrd: should do platform-specific extension string function here
758
759         vid_isfullscreen = false;
760         if (mode->fullscreen) {
761                 flags |= SDL_FULLSCREEN;
762                 vid_isfullscreen = true;
763         }
764         //flags |= SDL_HWSURFACE;
765
766         SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
767         if (mode->bitsperpixel >= 32)
768         {
769                 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 8);
770                 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 8);
771                 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 8);
772                 SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 8);
773                 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 24);
774                 SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 8);
775         }
776         else
777         {
778                 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 5);
779                 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 5);
780                 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 5);
781                 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
782         }
783         if (mode->stereobuffer)
784                 SDL_GL_SetAttribute (SDL_GL_STEREO, 1);
785         if (vid_vsync.integer)
786                 SDL_GL_SetAttribute (SDL_GL_SWAP_CONTROL, 1);
787         else
788                 SDL_GL_SetAttribute (SDL_GL_SWAP_CONTROL, 0);
789         if (mode->samples > 1)
790         {
791                 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLEBUFFERS, 1);
792                 SDL_GL_SetAttribute (SDL_GL_MULTISAMPLESAMPLES, mode->samples);
793         }
794
795         video_bpp = mode->bitsperpixel;
796         video_flags = flags;
797         VID_SetIcon();
798         screen = SDL_SetVideoMode(mode->width, mode->height, mode->bitsperpixel, flags);
799
800         if (screen == NULL)
801         {
802                 Con_Printf("Failed to set video mode to %ix%i: %s\n", mode->width, mode->height, SDL_GetError());
803                 VID_Shutdown();
804                 return false;
805         }
806
807         // set window title
808         VID_SetCaption();
809         // set up an event filter to ask confirmation on close button in WIN32
810         SDL_SetEventFilter( (SDL_EventFilter) Sys_EventFilter );
811         // init keyboard
812         SDL_EnableUNICODE( SDL_ENABLE );
813         // enable key repeat since everyone expects it
814         SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
815
816         gl_platform = "SDL";
817         gl_platformextensions = "";
818
819         GL_Init();
820
821         vid_numjoysticks = SDL_NumJoysticks();
822         vid_numjoysticks = bound(0, vid_numjoysticks, MAX_JOYSTICKS);
823         Cvar_SetValueQuick(&joy_detected, vid_numjoysticks);
824         Con_Printf("%d SDL joystick(s) found:\n", vid_numjoysticks);
825         memset(vid_joysticks, 0, sizeof(vid_joysticks));
826         for (i = 0;i < vid_numjoysticks;i++)
827         {
828                 SDL_Joystick *joy;
829                 joy = vid_joysticks[i] = SDL_JoystickOpen(i);
830                 if (!joy)
831                 {
832                         Con_Printf("joystick #%i: open failed: %s\n", i, SDL_GetError());
833                         continue;
834                 }
835                 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));
836         }
837
838         vid_hidden = false;
839         vid_activewindow = false;
840         vid_usingmouse = false;
841         vid_usinghidecursor = false;
842
843         SDL_WM_GrabInput(SDL_GRAB_OFF);
844         return true;
845 }
846
847 void VID_Shutdown (void)
848 {
849         VID_SetMouse(false, false, false);
850         VID_RestoreSystemGamma();
851
852         SDL_QuitSubSystem(SDL_INIT_VIDEO);
853
854         gl_driver[0] = 0;
855         gl_extensions = "";
856         gl_platform = "";
857         gl_platformextensions = "";
858 }
859
860 int VID_SetGamma (unsigned short *ramps, int rampsize)
861 {
862         return !SDL_SetGammaRamp (ramps, ramps + rampsize, ramps + rampsize*2);
863 }
864
865 int VID_GetGamma (unsigned short *ramps, int rampsize)
866 {
867         return !SDL_GetGammaRamp (ramps, ramps + rampsize, ramps + rampsize*2);
868 }
869
870 void VID_Finish (void)
871 {
872         Uint8 appstate;
873
874         //react on appstate changes
875         appstate = SDL_GetAppState();
876
877         vid_hidden = !(appstate & SDL_APPACTIVE);
878
879         if( vid_hidden || !( appstate & SDL_APPMOUSEFOCUS ) || !( appstate & SDL_APPINPUTFOCUS ) )
880                 vid_activewindow = false;
881         else
882                 vid_activewindow = true;
883
884         VID_UpdateGamma(false, 256);
885
886         if (!vid_hidden)
887         {
888                 CHECKGLERROR
889                 if (r_speeds.integer == 2 || gl_finish.integer)
890                 {
891                         qglFinish();CHECKGLERROR
892                 }
893                 SDL_GL_SwapBuffers();
894         }
895 }
896
897 size_t VID_ListModes(vid_mode_t *modes, size_t maxcount)
898 {
899         size_t k;
900         SDL_Rect **vidmodes;
901         int bpp = SDL_GetVideoInfo()->vfmt->BitsPerPixel;
902
903         k = 0;
904         for(vidmodes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE); *vidmodes; ++vidmodes)
905         {
906                 if(k >= maxcount)
907                         break;
908                 modes[k].width = (*vidmodes)->w;
909                 modes[k].height = (*vidmodes)->h;
910                 modes[k].bpp = bpp;
911                 modes[k].refreshrate = 60; // no support for refresh rate in SDL
912                 modes[k].pixelheight_num = 1;
913                 modes[k].pixelheight_denom = 1; // SDL does not provide this
914                 ++k;
915         }
916         return k;
917 }