]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - vid_wgl.c
changed VID_InitMode to take a viddef_mode_t structure which contains
[xonotic/darkplaces.git] / vid_wgl.c
index 36520ee2cb57368c34d9e068da704814ec00df8f..1b6a27a1ef6624baac3a9d5b0acb1d4b1c7a37f2 100644 (file)
--- a/vid_wgl.c
+++ b/vid_wgl.c
@@ -17,10 +17,16 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 */
-// gl_vidnt.c -- NT GL vid component
+// vid_wgl.c -- NT GL vid component
 
-// we don't need a very new dinput
-#define DIRECTINPUT_VERSION 0x0300
+#ifdef SUPPORTDIRECTX
+// Include DX libs
+#pragma comment(lib, "dinput8.lib")
+#pragma comment(lib, "dxguid.lib")
+#ifndef DIRECTINPUT_VERSION
+#      define DIRECTINPUT_VERSION 0x0500  /* Version 5.0 */
+#endif
+#endif
 
 #include "quakedef.h"
 #include <windows.h>
@@ -95,6 +101,7 @@ static DEVMODE gdevmode, initialdevmode;
 static qboolean vid_initialized = false;
 static qboolean vid_wassuspended = false;
 static qboolean vid_usingmouse = false;
+static qboolean vid_usinghidecursor = false;
 static qboolean vid_usingvsync = false;
 static qboolean vid_usevsync = false;
 static HICON hIcon;
@@ -121,8 +128,6 @@ static qboolean vid_isfullscreen;
 
 static int window_x, window_y;
 
-static void IN_Activate (qboolean grab);
-
 static qboolean mouseinitialized;
 
 #ifdef SUPPORTDIRECTX
@@ -243,10 +248,8 @@ static void IN_StartupMouse (void);
 
 //====================================
 
