gcc 4.0 signedness warning fixes and uninitialized vector fixes
[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 #include <SDL.h>
20 #include <stdio.h>
21
22 #include "quakedef.h"
23
24 // Tell startup code that we have a client
25 int cl_available = true;
26 static qboolean vid_usingmouse;
27 static qboolean vid_isfullscreen;
28
29 static SDL_Surface *screen;
30
31 /////////////////////////
32 // Input handling
33 ////
34 //TODO: Add joystick support
35 //TODO: Add error checking
36
37
38 //keysym to quake keysym mapping
39 #define tenoh   0,0,0,0,0, 0,0,0,0,0
40 #define fiftyoh tenoh, tenoh, tenoh, tenoh, tenoh
41 #define hundredoh fiftyoh, fiftyoh
42 static unsigned int tbl_sdltoquake[] =
43 {
44         0,0,0,0,                //SDLK_UNKNOWN          = 0,
45         0,0,0,0,                //SDLK_FIRST            = 0,
46         K_BACKSPACE,    //SDLK_BACKSPACE        = 8,
47         K_TAB,                  //SDLK_TAB                      = 9,
48         0,0,
49         0,                              //SDLK_CLEAR            = 12,
50         K_ENTER,                //SDLK_RETURN           = 13,
51     0,0,0,0,0,
52         K_PAUSE,                //SDLK_PAUSE            = 19,
53         0,0,0,0,0,0,0,
54         K_ESCAPE,               //SDLK_ESCAPE           = 27,
55         0,0,0,0,
56         K_SPACE,                //SDLK_SPACE            = 32,
57         '!',                    //SDLK_EXCLAIM          = 33,
58         '"',                    //SDLK_QUOTEDBL         = 34,
59         '#',                    //SDLK_HASH                     = 35,
60         '$',                    //SDLK_DOLLAR           = 36,
61         0,
62         '&',                    //SDLK_AMPERSAND        = 38,
63         '\'',                   //SDLK_QUOTE            = 39,
64         '(',                    //SDLK_LEFTPAREN        = 40,
65         ')',                    //SDLK_RIGHTPAREN       = 41,
66         '*',                    //SDLK_ASTERISK         = 42,
67         '+',                    //SDLK_PLUS                     = 43,
68         ',',                    //SDLK_COMMA            = 44,
69         '-',                    //SDLK_MINUS            = 45,
70         '.',                    //SDLK_PERIOD           = 46,
71         '/',                    //SDLK_SLASH            = 47,
72         '0',                    //SDLK_0                        = 48,
73         '1',                    //SDLK_1                        = 49,
74         '2',                    //SDLK_2                        = 50,
75         '3',                    //SDLK_3                        = 51,
76         '4',                    //SDLK_4                        = 52,
77         '5',                    //SDLK_5                        = 53,
78         '6',                    //SDLK_6                        = 54,
79         '7',                    //SDLK_7                        = 55,
80         '8',                    //SDLK_8                        = 56,
81         '9',                    //SDLK_9                        = 57,
82         ':',                    //SDLK_COLON            = 58,
83         ';',                    //SDLK_SEMICOLON        = 59,
84         '<',                    //SDLK_LESS                     = 60,
85         '=',                    //SDLK_EQUALS           = 61,
86         '>',                    //SDLK_GREATER          = 62,
87         '?',                    //SDLK_QUESTION         = 63,
88         '@',                    //SDLK_AT                       = 64,
89         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,
90         '[',            //SDLK_LEFTBRACKET      = 91,
91         '\\',           //SDLK_BACKSLASH        = 92,
92         ']',            //SDLK_RIGHTBRACKET     = 93,
93         '^',            //SDLK_CARET            = 94,
94         '_',            //SDLK_UNDERSCORE       = 95,
95         '`',            //SDLK_BACKQUOTE        = 96,
96         'a',            //SDLK_a                        = 97,
97         'b',            //SDLK_b                        = 98,
98         'c',            //SDLK_c                        = 99,
99         'd',            //SDLK_d                        = 100,
100         'e',            //SDLK_e                        = 101,
101         'f',            //SDLK_f                        = 102,
102         'g',            //SDLK_g                        = 103,
103         'h',            //SDLK_h                        = 104,
104         'i',            //SDLK_i                        = 105,
105         'j',            //SDLK_j                        = 106,
106         'k',            //SDLK_k                        = 107,
107         'l',            //SDLK_l                        = 108,
108         'm',            //SDLK_m                        = 109,
109         'n',            //SDLK_n                        = 110,
110         'o',            //SDLK_o                        = 111,
111         'p',            //SDLK_p                        = 112,
112         'q',            //SDLK_q                        = 113,
113         'r',            //SDLK_r                        = 114,
114         's',            //SDLK_s                        = 115,
115         't',            //SDLK_t                        = 116,
116         'u',            //SDLK_u                        = 117,
117         'v',            //SDLK_v                        = 118,
118         'w',            //SDLK_w                        = 119,
119         'x',            //SDLK_x                        = 120,
120         'y',            //SDLK_y                        = 121,
121         'z',            //SDLK_z                        = 122,
122         0,0,0,0,
123         K_DEL,          //SDLK_DELETE           = 127,
124         hundredoh /*227*/, tenoh, tenoh, 0,0,0,0,0,0,0,0,
125         K_KP_0,         //SDLK_KP0              = 256,
126         K_KP_1,         //SDLK_KP1              = 257,
127         K_KP_2,         //SDLK_KP2              = 258,
128         K_KP_3,         //SDLK_KP3              = 259,
129         K_KP_4,         //SDLK_KP4              = 260,
130         K_KP_5,         //SDLK_KP5              = 261,
131         K_KP_6,         //SDLK_KP6              = 262,
132         K_KP_7,         //SDLK_KP7              = 263,
133         K_KP_8,         //SDLK_KP8              = 264,
134         K_KP_9,         //SDLK_KP9              = 265,
135         K_KP_PERIOD,//SDLK_KP_PERIOD    = 266,
136         K_KP_DIVIDE,//SDLK_KP_DIVIDE    = 267,
137         K_KP_MULTIPLY,//SDLK_KP_MULTIPLY= 268,
138         K_KP_MINUS,     //SDLK_KP_MINUS         = 269,
139         K_KP_PLUS,      //SDLK_KP_PLUS          = 270,
140         K_KP_ENTER,     //SDLK_KP_ENTER         = 271,
141         K_KP_EQUALS,//SDLK_KP_EQUALS    = 272,
142         K_UPARROW,      //SDLK_UP               = 273,
143         K_DOWNARROW,//SDLK_DOWN         = 274,
144         K_RIGHTARROW,//SDLK_RIGHT       = 275,
145         K_LEFTARROW,//SDLK_LEFT         = 276,
146         K_INS,          //SDLK_INSERT   = 277,
147         K_HOME,         //SDLK_HOME             = 278,
148         K_END,          //SDLK_END              = 279,
149         K_PGUP,         //SDLK_PAGEUP   = 280,
150         K_PGDN,         //SDLK_PAGEDOWN = 281,
151         K_F1,           //SDLK_F1               = 282,
152         K_F2,           //SDLK_F2               = 283,
153         K_F3,           //SDLK_F3               = 284,
154         K_F4,           //SDLK_F4               = 285,
155         K_F5,           //SDLK_F5               = 286,
156         K_F6,           //SDLK_F6               = 287,
157         K_F7,           //SDLK_F7               = 288,
158         K_F8,           //SDLK_F8               = 289,
159         K_F9,           //SDLK_F9               = 290,
160         K_F10,          //SDLK_F10              = 291,
161         K_F11,          //SDLK_F11              = 292,
162         K_F12,          //SDLK_F12              = 293,
163         0,                      //SDLK_F13              = 294,
164         0,                      //SDLK_F14              = 295,
165         0,                      //SDLK_F15              = 296,
166         0,0,0,
167         K_NUMLOCK,      //SDLK_NUMLOCK  = 300,
168         K_CAPSLOCK,     //SDLK_CAPSLOCK = 301,
169         K_SCROLLOCK,//SDLK_SCROLLOCK= 302,
170         K_SHIFT,        //SDLK_RSHIFT   = 303,
171         K_SHIFT,        //SDLK_LSHIFT   = 304,
172         K_CTRL,         //SDLK_RCTRL    = 305,
173         K_CTRL,         //SDLK_LCTRL    = 306,
174         K_ALT,          //SDLK_RALT             = 307,
175         K_ALT,          //SDLK_LALT             = 308,
176         0,                      //SDLK_RMETA    = 309,
177         0,                      //SDLK_LMETA    = 310,
178         0,                      //SDLK_LSUPER   = 311,          /* Left "Windows" key */
179         0,                      //SDLK_RSUPER   = 312,          /* Right "Windows" key */
180         0,                      //SDLK_MODE             = 313,          /* "Alt Gr" key */
181         0,                      //SDLK_COMPOSE  = 314,          /* Multi-key compose key */
182         0,                      //SDLK_HELP             = 315,
183         0,                      //SDLK_PRINT    = 316,
184         0,                      //SDLK_SYSREQ   = 317,
185         K_PAUSE,        //SDLK_BREAK    = 318,
186         0,                      //SDLK_MENU             = 319,
187         0,                      //SDLK_POWER    = 320,          /* Power Macintosh power key */
188         'e',            //SDLK_EURO             = 321,          /* Some european keyboards */
189         0                       //SDLK_UNDO             = 322,          /* Atari keyboard has Undo */
190 };
191 #undef tenoh
192 #undef fiftyoh
193 #undef hundredoh
194
195 static int MapKey( unsigned int sdlkey )
196 {
197         if( sdlkey > sizeof(tbl_sdltoquake)/ sizeof(int) )
198                 return 0;
199     return tbl_sdltoquake[ sdlkey ];
200 }
201
202 static void IN_Activate( qboolean grab )
203 {
204         if (grab)
205         {
206                 if (!vid_usingmouse)
207                 {
208                         vid_usingmouse = true;
209                         cl_ignoremousemove = true;
210                         SDL_WM_GrabInput( SDL_GRAB_ON );
211                         SDL_ShowCursor( SDL_DISABLE );
212                 }
213         }
214         else
215         {
216                 if (vid_usingmouse)
217                 {
218                         vid_usingmouse = false;
219                         cl_ignoremousemove = true;
220                         SDL_WM_GrabInput( SDL_GRAB_OFF );
221                         SDL_ShowCursor( SDL_ENABLE );
222                 }
223         }
224 }
225
226 void IN_Move( void )
227 {
228         if( vid_usingmouse )
229         {
230                 int x, y;
231                 SDL_GetRelativeMouseState( &x, &y );
232                 in_mouse_x = x;
233                 in_mouse_y = y;
234         }
235 }
236
237 /////////////////////
238 // Message Handling
239 ////
240
241 static int Sys_EventFilter( SDL_Event *event )
242 {
243         //TODO: Add a quit query in linux, too - though linux user are more likely to know what they do
244 #ifdef WIN32
245         if( event->type == SDL_QUIT && MessageBox( NULL, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION ) == IDNO )
246                 return 0;
247         else
248                 return 1;
249 #else
250         return 1;
251 #endif
252 }
253
254 void Sys_SendKeyEvents( void )
255 {
256         SDL_Event event;
257
258         while( SDL_PollEvent( &event ) )
259                 switch( event.type ) {
260                         case SDL_QUIT:
261                                 Sys_Quit();
262                                 break;
263                         case SDL_KEYDOWN:
264                         case SDL_KEYUP:
265                                 Key_Event( MapKey( event.key.keysym.sym ), (char)event.key.keysym.unicode, (event.key.state == SDL_PRESSED) );
266                                 break;
267                         case SDL_ACTIVEEVENT:
268                                 if( event.active.state == SDL_APPACTIVE )
269                                 {
270                                         if( event.active.gain )
271                                                 vid_hidden = false;
272                                         else
273                                                 vid_hidden = true;
274                                 }
275                                 break;
276                         case SDL_MOUSEBUTTONDOWN:
277                                 if( event.button.button == SDL_BUTTON_MIDDLE )
278                                         event.button.button = SDL_BUTTON_RIGHT;
279                                 else if( event.button.button == SDL_BUTTON_RIGHT )
280                                         event.button.button = SDL_BUTTON_MIDDLE;
281                                 Key_Event( K_MOUSE1 + event.button.button - 1, 0, true );
282                                 break;
283                         case SDL_MOUSEBUTTONUP:
284                                 if( event.button.button == SDL_BUTTON_MIDDLE )
285                                         event.button.button = SDL_BUTTON_RIGHT;
286                                 else if( event.button.button == SDL_BUTTON_RIGHT )
287                                         event.button.button = SDL_BUTTON_MIDDLE;
288                                 Key_Event( K_MOUSE1 + event.button.button - 1, 0, false );
289                                 break;
290                 }
291 }
292
293 /////////////////
294 // Video system
295 ////
296
297 void *GL_GetProcAddress(const char *name)
298 {
299         void *p = NULL;
300         p = SDL_GL_GetProcAddress(name);
301         return p;
302 }
303
304 static int Sys_EventFilter( SDL_Event *event );
305 void VID_Init (void)
306 {
307         if (SDL_Init(SDL_INIT_VIDEO) < 0)
308                 Sys_Error ("Failed to init video: %s\n", SDL_GetError());
309         vid_isfullscreen = false;
310 }
311
312 // set the icon (we dont use SDL here since it would be too much a PITA)
313 #ifdef WIN32
314 #include "resource.h"
315 #include <SDL_syswm.h>
316 static void VID_SetCaption()
317 {
318     SDL_SysWMinfo       info;
319         HICON                   icon;
320
321         // set the caption
322         SDL_WM_SetCaption( gamename, NULL );
323
324         // get the HWND handle
325     SDL_VERSION( &info.version );
326         if( !SDL_GetWMInfo( &info ) )
327                 return;
328
329         icon = LoadIcon( GetModuleHandle( NULL ), MAKEINTRESOURCE( IDI_ICON1 ) );
330         SetClassLong( info.window, GCL_HICON, (LONG) icon );
331 }
332 #else
333 static void VID_SetCaption()
334 {
335         SDL_WM_SetCaption( gamename, NULL );
336 }
337 #endif
338
339 static void VID_OutputVersion()
340 {
341         const SDL_version *version;
342         version = SDL_Linked_Version();
343         Con_Printf(     "Linked against SDL version %d.%d.%d\n"
344                                         "Using SDL library version %d.%d.%d\n",
345                                         SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL,
346                                         version->major, version->minor, version->patch );
347 }
348
349 int VID_InitMode(int fullscreen, int width, int height, int bpp)
350 {
351         int i;
352         int flags = SDL_OPENGL;
353         const char *drivername;
354
355         VID_OutputVersion();
356
357         /*
358         SDL Hack
359                 We cant switch from one OpenGL video mode to another.
360                 Thus we first switch to some stupid 2D mode and then back to OpenGL.
361         */
362         SDL_SetVideoMode( 0, 0, 0, 0 );
363
364         // SDL usually knows best
365         drivername = NULL;
366
367 // 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
368         i = COM_CheckParm("-gl_driver");
369         if (i && i < com_argc - 1)
370                 drivername = com_argv[i + 1];
371         if (SDL_GL_LoadLibrary(drivername) < 0)
372         {
373                 Con_Printf("Unable to load GL driver \"%s\": %s\n", drivername, SDL_GetError());
374                 return false;
375         }
376
377         qglGetString = GL_GetProcAddress("glGetString");
378
379         // Knghtbrd: should do platform-specific extension string function here
380
381         if (qglGetString == NULL)
382         {
383                 VID_Shutdown();
384                 Con_Print("Required OpenGL function glGetString not found\n");
385                 return false;
386         }
387
388         vid_isfullscreen = false;
389         if (fullscreen) {
390                 flags |= SDL_FULLSCREEN;
391                 vid_isfullscreen = true;
392         }
393
394         SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
395         if (bpp >= 32)
396         {
397                 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 8);
398                 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 8);
399                 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 8);
400                 SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 8);
401                 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 24);
402                 SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 8);
403         }
404         else
405         {
406                 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 1);
407                 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 1);
408                 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 1);
409                 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
410         }
411
412         screen = SDL_SetVideoMode(width, height, bpp, flags);
413         if (screen == NULL)
414         {
415                 Con_Printf("Failed to set video mode to %ix%i: %s\n", width, height, SDL_GetError);
416                 VID_Shutdown();
417                 return false;
418         }
419
420         // set window title
421         VID_SetCaption();
422         // set up an event filter to ask confirmation on close button in WIN32
423         SDL_SetEventFilter( (SDL_EventFilter) Sys_EventFilter );
424         // init keyboard
425         SDL_EnableUNICODE( SDL_ENABLE );
426         // enable key repeat since everyone expects it
427         SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
428
429         gl_renderer = (const char *)qglGetString(GL_RENDERER);
430         gl_vendor = (const char *)qglGetString(GL_VENDOR);
431         gl_version = (const char *)qglGetString(GL_VERSION);
432         gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
433         gl_platform = "SDL";
434         // Knghtbrd: should assign platform-specific extensions here
435         //TODO: maybe ;)
436         gl_platformextensions = "";
437         gl_videosyncavailable = false;
438
439         GL_Init();
440
441         vid_hidden = false;
442         vid_activewindow = false;
443         vid_usingmouse = false;
444         return true;
445 }
446
447 void VID_Shutdown (void)
448 {
449         IN_Activate(false);
450         SDL_QuitSubSystem(SDL_INIT_VIDEO);
451 }
452
453 int VID_SetGamma (unsigned short *ramps)
454 {
455         return !SDL_SetGammaRamp (ramps, ramps + 256, ramps + 512);
456 }
457
458 int VID_GetGamma (unsigned short *ramps)
459 {
460         return !SDL_GetGammaRamp( ramps, ramps + 256, ramps + 512);
461 }
462
463 void VID_Finish (void)
464 {
465         Uint8 appstate;
466         qboolean vid_usemouse;
467
468         if (r_speeds.integer || gl_finish.integer)
469                 qglFinish();
470         SDL_GL_SwapBuffers();
471
472         //react on appstate changes
473         appstate = SDL_GetAppState();
474
475         if( !( appstate & SDL_APPMOUSEFOCUS ) || !( appstate & SDL_APPINPUTFOCUS ) )
476                 vid_activewindow = false;
477         else
478                 vid_activewindow = true;
479
480         vid_usemouse = false;
481         if( vid_mouse.integer && !key_consoleactive && !cls.demoplayback )
482                 vid_usemouse = true;
483         if( vid_isfullscreen )
484                 vid_usemouse = true;
485         if( !vid_activewindow )
486                 vid_usemouse = false;
487
488         IN_Activate(vid_usemouse);
489 }