X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=vid_wgl.c;h=4f97d31a716e057d63007f1364dd535e36acd8b1;hb=3eb300b2b135dd6eaed51c703f2d1622cac55732;hp=0e9b29443b48c94ce98e4741b7cdee1eef2fb638;hpb=58fb771fc98f2d04990c947944cd199007ea1666;p=xonotic%2Fdarkplaces.git diff --git a/vid_wgl.c b/vid_wgl.c index 0e9b2944..4f97d31a 100644 --- a/vid_wgl.c +++ b/vid_wgl.c @@ -19,34 +19,44 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ // gl_vidnt.c -- NT GL vid component +// we don't need a very new dinput +#define DIRECTINPUT_VERSION 0x0300 + #include "quakedef.h" -#include "winquake.h" +#include +#include +#include #include "resource.h" #include +#include -int cl_available = true; +extern HINSTANCE global_hInstance; -int (WINAPI *qwglChoosePixelFormat)(HDC, CONST PIXELFORMATDESCRIPTOR *); -int (WINAPI *qwglDescribePixelFormat)(HDC, int, UINT, LPPIXELFORMATDESCRIPTOR); -//int (WINAPI *qwglGetPixelFormat)(HDC); -BOOL (WINAPI *qwglSetPixelFormat)(HDC, int, CONST PIXELFORMATDESCRIPTOR *); -BOOL (WINAPI *qwglSwapBuffers)(HDC); -HGLRC (WINAPI *qwglCreateContext)(HDC); -BOOL (WINAPI *qwglDeleteContext)(HGLRC); -HGLRC (WINAPI *qwglGetCurrentContext)(VOID); -HDC (WINAPI *qwglGetCurrentDC)(VOID); -PROC (WINAPI *qwglGetProcAddress)(LPCSTR); -BOOL (WINAPI *qwglMakeCurrent)(HDC, HGLRC); -BOOL (WINAPI *qwglSwapIntervalEXT)(int interval); -const char *(WINAPI *qwglGetExtensionsStringARB)(HDC hdc); - -static gl_extensionfunctionlist_t getextensionsstringfuncs[] = -{ - {"wglGetExtensionsString", (void **) &qwglGetExtensionsStringARB}, - {NULL, NULL} -}; -static gl_extensionfunctionlist_t wglfuncs[] = +#ifndef WM_MOUSEWHEEL +#define WM_MOUSEWHEEL 0x020A +#endif + +// Tell startup code that we have a client +int cl_available = true; + +qboolean vid_supportrefreshrate = true; + +static int (WINAPI *qwglChoosePixelFormat)(HDC, CONST PIXELFORMATDESCRIPTOR *); +static int (WINAPI *qwglDescribePixelFormat)(HDC, int, UINT, LPPIXELFORMATDESCRIPTOR); +//static int (WINAPI *qwglGetPixelFormat)(HDC); +static BOOL (WINAPI *qwglSetPixelFormat)(HDC, int, CONST PIXELFORMATDESCRIPTOR *); +static BOOL (WINAPI *qwglSwapBuffers)(HDC); +static HGLRC (WINAPI *qwglCreateContext)(HDC); +static BOOL (WINAPI *qwglDeleteContext)(HGLRC); +static HGLRC (WINAPI *qwglGetCurrentContext)(VOID); +static HDC (WINAPI *qwglGetCurrentDC)(VOID); +static PROC (WINAPI *qwglGetProcAddress)(LPCSTR); +static BOOL (WINAPI *qwglMakeCurrent)(HDC, HGLRC); +static BOOL (WINAPI *qwglSwapIntervalEXT)(int interval); +static const char *(WINAPI *qwglGetExtensionsStringARB)(HDC hdc); + +static dllfunction_t wglfuncs[] = { {"wglChoosePixelFormat", (void **) &qwglChoosePixelFormat}, {"wglDescribePixelFormat", (void **) &qwglDescribePixelFormat}, @@ -57,455 +67,214 @@ static gl_extensionfunctionlist_t wglfuncs[] = {"wglDeleteContext", (void **) &qwglDeleteContext}, {"wglGetProcAddress", (void **) &qwglGetProcAddress}, {"wglMakeCurrent", (void **) &qwglMakeCurrent}, + {"wglGetCurrentContext", (void **) &qwglGetCurrentContext}, + {"wglGetCurrentDC", (void **) &qwglGetCurrentDC}, {NULL, NULL} }; -static gl_extensionfunctionlist_t wglswapintervalfuncs[] = +static dllfunction_t wglswapintervalfuncs[] = { {"wglSwapIntervalEXT", (void **) &qwglSwapIntervalEXT}, {NULL, NULL} }; -#define MAX_MODE_LIST 30 -#define VID_ROW_SIZE 3 -#define MAXWIDTH 10000 -#define MAXHEIGHT 10000 - -#define MODE_WINDOWED 0 -#define NO_MODE (MODE_WINDOWED - 1) -#define MODE_FULLSCREEN_DEFAULT (MODE_WINDOWED + 1) - -typedef struct { - modestate_t type; - int width; - int height; - int modenum; - int dib; - int fullscreen; - int bpp; - char modedesc[17]; -} vmode_t; - -typedef struct { - int width; - int height; -} lmode_t; - -lmode_t lowresmodes[] = { - {320, 200}, - {320, 240}, - {400, 300}, - {512, 384}, -}; - -qboolean scr_skipupdate; - -static vmode_t modelist[MAX_MODE_LIST]; -static int nummodes; -static vmode_t badmode; - static DEVMODE gdevmode; static qboolean vid_initialized = false; -static qboolean windowed, leavecurrentmode; -static qboolean vid_canalttab = false; static qboolean vid_wassuspended = false; -static int vid_usingmouse; -extern qboolean mouseactive; // from in_win.c +static qboolean vid_usingmouse = false; +static qboolean vid_usingvsync = false; +static qboolean vid_usevsync = false; static HICON hIcon; -int DIBWidth, DIBHeight; -RECT WindowRect; -DWORD WindowStyle, ExWindowStyle; - +// used by cd_win.c and snd_win.c HWND mainwindow; -int vid_modenum = NO_MODE; -int vid_realmode; -int vid_default = MODE_WINDOWED; -static int windowed_default; -unsigned char vid_curpal[256*3]; - -HGLRC baseRC; -HDC maindc; +static HDC baseDC; +static HGLRC baseRC; -HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow); +//HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow); -// global video state -viddef_t vid; +static qboolean vid_isfullscreen; -modestate_t modestate = MS_UNINIT; +//void VID_MenuDraw (void); +//void VID_MenuKey (int key); -void VID_MenuDraw (void); -void VID_MenuKey (int key); - -LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); -void AppActivate(BOOL fActive, BOOL minimize); -char *VID_GetModeDescription (int mode); -void ClearAllStates (void); -void VID_UpdateWindowStatus (void); +//LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); +//void AppActivate(BOOL fActive, BOOL minimize); +//void ClearAllStates (void); +//void VID_UpdateWindowStatus (void); //==================================== -int window_center_x, window_center_y, window_x, window_y, window_width, window_height; -RECT window_rect; - -// direct draw software compatability stuff - -void CenterWindow(HWND hWndCenter, int width, int height, BOOL lefttopjustify) -{ - int CenterX, CenterY; - - CenterX = (GetSystemMetrics(SM_CXSCREEN) - width) / 2; - CenterY = (GetSystemMetrics(SM_CYSCREEN) - height) / 2; - if (CenterX > CenterY*2) - CenterX >>= 1; // dual screens - CenterX = (CenterX < 0) ? 0: CenterX; - CenterY = (CenterY < 0) ? 0: CenterY; - SetWindowPos (hWndCenter, NULL, CenterX, CenterY, 0, 0, - SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME); -} - -qboolean VID_SetWindowedMode (int modenum) -{ - int lastmodestate, width, height; - RECT rect; - - lastmodestate = modestate; - - WindowRect.top = WindowRect.left = 0; - - WindowRect.right = modelist[modenum].width; - WindowRect.bottom = modelist[modenum].height; - - DIBWidth = modelist[modenum].width; - DIBHeight = modelist[modenum].height; - - WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX; - ExWindowStyle = 0; - - rect = WindowRect; - AdjustWindowRectEx(&rect, WindowStyle, false, 0); - - width = rect.right - rect.left; - height = rect.bottom - rect.top; - - // Create the DIB window - mainwindow = CreateWindowEx (ExWindowStyle, gamename, gamename, WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL); - - if (!mainwindow) - Sys_Error ("Couldn't create DIB window"); - - // Center and show the DIB window - CenterWindow(mainwindow, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top, false); - - ShowWindow (mainwindow, SW_SHOWDEFAULT); - UpdateWindow (mainwindow); - - modestate = MS_WINDOWED; - - SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon); - SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon); - - return true; -} - - -qboolean VID_SetFullDIBMode (int modenum) +static int window_x, window_y; + +static void IN_Activate (qboolean grab); + +static qboolean mouseinitialized; +static qboolean dinput; + +// input code + +#define DINPUT_BUFFERSIZE 16 +#define iDirectInputCreate(a,b,c,d) pDirectInputCreate(a,b,c,d) + +static HRESULT (WINAPI *pDirectInputCreate)(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUT * lplpDirectInput, LPUNKNOWN punkOuter); + +// LordHavoc: thanks to backslash for this support for mouse buttons 4 and 5 +/* backslash :: imouse explorer buttons */ +/* These are #ifdefed out for non-Win2K in the February 2001 version of + MS's platform SDK, but we need them for compilation. . . */ +#ifndef WM_XBUTTONDOWN + #define WM_XBUTTONDOWN 0x020B + #define WM_XBUTTONUP 0x020C +#endif +#ifndef MK_XBUTTON1 + #define MK_XBUTTON1 0x0020 + #define MK_XBUTTON2 0x0040 +#endif +#ifndef MK_XBUTTON3 +// LordHavoc: lets hope this allows more buttons in the future... + #define MK_XBUTTON3 0x0080 + #define MK_XBUTTON4 0x0100 + #define MK_XBUTTON5 0x0200 + #define MK_XBUTTON6 0x0400 + #define MK_XBUTTON7 0x0800 +#endif +/* :: backslash */ + +// mouse variables +static int mouse_buttons; +static int mouse_oldbuttonstate; + +static unsigned int uiWheelMessage; +static qboolean dinput_acquired; + +static unsigned int mstate_di; + +// joystick defines and variables +// where should defines be moved? +#define JOY_ABSOLUTE_AXIS 0x00000000 // control like a joystick +#define JOY_RELATIVE_AXIS 0x00000010 // control like a mouse, spinner, trackball +#define JOY_MAX_AXES 6 // X, Y, Z, R, U, V +#define JOY_AXIS_X 0 +#define JOY_AXIS_Y 1 +#define JOY_AXIS_Z 2 +#define JOY_AXIS_R 3 +#define JOY_AXIS_U 4 +#define JOY_AXIS_V 5 + +enum _ControlList { - int lastmodestate, width, height; - RECT rect; - - if (!leavecurrentmode) - { - gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; - gdevmode.dmBitsPerPel = modelist[modenum].bpp; - gdevmode.dmPelsWidth = modelist[modenum].width; - gdevmode.dmPelsHeight = modelist[modenum].height; - gdevmode.dmSize = sizeof (gdevmode); - - if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) - Sys_Error ("Couldn't set fullscreen DIB mode"); - } - - lastmodestate = modestate; - modestate = MS_FULLDIB; - - WindowRect.top = WindowRect.left = 0; - - WindowRect.right = modelist[modenum].width; - WindowRect.bottom = modelist[modenum].height; - - DIBWidth = modelist[modenum].width; - DIBHeight = modelist[modenum].height; - - WindowStyle = WS_POPUP; - ExWindowStyle = 0; - - rect = WindowRect; - AdjustWindowRectEx(&rect, WindowStyle, false, 0); - - width = rect.right - rect.left; - height = rect.bottom - rect.top; - - // Create the DIB window - mainwindow = CreateWindowEx (ExWindowStyle, gamename, gamename, WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL); - - if (!mainwindow) - Sys_Error ("Couldn't create DIB window"); - - ShowWindow (mainwindow, SW_SHOWDEFAULT); - UpdateWindow (mainwindow); - -// needed because we're not getting WM_MOVE messages fullscreen on NT - window_x = 0; - window_y = 0; - - SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon); - SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon); - - return true; -} - + AxisNada = 0, AxisForward, AxisLook, AxisSide, AxisTurn +}; -int VID_SetMode (int modenum) +static DWORD dwAxisFlags[JOY_MAX_AXES] = { - int original_mode; - qboolean stat = 0; - MSG msg; - - if ((windowed && (modenum != 0)) || (!windowed && (modenum < 1)) || (!windowed && (modenum >= nummodes))) - Sys_Error ("Bad video mode\n"); - - CDAudio_Pause (); - - if (vid_modenum == NO_MODE) - original_mode = windowed_default; - else - original_mode = vid_modenum; - - // Set either the fullscreen or windowed mode - if (modelist[modenum].type == MS_WINDOWED) - stat = VID_SetWindowedMode(modenum); - else if (modelist[modenum].type == MS_FULLDIB) - stat = VID_SetFullDIBMode(modenum); - else - Sys_Error ("VID_SetMode: Bad mode type in modelist"); - - window_width = DIBWidth; - window_height = DIBHeight; - VID_UpdateWindowStatus (); - - CDAudio_Resume (); - - if (!stat) - Sys_Error ("Couldn't set video mode"); - -// now we try to make sure we get the focus on the mode switch, because -// sometimes in some systems we don't. We grab the foreground, then -// finish setting up, pump all our messages, and sleep for a little while -// to let messages finish bouncing around the system, then we put -// ourselves at the top of the z order, then grab the foreground again, -// Who knows if it helps, but it probably doesn't hurt - SetForegroundWindow (mainwindow); - vid_modenum = modenum; - Cvar_SetValue ("vid_mode", (float)vid_modenum); - - while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) - { - TranslateMessage (&msg); - DispatchMessage (&msg); - } - - Sleep (100); - - SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS); - - SetForegroundWindow (mainwindow); - -// fix the leftover Alt from any Alt-Tab or the like that switched us away - ClearAllStates (); - - if (!msg_suppress_1) - Con_SafePrintf ("Video mode %s initialized.\n", VID_GetModeDescription (vid_modenum)); - - return true; -} - + JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ, JOY_RETURNR, JOY_RETURNU, JOY_RETURNV +}; -/* -================ -VID_UpdateWindowStatus -================ -*/ -void VID_UpdateWindowStatus (void) -{ - window_rect.left = window_x; - window_rect.top = window_y; - window_rect.right = window_x + window_width; - window_rect.bottom = window_y + window_height; - window_center_x = (window_rect.left + window_rect.right) / 2; - window_center_y = (window_rect.top + window_rect.bottom) / 2; - - IN_UpdateClipCursor (); -} +static DWORD dwAxisMap[JOY_MAX_AXES]; +static DWORD dwControlMap[JOY_MAX_AXES]; +static PDWORD pdwRawValue[JOY_MAX_AXES]; + +// none of these cvars are saved over a session +// this means that advanced controller configuration needs to be executed +// each time. this avoids any problems with getting back to a default usage +// or when changing from one controller to another. this way at least something +// works. +static cvar_t in_joystick = {CVAR_SAVE, "joystick","0", "enables joysticks"}; +static cvar_t joy_name = {0, "joyname", "joystick", "name of joystick to use (informational only, used only by joyadvanced 1 mode)"}; +static cvar_t joy_advanced = {0, "joyadvanced", "0", "use more than 2 axis joysticks (configuring this is very technical)"}; +static cvar_t joy_advaxisx = {0, "joyadvaxisx", "0", "axis mapping for joyadvanced 1 mode"}; +static cvar_t joy_advaxisy = {0, "joyadvaxisy", "0", "axis mapping for joyadvanced 1 mode"}; +static cvar_t joy_advaxisz = {0, "joyadvaxisz", "0", "axis mapping for joyadvanced 1 mode"}; +static cvar_t joy_advaxisr = {0, "joyadvaxisr", "0", "axis mapping for joyadvanced 1 mode"}; +static cvar_t joy_advaxisu = {0, "joyadvaxisu", "0", "axis mapping for joyadvanced 1 mode"}; +static cvar_t joy_advaxisv = {0, "joyadvaxisv", "0", "axis mapping for joyadvanced 1 mode"}; +static cvar_t joy_forwardthreshold = {0, "joyforwardthreshold", "0.15", "minimum joystick movement necessary to move forward"}; +static cvar_t joy_sidethreshold = {0, "joysidethreshold", "0.15", "minimum joystick movement necessary to move sideways (strafing)"}; +static cvar_t joy_pitchthreshold = {0, "joypitchthreshold", "0.15", "minimum joystick movement necessary to look up/down"}; +static cvar_t joy_yawthreshold = {0, "joyyawthreshold", "0.15", "minimum joystick movement necessary to turn left/right"}; +static cvar_t joy_forwardsensitivity = {0, "joyforwardsensitivity", "-1.0", "how fast the joystick moves forward"}; +static cvar_t joy_sidesensitivity = {0, "joysidesensitivity", "-1.0", "how fast the joystick moves sideways (strafing)"}; +static cvar_t joy_pitchsensitivity = {0, "joypitchsensitivity", "1.0", "how fast the joystick looks up/down"}; +static cvar_t joy_yawsensitivity = {0, "joyyawsensitivity", "-1.0", "how fast the joystick turns left/right"}; +static cvar_t joy_wwhack1 = {0, "joywwhack1", "0.0", "special hack for wingman warrior"}; +static cvar_t joy_wwhack2 = {0, "joywwhack2", "0.0", "special hack for wingman warrior"}; + +static qboolean joy_avail, joy_advancedinit, joy_haspov; +static DWORD joy_oldbuttonstate, joy_oldpovstate; + +static int joy_id; +static DWORD joy_flags; +static DWORD joy_numbuttons; + +static LPDIRECTINPUT g_pdi; +static LPDIRECTINPUTDEVICE g_pMouse; + +static JOYINFOEX ji; + +static HINSTANCE hInstDI; + +// forward-referenced functions +static void IN_StartupJoystick (void); +static void Joy_AdvancedUpdate_f (void); +static void IN_JoyMove (void); +static void IN_StartupMouse (void); //==================================== -/* -================= -VID_GetWindowSize -================= -*/ -void VID_GetWindowSize (int *x, int *y, int *width, int *height) +void VID_Finish (qboolean allowmousegrab) { - *x = *y = 0; - *width = WindowRect.right - WindowRect.left; - *height = WindowRect.bottom - WindowRect.top; -} - + qboolean vid_usemouse; -void VID_Finish (void) -{ - int vid_usemouse; - if (r_render.integer && !scr_skipupdate) + vid_usevsync = vid_vsync.integer && !cls.timedemo && gl_videosyncavailable; + if (vid_usingvsync != vid_usevsync && gl_videosyncavailable) { - qglFinish(); - SwapBuffers(maindc); + vid_usingvsync = vid_usevsync; + qwglSwapIntervalEXT (vid_usevsync); } // handle the mouse state when windowed if that's changed vid_usemouse = false; - if (vid_mouse.integer && !key_consoleactive) + if (allowmousegrab && vid_mouse.integer && !key_consoleactive && (key_dest != key_game || !cls.demoplayback)) vid_usemouse = true; - if (modestate == MS_FULLDIB) + if (vid_isfullscreen) vid_usemouse = true; if (!vid_activewindow) vid_usemouse = false; - if (vid_usemouse) - { - if (!vid_usingmouse) - { - vid_usingmouse = true; - IN_ActivateMouse (); - IN_HideMouse(); - } - } - else + IN_Activate(vid_usemouse); + + if (r_render.integer && !vid_hidden) { - if (vid_usingmouse) + CHECKGLERROR + if (r_speeds.integer || gl_finish.integer) { - vid_usingmouse = false; - IN_DeactivateMouse (); - IN_ShowMouse(); + qglFinish();CHECKGLERROR } + SwapBuffers(baseDC); } -} -void VID_SetDefaultMode (void) -{ - IN_DeactivateMouse (); -} - -void VID_RestoreSystemGamma(void); - -void VID_Shutdown (void) -{ - HGLRC hRC; - HDC hDC; - int i; - GLuint temp[8192]; - - if (vid_initialized) - { - vid_canalttab = false; - hRC = qwglGetCurrentContext(); - hDC = qwglGetCurrentDC(); - - qwglMakeCurrent(NULL, NULL); - - // LordHavoc: free textures before closing (may help NVIDIA) - for (i = 0;i < 8192;i++) - temp[i] = i+1; - qglDeleteTextures(8192, temp); - - if (hRC) - qwglDeleteContext(hRC); - - // close the library before we get rid of the window - GL_CloseLibrary(); - - if (hDC && mainwindow) - ReleaseDC(mainwindow, hDC); - - if (modestate == MS_FULLDIB) - ChangeDisplaySettings (NULL, 0); - - if (maindc && mainwindow) - ReleaseDC (mainwindow, maindc); - - AppActivate(false, false); - - VID_RestoreSystemGamma(); - } + VID_UpdateGamma(false, 256); } - //========================================================================== -BOOL bSetupPixelFormat(HDC hDC) -{ - static PIXELFORMATDESCRIPTOR pfd = { - sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd - 1, // version number - PFD_DRAW_TO_WINDOW // support window - | PFD_SUPPORT_OPENGL // support OpenGL - | PFD_DOUBLEBUFFER , // double buffered - PFD_TYPE_RGBA, // RGBA type - 24, // 24-bit color depth - 0, 0, 0, 0, 0, 0, // color bits ignored - 0, // no alpha buffer - 0, // shift bit ignored - 0, // no accumulation buffer - 0, 0, 0, 0, // accum bits ignored - 32, // 32-bit z-buffer - 0, // no stencil buffer - 0, // no auxiliary buffer - PFD_MAIN_PLANE, // main layer - 0, // reserved - 0, 0, 0 // layer masks ignored - }; - int pixelformat; - - if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 ) - { - MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK); - return false; - } - - if (SetPixelFormat(hDC, pixelformat, &pfd) == false) - { - MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK); - return false; - } - - return true; -} - -qbyte scantokey[128] = +static unsigned char scantokey[128] = { -// 0 1 2 3 4 5 6 7 8 9 A B C D E F - 0 ,27 ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'0' ,'-' ,'=' ,K_BACKSPACE,9 , // 0 - 'q' ,'w' ,'e' ,'r' ,'t' ,'y' ,'u' ,'i' ,'o' ,'p' ,'[' ,']' ,13 ,K_CTRL ,'a' ,'s' , // 1 - 'd' ,'f' ,'g' ,'h' ,'j' ,'k' ,'l' ,';' ,'\'' ,'`' ,K_SHIFT,'\\' ,'z' ,'x' ,'c' ,'v' , // 2 - 'b' ,'n' ,'m' ,',' ,'.' ,'/' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2 ,K_F3 ,K_F4 ,K_F5 , // 3 - K_F6 ,K_F7 ,K_F8 ,K_F9 ,K_F10,K_PAUSE,0 ,K_HOME,K_UPARROW,K_PGUP,'-' ,K_LEFTARROW,'5' ,K_RIGHTARROW,'+' ,K_END , // 4 - K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5 - 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6 - 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7 +// 0 1 2 3 4 5 6 7 8 9 A B C D E F + 0 ,27 ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'0' ,'-' ,'=' ,K_BACKSPACE,9 ,//0 + 'q' ,'w' ,'e' ,'r' ,'t' ,'y' ,'u' ,'i' ,'o' ,'p' ,'[' ,']' ,13 ,K_CTRL ,'a' ,'s' ,//1 + 'd' ,'f' ,'g' ,'h' ,'j' ,'k' ,'l' ,';' ,'\'' ,'`' ,K_SHIFT ,'\\' ,'z' ,'x' ,'c' ,'v' ,//2 + 'b' ,'n' ,'m' ,',' ,'.' ,'/' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2 ,K_F3 ,K_F4 ,K_F5 ,//3 + K_F6 ,K_F7 ,K_F8 ,K_F9 ,K_F10,K_PAUSE,0 ,K_HOME,K_UPARROW,K_PGUP,K_KP_MINUS,K_LEFTARROW,K_KP_5,K_RIGHTARROW,K_KP_PLUS ,K_END,//4 + K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,//5 + 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,//6 + 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 //7 }; @@ -516,14 +285,64 @@ MapKey Map from windows to quake keynums ======= */ -int MapKey (int key, int virtualkey) +static int MapKey (int key, int virtualkey) { - key = (key>>16)&255; - if (key > 127) - return 0; - if (scantokey[key] == 0) - Con_DPrintf("key 0x%02x has no translation\n", key); - return scantokey[key]; + int result; + int modified = (key >> 16) & 255; + qboolean is_extended = false; + + if (modified < 128 && scantokey[modified]) + result = scantokey[modified]; + else + { + result = 0; + Con_DPrintf("key 0x%02x (0x%8x, 0x%8x) has no translation\n", modified, key, virtualkey); + } + + if (key & (1 << 24)) + is_extended = true; + + if ( !is_extended ) + { + switch ( result ) + { + case K_HOME: + return K_KP_HOME; + case K_UPARROW: + return K_KP_UPARROW; + case K_PGUP: + return K_KP_PGUP; + case K_LEFTARROW: + return K_KP_LEFTARROW; + case K_RIGHTARROW: + return K_KP_RIGHTARROW; + case K_END: + return K_KP_END; + case K_DOWNARROW: + return K_KP_DOWNARROW; + case K_PGDN: + return K_KP_PGDN; + case K_INS: + return K_KP_INS; + case K_DEL: + return K_KP_DEL; + default: + return result; + } + } + else + { + switch ( result ) + { + case 0x0D: + return K_KP_ENTER; + case 0x2F: + return K_KP_SLASH; + case 0xAF: + return K_KP_PLUS; + } + return result; + } } /* @@ -539,23 +358,13 @@ MAIN WINDOW ClearAllStates ================ */ -void ClearAllStates (void) +static void ClearAllStates (void) { - int i; - -// send an up event for each key, to make sure the server clears them all - for (i=0 ; i<256 ; i++) - { - Key_Event (i, false); - } - Key_ClearStates (); - IN_ClearStates (); + if (vid_usingmouse) + mouse_oldbuttonstate = 0; } -void VID_RestoreGameGamma(void); -extern qboolean host_loopactive; - void AppActivate(BOOL fActive, BOOL minimize) /**************************************************************************** * @@ -568,28 +377,34 @@ void AppActivate(BOOL fActive, BOOL minimize) * ****************************************************************************/ { - static BOOL sound_active; + static qboolean sound_active = false; // initially blocked by Sys_InitConsole() vid_activewindow = fActive; vid_hidden = minimize; -// enable/disable sound on focus gain/loss - if (!vid_activewindow && sound_active) + // enable/disable sound on focus gain/loss + if (!vid_hidden && (vid_activewindow || !snd_mutewhenidle.integer)) { - S_BlockSound (); - sound_active = false; + if (!sound_active) + { + S_UnblockSound (); + sound_active = true; + } } - else if (vid_activewindow && !sound_active) + else { - S_UnblockSound (); - sound_active = true; + if (sound_active) + { + S_BlockSound (); + sound_active = false; + } } if (fActive) { - if (modestate == MS_FULLDIB) + if (vid_isfullscreen) { - if (vid_canalttab && vid_wassuspended) + if (vid_wassuspended) { vid_wassuspended = false; ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN); @@ -599,16 +414,12 @@ void AppActivate(BOOL fActive, BOOL minimize) // LordHavoc: from dabb, fix for alt-tab bug in NVidia drivers MoveWindow(mainwindow,0,0,gdevmode.dmPelsWidth,gdevmode.dmPelsHeight,false); } - if (host_loopactive) - VID_RestoreGameGamma(); } if (!fActive) { - vid_usingmouse = false; - IN_DeactivateMouse (); - IN_ShowMouse (); - if (modestate == MS_FULLDIB && vid_canalttab) + IN_Activate (false); + if (vid_isfullscreen) { ChangeDisplaySettings (NULL, 0); vid_wassuspended = true; @@ -617,14 +428,53 @@ void AppActivate(BOOL fActive, BOOL minimize) } } +//TODO: move it around in vid_wgl.c since I dont think this is the right position +void Sys_SendKeyEvents (void) +{ + MSG msg; + + while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)) + { + if (!GetMessage (&msg, NULL, 0, 0)) + Sys_Quit (1); + + TranslateMessage (&msg); + DispatchMessage (&msg); + } +} + LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); +static keynum_t buttonremap[16] = +{ + K_MOUSE1, + K_MOUSE2, + K_MOUSE3, + 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, +}; + /* main window procedure */ LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { LONG lRet = 1; int fActive, fMinimized, temp; - extern unsigned int uiWheelMessage; + char state[256]; + char asciichar[4]; + int vkey; + int charlength; + qboolean down = false; if ( uMsg == uiWheelMessage ) uMsg = WM_MOUSEWHEEL; @@ -632,7 +482,7 @@ LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) switch (uMsg) { case WM_KILLFOCUS: - if (modestate == MS_FULLDIB) + if (vid_isfullscreen) ShowWindow(mainwindow, SW_SHOWMINNOACTIVE); break; @@ -642,17 +492,25 @@ LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) case WM_MOVE: window_x = (int) LOWORD(lParam); window_y = (int) HIWORD(lParam); - VID_UpdateWindowStatus (); + IN_Activate(false); break; case WM_KEYDOWN: case WM_SYSKEYDOWN: - Key_Event (MapKey(lParam, wParam), true); - break; - + down = true; case WM_KEYUP: case WM_SYSKEYUP: - Key_Event (MapKey(lParam, wParam), false); + vkey = MapKey(lParam, wParam); + 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); + if (vkey == K_ALT || vkey == K_CTRL || vkey == K_SHIFT || charlength == 0) + asciichar[0] = 0; + else if( charlength == 2 ) { + asciichar[0] = asciichar[1]; + } + Key_Event (vkey, asciichar[0], down); break; case WM_SYSCHAR: @@ -667,6 +525,8 @@ LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) case WM_RBUTTONUP: case WM_MBUTTONDOWN: case WM_MBUTTONUP: + case WM_XBUTTONDOWN: // backslash :: imouse explorer buttons + case WM_XBUTTONUP: // backslash :: imouse explorer buttons case WM_MOUSEMOVE: temp = 0; @@ -679,7 +539,35 @@ LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) if (wParam & MK_MBUTTON) temp |= 4; - IN_MouseEvent (temp); + /* backslash :: imouse explorer buttons */ + if (wParam & MK_XBUTTON1) + temp |= 8; + + if (wParam & MK_XBUTTON2) + temp |= 16; + /* :: backslash */ + + // LordHavoc: lets hope this allows more buttons in the future... + if (wParam & MK_XBUTTON3) + temp |= 32; + if (wParam & MK_XBUTTON4) + temp |= 64; + if (wParam & MK_XBUTTON5) + temp |= 128; + if (wParam & MK_XBUTTON6) + temp |= 256; + if (wParam & MK_XBUTTON7) + temp |= 512; + + if (vid_usingmouse && !dinput_acquired) + { + // perform button actions + int i; + for (i=0 ; i 0) { - Key_Event(K_MWHEELUP, true); - Key_Event(K_MWHEELUP, false); + Key_Event(K_MWHEELUP, 0, true); + Key_Event(K_MWHEELUP, 0, false); } else { - Key_Event(K_MWHEELDOWN, true); - Key_Event(K_MWHEELDOWN, false); + Key_Event(K_MWHEELDOWN, 0, true); + Key_Event(K_MWHEELDOWN, 0, false); } break; @@ -701,7 +589,7 @@ LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) case WM_CLOSE: if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES) - Sys_Quit (); + Sys_Quit (0); break; @@ -715,14 +603,9 @@ LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) break; - case WM_DESTROY: - { - if (mainwindow) - DestroyWindow (mainwindow); - - PostQuitMessage (0); - } - break; + //case WM_DESTROY: + // PostQuitMessage (0); + // break; case MM_MCINOTIFY: lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam); @@ -738,613 +621,1171 @@ LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) return lRet; } - -/* -================= -VID_NumModes -================= -*/ -int VID_NumModes (void) +int VID_SetGamma(unsigned short *ramps, int rampsize) { - return nummodes; + HDC hdc = GetDC (NULL); + int i = SetDeviceGammaRamp(hdc, ramps); + ReleaseDC (NULL, hdc); + return i; // return success or failure } - -/* -================= -VID_GetModePtr -================= -*/ -vmode_t *VID_GetModePtr (int modenum) +int VID_GetGamma(unsigned short *ramps, int rampsize) { - - if ((modenum >= 0) && (modenum < nummodes)) - return &modelist[modenum]; - else - return &badmode; + HDC hdc = GetDC (NULL); + int i = GetDeviceGammaRamp(hdc, ramps); + ReleaseDC (NULL, hdc); + return i; // return success or failure } +static HINSTANCE gldll; -/* -================= -VID_GetModeDescription -================= -*/ -char *VID_GetModeDescription (int mode) +static void GL_CloseLibrary(void) { - char *pinfo; - vmode_t *pv; - static char temp[100]; - - if ((mode < 0) || (mode >= nummodes)) - return NULL; + FreeLibrary(gldll); + gldll = 0; + gl_driver[0] = 0; + qwglGetProcAddress = NULL; + gl_extensions = ""; + gl_platform = ""; + gl_platformextensions = ""; +} - if (!leavecurrentmode) - { - pv = VID_GetModePtr (mode); - pinfo = pv->modedesc; - } - else +static int GL_OpenLibrary(const char *name) +{ + Con_Printf("Loading OpenGL driver %s\n", name); + GL_CloseLibrary(); + if (!(gldll = LoadLibrary(name))) { - sprintf (temp, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height); - pinfo = temp; + Con_Printf("Unable to LoadLibrary %s\n", name); + return false; } - - return pinfo; + strlcpy(gl_driver, name, sizeof(gl_driver)); + return true; } - -// KJB: Added this to return the mode driver name in description for console - -char *VID_GetExtModeDescription (int mode) +void *GL_GetProcAddress(const char *name) { - static char pinfo[40]; - vmode_t *pv; - - if ((mode < 0) || (mode >= nummodes)) - return NULL; - - pv = VID_GetModePtr (mode); - if (modelist[mode].type == MS_FULLDIB) - { - if (!leavecurrentmode) - sprintf(pinfo,"%s fullscreen", pv->modedesc); - else - sprintf (pinfo, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height); - } + void *p = NULL; + if (qwglGetProcAddress != NULL) + p = (void *) qwglGetProcAddress(name); + if (p == NULL) + p = (void *) GetProcAddress(gldll, name); + return p; +} + +static void IN_Init(void); +void VID_Init(void) +{ + WNDCLASS wc; + + InitCommonControls(); + hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON1)); + + // Register the frame class + wc.style = 0; + wc.lpfnWndProc = (WNDPROC)MainWndProc; + wc.cbClsExtra = 0; + wc.cbWndExtra = 0; + wc.hInstance = global_hInstance; + wc.hIcon = hIcon; + wc.hCursor = LoadCursor (NULL,IDC_ARROW); + wc.hbrBackground = NULL; + wc.lpszMenuName = 0; + wc.lpszClassName = "DarkPlacesWindowClass"; + + if (!RegisterClass (&wc)) + Con_Printf ("Couldn't register window class\n"); + + IN_Init(); +} + +int VID_InitMode (int fullscreen, int width, int height, int bpp, int refreshrate, int stereobuffer) +{ + int i; + HDC hdc; + RECT rect; + MSG msg; + PIXELFORMATDESCRIPTOR pfd = + { + sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd + 1, // version number + PFD_DRAW_TO_WINDOW // support window + | PFD_SUPPORT_OPENGL // support OpenGL + | PFD_DOUBLEBUFFER , // double buffered + PFD_TYPE_RGBA, // RGBA type + 24, // 24-bit color depth + 0, 0, 0, 0, 0, 0, // color bits ignored + 0, // no alpha buffer + 0, // shift bit ignored + 0, // no accumulation buffer + 0, 0, 0, 0, // accum bits ignored + 32, // 32-bit z-buffer + 0, // no stencil buffer + 0, // no auxiliary buffer + PFD_MAIN_PLANE, // main layer + 0, // reserved + 0, 0, 0 // layer masks ignored + }; + int pixelformat; + DWORD WindowStyle, ExWindowStyle; + int CenterX, CenterY; + const char *gldrivername; + int depth; + + if (vid_initialized) + Sys_Error("VID_InitMode called when video is already initialised"); + + // if stencil is enabled, ask for alpha too + if (bpp >= 32) + { + pfd.cStencilBits = 8; + pfd.cAlphaBits = 8; + } else { - if (modestate == MS_WINDOWED) - sprintf(pinfo, "%s windowed", pv->modedesc); - else - sprintf(pinfo, "windowed"); + pfd.cStencilBits = 0; + pfd.cAlphaBits = 0; } - return pinfo; -} + if (stereobuffer) + pfd.dwFlags |= PFD_STEREO; + gldrivername = "opengl32.dll"; +// COMMANDLINEOPTION: Windows WGL: -gl_driver selects a GL driver library, default is opengl32.dll, useful only for 3dfxogl.dll or 3dfxvgl.dll, 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) + gldrivername = com_argv[i + 1]; + if (!GL_OpenLibrary(gldrivername)) + { + Con_Printf("Unable to load GL driver %s\n", gldrivername); + return false; + } -/* -================= -VID_DescribeCurrentMode_f -================= -*/ -void VID_DescribeCurrentMode_f (void) -{ - Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum)); -} + memset(&gdevmode, 0, sizeof(gdevmode)); + vid_isfullscreen = false; + if (fullscreen) + { + gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY; + gdevmode.dmBitsPerPel = bpp; + gdevmode.dmPelsWidth = width; + gdevmode.dmPelsHeight = height; + gdevmode.dmDisplayFrequency = refreshrate; + gdevmode.dmSize = sizeof (gdevmode); + if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) + { + VID_Shutdown(); + Con_Printf("Unable to change to requested mode %dx%dx%dbpp\n", width, height, bpp); + return false; + } -/* -================= -VID_NumModes_f -================= -*/ -void VID_NumModes_f (void) -{ - if (nummodes == 1) - Con_Printf ("%d video mode is available\n", nummodes); + vid_isfullscreen = true; + WindowStyle = WS_POPUP; + ExWindowStyle = WS_EX_TOPMOST; + } else - Con_Printf ("%d video modes are available\n", nummodes); -} + { + hdc = GetDC (NULL); + i = GetDeviceCaps(hdc, RASTERCAPS); + depth = GetDeviceCaps(hdc, PLANES) * GetDeviceCaps(hdc, BITSPIXEL); + ReleaseDC (NULL, hdc); + if (i & RC_PALETTE) + { + VID_Shutdown(); + Con_Print("Can't run in non-RGB mode\n"); + return false; + } + if (bpp > depth) + { + VID_Shutdown(); + Con_Print("A higher desktop depth is required to run this video mode\n"); + return false; + } + WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX; + ExWindowStyle = 0; + } -/* -================= -VID_DescribeMode_f -================= -*/ -void VID_DescribeMode_f (void) -{ - int t, modenum; + rect.top = 0; + rect.left = 0; + rect.right = width; + rect.bottom = height; + AdjustWindowRectEx(&rect, WindowStyle, false, 0); - modenum = atoi (Cmd_Argv(1)); + if (fullscreen) + { + CenterX = 0; + CenterY = 0; + } + else + { + CenterX = (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) / 2; + CenterY = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) / 2; + } + CenterX = max(0, CenterX); + CenterY = max(0, CenterY); + + // x and y may be changed by WM_MOVE messages + window_x = CenterX; + window_y = CenterY; + rect.left += CenterX; + rect.right += CenterX; + rect.top += CenterY; + rect.bottom += CenterY; + + 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) + { + Con_Printf("CreateWindowEx(%d, %s, %s, %d, %d, %d, %d, %d, %p, %p, %d, %p) failed\n", (int)ExWindowStyle, "DarkPlacesWindowClass", gamename, (int)WindowStyle, (int)(rect.left), (int)(rect.top), (int)(rect.right - rect.left), (int)(rect.bottom - rect.top), NULL, NULL, (int)global_hInstance, NULL); + VID_Shutdown(); + return false; + } - t = leavecurrentmode; - leavecurrentmode = 0; + /* + if (!fullscreen) + SetWindowPos (mainwindow, NULL, CenterX, CenterY, 0, 0,SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME); + */ - Con_Printf ("%s\n", VID_GetExtModeDescription (modenum)); + ShowWindow (mainwindow, SW_SHOWDEFAULT); + UpdateWindow (mainwindow); - leavecurrentmode = t; -} + // now we try to make sure we get the focus on the mode switch, because + // sometimes in some systems we don't. We grab the foreground, then + // finish setting up, pump all our messages, and sleep for a little while + // to let messages finish bouncing around the system, then we put + // ourselves at the top of the z order, then grab the foreground again, + // Who knows if it helps, but it probably doesn't hurt + SetForegroundWindow (mainwindow); + while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE)) + { + TranslateMessage (&msg); + DispatchMessage (&msg); + } -/* -================= -VID_DescribeModes_f -================= -*/ -void VID_DescribeModes_f (void) -{ - int i, lnummodes, t; - char *pinfo; - vmode_t *pv; + Sleep (100); - lnummodes = VID_NumModes (); + SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS); + + SetForegroundWindow (mainwindow); - t = leavecurrentmode; - leavecurrentmode = 0; + // fix the leftover Alt from any Alt-Tab or the like that switched us away + ClearAllStates (); - for (i=1 ; i= MAX_MODE_LIST) - return; - modelist[nummodes].type = type; - modelist[nummodes].width = width; - modelist[nummodes].height = height; - modelist[nummodes].modenum = modenum; - modelist[nummodes].dib = dib; - modelist[nummodes].fullscreen = fullscreen; - modelist[nummodes].bpp = bpp; - if (bpp == 0) - sprintf (modelist[nummodes].modedesc, "%dx%d", width, height); - else - sprintf (modelist[nummodes].modedesc, "%dx%dx%d", width, height, bpp); - for (i = 0;i < nummodes;i++) + if (!GL_CheckExtension("wgl", wglfuncs, NULL, false)) { - if (!memcmp(&modelist[i], &modelist[nummodes], sizeof(vmode_t))) - return; + VID_Shutdown(); + Con_Print("wgl functions not found\n"); + return false; } - nummodes++; -} -void VID_InitDIB (HINSTANCE hInstance) -{ - int w, h; - WNDCLASS wc; + baseRC = qwglCreateContext(baseDC); + if (!baseRC) + { + VID_Shutdown(); + Con_Print("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.\n"); + return false; + } + if (!qwglMakeCurrent(baseDC, baseRC)) + { + VID_Shutdown(); + Con_Printf("wglMakeCurrent(%d, %d) failed\n", (int)baseDC, (int)baseRC); + return false; + } - // Register the frame class - wc.style = 0; - wc.lpfnWndProc = (WNDPROC)MainWndProc; - wc.cbClsExtra = 0; - wc.cbWndExtra = 0; - wc.hInstance = hInstance; - wc.hIcon = 0; - wc.hCursor = LoadCursor (NULL,IDC_ARROW); - wc.hbrBackground = NULL; - wc.lpszMenuName = 0; - wc.lpszClassName = gamename; + if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL) + { + VID_Shutdown(); + Con_Print("glGetString not found\n"); + return false; + } + 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_platform = "WGL"; + gl_platformextensions = ""; - if (!RegisterClass (&wc) ) - Sys_Error ("Couldn't register window class"); + gl_videosyncavailable = false; - if (COM_CheckParm("-width")) - w = atoi(com_argv[COM_CheckParm("-width")+1]); - else - w = 640; + if (qwglGetExtensionsStringARB) + gl_platformextensions = qwglGetExtensionsStringARB(baseDC); - if (w < 320) - w = 320; +// COMMANDLINEOPTION: Windows WGL: -novideosync disables WGL_EXT_swap_control + gl_videosyncavailable = GL_CheckExtension("WGL_EXT_swap_control", wglswapintervalfuncs, "-novideosync", false); + //ReleaseDC(mainwindow, hdc); - if (COM_CheckParm("-height")) - h = atoi(com_argv[COM_CheckParm("-height")+1]); - else - h = w * 240/320; + GL_Init (); + + // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!) + if (strncasecmp(gl_vendor,"ATI",3)==0) + { + if (strncasecmp(gl_renderer,"Rage Pro",8)==0) + isRagePro = true; + } + if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh... + isG200 = true; - if (h < 240) - h = 240; + //vid_menudrawfn = VID_MenuDraw; + //vid_menukeyfn = VID_MenuKey; + vid_usingmouse = false; + vid_usingvsync = false; + vid_hidden = false; + vid_initialized = true; - VID_AddMode(MS_WINDOWED, w, h, 0, 1, 0, 0); -} + IN_StartupMouse (); + IN_StartupJoystick (); + if (gl_videosyncavailable) + { + vid_usevsync = vid_vsync.integer; + vid_usingvsync = vid_vsync.integer; + qwglSwapIntervalEXT (vid_usevsync); + } -/* -================= -VID_InitFullDIB -================= -*/ -void VID_InitFullDIB (HINSTANCE hInstance) + return true; +} + +static void IN_Shutdown(void); +void VID_Shutdown (void) { - DEVMODE devmode; - int modenum; - int originalnummodes; - int numlowresmodes; - int j; - int bpp; - int done; - BOOL stat; - -// enumerate >8 bpp modes - originalnummodes = nummodes; - modenum = 0; - - do - { - stat = EnumDisplaySettings (NULL, modenum, &devmode); + if(vid_initialized == false) + return; - if ((devmode.dmBitsPerPel >= 15) && (devmode.dmPelsWidth <= MAXWIDTH) && (devmode.dmPelsHeight <= MAXHEIGHT) && (nummodes < MAX_MODE_LIST)) - { - devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; + VID_RestoreSystemGamma(); - if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL) - VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 1, 1, devmode.dmBitsPerPel); - } + vid_initialized = false; + IN_Shutdown(); + if (qwglMakeCurrent) + qwglMakeCurrent(NULL, NULL); + if (baseRC && qwglDeleteContext) + qwglDeleteContext(baseRC); + // close the library before we get rid of the window + GL_CloseLibrary(); + if (baseDC && mainwindow) + ReleaseDC(mainwindow, baseDC); + AppActivate(false, false); + if (mainwindow) + DestroyWindow(mainwindow); + mainwindow = 0; + if (vid_isfullscreen) + ChangeDisplaySettings (NULL, 0); + vid_isfullscreen = false; +} - modenum++; - } - while (stat); +static void IN_Activate (qboolean grab) +{ + static qboolean restore_spi; + static int originalmouseparms[3]; -// see if there are any low-res modes that aren't being reported - numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]); - bpp = 16; - done = 0; + if (!mouseinitialized) + return; - do + if (grab) { - for (j=0 ; (j= 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); + } + else + restore_spi = false; + SetCursorPos ((window_x + vid.width / 2), (window_y + vid.height / 2)); - if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL) - VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 1, 1, devmode.dmBitsPerPel); + SetCapture (mainwindow); + ClipCursor (&window_rect); + } + ShowCursor (false); } - switch (bpp) + } + else + { + if (vid_usingmouse) { - case 16: - bpp = 32; - break; - - case 32: - bpp = 24; - break; - - case 24: - done = 1; - break; + vid_usingmouse = false; + cl_ignoremousemoves = 2; + if (dinput_acquired) + { + IDirectInputDevice_Unacquire(g_pMouse); + dinput_acquired = false; + } + else + { + // restore system mouseparms if we changed them + if (restore_spi) + SystemParametersInfo (SPI_SETMOUSE, 0, originalmouseparms, 0); + restore_spi = false; + ClipCursor (NULL); + ReleaseCapture (); + } + ShowCursor (true); } } - while (!done); - - if (nummodes == originalnummodes) - Con_SafePrintf ("No fullscreen DIB modes found\n"); } -//static int grabsysgamma = true; -WORD systemgammaramps[3][256], currentgammaramps[3][256]; -int VID_SetGamma(float prescale, float gamma, float scale, float base) +/* +=========== +IN_InitDInput +=========== +*/ +static qboolean IN_InitDInput (void) { - int i; - HDC hdc; - hdc = GetDC (NULL); + HRESULT hr; + DIPROPDWORD dipdw = { + { + sizeof(DIPROPDWORD), // diph.dwSize + sizeof(DIPROPHEADER), // diph.dwHeaderSize + 0, // diph.dwObj + DIPH_DEVICE, // diph.dwHow + }, + DINPUT_BUFFERSIZE, // dwData + }; - BuildGammaTable16(prescale, gamma, scale, base, ¤tgammaramps[0][0]); - for (i = 0;i < 256;i++) - currentgammaramps[1][i] = currentgammaramps[2][i] = currentgammaramps[0][i]; + if (!hInstDI) + { + hInstDI = LoadLibrary("dinput.dll"); - i = SetDeviceGammaRamp(hdc, ¤tgammaramps[0][0]); + if (hInstDI == NULL) + { + Con_Print("Couldn't load dinput.dll\n"); + return false; + } + } - ReleaseDC (NULL, hdc); - return i; // return success or failure -} + if (!pDirectInputCreate) + { + pDirectInputCreate = (void *)GetProcAddress(hInstDI,"DirectInputCreateA"); -void VID_RestoreGameGamma(void) -{ - VID_UpdateGamma(true); -} + if (!pDirectInputCreate) + { + Con_Print("Couldn't get DI proc addr\n"); + return false; + } + } -void VID_GetSystemGamma(void) -{ - HDC hdc; - hdc = GetDC (NULL); +// register with DirectInput and get an IDirectInput to play with. + hr = iDirectInputCreate(global_hInstance, DIRECTINPUT_VERSION, &g_pdi, NULL); - GetDeviceGammaRamp(hdc, &systemgammaramps[0][0]); + if (FAILED(hr)) + { + return false; + } - ReleaseDC (NULL, hdc); -} +// obtain an interface to the system mouse device. + hr = IDirectInput_CreateDevice(g_pdi, &GUID_SysMouse, &g_pMouse, NULL); -void VID_RestoreSystemGamma(void) -{ - HDC hdc; - hdc = GetDC (NULL); + if (FAILED(hr)) + { + Con_Print("Couldn't open DI mouse device\n"); + return false; + } - SetDeviceGammaRamp(hdc, &systemgammaramps[0][0]); +// set the data format to "mouse format". + hr = IDirectInputDevice_SetDataFormat(g_pMouse, &c_dfDIMouse); - ReleaseDC (NULL, hdc); -} + if (FAILED(hr)) + { + Con_Print("Couldn't set DI mouse format\n"); + return false; + } + +// set the cooperativity level. + hr = IDirectInputDevice_SetCooperativeLevel(g_pMouse, mainwindow, + DISCL_EXCLUSIVE | DISCL_FOREGROUND); -//======================================================== -// Video menu stuff -//======================================================== + if (FAILED(hr)) + { + Con_Print("Couldn't set DI coop level\n"); + return false; + } -extern void M_Menu_Options_f (void); -extern void M_Print (float cx, float cy, char *str); -extern void M_PrintWhite (float cx, float cy, char *str); -extern void M_DrawCharacter (float cx, float cy, int num); -extern void M_DrawPic (float cx, float cy, char *picname); -static int vid_wmodes; +// set the buffer size to DINPUT_BUFFERSIZE elements. +// the buffer size is a DWORD property associated with the device + hr = IDirectInputDevice_SetProperty(g_pMouse, DIPROP_BUFFERSIZE, &dipdw.diph); -typedef struct -{ - int modenum; - char *desc; - int iscur; -} modedesc_t; + if (FAILED(hr)) + { + Con_Print("Couldn't set DI buffersize\n"); + return false; + } -#define MAX_COLUMN_SIZE 9 -#define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 2) -#define MAX_MODEDESCS (MAX_COLUMN_SIZE*3) + return true; +} -static modedesc_t modedescs[MAX_MODEDESCS]; /* -================ -VID_MenuDraw -================ +=========== +IN_StartupMouse +=========== */ -void VID_MenuDraw (void) +static void IN_StartupMouse (void) { - cachepic_t *p; - char *ptr; - int lnummodes, i, k, column, row; - vmode_t *pv; - - p = Draw_CachePic ("gfx/vidmodes.lmp"); - M_DrawPic ( (320-p->width)/2, 4, "gfx/vidmodes.lmp"); + if (COM_CheckParm ("-nomouse")) + return; - vid_wmodes = 0; - lnummodes = VID_NumModes (); + mouseinitialized = true; - for (i=1 ; (i 0) + if (dinput_acquired) { - M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)"); - - column = 8; - row = 36+2*8; + DIDEVICEOBJECTDATA od; + DWORD dwElements; + HRESULT hr; + mx = 0; + my = 0; - for (i=0 ; i 0) + { + Key_Event (K_MWHEELUP, 0, true); + Key_Event (K_MWHEELUP, 0, false); + } + break; + + case DIMOFS_BUTTON0: + if (od.dwData & 0x80) + mstate_di |= 1; + else + mstate_di &= ~1; + break; + + case DIMOFS_BUTTON1: + if (od.dwData & 0x80) + mstate_di |= (1<<1); + else + mstate_di &= ~(1<<1); + break; + + case DIMOFS_BUTTON2: + if (od.dwData & 0x80) + mstate_di |= (1<<2); + else + mstate_di &= ~(1<<2); + break; + + case DIMOFS_BUTTON3: + if (od.dwData & 0x80) + mstate_di |= (1<<3); + else + mstate_di &= ~(1<<3); + break; } } + + // perform button actions + for (i=0 ; i"); - M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4, "and -bpp "); - M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6, "Select windowed mode with -window"); + // if the mouse has moved, force it to the center, so there's room to move + if (!cl.csqc_wantsmousemove) + if (mx || my) + SetCursorPos ((window_x + vid.width / 2), (window_y + vid.height / 2)); + } } /* -================ -VID_MenuKey -================ +=========== +IN_Move +=========== */ -void VID_MenuKey (int key) +void IN_Move (void) { - switch (key) + if (vid_activewindow && !vid_hidden) { - case K_ESCAPE: - S_LocalSound ("misc/menu1.wav"); - M_Menu_Options_f (); - break; - - default: - break; + IN_MouseMove (); + IN_JoyMove (); } } -static HINSTANCE gldll; -int GL_OpenLibrary(const char *name) +/* +=============== +IN_StartupJoystick +=============== +*/ +static void IN_StartupJoystick (void) { - Con_Printf("Loading GL driver %s\n", name); - GL_CloseLibrary(); - if (!(gldll = LoadLibrary(name))) + int numdevs; + JOYCAPS jc; + MMRESULT mmr; + mmr = 0; + + // assume no joystick + joy_avail = false; + + // abort startup if user requests no joystick +// COMMANDLINEOPTION: Windows Input: -nojoy disables joystick support, may be a small speed increase + if (COM_CheckParm ("-nojoy")) + return; + + // verify joystick driver is present + if ((numdevs = joyGetNumDevs ()) == 0) { - Con_Printf("Unable to LoadLibrary %s\n", name); - return false; + Con_Print("\njoystick not found -- driver not present\n\n"); + return; } - strcpy(gl_driver, name); - return true; -} -void GL_CloseLibrary(void) -{ - FreeLibrary(gldll); - gldll = 0; - gl_driver[0] = 0; - qwglGetProcAddress = NULL; - gl_extensions = ""; - gl_platform = ""; - gl_platformextensions = ""; + // cycle through the joystick ids for the first valid one + for (joy_id=0 ; joy_id rating) + if ( (povstate & (1< 14000.0) + fTemp = 14000.0; + // restore direction information + fAxisValue = (fAxisValue > 0.0) ? fTemp : -fTemp; + } + } - baseRC = qwglCreateContext( maindc ); - if (!baseRC) - Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window."); - if (!qwglMakeCurrent( maindc, baseRC )) - Sys_Error ("wglMakeCurrent failed"); + // convert range from -32768..32767 to -1..1 + fAxisValue /= 32768.0; - gl_renderer = qglGetString(GL_RENDERER); - gl_vendor = qglGetString(GL_VENDOR); - gl_version = qglGetString(GL_VERSION); - gl_extensions = qglGetString(GL_EXTENSIONS); - gl_platform = "WGL"; - gl_platformextensions = ""; + switch (dwAxisMap[i]) + { + case AxisForward: + if ((joy_advanced.integer == 0) && mouselook) + { + // user wants forward control to become look control + if (fabs(fAxisValue) > joy_pitchthreshold.value) + { + // if mouse invert is on, invert the joystick pitch value + // only absolute control support here (joy_advanced is false) + if (m_pitch.value < 0.0) + { + cl.viewangles[PITCH] -= (fAxisValue * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value; + } + else + { + cl.viewangles[PITCH] += (fAxisValue * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value; + } + V_StopPitchDrift(); + } + else + { + // no pitch movement + // disable pitch return-to-center unless requested by user + // *** this code can be removed when the lookspring bug is fixed + // *** the bug always has the lookspring feature on + if(lookspring.value == 0.0) + V_StopPitchDrift(); + } + } + else + { + // user wants forward control to be forward control + if (fabs(fAxisValue) > joy_forwardthreshold.value) + { + cl.cmd.forwardmove += (fAxisValue * joy_forwardsensitivity.value) * speed * cl_forwardspeed.value; + } + } + break; - if (GL_CheckExtension("WGL_ARB_extensions_string", getextensionsstringfuncs, NULL, false)) - gl_platformextensions = qwglGetExtensionsStringARB(maindc); + case AxisSide: + if (fabs(fAxisValue) > joy_sidethreshold.value) + { + cl.cmd.sidemove += (fAxisValue * joy_sidesensitivity.value) * speed * cl_sidespeed.value; + } + break; - gl_videosyncavailable = GL_CheckExtension("WGL_EXT_swap_control", wglswapintervalfuncs, NULL, false); + case AxisTurn: + if ((in_strafe.state & 1) || (lookstrafe.integer && mouselook)) + { + // user wants turn control to become side control + if (fabs(fAxisValue) > joy_sidethreshold.value) + { + cl.cmd.sidemove -= (fAxisValue * joy_sidesensitivity.value) * speed * cl_sidespeed.value; + } + } + else + { + // user wants turn control to be turn control + if (fabs(fAxisValue) > joy_yawthreshold.value) + { + if(dwControlMap[i] == JOY_ABSOLUTE_AXIS) + { + cl.viewangles[YAW] += (fAxisValue * joy_yawsensitivity.value) * aspeed * cl_yawspeed.value; + } + else + { + cl.viewangles[YAW] += (fAxisValue * joy_yawsensitivity.value) * speed * 180.0; + } - GL_Init (); + } + } + break; - // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!) - if (strncasecmp(gl_vendor,"ATI",3)==0) - { - if (strncasecmp(gl_renderer,"Rage Pro",8)==0) - isRagePro = true; + case AxisLook: + if (mouselook) + { + if (fabs(fAxisValue) > joy_pitchthreshold.value) + { + // pitch movement detected and pitch movement desired by user + if(dwControlMap[i] == JOY_ABSOLUTE_AXIS) + { + cl.viewangles[PITCH] += (fAxisValue * joy_pitchsensitivity.value) * aspeed * cl_pitchspeed.value; + } + else + { + cl.viewangles[PITCH] += (fAxisValue * joy_pitchsensitivity.value) * speed * 180.0; + } + V_StopPitchDrift(); + } + else + { + // no pitch movement + // disable pitch return-to-center unless requested by user + // *** this code can be removed when the lookspring bug is fixed + // *** the bug always has the lookspring feature on + if(lookspring.integer == 0) + V_StopPitchDrift(); + } + } + break; + + default: + break; + } } - if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh... - isG200 = true; +} - vid_realmode = vid_modenum; +static void IN_Init(void) +{ + uiWheelMessage = RegisterWindowMessage ( "MSWHEEL_ROLLMSG" ); + + // joystick variables + Cvar_RegisterVariable (&in_joystick); + Cvar_RegisterVariable (&joy_name); + Cvar_RegisterVariable (&joy_advanced); + Cvar_RegisterVariable (&joy_advaxisx); + Cvar_RegisterVariable (&joy_advaxisy); + Cvar_RegisterVariable (&joy_advaxisz); + Cvar_RegisterVariable (&joy_advaxisr); + Cvar_RegisterVariable (&joy_advaxisu); + Cvar_RegisterVariable (&joy_advaxisv); + Cvar_RegisterVariable (&joy_forwardthreshold); + Cvar_RegisterVariable (&joy_sidethreshold); + Cvar_RegisterVariable (&joy_pitchthreshold); + Cvar_RegisterVariable (&joy_yawthreshold); + Cvar_RegisterVariable (&joy_forwardsensitivity); + Cvar_RegisterVariable (&joy_sidesensitivity); + Cvar_RegisterVariable (&joy_pitchsensitivity); + Cvar_RegisterVariable (&joy_yawsensitivity); + Cvar_RegisterVariable (&joy_wwhack1); + Cvar_RegisterVariable (&joy_wwhack2); + Cmd_AddCommand ("joyadvancedupdate", Joy_AdvancedUpdate_f, "applies current joyadv* cvar settings to the joystick driver"); +} - vid_menudrawfn = VID_MenuDraw; - vid_menukeyfn = VID_MenuKey; +static void IN_Shutdown(void) +{ + IN_Activate (false); - strcpy (badmode.modedesc, "Bad mode"); - vid_canalttab = true; + if (g_pMouse) + IDirectInputDevice_Release(g_pMouse); + g_pMouse = NULL; - vid_hidden = false; + if (g_pdi) + IDirectInput_Release(g_pdi); + g_pdi = NULL; } -