]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - vid_sdl.c
major cleanup of input code - CL_Move replaces most of IN_Move, IN_Commands, many...
[xonotic/darkplaces.git] / vid_sdl.c
index 69a11d756d8034df4b58939df31cde6d701d3882..1aee0c8b8c1ccdc063de726f0e6909ed3b2b2bc0 100644 (file)
--- a/vid_sdl.c
+++ b/vid_sdl.c
@@ -15,14 +15,6 @@ See the GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-*/
-/*
-TODOs until first commit:
-       1. Hide and/or show the mouse whether the mainwindow is focused or not.
-       2. Minimize when losing focus in fullscreen mode.
-d      3. GrabInput while mainwindow is active.
-       4
-
 */
 #include <SDL.h>
 #include <stdio.h>
@@ -31,13 +23,11 @@ d   3. GrabInput while mainwindow is active.
 
 // Tell startup code that we have a client
 int cl_available = true;
-static int vid_usingmouse;
-static int vid_isfullscreen;
+static qboolean vid_usingmouse;
+static qboolean vid_isfullscreen;
 
 static SDL_Surface *screen;
 
-static Uint8 in_mouse_buttons;
-
 static void IN_Init( void );
 static void IN_Shutdown( void );
 
@@ -52,7 +42,7 @@ static void IN_Shutdown( void );
 #define tenoh  0,0,0,0,0, 0,0,0,0,0
 #define fiftyoh tenoh, tenoh, tenoh, tenoh, tenoh
 #define hundredoh fiftyoh, fiftyoh