-void VID_Finish (qboolean allowmousegrab)
+void VID_Finish (void)
 {
-       qboolean vid_usemouse;
-
        vid_usevsync = vid_vsync.integer && !cls.timedemo && gl_videosyncavailable;
        if (vid_usingvsync != vid_usevsync && gl_videosyncavailable)
        {
@@ -254,20 +257,10 @@ void VID_Finish (qboolean allowmousegrab)
                qwglSwapIntervalEXT (vid_usevsync);
        }
 
-// handle the mouse state when windowed if that's changed
-       vid_usemouse = false;
-       if (allowmousegrab && vid_mouse.integer && !key_consoleactive && (key_dest != key_game || !cls.demoplayback))
-               vid_usemouse = true;
-       if (vid_isfullscreen)
-               vid_usemouse = true;
-       if (!vid_activewindow)
-               vid_usemouse = false;
-       IN_Activate(vid_usemouse);
-
-       if (r_render.integer && !vid_hidden)
+       if (!vid_hidden)
        {
                CHECKGLERROR
-               if (r_speeds.integer || gl_finish.integer)
+               if (r_speeds.integer == 2 || gl_finish.integer)
                {
                        qglFinish();CHECKGLERROR
                }
@@ -402,11 +395,11 @@ void AppActivate(BOOL fActive, BOOL minimize)
 {
        static qboolean sound_active = false;  // initially blocked by Sys_InitConsole()
 
-       vid_activewindow = fActive;
-       vid_hidden = minimize;
+       vid_activewindow = fActive != FALSE;
+       vid_hidden = minimize != FALSE;
 
        // enable/disable sound on focus gain/loss
-       if (!vid_hidden && (vid_activewindow || !snd_mutewhenidle.integer))
+       if ((!vid_hidden && vid_activewindow) || !snd_mutewhenidle.integer)
        {
                if (!sound_active)
                {
@@ -441,7 +434,7 @@ void AppActivate(BOOL fActive, BOOL minimize)
 
        if (!fActive)
        {
-               IN_Activate (false);
+               VID_SetMouse(false, false, false);
                if (vid_isfullscreen)
                {
                        ChangeDisplaySettings (NULL, 0);
@@ -493,8 +486,8 @@ LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM  wParam, LPARAM lParam)
 {
        LONG    lRet = 1;
        int             fActive, fMinimized, temp;
-       char    state[256];
-       char    asciichar[4];
+       unsigned char state[256];
+       unsigned char asciichar[4];
        int             vkey;
        int             charlength;
        qboolean down = false;
@@ -515,7 +508,7 @@ LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM  wParam, LPARAM lParam)
                case WM_MOVE:
                        window_x = (int) LOWORD(lParam);
                        window_y = (int) HIWORD(lParam);
-                       IN_Activate(false);
+                       VID_SetMouse(false, false, false);
                        break;
 
                case WM_KEYDOWN:
@@ -527,7 +520,7 @@ LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM  wParam, LPARAM lParam)
                        GetKeyboardState (state);
                        // alt/ctrl/shift tend to produce funky ToAscii values,
                        // and if it's not a single character we don't know care about it
-                       charlength = ToAscii (wParam, lParam >> 16, state, (unsigned short *)asciichar, 0);
+                       charlength = ToAscii (wParam, lParam >> 16, state, (LPWORD)asciichar, 0);
                        if (vkey == K_ALT || vkey == K_CTRL || vkey == K_SHIFT || charlength == 0)
                                asciichar[0] = 0;
                        else if( charlength == 2 ) {
@@ -592,9 +585,7 @@ LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM  wParam, LPARAM lParam)
                                temp |= 512;
 
 #ifdef SUPPORTDIRECTX
-                       if (vid_usingmouse && !dinput_acquired)
-#else
-                       if (vid_usingmouse)
+                       if (!dinput_acquired)
 #endif
                        {
                                // perform button actions
@@ -796,7 +787,7 @@ void VID_Init(void)
        IN_Init();
 }
 
-int VID_InitMode (int fullscreen, int width, int height, int bpp, int refreshrate, int stereobuffer, int samples)
+qboolean VID_InitMode(viddef_mode_t *mode)
 {
        int i;
        HDC hdc;
@@ -825,7 +816,7 @@ int VID_InitMode (int fullscreen, int width, int height, int bpp, int refreshrat
        };
        int windowpass;
        int pixelformat, newpixelformat;
-       int numpixelformats;
+       UINT numpixelformats;
        DWORD WindowStyle, ExWindowStyle;
        int CenterX, CenterY;
        const char *gldrivername;
@@ -836,6 +827,13 @@ int VID_InitMode (int fullscreen, int width, int height, int bpp, int refreshrat
        float *af;
        int attribs[128];
        float attribsf[16];
+       int bpp = mode->bitsperpixel;
+       int width = mode->width;
+       int height = mode->height;
+       int refreshrate = (int)floor(mode->refreshrate+0.5);
+       int stereobuffer = mode->stereobuffer;
+       int samples = mode->samples;
+       int fullscreen = mode->fullscreen;
 
        if (vid_initialized)
                Sys_Error("VID_InitMode called when video is already initialised");
@@ -843,13 +841,21 @@ int VID_InitMode (int fullscreen, int width, int height, int bpp, int refreshrat
        // if stencil is enabled, ask for alpha too
        if (bpp >= 32)
        {
-               pfd.cStencilBits = 8;
+               pfd.cRedBits = 8;
+               pfd.cGreenBits = 8;
+               pfd.cBlueBits = 8;
                pfd.cAlphaBits = 8;
+               pfd.cDepthBits = 24;
+               pfd.cStencilBits = 8;
        }
        else
        {
-               pfd.cStencilBits = 0;
+               pfd.cRedBits = 5;
+               pfd.cGreenBits = 5;
+               pfd.cBlueBits = 5;
                pfd.cAlphaBits = 0;
+               pfd.cDepthBits = 16;
+               pfd.cStencilBits = 0;
        }
 
        if (stereobuffer)
@@ -873,7 +879,7 @@ int VID_InitMode (int fullscreen, int width, int height, int bpp, int refreshrat
                *a++ = WGL_BLUE_BITS_ARB;
                *a++ = 8;
                *a++ = WGL_ALPHA_BITS_ARB;
-               *a++ = 1;
+               *a++ = 8;
                *a++ = WGL_DEPTH_BITS_ARB;
                *a++ = 24;
                *a++ = WGL_STENCIL_BITS_ARB;
@@ -1087,8 +1093,12 @@ int VID_InitMode (int fullscreen, int width, int height, int bpp, int refreshrat
 
        pixelformat = 0;
        newpixelformat = 0;
-       for (windowpass = 0;windowpass < 2;windowpass++)
+       // start out at the final windowpass if samples is 1 as it's the only feature we need extended pixel formats for
+       for (windowpass = samples == 1;windowpass < 2;windowpass++)
        {
+               gl_extensions = "";
+               gl_platformextensions = "";
+
                mainwindow = CreateWindowEx (ExWindowStyle, "DarkPlacesWindowClass", gamename, WindowStyle, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, global_hInstance, NULL);
                if (!mainwindow)
                {
@@ -1146,15 +1156,17 @@ int VID_InitMode (int fullscreen, int width, int height, int bpp, int refreshrat
                if ((qwglGetExtensionsStringARB = (const char *(WINAPI *)(HDC hdc))GL_GetProcAddress("wglGetExtensionsStringARB")) == NULL)
                        Con_Print("wglGetExtensionsStringARB not found\n");
 
-               gl_renderer = qglGetString(GL_RENDERER);
-               gl_vendor = qglGetString(GL_VENDOR);
-               gl_version = qglGetString(GL_VERSION);
-               gl_extensions = qglGetString(GL_EXTENSIONS);
+               gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
                gl_platform = "WGL";
                gl_platformextensions = "";
 
                if (qwglGetExtensionsStringARB)
-                       gl_platformextensions = qwglGetExtensionsStringARB(baseDC);
+                       gl_platformextensions = (const char *)qwglGetExtensionsStringARB(baseDC);
+
+               if (!gl_extensions)
+                       gl_extensions = "";
+               if (!gl_platformextensions)
+                       gl_platformextensions = "";
 
                // now some nice Windows pain:
                // we have created a window, we needed one to find out if there are
@@ -1179,12 +1191,6 @@ int VID_InitMode (int fullscreen, int width, int height, int bpp, int refreshrat
                }
        }
 
-       Con_DPrintf("GL_VENDOR: %s\n", gl_vendor);
-       Con_DPrintf("GL_RENDERER: %s\n", gl_renderer);
-       Con_DPrintf("GL_VERSION: %s\n", gl_version);
-       Con_DPrintf("GL_EXTENSIONS: %s\n", gl_extensions);
-       Con_DPrintf("%s_EXTENSIONS: %s\n", gl_platform, gl_platformextensions);
-
        /*
        if (!fullscreen)
                SetWindowPos (mainwindow, NULL, CenterX, CenterY, 0, 0,SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
@@ -1216,8 +1222,6 @@ int VID_InitMode (int fullscreen, int width, int height, int bpp, int refreshrat
        // fix the leftover Alt from any Alt-Tab or the like that switched us away
        ClearAllStates ();
 
-       gl_videosyncavailable = false;
-
 // COMMANDLINEOPTION: Windows WGL: -novideosync disables WGL_EXT_swap_control
        gl_videosyncavailable = GL_CheckExtension("WGL_EXT_swap_control", wglswapintervalfuncs, "-novideosync", false);
 
@@ -1226,6 +1230,7 @@ int VID_InitMode (int fullscreen, int width, int height, int bpp, int refreshrat
        //vid_menudrawfn = VID_MenuDraw;
        //vid_menukeyfn = VID_MenuKey;
        vid_usingmouse = false;
+       vid_usinghidecursor = false;
        vid_usingvsync = false;
        vid_hidden = false;
        vid_initialized = true;
@@ -1235,8 +1240,8 @@ int VID_InitMode (int fullscreen, int width, int height, int bpp, int refreshrat
 
        if (gl_videosyncavailable)
        {
-               vid_usevsync = vid_vsync.integer;
-               vid_usingvsync = vid_vsync.integer;
+               vid_usevsync = vid_vsync.integer != 0;
+               vid_usingvsync = vid_vsync.integer != 0;
                qwglSwapIntervalEXT (vid_usevsync);
        }
 
@@ -1249,6 +1254,7 @@ void VID_Shutdown (void)
        if(vid_initialized == false)
                return;
 
+       VID_SetMouse(false, false, false);
        VID_RestoreSystemGamma();
 
        vid_initialized = false;
@@ -1270,7 +1276,7 @@ void VID_Shutdown (void)
        vid_isfullscreen = false;
 }
 
-static void IN_Activate (qboolean grab)
+void VID_SetMouse(qboolean fullscreengrab, qboolean relative, qboolean hidecursor)
 {
        static qboolean restore_spi;
        static int originalmouseparms[3];
@@ -1278,7 +1284,7 @@ static void IN_Activate (qboolean grab)
        if (!mouseinitialized)
                return;
 
-       if (grab)
+       if (relative)
        {
                if (!vid_usingmouse)
                {
@@ -1307,7 +1313,7 @@ static void IN_Activate (qboolean grab)
                                        newmouseparms[0] = 0; // threshold to double movement (only if accel level is >= 1)
                                        newmouseparms[1] = 0; // threshold to quadruple movement (only if accel level is >= 2)
                                        newmouseparms[2] = 0; // maximum level of acceleration (0 = off)
-                                       restore_spi = SystemParametersInfo (SPI_SETMOUSE, 0, newmouseparms, 0);
+                                       restore_spi = SystemParametersInfo (SPI_SETMOUSE, 0, newmouseparms, 0) != FALSE;
                                }
                                else
                                        restore_spi = false;
@@ -1316,7 +1322,6 @@ static void IN_Activate (qboolean grab)
                                SetCapture (mainwindow);
                                ClipCursor (&window_rect);
                        }
-                       ShowCursor (false);
                }
        }
        else
@@ -1341,9 +1346,14 @@ static void IN_Activate (qboolean grab)
                                ClipCursor (NULL);
                                ReleaseCapture ();
                        }
-                       ShowCursor (true);
                }
        }
+
+       if (vid_usinghidecursor != hidecursor)
+       {
+               vid_usinghidecursor = hidecursor;
+               ShowCursor (!hidecursor);
+       }
 }
 
 
@@ -1379,7 +1389,7 @@ static qboolean IN_InitDInput (void)
 
        if (!pDirectInputCreate)
        {
-               pDirectInputCreate = (void *)GetProcAddress(hInstDI,"DirectInputCreateA");
+               pDirectInputCreate = (HRESULT (__stdcall *)(HINSTANCE,DWORD,LPDIRECTINPUT *,LPUNKNOWN))GetProcAddress(hInstDI,"DirectInputCreateA");
 
                if (!pDirectInputCreate)
                {
@@ -1397,7 +1407,11 @@ static qboolean IN_InitDInput (void)
        }
 
 // obtain an interface to the system mouse device.
+#ifdef __cplusplus
+       hr = IDirectInput_CreateDevice(g_pdi, GUID_SysMouse, &g_pMouse, NULL);
+#else
        hr = IDirectInput_CreateDevice(g_pdi, &GUID_SysMouse, &g_pMouse, NULL);
+#endif
 
        if (FAILED(hr))
        {
@@ -1474,15 +1488,14 @@ IN_MouseMove
 */
 static void IN_MouseMove (void)
 {
-       int mx, my;
        POINT current_pos;
 
+       GetCursorPos (&current_pos);
+       in_windowmouse_x = current_pos.x - window_x;
+       in_windowmouse_y = current_pos.y - window_y;
+
        if (!vid_usingmouse)
-       {
-               //GetCursorPos (&current_pos);
-               //ui_mouseupdate(current_pos.x - window_x, current_pos.y - window_y);
                return;
-       }
 
 #ifdef SUPPORTDIRECTX
        if (dinput_acquired)
@@ -1491,8 +1504,6 @@ static void IN_MouseMove (void)
                DIDEVICEOBJECTDATA      od;
                DWORD                           dwElements;
                HRESULT                         hr;
-               mx = 0;
-               my = 0;
 
                for (;;)
                {
@@ -1516,11 +1527,11 @@ static void IN_MouseMove (void)
                        switch (od.dwOfs)
                        {
                                case DIMOFS_X:
-                                       mx += (LONG) od.dwData;
+                                       in_mouse_x += (LONG) od.dwData;
                                        break;
 
                                case DIMOFS_Y:
-                                       my += (LONG) od.dwData;
+                                       in_mouse_y += (LONG) od.dwData;
                                        break;
 
                                case DIMOFS_Z:
@@ -1571,23 +1582,15 @@ static void IN_MouseMove (void)
                        if ((mstate_di ^ mouse_oldbuttonstate) & (1<<i))
                                Key_Event (buttonremap[i], 0, (mstate_di & (1<<i)) != 0);
                mouse_oldbuttonstate = mstate_di;
-
-               in_mouse_x = mx;
-               in_mouse_y = my;
        }
        else
 #endif
        {
-               GetCursorPos (&current_pos);
-               mx = current_pos.x - (window_x + vid.width / 2);
-               my = current_pos.y - (window_y + vid.height / 2);
-
-               in_mouse_x = mx;
-               in_mouse_y = my;
+               in_mouse_x += in_windowmouse_x - (int)(vid.width / 2);
+               in_mouse_y += in_windowmouse_y - (int)(vid.height / 2);
 
                // if the mouse has moved, force it to the center, so there's room to move
-               if (!cl.csqc_wantsmousemove)
-               if (mx || my)
+               if (in_mouse_x || in_mouse_y)
                        SetCursorPos ((window_x + vid.width / 2), (window_y + vid.height / 2));
        }
 }
@@ -1664,7 +1667,7 @@ static void IN_StartupJoystick (void)
 
        // save the joystick's number of buttons and POV status
        joy_numbuttons = jc.wNumButtons;
-       joy_haspov = jc.wCaps & JOYCAPS_HASPOV;
+       joy_haspov = (jc.wCaps & JOYCAPS_HASPOV) != 0;
 
        // old button and POV states default to no buttons pressed
        joy_oldbuttonstate = joy_oldpovstate = 0;
@@ -2070,8 +2073,6 @@ static void IN_Init(void)
 
 static void IN_Shutdown(void)
 {
-       IN_Activate (false);
-
 #ifdef SUPPORTDIRECTX
        if (g_pMouse)
                IDirectInputDevice_Release(g_pMouse);
@@ -2082,3 +2083,32 @@ static void IN_Shutdown(void)
        g_pdi = NULL;
 #endif
 }
+
+size_t VID_ListModes(vid_mode_t *modes, size_t maxcount)
+{
+       int i;
+       size_t k;
+       DEVMODE thismode;
+
+       thismode.dmSize = sizeof(thismode);
+       thismode.dmDriverExtra = 0;
+       k = 0;
+       for(i = 0; EnumDisplaySettings(NULL, i, &thismode); ++i)
+       {
+               if(~thismode.dmFields & (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY))
+               {
+                       Con_DPrintf("enumerating modes yielded a bogus item... please debug this\n");
+                       continue;
+               }
+               if(k >= maxcount)
+                       break;
+               modes[k].width = thismode.dmPelsWidth;
+               modes[k].height = thismode.dmPelsHeight;
+               modes[k].bpp = thismode.dmBitsPerPel;
+               modes[k].refreshrate = thismode.dmDisplayFrequency;
+               modes[k].pixelheight_num = 1;
+               modes[k].pixelheight_denom = 1; // Win32 apparently does not provide this (FIXME)
+               ++k;
+       }
+       return k;
+}