]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - vid_sdl.c
modified gamma handling, now VID_UpdateGamma is only called from VID_Finish, and...
[xonotic/darkplaces.git] / vid_sdl.c
index 826d8d98a1f864a91821a3e60cf77ac2a7a8d6e7..0df9e422a0b1649ca1d809584d86c9c1bdc23d13 100644 (file)
--- a/vid_sdl.c
+++ b/vid_sdl.c
@@ -16,6 +16,7 @@ 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.
 */
+#undef WIN32_LEAN_AND_MEAN  //hush a warning, SDL.h redefines this
 #include <SDL.h>
 #include <stdio.h>
 
@@ -23,13 +24,13 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 // Tell startup code that we have a client
 int cl_available = true;
-static int vid_usingmouse;
-static int vid_isfullscreen;
 
-static SDL_Surface *screen;
+qboolean vid_supportrefreshrate = false;
+
+static qboolean vid_usingmouse;
+static qboolean vid_isfullscreen;
 
-static void IN_Init( void );
-static void IN_Shutdown( void );
+static SDL_Surface *screen;
 
 /////////////////////////
 // Input handling
@@ -42,7 +43,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,
@@ -202,72 +203,80 @@ static int MapKey( unsigned int sdlkey )
     return tbl_sdltoquake[ sdlkey ];
 }
 
-static void IN_Activate( void )
-{
-       SDL_WM_GrabInput( SDL_GRAB_ON );
-       SDL_ShowCursor( SDL_DISABLE );
-}
-
-static void IN_Deactivate( void )
-{
-       SDL_WM_GrabInput( SDL_GRAB_OFF );
-       SDL_ShowCursor( SDL_ENABLE );
-}
-
-void IN_Commands (void)
-{
-}
-
-static void IN_MouseMove (void)
+static void IN_Activate( qboolean grab )
 {
-       int x, y;
-
-       if( !vid_usingmouse ) {
-               IN_Mouse( 0, 0 );
-               return;
+       if (grab)
+       {
+               if (!vid_usingmouse)
+               {
+                       vid_usingmouse = true;
+                       cl_ignoremousemove = true;
+                       SDL_WM_GrabInput( SDL_GRAB_ON );
+                       SDL_ShowCursor( SDL_DISABLE );
+               }
+       }
+       else
+       {
+               if (vid_usingmouse)
+               {
+                       vid_usingmouse = false;
+                       cl_ignoremousemove = true;
+                       SDL_WM_GrabInput( SDL_GRAB_OFF );
+                       SDL_ShowCursor( SDL_ENABLE );
+               }
        }
-
-       SDL_GetRelativeMouseState( &x, &y );
-       IN_Mouse( x, y );
 }
 
 void IN_Move( void )
 {
-       IN_MouseMove(); 
-}
-
-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
-       vid_usingmouse = false;
-}
-
-static void IN_Shutdown( void )
-{
+       if( vid_usingmouse )
+       {
+               int x, y;
+               SDL_GetRelativeMouseState( &x, &y );
+               in_mouse_x = x;
+               in_mouse_y = y;
+       }
 }
 
 /////////////////////
 // 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;
 #endif
 }
 
+static keynum_t buttonremap[18] =
+{
+       K_MOUSE1,
+       K_MOUSE3,
+       K_MOUSE2,
+       K_MWHEELUP,
+       K_MWHEELDOWN,
+       K_MOUSE4,
+       K_MOUSE5,
+       K_MOUSE6,
+       K_MOUSE7,
+       K_MOUSE8,
+       K_MOUSE9,
+       K_MOUSE10,
+       K_MOUSE11,
+       K_MOUSE12,
+       K_MOUSE13,
+       K_MOUSE14,
+       K_MOUSE15,
+       K_MOUSE16,
+};
+
 void Sys_SendKeyEvents( void )
 {
        SDL_Event event;
@@ -282,7 +291,7 @@ void Sys_SendKeyEvents( void )
                                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;
@@ -291,18 +300,12 @@ void Sys_SendKeyEvents( void )
                                }
                                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 );
+                               if (event.button.button <= 18)
+                                       Key_Event( buttonremap[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 );
+                               if (event.button.button <= 18)
+                                       Key_Event( buttonremap[event.button.button - 1], 0, false );
                                break;
                }
 }
@@ -322,11 +325,8 @@ static int Sys_EventFilter( SDL_Event *event );
 void VID_Init (void)
 {
        if (SDL_Init(SDL_INIT_VIDEO) < 0)
-               Sys_Error ("Failed to init video: %s\n", SDL_GetError());
+               Sys_Error ("Failed to init video: %s", SDL_GetError());
        vid_isfullscreen = false;
-
-       SDL_SetEventFilter( (SDL_EventFilter) Sys_EventFilter );
-       IN_Init();
 }
 
 // set the icon (we dont use SDL here since it would be too much a PITA)
@@ -341,13 +341,18 @@ static void VID_SetCaption()
        // set the caption
        SDL_WM_SetCaption( gamename, NULL );
 