-static unsigned int tbl_sdltoquake[] = 
+static unsigned int tbl_sdltoquake[] =
 {
        0,0,0,0,                //SDLK_UNKNOWN          = 0,
        0,0,0,0,                //SDLK_FIRST            = 0,
@@ -205,64 +195,54 @@ static unsigned int tbl_sdltoquake[] =
 #undef fiftyoh
 #undef hundredoh
 
-static int MapKey( int sdlkey )
+static int MapKey( unsigned int sdlkey )
 {
        if( sdlkey > sizeof(tbl_sdltoquake)/ sizeof(int) )
                return 0;
     return tbl_sdltoquake[ sdlkey ];
 }
 
-static void IN_Activate( void )
-{
-       SDL_WM_GrabInput( SDL_GRAB_ON );
-       SDL_ShowCursor( SDL_DISABLE );
-}
-
-static void IN_Deactivate( void )
+static void IN_Activate( qboolean grab )
 {
-       SDL_WM_GrabInput( SDL_GRAB_OFF );
-       SDL_ShowCursor( SDL_ENABLE );
-}
-
-void IN_Commands (void)
-{
-}
-
-static void IN_MouseMove (usercmd_t *cmd)
-{
-       int x, y;
-       Uint8 state;
-       int i;
-
-       if( !vid_usingmouse ) {
-               IN_Mouse( cmd, 0, 0 );
-               return;
+       if (grab)
+       {
+               if (!vid_usingmouse)
+               {
+                       vid_usingmouse = true;
+                       SDL_WM_GrabInput( SDL_GRAB_ON );
+                       SDL_ShowCursor( SDL_DISABLE );
+               }
+       }
+       else
+       {
+               if (vid_usingmouse)
+               {
+                       vid_usingmouse = false;
+                       SDL_WM_GrabInput( SDL_GRAB_OFF );
+                       SDL_ShowCursor( SDL_ENABLE );
+               }
        }
-
-       state = SDL_GetRelativeMouseState( &x, &y );
-       for( i = 0; i < 8 ; i++ )
-               if( state & ~in_mouse_buttons & 1 << i  )
-                       Key_Event( K_MOUSE1 + i, 0, true );
-               else if( ~state & in_mouse_buttons & 1 << i )
-                       Key_Event( K_MOUSE1 + i, 0, false );
-
-       in_mouse_buttons = state;
-    
-       IN_Mouse( cmd, x, y );
 }
 
-void IN_Move( usercmd_t *cmd )
+void IN_Move( void )
 {
-       IN_MouseMove( cmd );    
+       if( vid_usingmouse )
+       {
+               int x, y;
+               SDL_GetRelativeMouseState( &x, &y );
+               in_mouse_x = x;
+               in_mouse_y = y;
+       }
 }
 
 static void IN_Init( void )
 {
        // init keyboard
        SDL_EnableUNICODE( SDL_ENABLE );
+       // enable key repeat since everyone expects it
+       SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
 
        // init mouse
-       in_mouse_buttons = SDL_GetMouseState( NULL, NULL );
        vid_usingmouse = false;
 }
 
@@ -274,13 +254,13 @@ static void IN_Shutdown( void )
 // Message Handling
 ////
 
-static int Sys_EventFilter( SDL_Event *event ) 
+static int Sys_EventFilter( SDL_Event *event )
 {
        //TODO: Add a quit query in linux, too - though linux user are more likely to know what they do
 #ifdef WIN32
        if( event->type == SDL_QUIT && MessageBox( NULL, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION ) == IDNO )
                return 0;
-       else 
+       else
                return 1;
 #else
        return 1;
@@ -298,14 +278,30 @@ void Sys_SendKeyEvents( void )
                                break;
                        case SDL_KEYDOWN:
                        case SDL_KEYUP:
-                               Key_Event( MapKey( event.key.keysym.sym ), event.key.keysym.unicode, (event.key.state == SDL_PRESSED) );
+                               Key_Event( MapKey( event.key.keysym.sym ), (char)event.key.keysym.unicode, (event.key.state == SDL_PRESSED) );
                                break;
                        case SDL_ACTIVEEVENT:
-                               if( event.active.state == SDL_APPACTIVE ) 
+                               if( event.active.state == SDL_APPACTIVE )
+                               {
                                        if( event.active.gain )
                                                vid_hidden = false;
                                        else
                                                vid_hidden = true;
+                               }
+                               break;
+                       case SDL_MOUSEBUTTONDOWN:
+                               if( event.button.button == SDL_BUTTON_MIDDLE )
+                                       event.button.button = SDL_BUTTON_RIGHT;
+                               else if( event.button.button == SDL_BUTTON_RIGHT )
+                                       event.button.button = SDL_BUTTON_MIDDLE;
+                               Key_Event( K_MOUSE1 + event.button.button - 1, 0, true );
+                               break;
+                       case SDL_MOUSEBUTTONUP:
+                               if( event.button.button == SDL_BUTTON_MIDDLE )
+                                       event.button.button = SDL_BUTTON_RIGHT;
+                               else if( event.button.button == SDL_BUTTON_RIGHT )
+                                       event.button.button = SDL_BUTTON_MIDDLE;
+                               Key_Event( K_MOUSE1 + event.button.button - 1, 0, false );
                                break;
                }
 }
@@ -332,38 +328,75 @@ void VID_Init (void)
        IN_Init();
 }
 
