2 Copyright (C) 2003 T. Joseph Carter
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.
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.
13 See the GNU General Public License for more details.
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.
24 // Tell startup code that we have a client
25 int cl_available = true;
26 static qboolean vid_usingmouse;
27 static qboolean vid_isfullscreen;
29 static SDL_Surface *screen;
31 /////////////////////////
34 //TODO: Add joystick support
35 //TODO: Add error checking
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[] =
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,
50 K_ENTER, //SDLK_RETURN = 13,
52 K_PAUSE, //SDLK_PAUSE = 19,
54 K_ESCAPE, //SDLK_ESCAPE = 27,
56 K_SPACE, //SDLK_SPACE = 32,
57 '!', //SDLK_EXCLAIM = 33,
58 '"', //SDLK_QUOTEDBL = 34,
59 '#', //SDLK_HASH = 35,
60 '$', //SDLK_DOLLAR = 36,
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,
82 ':', //SDLK_COLON = 58,
83 ';', //SDLK_SEMICOLON = 59,
84 '<', //SDLK_LESS = 60,
85 '=', //SDLK_EQUALS = 61,
86 '>', //SDLK_GREATER = 62,
87 '?', //SDLK_QUESTION = 63,
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,
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,
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 */
195 static int MapKey( unsigned int sdlkey )
197 if( sdlkey > sizeof(tbl_sdltoquake)/ sizeof(int) )
199 return tbl_sdltoquake[ sdlkey ];
202 static void IN_Activate( qboolean grab )
208 vid_usingmouse = true;
209 cl_ignoremousemove = true;
210 SDL_WM_GrabInput( SDL_GRAB_ON );
211 SDL_ShowCursor( SDL_DISABLE );
218 vid_usingmouse = false;
219 cl_ignoremousemove = true;
220 SDL_WM_GrabInput( SDL_GRAB_OFF );
221 SDL_ShowCursor( SDL_ENABLE );
231 SDL_GetRelativeMouseState( &x, &y );
237 /////////////////////
241 static int Sys_EventFilter( SDL_Event *event )
243 //TODO: Add a quit query in linux, too - though linux user are more likely to know what they do
245 if( event->type == SDL_QUIT && MessageBox( NULL, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION ) == IDNO )
254 void Sys_SendKeyEvents( void )
258 while( SDL_PollEvent( &event ) )
259 switch( event.type ) {
265 Key_Event( MapKey( event.key.keysym.sym ), (char)event.key.keysym.unicode, (event.key.state == SDL_PRESSED) );
267 case SDL_ACTIVEEVENT:
268 if( event.active.state == SDL_APPACTIVE )
270 if( event.active.gain )
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 );
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 );
297 void *GL_GetProcAddress(const char *name)
300 p = SDL_GL_GetProcAddress(name);
304 static int Sys_EventFilter( SDL_Event *event );
307 if (SDL_Init(SDL_INIT_VIDEO) < 0)
308 Sys_Error ("Failed to init video: %s\n", SDL_GetError());
309 vid_isfullscreen = false;
312 // set the icon (we dont use SDL here since it would be too much a PITA)
314 #include "resource.h"
315 #include <SDL_syswm.h>
316 static void VID_SetCaption()
322 SDL_WM_SetCaption( gamename, NULL );
324 // get the HWND handle
325 SDL_VERSION( &info.version );
326 if( !SDL_GetWMInfo( &info ) )
329 icon = LoadIcon( GetModuleHandle( NULL ), MAKEINTRESOURCE( IDI_ICON1 ) );
330 SetClassLong( info.window, GCL_HICON, (LONG) icon );
333 static void VID_SetCaption()
335 SDL_WM_SetCaption( gamename, NULL );
339 static void VID_OutputVersion()
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 );
349 int VID_InitMode(int fullscreen, int width, int height, int bpp)
352 int flags = SDL_OPENGL;
353 const char *drivername;
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.
363 SDL_SetVideoMode( 0, 0, 0, 0 );
366 // SDL usually knows best
369 // 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
370 i = COM_CheckParm("-gl_driver");
371 if (i && i < com_argc - 1)
372 drivername = com_argv[i + 1];
373 if (SDL_GL_LoadLibrary(drivername) < 0)
375 Con_Printf("Unable to load GL driver \"%s\": %s\n", drivername, SDL_GetError());
379 qglGetString = GL_GetProcAddress("glGetString");
381 // Knghtbrd: should do platform-specific extension string function here
383 if (qglGetString == NULL)
386 Con_Print("Required OpenGL function glGetString not found\n");
390 vid_isfullscreen = false;
392 flags |= SDL_FULLSCREEN;
393 vid_isfullscreen = true;
396 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
399 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 8);
400 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 8);
401 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 8);
402 SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 8);
403 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 24);
404 SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 8);
408 SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 1);
409 SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 1);
410 SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 1);
411 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
414 screen = SDL_SetVideoMode(width, height, bpp, flags);
417 Con_Printf("Failed to set video mode to %ix%i: %s\n", width, height, SDL_GetError);
424 // set up an event filter to ask confirmation on close button in WIN32
425 SDL_SetEventFilter( (SDL_EventFilter) Sys_EventFilter );
427 SDL_EnableUNICODE( SDL_ENABLE );
428 // enable key repeat since everyone expects it
429 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
431 gl_renderer = qglGetString(GL_RENDERER);
432 gl_vendor = qglGetString(GL_VENDOR);
433 gl_version = qglGetString(GL_VERSION);
434 gl_extensions = qglGetString(GL_EXTENSIONS);
436 // Knghtbrd: should assign platform-specific extensions here
438 gl_platformextensions = "";
439 gl_videosyncavailable = false;
444 vid_activewindow = false;
445 vid_usingmouse = false;
449 void VID_Shutdown (void)
452 SDL_QuitSubSystem(SDL_INIT_VIDEO);
455 int VID_SetGamma (unsigned short *ramps)
457 return !SDL_SetGammaRamp (ramps, ramps + 256, ramps + 512);
460 int VID_GetGamma (unsigned short *ramps)
462 return !SDL_GetGammaRamp( ramps, ramps + 256, ramps + 512);
465 void VID_Finish (void)
468 qboolean vid_usemouse;
470 if (r_speeds.integer || gl_finish.integer)
472 SDL_GL_SwapBuffers();
474 //react on appstate changes
475 appstate = SDL_GetAppState();
477 if( !( appstate & SDL_APPMOUSEFOCUS ) || !( appstate & SDL_APPINPUTFOCUS ) )
478 vid_activewindow = false;
480 vid_activewindow = true;
482 vid_usemouse = false;
483 if( vid_mouse.integer && !key_consoleactive && !cls.demoplayback )
485 if( vid_isfullscreen )
487 if( !vid_activewindow )
488 vid_usemouse = false;
490 IN_Activate(vid_usemouse);