changed a lot of Con_DPrint/Con_DPrintf calls to Con_Print/Con_Printf (non-technical...
[xonotic/darkplaces.git] / vid_sdl.c
index 69a11d7..ce9f0f6 100644 (file)
--- a/vid_sdl.c
+++ b/vid_sdl.c
@@ -16,14 +16,6 @@ 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>
 
@@ -36,8 +28,6 @@ static int vid_isfullscreen;
 
 static SDL_Surface *screen;
 
-static Uint8 in_mouse_buttons;
-
 static void IN_Init( void );
 static void IN_Shutdown( void );
 
@@ -205,7 +195,7 @@ 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;
@@ -228,32 +218,22 @@ void IN_Commands (void)
 {
 }
 
-static void IN_MouseMove (usercmd_t *cmd)
+static void IN_MouseMove (void)
 {
        int x, y;
-       Uint8 state;
-       int i;
 
        if( !vid_usingmouse ) {
-               IN_Mouse( cmd, 0, 0 );
+               IN_Mouse( 0, 0 );
                return;
        }
 
-       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 );
+       SDL_GetRelativeMouseState( &x, &y );
+       IN_Mouse( x, y );
 }
 
-void IN_Move( usercmd_t *cmd )
+void IN_Move( void )
 {
-       IN_MouseMove( cmd );    
+       IN_MouseMove(); 
 }
 
 static void IN_Init( void )
@@ -262,7 +242,6 @@ static void IN_Init( void )
        SDL_EnableUNICODE( SDL_ENABLE );
 
        // init mouse
-       in_mouse_buttons = SDL_GetMouseState( NULL, NULL );
        vid_usingmouse = false;
 }
 
@@ -302,10 +281,26 @@ void Sys_SendKeyEvents( void )
                                break;
                        case SDL_ACTIVEEVENT:
                                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,12 +327,52 @@ 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 ) );
+       icon = LoadIcon( NULL, IDI_ERROR );
+       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;
 
+       VID_OutputVersion();
+
        /* 
        SDL Hack 
                We cant switch from one OpenGL video mode to another.
@@ -345,14 +380,10 @@ int VID_InitMode(int fullscreen, int width, int height, int bpp)
        */
        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];
@@ -379,11 +410,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,6 +435,7 @@ 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);
@@ -440,7 +484,8 @@ void VID_Finish (void)
        Uint8 appstate;
        int vid_usemouse;
 
-       qglFinish();
+       if (r_speeds.integer || gl_finish.integer)
+               qglFinish();
        SDL_GL_SwapBuffers();
 
        //react on appstate changes