+// set the icon (we dont use SDL here since it would be too much a PITA)
+#ifdef WIN32
+#include "resource.h"
+#include <SDL_syswm.h>
+static void VID_SetCaption()
+{
+    SDL_SysWMinfo      info;
+       HICON                   icon;
+
+       // set the caption
+       SDL_WM_SetCaption( gamename, NULL );
+
+       // get the HWND handle
+    SDL_VERSION( &info.version );
+       if( !SDL_GetWMInfo( &info ) )
+               return;
+
+       icon = LoadIcon( GetModuleHandle( NULL ), MAKEINTRESOURCE( IDI_ICON1 ) );
+       SetClassLong( info.window, GCL_HICON, (LONG) icon );
+}
+#else
+static void VID_SetCaption()
+{
+       SDL_WM_SetCaption( gamename, NULL );
+}
+#endif
+
+static void VID_OutputVersion()
+{
+       const SDL_version *version;
+       version = SDL_Linked_Version();
+       Con_Printf(     "Linked against SDL version %d.%d.%d\n"
+                                       "Using SDL library version %d.%d.%d\n",
+                                       SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL,
+                                       version->major, version->minor, version->patch );
+}
+
 int VID_InitMode(int fullscreen, int width, int height, int bpp)
 {
        int i;
        int flags = SDL_OPENGL;
        const char *drivername;
 
-       /* 
-       SDL Hack 
+       VID_OutputVersion();
+
+       /*
+       SDL Hack
                We cant switch from one OpenGL video mode to another.
                Thus we first switch to some stupid 2D mode and then back to OpenGL.
        */
+#ifndef MACOSX
        SDL_SetVideoMode( 0, 0, 0, 0 );
-
-#ifdef WIN32
-       drivername = "opengl32.dll";
-#elif defined(__APPLE__) && defined(__MACH__)
-       drivername = "OpenGL.framework";
-#else
-       drivername = "libGL.so.1";
 #endif
 
+       // SDL usually knows best
+       drivername = NULL;
+
+// 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
        i = COM_CheckParm("-gl_driver");
        if (i && i < com_argc - 1)
                drivername = com_argv[i + 1];
-       if (SDL_GL_LoadLibrary(drivername))
-       {   
-               Con_Printf("Unable to load GL driver \"%s\": ", drivername, SDL_GetError());
+       if (SDL_GL_LoadLibrary(drivername) < 0)
+       {
+               Con_Printf("Unable to load GL driver \"%s\": %s\n", drivername, SDL_GetError());
                return false;
        }
 
        qglGetString = GL_GetProcAddress("glGetString");
-       
+
        // Knghtbrd: should do platform-specific extension string function here
 
        if (qglGetString == NULL)
@@ -379,11 +412,23 @@ int VID_InitMode(int fullscreen, int width, int height, int bpp)
                vid_isfullscreen = true;
        }
 
-       SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 1);
-       SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 1);
-       SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 1);
-       SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
        SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
+       if (bpp >= 32)
+       {
+               SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 8);
+               SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 8);
+               SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 8);
+               SDL_GL_SetAttribute (SDL_GL_ALPHA_SIZE, 8);
+               SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 24);
+               SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 8);
+       }
+       else
+       {
+               SDL_GL_SetAttribute (SDL_GL_RED_SIZE, 1);
+               SDL_GL_SetAttribute (SDL_GL_GREEN_SIZE, 1);
+               SDL_GL_SetAttribute (SDL_GL_BLUE_SIZE, 1);
+               SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16);
+       }
 
        screen = SDL_SetVideoMode(width, height, bpp, flags);
        if (screen == NULL)
@@ -392,7 +437,8 @@ int VID_InitMode(int fullscreen, int width, int height, int bpp)
                VID_Shutdown();
                return false;
        }
-       
+       VID_SetCaption();
+
        gl_renderer = qglGetString(GL_RENDERER);
        gl_vendor = qglGetString(GL_VENDOR);
        gl_version = qglGetString(GL_VERSION);
@@ -402,7 +448,7 @@ int VID_InitMode(int fullscreen, int width, int height, int bpp)
        //TODO: maybe ;)
        gl_platformextensions = "";
        gl_videosyncavailable = false;
-       
+
        GL_Init();
 
        vid_hidden = false;
@@ -438,9 +484,10 @@ void VID_GetWindowSize (int *x, int *y, int *width, int *height)
 void VID_Finish (void)
 {
        Uint8 appstate;
-       int vid_usemouse;
+       qboolean vid_usemouse;
 
-       qglFinish();
+       if (r_speeds.integer || gl_finish.integer)
+               qglFinish();
        SDL_GL_SwapBuffers();
 
        //react on appstate changes
@@ -448,22 +495,16 @@ void VID_Finish (void)
 
        if( !( appstate & SDL_APPMOUSEFOCUS ) || !( appstate & SDL_APPINPUTFOCUS ) )
                vid_activewindow = false;
-       else 
+       else
                vid_activewindow = true;
 
        vid_usemouse = false;
-       if( vid_mouse.integer && !key_consoleactive )
+       if( vid_mouse.integer && !key_consoleactive && !cls.demoplayback )
                vid_usemouse = true;
        if( vid_isfullscreen )
                vid_usemouse = true;
        if( !vid_activewindow )
                vid_usemouse = false;
 
-       if( vid_usemouse && !vid_usingmouse ) {
-               vid_usingmouse = true;
-               IN_Activate();
-       } else if( !vid_usemouse && vid_usingmouse ) {
-               vid_usingmouse = false;
-               IN_Deactivate();
-       }
+       IN_Activate(vid_usemouse);
 }