-       // get the HWND handle 
+       // 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 );
+#ifndef _W64 //If Windows 64bit data types don't exist
+#define SetClassLongPtr SetClassLong
+#define GCLP_HICON GCL_HICON
+#define LONG_PTR LONG
+#endif
+       SetClassLongPtr( info.window, GCLP_HICON, (LONG_PTR)icon );
 }
 #else
 static void VID_SetCaption()
@@ -363,10 +368,10 @@ static void VID_OutputVersion()
        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 );       
+                                       version->major, version->minor, version->patch );
 }
 
-int VID_InitMode(int fullscreen, int width, int height, int bpp)
+int VID_InitMode(int fullscreen, int width, int height, int bpp, int refreshrate)
 {
        int i;
        int flags = SDL_OPENGL;
@@ -374,14 +379,12 @@ int VID_InitMode(int fullscreen, int width, int height, int bpp)
 
        VID_OutputVersion();
 
-       /* 
-       SDL Hack 
+       /*
+       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 );
-#endif
 
        // SDL usually knows best
        drivername = NULL;
@@ -391,22 +394,20 @@ int VID_InitMode(int fullscreen, int width, int height, int bpp)
        if (i && i < com_argc - 1)
                drivername = com_argv[i + 1];
        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)
+       if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL)
        {
                VID_Shutdown();
                Con_Print("Required OpenGL function glGetString not found\n");
                return false;
        }
 
+       // Knghtbrd: should do platform-specific extension string function here
+
        vid_isfullscreen = false;
        if (fullscreen) {
                flags |= SDL_FULLSCREEN;
@@ -438,54 +439,54 @@ int VID_InitMode(int fullscreen, int width, int height, int bpp)
                VID_Shutdown();
                return false;
        }
+
+       // set window title
        VID_SetCaption();
-       
-       gl_renderer = qglGetString(GL_RENDERER);
-       gl_vendor = qglGetString(GL_VENDOR);
-       gl_version = qglGetString(GL_VERSION);
-       gl_extensions = qglGetString(GL_EXTENSIONS);
+       // set up an event filter to ask confirmation on close button in WIN32
+       SDL_SetEventFilter( (SDL_EventFilter) Sys_EventFilter );
+       // init keyboard
+       SDL_EnableUNICODE( SDL_ENABLE );
+       // enable key repeat since everyone expects it
+       SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
+
+       gl_renderer = (const char *)qglGetString(GL_RENDERER);
+       gl_vendor = (const char *)qglGetString(GL_VENDOR);
+       gl_version = (const char *)qglGetString(GL_VERSION);
+       gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
        gl_platform = "SDL";
        // Knghtbrd: should assign platform-specific extensions here
        //TODO: maybe ;)
        gl_platformextensions = "";
        gl_videosyncavailable = false;
-       
+
        GL_Init();
 
        vid_hidden = false;
        vid_activewindow = false;
        vid_usingmouse = false;
-       IN_Init();
        return true;
 }
 
 void VID_Shutdown (void)
 {
-       IN_Shutdown();
+       IN_Activate(false);
        SDL_QuitSubSystem(SDL_INIT_VIDEO);
 }
 
-int VID_SetGamma (unsigned short *ramps)
+int VID_SetGamma (unsigned short *ramps, int rampsize)
 {
-       return !SDL_SetGammaRamp (ramps, ramps + 256, ramps + 512);
+       return !SDL_SetGammaRamp (ramps, ramps + rampsize, ramps + rampsize*2);
 }
 
-int VID_GetGamma (unsigned short *ramps)
+int VID_GetGamma (unsigned short *ramps, int rampsize)
 {
-       return !SDL_GetGammaRamp( ramps, ramps + 256, ramps + 512);
+       return !SDL_GetGammaRamp (ramps, ramps + rampsize, ramps + rampsize*2);
 }
 
-void VID_GetWindowSize (int *x, int *y, int *width, int *height)
-{
-       *x = *y = 0;
-       *width = screen->w;
-       *height = screen->h;
-}
-
-void VID_Finish (void)
+void VID_Finish (qboolean allowmousegrab)
 {
        Uint8 appstate;
-       int vid_usemouse;
+       qboolean vid_usemouse;
 
        if (r_speeds.integer || gl_finish.integer)
                qglFinish();
@@ -494,24 +495,22 @@ void VID_Finish (void)
        //react on appstate changes
        appstate = SDL_GetAppState();
 
-       if( !( appstate & SDL_APPMOUSEFOCUS ) || !( appstate & SDL_APPINPUTFOCUS ) )
+       vid_hidden = !(appstate & SDL_APPACTIVE);
+
+       if( vid_hidden || !( appstate & SDL_APPMOUSEFOCUS ) || !( appstate & SDL_APPINPUTFOCUS ) )
                vid_activewindow = false;
-       else 
+       else
                vid_activewindow = true;
 
        vid_usemouse = false;
-       if( vid_mouse.integer && !key_consoleactive )
+       if( allowmousegrab && 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);
+
+       VID_UpdateGamma(false, 256);
 }