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