2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // vid_wgl.c -- NT GL vid component
23 #pragma comment(lib, "comctl32.lib")
29 #pragma comment(lib, "dinput8.lib")
30 #pragma comment(lib, "dxguid.lib")
32 #ifndef DIRECTINPUT_VERSION
33 # define DIRECTINPUT_VERSION 0x0500 /* Version 5.0 */
48 #include "dpsoftrast.h"
53 cvar_t vid_dx9 = {CVAR_SAVE, "vid_dx9", "0", "use Microsoft Direct3D9(r) for rendering"};
54 cvar_t vid_dx9_hal = {CVAR_SAVE, "vid_dx9_hal", "1", "enables hardware rendering (1), otherwise software reference rasterizer (0 - very slow), note that 0 is necessary when using NVPerfHUD (which renders in hardware but requires this option to enable it)"};
55 cvar_t vid_dx9_softvertex = {CVAR_SAVE, "vid_dx9_softvertex", "0", "enables software vertex processing (for compatibility testing? or if you have a very fast CPU), usually you want this off"};
56 cvar_t vid_dx9_triplebuffer = {CVAR_SAVE, "vid_dx9_triplebuffer", "0", "enables triple buffering when using vid_vsync in fullscreen, this options adds some latency and only helps when framerate is below 60 so you usually don't want it"};
57 //cvar_t vid_dx10 = {CVAR_SAVE, "vid_dx10", "1", "use Microsoft Direct3D10(r) for rendering"};
58 //cvar_t vid_dx11 = {CVAR_SAVE, "vid_dx11", "1", "use Microsoft Direct3D11(r) for rendering"};
60 D3DPRESENT_PARAMETERS vid_d3dpresentparameters;
62 // we declare this in vid_shared.c because it is required by dedicated server and all clients when SUPPORTD3D is defined
63 extern LPDIRECT3DDEVICE9 vid_d3d9dev;
66 D3DCAPS9 vid_d3d9caps;
67 qboolean vid_d3ddevicelost;
70 extern HINSTANCE global_hInstance;
72 static HINSTANCE gldll;
75 #define WM_MOUSEWHEEL 0x020A
78 // Tell startup code that we have a client
79 int cl_available = true;
81 qboolean vid_supportrefreshrate = true;
83 static int (WINAPI *qwglChoosePixelFormat)(HDC, CONST PIXELFORMATDESCRIPTOR *);
84 static int (WINAPI *qwglDescribePixelFormat)(HDC, int, UINT, LPPIXELFORMATDESCRIPTOR);
85 //static int (WINAPI *qwglGetPixelFormat)(HDC);
86 static BOOL (WINAPI *qwglSetPixelFormat)(HDC, int, CONST PIXELFORMATDESCRIPTOR *);
87 static BOOL (WINAPI *qwglSwapBuffers)(HDC);
88 static HGLRC (WINAPI *qwglCreateContext)(HDC);
89 static BOOL (WINAPI *qwglDeleteContext)(HGLRC);
90 static HGLRC (WINAPI *qwglGetCurrentContext)(VOID);
91 static HDC (WINAPI *qwglGetCurrentDC)(VOID);
92 static PROC (WINAPI *qwglGetProcAddress)(LPCSTR);
93 static BOOL (WINAPI *qwglMakeCurrent)(HDC, HGLRC);
94 static BOOL (WINAPI *qwglSwapIntervalEXT)(int interval);
95 static const char *(WINAPI *qwglGetExtensionsStringARB)(HDC hdc);
96 static BOOL (WINAPI *qwglChoosePixelFormatARB)(HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
97 static BOOL (WINAPI *qwglGetPixelFormatAttribivARB)(HDC hdc, int iPixelFormat, int iLayerPlane, UINT nAttributes, const int *piAttributes, int *piValues);
99 static dllfunction_t wglfuncs[] =
101 {"wglChoosePixelFormat", (void **) &qwglChoosePixelFormat},
102 {"wglDescribePixelFormat", (void **) &qwglDescribePixelFormat},
103 // {"wglGetPixelFormat", (void **) &qwglGetPixelFormat},
104 {"wglSetPixelFormat", (void **) &qwglSetPixelFormat},
105 {"wglSwapBuffers", (void **) &qwglSwapBuffers},
106 {"wglCreateContext", (void **) &qwglCreateContext},
107 {"wglDeleteContext", (void **) &qwglDeleteContext},
108 {"wglGetProcAddress", (void **) &qwglGetProcAddress},
109 {"wglMakeCurrent", (void **) &qwglMakeCurrent},
110 {"wglGetCurrentContext", (void **) &qwglGetCurrentContext},
111 {"wglGetCurrentDC", (void **) &qwglGetCurrentDC},
115 static dllfunction_t wglswapintervalfuncs[] =
117 {"wglSwapIntervalEXT", (void **) &qwglSwapIntervalEXT},
121 static dllfunction_t wglpixelformatfuncs[] =
123 {"wglChoosePixelFormatARB", (void **) &qwglChoosePixelFormatARB},
124 {"wglGetPixelFormatAttribivARB", (void **) &qwglGetPixelFormatAttribivARB},
128 static DEVMODE gdevmode, initialdevmode;
129 static qboolean vid_initialized = false;
130 static qboolean vid_wassuspended = false;
131 static qboolean vid_usingmouse = false;
132 static qboolean vid_usinghidecursor = false;
133 static qboolean vid_usingvsync = false;
134 static qboolean vid_usevsync = false;
137 // used by cd_win.c and snd_win.c
143 static HDC vid_softhdc;
144 static HGDIOBJ vid_softhdc_backup;
145 static BITMAPINFO vid_softbmi;
146 static HBITMAP vid_softdibhandle;
148 //HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow);
150 static qboolean vid_isfullscreen;
152 //void VID_MenuDraw (void);
153 //void VID_MenuKey (int key);
155 //LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
156 //void AppActivate(BOOL fActive, BOOL minimize);
157 //void ClearAllStates (void);
158 //void VID_UpdateWindowStatus (void);
160 //====================================
162 static int window_x, window_y;
164 static qboolean mouseinitialized;
166 #ifdef SUPPORTDIRECTX
167 static qboolean dinput;
168 #define DINPUT_BUFFERSIZE 16
169 #define iDirectInputCreate(a,b,c,d) pDirectInputCreate(a,b,c,d)
171 static HRESULT (WINAPI *pDirectInputCreate)(HINSTANCE hinst, DWORD dwVersion, LPDIRECTINPUT * lplpDirectInput, LPUNKNOWN punkOuter);
174 // LordHavoc: thanks to backslash for this support for mouse buttons 4 and 5
175 /* backslash :: imouse explorer buttons */
176 /* These are #ifdefed out for non-Win2K in the February 2001 version of
177 MS's platform SDK, but we need them for compilation. . . */
178 #ifndef WM_XBUTTONDOWN
179 #define WM_XBUTTONDOWN 0x020B
180 #define WM_XBUTTONUP 0x020C
183 #define MK_XBUTTON1 0x0020
184 #define MK_XBUTTON2 0x0040
187 // LordHavoc: lets hope this allows more buttons in the future...
188 #define MK_XBUTTON3 0x0080
189 #define MK_XBUTTON4 0x0100
190 #define MK_XBUTTON5 0x0200
191 #define MK_XBUTTON6 0x0400
192 #define MK_XBUTTON7 0x0800
197 static int mouse_buttons;
198 static int mouse_oldbuttonstate;
200 static unsigned int uiWheelMessage;
201 #ifdef SUPPORTDIRECTX
202 static qboolean dinput_acquired;
204 static unsigned int mstate_di;
207 // joystick defines and variables
208 // where should defines be moved?
209 #define JOY_ABSOLUTE_AXIS 0x00000000 // control like a joystick
210 #define JOY_RELATIVE_AXIS 0x00000010 // control like a mouse, spinner, trackball
211 #define JOY_MAX_AXES 6 // X, Y, Z, R, U, V
219 // joystick axes state
227 static joy_axiscache_t joy_axescache[JOY_MAX_AXES];
231 AxisNada = 0, AxisForward, AxisLook, AxisSide, AxisTurn
234 static DWORD dwAxisFlags[JOY_MAX_AXES] =
236 JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ, JOY_RETURNR, JOY_RETURNU, JOY_RETURNV
239 static DWORD dwAxisMap[JOY_MAX_AXES];
240 static DWORD dwControlMap[JOY_MAX_AXES];
241 static PDWORD pdwRawValue[JOY_MAX_AXES];
243 // none of these cvars are saved over a session
244 // this means that advanced controller configuration needs to be executed
245 // each time. this avoids any problems with getting back to a default usage
246 // or when changing from one controller to another. this way at least something
248 static cvar_t in_joystick = {CVAR_SAVE, "joystick","0", "enables joysticks"};
249 static cvar_t joy_name = {0, "joyname", "joystick", "name of joystick to use (informational only, used only by joyadvanced 1 mode)"};
250 static cvar_t joy_advanced = {0, "joyadvanced", "0", "use more than 2 axis joysticks (configuring this is very technical)"};
251 static cvar_t joy_advaxisx = {0, "joyadvaxisx", "0", "axis mapping for joyadvanced 1 mode"};
252 static cvar_t joy_advaxisy = {0, "joyadvaxisy", "0", "axis mapping for joyadvanced 1 mode"};
253 static cvar_t joy_advaxisz = {0, "joyadvaxisz", "0", "axis mapping for joyadvanced 1 mode"};
254 static cvar_t joy_advaxisr = {0, "joyadvaxisr", "0", "axis mapping for joyadvanced 1 mode"};
255 static cvar_t joy_advaxisu = {0, "joyadvaxisu", "0", "axis mapping for joyadvanced 1 mode"};
256 static cvar_t joy_advaxisv = {0, "joyadvaxisv", "0", "axis mapping for joyadvanced 1 mode"};
257 static cvar_t joy_forwardthreshold = {0, "joyforwardthreshold", "0.15", "minimum joystick movement necessary to move forward"};
258 static cvar_t joy_sidethreshold = {0, "joysidethreshold", "0.15", "minimum joystick movement necessary to move sideways (strafing)"};
259 static cvar_t joy_pitchthreshold = {0, "joypitchthreshold", "0.15", "minimum joystick movement necessary to look up/down"};
260 static cvar_t joy_yawthreshold = {0, "joyyawthreshold", "0.15", "minimum joystick movement necessary to turn left/right"};
261 static cvar_t joy_forwardsensitivity = {0, "joyforwardsensitivity", "-1.0", "how fast the joystick moves forward"};
262 static cvar_t joy_sidesensitivity = {0, "joysidesensitivity", "-1.0", "how fast the joystick moves sideways (strafing)"};
263 static cvar_t joy_pitchsensitivity = {0, "joypitchsensitivity", "1.0", "how fast the joystick looks up/down"};
264 static cvar_t joy_yawsensitivity = {0, "joyyawsensitivity", "-1.0", "how fast the joystick turns left/right"};
265 static cvar_t joy_wwhack1 = {0, "joywwhack1", "0.0", "special hack for wingman warrior"};
266 static cvar_t joy_wwhack2 = {0, "joywwhack2", "0.0", "special hack for wingman warrior"};
267 static cvar_t joy_axiskeyevents = {CVAR_SAVE, "joy_axiskeyevents", "0", "generate uparrow/leftarrow etc. keyevents for joystick axes, use if your joystick driver is not generating them"};
268 static cvar_t joy_axiskeyevents_deadzone = {CVAR_SAVE, "joy_axiskeyevents_deadzone", "0.5", "deadzone value for axes"};
270 static cvar_t vid_forcerefreshrate = {0, "vid_forcerefreshrate", "0", "try to set the given vid_refreshrate even if Windows doesn't list it as valid video mode"};
272 static qboolean joy_avail, joy_advancedinit, joy_haspov;
273 static DWORD joy_oldbuttonstate, joy_oldpovstate;
276 static DWORD joy_flags;
277 static DWORD joy_numbuttons;
279 #ifdef SUPPORTDIRECTX
280 static LPDIRECTINPUT g_pdi;
281 static LPDIRECTINPUTDEVICE g_pMouse;
282 static HINSTANCE hInstDI;
287 // forward-referenced functions
288 static void IN_StartupJoystick (void);
289 static void Joy_AdvancedUpdate_f (void);
290 static void IN_JoyMove (void);
291 static void IN_StartupMouse (void);
294 //====================================
296 qboolean vid_reallyhidden = true;
298 qboolean vid_begunscene = false;
300 void VID_Finish (void)
305 vid_hidden = vid_reallyhidden;
307 vid_usevsync = vid_vsync.integer && !cls.timedemo && qwglSwapIntervalEXT;
311 switch(vid.renderpath)
313 case RENDERPATH_GL11:
314 case RENDERPATH_GL13:
315 case RENDERPATH_GL20:
316 case RENDERPATH_GLES2:
317 if (vid_usingvsync != vid_usevsync)
319 vid_usingvsync = vid_usevsync;
320 qwglSwapIntervalEXT (vid_usevsync);
322 if (r_speeds.integer == 2 || gl_finish.integer)
326 case RENDERPATH_D3D9:
330 IDirect3DDevice9_EndScene(vid_d3d9dev);
331 vid_begunscene = false;
333 if (!vid_reallyhidden)
335 if (!vid_d3ddevicelost)
337 vid_hidden = vid_reallyhidden;
338 hr = IDirect3DDevice9_Present(vid_d3d9dev, NULL, NULL, NULL, NULL);
339 if (hr == D3DERR_DEVICELOST)
341 vid_d3ddevicelost = true;
348 hr = IDirect3DDevice9_TestCooperativeLevel(vid_d3d9dev);
351 case D3DERR_DEVICELOST:
352 vid_d3ddevicelost = true;
356 case D3DERR_DEVICENOTRESET:
357 vid_d3ddevicelost = false;
358 vid_hidden = vid_reallyhidden;
359 R_Modules_DeviceLost();
360 IDirect3DDevice9_Reset(vid_d3d9dev, &vid_d3dpresentparameters);
361 R_Modules_DeviceRestored();
364 vid_hidden = vid_reallyhidden;
365 IDirect3DDevice9_Present(vid_d3d9dev, NULL, NULL, NULL, NULL);
369 if (!vid_begunscene && !vid_hidden)
371 IDirect3DDevice9_BeginScene(vid_d3d9dev);
372 vid_begunscene = true;
377 case RENDERPATH_D3D10:
379 case RENDERPATH_D3D11:
381 case RENDERPATH_SOFT:
383 // baseDC = GetDC(mainwindow);
384 BitBlt(baseDC, 0, 0, vid.width, vid.height, vid_softhdc, 0, 0, SRCCOPY);
385 // ReleaseDC(mainwindow, baseDC);
391 // make sure a context switch can happen every frame - Logitech drivers
392 // input drivers sometimes eat cpu time every 3 seconds or lag badly
396 VID_UpdateGamma(false, 256);
399 //==========================================================================
404 static unsigned char scantokey[128] =
406 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
407 0 ,27 ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'0' ,'-' ,'=' ,K_BACKSPACE,9 ,//0
408 'q' ,'w' ,'e' ,'r' ,'t' ,'y' ,'u' ,'i' ,'o' ,'p' ,'[' ,']' ,13 ,K_CTRL ,'a' ,'s' ,//1
409 'd' ,'f' ,'g' ,'h' ,'j' ,'k' ,'l' ,';' ,'\'' ,'`' ,K_SHIFT ,'\\' ,'z' ,'x' ,'c' ,'v' ,//2
410 'b' ,'n' ,'m' ,',' ,'.' ,'/' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2 ,K_F3 ,K_F4 ,K_F5 ,//3
411 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
412 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,//5
413 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,//6
414 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 //7
422 Map from windows to quake keynums
425 static int MapKey (int key, int virtualkey)
428 int modified = (key >> 16) & 255;
429 qboolean is_extended = false;
431 if (modified < 128 && scantokey[modified])
432 result = scantokey[modified];
436 Con_DPrintf("key 0x%02x (0x%8x, 0x%8x) has no translation\n", modified, key, virtualkey);
453 return K_KP_LEFTARROW;
455 return K_KP_RIGHTARROW;
459 return K_KP_DOWNARROW;
486 ===================================================================
490 ===================================================================
498 static void ClearAllStates (void)
502 mouse_oldbuttonstate = 0;
505 void AppActivate(BOOL fActive, BOOL minimize)
506 /****************************************************************************
508 * Function: AppActivate
509 * Parameters: fActive - True if app is activating
511 * Description: If the application is activating, then swap the system
512 * into SYSPAL_NOSTATIC mode so that our palettes will display
515 ****************************************************************************/
517 static qboolean sound_active = false; // initially blocked by Sys_InitConsole()
519 vid_activewindow = fActive != FALSE;
520 vid_reallyhidden = minimize != FALSE;
522 // enable/disable sound on focus gain/loss
523 if ((!vid_reallyhidden && vid_activewindow) || !snd_mutewhenidle.integer)
536 sound_active = false;
542 if (vid_isfullscreen)
544 if (vid_wassuspended)
546 vid_wassuspended = false;
549 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
550 ShowWindow(mainwindow, SW_SHOWNORMAL);
554 // LordHavoc: from dabb, fix for alt-tab bug in NVidia drivers
556 MoveWindow(mainwindow,0,0,gdevmode.dmPelsWidth,gdevmode.dmPelsHeight,false);
562 VID_SetMouse(false, false, false);
563 if (vid_isfullscreen)
566 ChangeDisplaySettings (NULL, 0);
567 vid_wassuspended = true;
569 VID_RestoreSystemGamma();
573 //TODO: move it around in vid_wgl.c since I dont think this is the right position
574 void Sys_SendKeyEvents (void)
578 while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
580 if (!GetMessage (&msg, NULL, 0, 0))
583 TranslateMessage (&msg);
584 DispatchMessage (&msg);
588 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
590 static keynum_t buttonremap[16] =
610 /* main window procedure */
611 static qboolean IN_JoystickBlockDoubledKeyEvents(int keycode);
612 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
615 int fActive, fMinimized, temp;
616 unsigned char state[256];
617 unsigned char asciichar[4];
620 qboolean down = false;
622 if ( uMsg == uiWheelMessage )
623 uMsg = WM_MOUSEWHEEL;
628 if (vid_isfullscreen)
629 ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
636 window_x = (int) LOWORD(lParam);
637 window_y = (int) HIWORD(lParam);
638 VID_SetMouse(false, false, false);
646 vkey = MapKey(lParam, wParam);
647 GetKeyboardState (state);
648 // alt/ctrl/shift tend to produce funky ToAscii values,
649 // and if it's not a single character we don't know care about it
650 charlength = ToAscii (wParam, lParam >> 16, state, (LPWORD)asciichar, 0);
651 if (vkey == K_ALT || vkey == K_CTRL || vkey == K_SHIFT || charlength == 0)
653 else if( charlength == 2 ) {
654 asciichar[0] = asciichar[1];
656 if (!IN_JoystickBlockDoubledKeyEvents(vkey))
657 Key_Event (vkey, asciichar[0], down);
661 // keep Alt-Space from happening
665 // prevent screensaver from occuring while the active window
666 // note: password-locked screensavers on Vista still work
667 if (vid_activewindow && ((wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER))
670 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
673 // this is complicated because Win32 seems to pack multiple mouse events into
674 // one update sometimes, so we always check all states and look for events
681 case WM_XBUTTONDOWN: // backslash :: imouse explorer buttons
682 case WM_XBUTTONUP: // backslash :: imouse explorer buttons
686 if (wParam & MK_LBUTTON)
689 if (wParam & MK_RBUTTON)
692 if (wParam & MK_MBUTTON)
695 /* backslash :: imouse explorer buttons */
696 if (wParam & MK_XBUTTON1)
699 if (wParam & MK_XBUTTON2)
703 // LordHavoc: lets hope this allows more buttons in the future...
704 if (wParam & MK_XBUTTON3)
706 if (wParam & MK_XBUTTON4)
708 if (wParam & MK_XBUTTON5)
710 if (wParam & MK_XBUTTON6)
712 if (wParam & MK_XBUTTON7)
715 #ifdef SUPPORTDIRECTX
716 if (!dinput_acquired)
719 // perform button actions
721 for (i=0 ; i<mouse_buttons && i < 16 ; i++)
722 if ((temp ^ mouse_oldbuttonstate) & (1<<i))
723 Key_Event (buttonremap[i], 0, (temp & (1<<i)) != 0);
724 mouse_oldbuttonstate = temp;
729 // JACK: This is the mouse wheel with the Intellimouse
730 // Its delta is either positive or neg, and we generate the proper
733 if ((short) HIWORD(wParam) > 0) {
734 Key_Event(K_MWHEELUP, 0, true);
735 Key_Event(K_MWHEELUP, 0, false);
737 Key_Event(K_MWHEELDOWN, 0, true);
738 Key_Event(K_MWHEELDOWN, 0, false);
746 if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
752 fActive = LOWORD(wParam);
753 fMinimized = (BOOL) HIWORD(wParam);
754 AppActivate(!(fActive == WA_INACTIVE), fMinimized);
756 // fix the leftover Alt from any Alt-Tab or the like that switched us away
762 // PostQuitMessage (0);
766 lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
770 /* pass all unhandled messages to DefWindowProc */
771 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
775 /* return 1 if handled message, 0 if not */
779 int VID_SetGamma(unsigned short *ramps, int rampsize)
783 HDC hdc = GetDC (NULL);
784 int i = SetDeviceGammaRamp(hdc, ramps);
785 ReleaseDC (NULL, hdc);
786 return i; // return success or failure
792 int VID_GetGamma(unsigned short *ramps, int rampsize)
796 HDC hdc = GetDC (NULL);
797 int i = GetDeviceGammaRamp(hdc, ramps);
798 ReleaseDC (NULL, hdc);
799 return i; // return success or failure
805 static void GL_CloseLibrary(void)
812 qwglGetProcAddress = NULL;
815 gl_platformextensions = "";
819 static int GL_OpenLibrary(const char *name)
821 Con_Printf("Loading OpenGL driver %s\n", name);
823 if (!(gldll = LoadLibrary(name)))
825 Con_Printf("Unable to LoadLibrary %s\n", name);
828 strlcpy(gl_driver, name, sizeof(gl_driver));
832 void *GL_GetProcAddress(const char *name)
837 if (qwglGetProcAddress != NULL)
838 p = (void *) qwglGetProcAddress(name);
840 p = (void *) GetProcAddress(gldll, name);
847 #ifndef WGL_ARB_pixel_format
848 #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
849 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
850 #define WGL_DRAW_TO_BITMAP_ARB 0x2002
851 #define WGL_ACCELERATION_ARB 0x2003
852 #define WGL_NEED_PALETTE_ARB 0x2004
853 #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005
854 #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006
855 #define WGL_SWAP_METHOD_ARB 0x2007
856 #define WGL_NUMBER_OVERLAYS_ARB 0x2008
857 #define WGL_NUMBER_UNDERLAYS_ARB 0x2009
858 #define WGL_TRANSPARENT_ARB 0x200A
859 #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037
860 #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038
861 #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039
862 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A
863 #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B
864 #define WGL_SHARE_DEPTH_ARB 0x200C
865 #define WGL_SHARE_STENCIL_ARB 0x200D
866 #define WGL_SHARE_ACCUM_ARB 0x200E
867 #define WGL_SUPPORT_GDI_ARB 0x200F
868 #define WGL_SUPPORT_OPENGL_ARB 0x2010
869 #define WGL_DOUBLE_BUFFER_ARB 0x2011
870 #define WGL_STEREO_ARB 0x2012
871 #define WGL_PIXEL_TYPE_ARB 0x2013
872 #define WGL_COLOR_BITS_ARB 0x2014
873 #define WGL_RED_BITS_ARB 0x2015
874 #define WGL_RED_SHIFT_ARB 0x2016
875 #define WGL_GREEN_BITS_ARB 0x2017
876 #define WGL_GREEN_SHIFT_ARB 0x2018
877 #define WGL_BLUE_BITS_ARB 0x2019
878 #define WGL_BLUE_SHIFT_ARB 0x201A
879 #define WGL_ALPHA_BITS_ARB 0x201B
880 #define WGL_ALPHA_SHIFT_ARB 0x201C
881 #define WGL_ACCUM_BITS_ARB 0x201D
882 #define WGL_ACCUM_RED_BITS_ARB 0x201E
883 #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
884 #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
885 #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
886 #define WGL_DEPTH_BITS_ARB 0x2022
887 #define WGL_STENCIL_BITS_ARB 0x2023
888 #define WGL_AUX_BUFFERS_ARB 0x2024
889 #define WGL_NO_ACCELERATION_ARB 0x2025
890 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
891 #define WGL_FULL_ACCELERATION_ARB 0x2027
892 #define WGL_SWAP_EXCHANGE_ARB 0x2028
893 #define WGL_SWAP_COPY_ARB 0x2029
894 #define WGL_SWAP_UNDEFINED_ARB 0x202A
895 #define WGL_TYPE_RGBA_ARB 0x202B
896 #define WGL_TYPE_COLORINDEX_ARB 0x202C
899 #ifndef WGL_ARB_multisample
900 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
901 #define WGL_SAMPLES_ARB 0x2042
905 static void IN_Init(void);
911 Cvar_RegisterVariable(&vid_dx9);
912 Cvar_RegisterVariable(&vid_dx9_hal);
913 Cvar_RegisterVariable(&vid_dx9_softvertex);
914 Cvar_RegisterVariable(&vid_dx9_triplebuffer);
915 // Cvar_RegisterVariable(&vid_dx10);
916 // Cvar_RegisterVariable(&vid_dx11);
919 InitCommonControls();
920 hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON1));
922 // Register the frame class
924 wc.lpfnWndProc = (WNDPROC)MainWndProc;
927 wc.hInstance = global_hInstance;
929 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
930 wc.hbrBackground = NULL;
932 wc.lpszClassName = "DarkPlacesWindowClass";
934 if (!RegisterClass (&wc))
935 Con_Printf ("Couldn't register window class\n");
937 memset(&initialdevmode, 0, sizeof(initialdevmode));
938 EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &initialdevmode);
943 qboolean VID_InitModeGL(viddef_mode_t *mode)
949 PIXELFORMATDESCRIPTOR pfd =
951 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
953 PFD_DRAW_TO_WINDOW // support window
954 | PFD_SUPPORT_OPENGL // support OpenGL
955 | PFD_DOUBLEBUFFER , // double buffered
956 PFD_TYPE_RGBA, // RGBA type
957 24, // 24-bit color depth
958 0, 0, 0, 0, 0, 0, // color bits ignored
959 0, // no alpha buffer
960 0, // shift bit ignored
961 0, // no accumulation buffer
962 0, 0, 0, 0, // accum bits ignored
963 32, // 32-bit z-buffer
964 0, // no stencil buffer
965 0, // no auxiliary buffer
966 PFD_MAIN_PLANE, // main layer
968 0, 0, 0 // layer masks ignored
971 int pixelformat, newpixelformat;
972 UINT numpixelformats;
973 DWORD WindowStyle, ExWindowStyle;
974 int CenterX, CenterY;
975 const char *gldrivername;
978 qboolean foundmode, foundgoodmode;
983 int bpp = mode->bitsperpixel;
984 int width = mode->width;
985 int height = mode->height;
986 int refreshrate = (int)floor(mode->refreshrate+0.5);
987 int stereobuffer = mode->stereobuffer;
988 int samples = mode->samples;
989 int fullscreen = mode->fullscreen;
992 Sys_Error("VID_InitMode called when video is already initialised");
994 // if stencil is enabled, ask for alpha too
1001 pfd.cDepthBits = 24;
1002 pfd.cStencilBits = 8;
1010 pfd.cDepthBits = 16;
1011 pfd.cStencilBits = 0;
1015 pfd.dwFlags |= PFD_STEREO;
1019 *a++ = WGL_DRAW_TO_WINDOW_ARB;
1021 *a++ = WGL_ACCELERATION_ARB;
1022 *a++ = WGL_FULL_ACCELERATION_ARB;
1023 *a++ = WGL_DOUBLE_BUFFER_ARB;
1028 *a++ = WGL_RED_BITS_ARB;
1030 *a++ = WGL_GREEN_BITS_ARB;
1032 *a++ = WGL_BLUE_BITS_ARB;
1034 *a++ = WGL_ALPHA_BITS_ARB;
1036 *a++ = WGL_DEPTH_BITS_ARB;
1038 *a++ = WGL_STENCIL_BITS_ARB;
1043 *a++ = WGL_RED_BITS_ARB;
1045 *a++ = WGL_GREEN_BITS_ARB;
1047 *a++ = WGL_BLUE_BITS_ARB;
1049 *a++ = WGL_DEPTH_BITS_ARB;
1055 *a++ = WGL_STEREO_ARB;
1061 *a++ = WGL_SAMPLE_BUFFERS_ARB;
1063 *a++ = WGL_SAMPLES_ARB;
1070 gldrivername = "opengl32.dll";
1071 // COMMANDLINEOPTION: Windows WGL: -gl_driver <drivername> 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
1072 i = COM_CheckParm("-gl_driver");
1073 if (i && i < com_argc - 1)
1074 gldrivername = com_argv[i + 1];
1075 if (!GL_OpenLibrary(gldrivername))
1077 Con_Printf("Unable to load GL driver %s\n", gldrivername);
1081 memset(&gdevmode, 0, sizeof(gdevmode));
1083 vid_isfullscreen = false;
1086 if(vid_forcerefreshrate.integer)
1089 gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1090 gdevmode.dmBitsPerPel = bpp;
1091 gdevmode.dmPelsWidth = width;
1092 gdevmode.dmPelsHeight = height;
1093 gdevmode.dmSize = sizeof (gdevmode);
1096 gdevmode.dmFields |= DM_DISPLAYFREQUENCY;
1097 gdevmode.dmDisplayFrequency = refreshrate;
1102 if(refreshrate == 0)
1103 refreshrate = initialdevmode.dmDisplayFrequency; // default vid_refreshrate to the rate of the desktop
1106 foundgoodmode = false;
1108 thismode.dmSize = sizeof(thismode);
1109 thismode.dmDriverExtra = 0;
1110 for(i = 0; EnumDisplaySettings(NULL, i, &thismode); ++i)
1112 if(~thismode.dmFields & (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY))
1114 Con_DPrintf("enumerating modes yielded a bogus item... please debug this\n");
1117 if(developer_extra.integer)
1118 Con_DPrintf("Found mode %dx%dx%dbpp %dHz... ", (int)thismode.dmPelsWidth, (int)thismode.dmPelsHeight, (int)thismode.dmBitsPerPel, (int)thismode.dmDisplayFrequency);
1119 if(thismode.dmBitsPerPel != (DWORD)bpp)
1121 if(developer_extra.integer)
1122 Con_DPrintf("wrong bpp\n");
1125 if(thismode.dmPelsWidth != (DWORD)width)
1127 if(developer_extra.integer)
1128 Con_DPrintf("wrong width\n");
1131 if(thismode.dmPelsHeight != (DWORD)height)
1133 if(developer_extra.integer)
1134 Con_DPrintf("wrong height\n");
1140 // if we have a good mode, make sure this mode is better than the previous one, and allowed by the refreshrate
1141 if(thismode.dmDisplayFrequency > (DWORD)refreshrate)
1143 if(developer_extra.integer)
1144 Con_DPrintf("too high refresh rate\n");
1147 else if(thismode.dmDisplayFrequency <= gdevmode.dmDisplayFrequency)
1149 if(developer_extra.integer)
1150 Con_DPrintf("doesn't beat previous best match (too low)\n");
1156 // we do have one, but it isn't good... make sure it has a lower frequency than the previous one
1157 if(thismode.dmDisplayFrequency >= gdevmode.dmDisplayFrequency)
1159 if(developer_extra.integer)
1160 Con_DPrintf("doesn't beat previous best match (too high)\n");
1164 // otherwise, take anything
1166 memcpy(&gdevmode, &thismode, sizeof(gdevmode));
1167 if(thismode.dmDisplayFrequency <= (DWORD)refreshrate)
1168 foundgoodmode = true;
1171 if(developer_extra.integer)
1172 Con_DPrintf("(out of range)\n");
1175 if(developer_extra.integer)
1176 Con_DPrintf("accepted\n");
1183 Con_Printf("Unable to find the requested mode %dx%dx%dbpp\n", width, height, bpp);
1186 else if(ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
1189 Con_Printf("Unable to change to requested mode %dx%dx%dbpp\n", width, height, bpp);
1193 vid_isfullscreen = true;
1194 WindowStyle = WS_POPUP;
1195 ExWindowStyle = WS_EX_TOPMOST;
1200 i = GetDeviceCaps(hdc, RASTERCAPS);
1201 depth = GetDeviceCaps(hdc, PLANES) * GetDeviceCaps(hdc, BITSPIXEL);
1202 ReleaseDC (NULL, hdc);
1206 Con_Print("Can't run in non-RGB mode\n");
1212 Con_Print("A higher desktop depth is required to run this video mode\n");
1216 WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
1223 rect.bottom = height;
1224 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
1233 CenterX = (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) / 2;
1234 CenterY = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) / 2;
1236 CenterX = max(0, CenterX);
1237 CenterY = max(0, CenterY);
1239 // x and y may be changed by WM_MOVE messages
1242 rect.left += CenterX;
1243 rect.right += CenterX;
1244 rect.top += CenterY;
1245 rect.bottom += CenterY;
1249 // start out at the final windowpass if samples is 1 as it's the only feature we need extended pixel formats for
1250 for (windowpass = samples == 1;windowpass < 2;windowpass++)
1253 gl_platformextensions = "";
1255 mainwindow = CreateWindowEx (ExWindowStyle, "DarkPlacesWindowClass", gamename, WindowStyle, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, global_hInstance, NULL);
1258 Con_Printf("CreateWindowEx(%d, %s, %s, %d, %d, %d, %d, %d, %p, %p, %p, %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), (void *)NULL, (void *)NULL, (void *)global_hInstance, (void *)NULL);
1263 baseDC = GetDC(mainwindow);
1265 if (!newpixelformat)
1266 newpixelformat = ChoosePixelFormat(baseDC, &pfd);
1267 pixelformat = newpixelformat;
1271 Con_Printf("ChoosePixelFormat(%p, %p) failed\n", (void *)baseDC, (void *)&pfd);
1275 if (SetPixelFormat(baseDC, pixelformat, &pfd) == false)
1278 Con_Printf("SetPixelFormat(%p, %d, %p) failed\n", (void *)baseDC, pixelformat, (void *)&pfd);
1282 if (!GL_CheckExtension("wgl", wglfuncs, NULL, false))
1285 Con_Print("wgl functions not found\n");
1289 baseRC = qwglCreateContext(baseDC);
1293 Con_Print("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.\n");
1296 if (!qwglMakeCurrent(baseDC, baseRC))
1299 Con_Printf("wglMakeCurrent(%p, %p) failed\n", (void *)baseDC, (void *)baseRC);
1303 if ((qglGetString = (const GLubyte* (GLAPIENTRY *)(GLenum name))GL_GetProcAddress("glGetString")) == NULL)
1306 Con_Print("glGetString not found\n");
1309 if ((qwglGetExtensionsStringARB = (const char *(WINAPI *)(HDC hdc))GL_GetProcAddress("wglGetExtensionsStringARB")) == NULL)
1310 Con_Print("wglGetExtensionsStringARB not found\n");
1312 gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
1313 gl_platform = "WGL";
1314 gl_platformextensions = "";
1316 if (qwglGetExtensionsStringARB)
1317 gl_platformextensions = (const char *)qwglGetExtensionsStringARB(baseDC);
1321 if (!gl_platformextensions)
1322 gl_platformextensions = "";
1324 // now some nice Windows pain:
1325 // we have created a window, we needed one to find out if there are
1326 // any multisample pixel formats available, the problem is that to
1327 // actually use one of those multisample formats we now have to
1328 // recreate the window (yes Microsoft OpenGL really is that bad)
1330 if (windowpass == 0)
1332 if (!GL_CheckExtension("WGL_ARB_pixel_format", wglpixelformatfuncs, "-noarbpixelformat", false) || !qwglChoosePixelFormatARB(baseDC, attribs, attribsf, 1, &newpixelformat, &numpixelformats) || !newpixelformat)
1334 // ok we got one - do it all over again with newpixelformat
1335 qwglMakeCurrent(NULL, NULL);
1336 qwglDeleteContext(baseRC);baseRC = 0;
1337 ReleaseDC(mainwindow, baseDC);baseDC = 0;
1338 // eat up any messages waiting for us
1339 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
1341 TranslateMessage (&msg);
1342 DispatchMessage (&msg);
1349 SetWindowPos (mainwindow, NULL, CenterX, CenterY, 0, 0,SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
1352 ShowWindow (mainwindow, SW_SHOWDEFAULT);
1353 UpdateWindow (mainwindow);
1355 // now we try to make sure we get the focus on the mode switch, because
1356 // sometimes in some systems we don't. We grab the foreground, then
1357 // finish setting up, pump all our messages, and sleep for a little while
1358 // to let messages finish bouncing around the system, then we put
1359 // ourselves at the top of the z order, then grab the foreground again,
1360 // Who knows if it helps, but it probably doesn't hurt
1361 SetForegroundWindow (mainwindow);
1363 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
1365 TranslateMessage (&msg);
1366 DispatchMessage (&msg);
1371 SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS);
1373 SetForegroundWindow (mainwindow);
1375 // fix the leftover Alt from any Alt-Tab or the like that switched us away
1378 // COMMANDLINEOPTION: Windows WGL: -novideosync disables WGL_EXT_swap_control
1379 GL_CheckExtension("WGL_EXT_swap_control", wglswapintervalfuncs, "-novideosync", false);
1383 //vid_menudrawfn = VID_MenuDraw;
1384 //vid_menukeyfn = VID_MenuKey;
1385 vid_usingmouse = false;
1386 vid_usinghidecursor = false;
1387 vid_usingvsync = false;
1388 vid_reallyhidden = vid_hidden = false;
1389 vid_initialized = true;
1392 IN_StartupJoystick ();
1394 if (qwglSwapIntervalEXT)
1396 vid_usevsync = vid_vsync.integer != 0;
1397 vid_usingvsync = vid_vsync.integer != 0;
1398 qwglSwapIntervalEXT (vid_usevsync);
1405 static D3DADAPTER_IDENTIFIER9 d3d9adapteridentifier;
1407 extern cvar_t gl_info_extensions;
1408 extern cvar_t gl_info_vendor;
1409 extern cvar_t gl_info_renderer;
1410 extern cvar_t gl_info_version;
1411 extern cvar_t gl_info_platform;
1412 extern cvar_t gl_info_driver;
1413 qboolean VID_InitModeDX(viddef_mode_t *mode, int version)
1418 DWORD WindowStyle, ExWindowStyle;
1419 int CenterX, CenterY;
1420 int bpp = mode->bitsperpixel;
1421 int width = mode->width;
1422 int height = mode->height;
1423 int refreshrate = (int)floor(mode->refreshrate+0.5);
1424 // int stereobuffer = mode->stereobuffer;
1425 int samples = mode->samples;
1426 int fullscreen = mode->fullscreen;
1429 if (vid_initialized)
1430 Sys_Error("VID_InitMode called when video is already initialised");
1432 vid_isfullscreen = fullscreen != 0;
1435 WindowStyle = WS_POPUP;
1436 ExWindowStyle = WS_EX_TOPMOST;
1440 WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
1447 rect.bottom = height;
1448 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
1457 CenterX = (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) / 2;
1458 CenterY = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) / 2;
1460 CenterX = max(0, CenterX);
1461 CenterY = max(0, CenterY);
1463 // x and y may be changed by WM_MOVE messages
1466 rect.left += CenterX;
1467 rect.right += CenterX;
1468 rect.top += CenterY;
1469 rect.bottom += CenterY;
1472 gl_platformextensions = "";
1474 mainwindow = CreateWindowEx (ExWindowStyle, "DarkPlacesWindowClass", gamename, WindowStyle, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, global_hInstance, NULL);
1477 Con_Printf("CreateWindowEx(%d, %s, %s, %d, %d, %d, %d, %d, %p, %p, %p, %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), (void *)NULL, (void *)NULL, global_hInstance, (void *)NULL);
1482 baseDC = GetDC(mainwindow);
1484 vid_d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
1486 Sys_Error("VID_InitMode: Direct3DCreate9 failed");
1488 numdevices = IDirect3D9_GetAdapterCount(vid_d3d9);
1490 memset(&d3d9adapteridentifier, 0, sizeof(d3d9adapteridentifier));
1491 for (deviceindex = 0;deviceindex < numdevices && !vid_d3d9dev;deviceindex++)
1493 memset(&vid_d3dpresentparameters, 0, sizeof(vid_d3dpresentparameters));
1494 // vid_d3dpresentparameters.Flags = D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL;
1495 vid_d3dpresentparameters.Flags = 0;
1496 vid_d3dpresentparameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
1497 vid_d3dpresentparameters.hDeviceWindow = mainwindow;
1498 vid_d3dpresentparameters.BackBufferWidth = width;
1499 vid_d3dpresentparameters.BackBufferHeight = height;
1500 vid_d3dpresentparameters.MultiSampleType = samples > 1 ? (D3DMULTISAMPLE_TYPE)samples : D3DMULTISAMPLE_NONE;
1501 vid_d3dpresentparameters.BackBufferCount = fullscreen ? (vid_dx9_triplebuffer.integer ? 3 : 2) : 1;
1502 vid_d3dpresentparameters.FullScreen_RefreshRateInHz = fullscreen ? refreshrate : 0;
1503 vid_d3dpresentparameters.Windowed = !fullscreen;
1504 vid_d3dpresentparameters.EnableAutoDepthStencil = true;
1505 vid_d3dpresentparameters.AutoDepthStencilFormat = bpp > 16 ? D3DFMT_D24S8 : D3DFMT_D16;
1506 vid_d3dpresentparameters.BackBufferFormat = fullscreen?D3DFMT_X8R8G8B8:D3DFMT_UNKNOWN;
1507 vid_d3dpresentparameters.PresentationInterval = vid_vsync.integer ? D3DPRESENT_INTERVAL_ONE : D3DPRESENT_INTERVAL_IMMEDIATE;
1509 memset(&d3d9adapteridentifier, 0, sizeof(d3d9adapteridentifier));
1510 IDirect3D9_GetAdapterIdentifier(vid_d3d9, deviceindex, 0, &d3d9adapteridentifier);
1512 IDirect3D9_CreateDevice(vid_d3d9, deviceindex, vid_dx9_hal.integer ? D3DDEVTYPE_HAL : D3DDEVTYPE_REF, mainwindow, vid_dx9_softvertex.integer ? D3DCREATE_SOFTWARE_VERTEXPROCESSING : D3DCREATE_HARDWARE_VERTEXPROCESSING, &vid_d3dpresentparameters, &vid_d3d9dev);
1521 IDirect3DDevice9_GetDeviceCaps(vid_d3d9dev, &vid_d3d9caps);
1523 Con_Printf("Using D3D9 device: %s\n", d3d9adapteridentifier.Description);
1525 gl_platform = "D3D9";
1526 gl_platformextensions = "";
1528 ShowWindow (mainwindow, SW_SHOWDEFAULT);
1529 UpdateWindow (mainwindow);
1531 // now we try to make sure we get the focus on the mode switch, because
1532 // sometimes in some systems we don't. We grab the foreground, then
1533 // finish setting up, pump all our messages, and sleep for a little while
1534 // to let messages finish bouncing around the system, then we put
1535 // ourselves at the top of the z order, then grab the foreground again,
1536 // Who knows if it helps, but it probably doesn't hurt
1537 SetForegroundWindow (mainwindow);
1539 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
1541 TranslateMessage (&msg);
1542 DispatchMessage (&msg);
1547 SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS);
1549 SetForegroundWindow (mainwindow);
1551 // fix the leftover Alt from any Alt-Tab or the like that switched us away
1554 gl_renderer = d3d9adapteridentifier.Description;
1555 gl_vendor = d3d9adapteridentifier.Driver;
1559 Con_Printf("D3D9 adapter info:\n");
1560 Con_Printf("Description: %s\n", d3d9adapteridentifier.Description);
1561 Con_Printf("DeviceId: %x\n", (unsigned int)d3d9adapteridentifier.DeviceId);
1562 Con_Printf("DeviceName: %p\n", d3d9adapteridentifier.DeviceName);
1563 Con_Printf("Driver: %s\n", d3d9adapteridentifier.Driver);
1564 Con_Printf("DriverVersion: %08x%08x\n", (unsigned int)d3d9adapteridentifier.DriverVersion.u.HighPart, (unsigned int)d3d9adapteridentifier.DriverVersion.u.LowPart);
1565 Con_DPrintf("GL_EXTENSIONS: %s\n", gl_extensions);
1566 Con_DPrintf("%s_EXTENSIONS: %s\n", gl_platform, gl_platformextensions);
1568 // clear the extension flags
1569 memset(&vid.support, 0, sizeof(vid.support));
1570 Cvar_SetQuick(&gl_info_extensions, "");
1572 // D3D9 requires BGRA
1573 vid.forcetextype = TEXTYPE_BGRA;
1575 vid.forcevbo = false;
1576 vid.support.arb_depth_texture = true;
1577 vid.support.arb_draw_buffers = vid_d3d9caps.NumSimultaneousRTs > 1;
1578 vid.support.arb_occlusion_query = true; // can't find a cap for this
1579 vid.support.arb_shadow = true;
1580 vid.support.arb_texture_compression = true;
1581 vid.support.arb_texture_cube_map = true;
1582 vid.support.arb_texture_non_power_of_two = (vid_d3d9caps.TextureCaps & D3DPTEXTURECAPS_POW2) == 0;
1583 vid.support.arb_vertex_buffer_object = true;
1584 vid.support.ext_blend_subtract = true;
1585 vid.support.ext_draw_range_elements = true;
1586 vid.support.ext_framebuffer_object = true;
1587 vid.support.ext_texture_3d = true;
1588 vid.support.ext_texture_compression_s3tc = true;
1589 vid.support.ext_texture_filter_anisotropic = true;
1590 vid.support.ati_separate_stencil = (vid_d3d9caps.StencilCaps & D3DSTENCILCAPS_TWOSIDED) != 0;
1591 vid.support.ext_texture_srgb = false; // FIXME use D3DSAMP_SRGBTEXTURE if CheckDeviceFormat agrees
1593 vid.maxtexturesize_2d = min(vid_d3d9caps.MaxTextureWidth, vid_d3d9caps.MaxTextureHeight);
1594 vid.maxtexturesize_3d = vid_d3d9caps.MaxVolumeExtent;
1595 vid.maxtexturesize_cubemap = vid.maxtexturesize_2d;
1597 vid.teximageunits = vid_d3d9caps.MaxSimultaneousTextures;
1598 vid.texarrayunits = 8; // can't find a caps field for this?
1599 vid.max_anisotropy = vid_d3d9caps.MaxAnisotropy;
1600 vid.maxdrawbuffers = vid_d3d9caps.NumSimultaneousRTs;
1602 vid.texunits = bound(4, vid.texunits, MAX_TEXTUREUNITS);
1603 vid.teximageunits = bound(16, vid.teximageunits, MAX_TEXTUREUNITS);
1604 vid.texarrayunits = bound(8, vid.texarrayunits, MAX_TEXTUREUNITS);
1605 Con_DPrintf("Using D3D9.0 rendering path - %i texture matrix, %i texture images, %i texcoords, shadowmapping supported%s\n", vid.texunits, vid.teximageunits, vid.texarrayunits, vid.maxdrawbuffers > 1 ? ", MRT detected (allows prepass deferred lighting)" : "");
1606 vid.renderpath = RENDERPATH_D3D9;
1607 vid.useinterleavedarrays = true;
1609 Cvar_SetQuick(&gl_info_vendor, gl_vendor);
1610 Cvar_SetQuick(&gl_info_renderer, gl_renderer);
1611 Cvar_SetQuick(&gl_info_version, gl_version);
1612 Cvar_SetQuick(&gl_info_platform, gl_platform ? gl_platform : "");
1613 Cvar_SetQuick(&gl_info_driver, gl_driver);
1615 // LordHavoc: report supported extensions
1616 Con_DPrintf("\nQuakeC extensions for server and client: %s\nQuakeC extensions for menu: %s\n", vm_sv_extensions, vm_m_extensions );
1618 // clear to black (loading plaque will be seen over this)
1619 IDirect3DDevice9_Clear(vid_d3d9dev, 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
1620 IDirect3DDevice9_BeginScene(vid_d3d9dev);
1621 IDirect3DDevice9_EndScene(vid_d3d9dev);
1622 IDirect3DDevice9_Present(vid_d3d9dev, NULL, NULL, NULL, NULL);
1623 // because the only time we end/begin scene is in VID_Finish, we'd better start a scene now...
1624 IDirect3DDevice9_BeginScene(vid_d3d9dev);
1625 vid_begunscene = true;
1627 //vid_menudrawfn = VID_MenuDraw;
1628 //vid_menukeyfn = VID_MenuKey;
1629 vid_usingmouse = false;
1630 vid_usinghidecursor = false;
1631 vid_usingvsync = false;
1632 vid_hidden = vid_reallyhidden = false;
1633 vid_initialized = true;
1636 IN_StartupJoystick ();
1642 qboolean VID_InitModeSOFT(viddef_mode_t *mode)
1648 int pixelformat, newpixelformat;
1649 DWORD WindowStyle, ExWindowStyle;
1650 int CenterX, CenterY;
1653 qboolean foundmode, foundgoodmode;
1654 int bpp = mode->bitsperpixel;
1655 int width = mode->width;
1656 int height = mode->height;
1657 int refreshrate = (int)floor(mode->refreshrate+0.5);
1658 int fullscreen = mode->fullscreen;
1660 if (vid_initialized)
1661 Sys_Error("VID_InitMode called when video is already initialised");
1663 memset(&gdevmode, 0, sizeof(gdevmode));
1665 vid_isfullscreen = false;
1668 if(vid_forcerefreshrate.integer)
1671 gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1672 gdevmode.dmBitsPerPel = bpp;
1673 gdevmode.dmPelsWidth = width;
1674 gdevmode.dmPelsHeight = height;
1675 gdevmode.dmSize = sizeof (gdevmode);
1678 gdevmode.dmFields |= DM_DISPLAYFREQUENCY;
1679 gdevmode.dmDisplayFrequency = refreshrate;
1684 if(refreshrate == 0)
1685 refreshrate = initialdevmode.dmDisplayFrequency; // default vid_refreshrate to the rate of the desktop
1688 foundgoodmode = false;
1690 thismode.dmSize = sizeof(thismode);
1691 thismode.dmDriverExtra = 0;
1692 for(i = 0; EnumDisplaySettings(NULL, i, &thismode); ++i)
1694 if(~thismode.dmFields & (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY))
1696 Con_DPrintf("enumerating modes yielded a bogus item... please debug this\n");
1699 if(developer_extra.integer)
1700 Con_DPrintf("Found mode %dx%dx%dbpp %dHz... ", (int)thismode.dmPelsWidth, (int)thismode.dmPelsHeight, (int)thismode.dmBitsPerPel, (int)thismode.dmDisplayFrequency);
1701 if(thismode.dmBitsPerPel != (DWORD)bpp)
1703 if(developer_extra.integer)
1704 Con_DPrintf("wrong bpp\n");
1707 if(thismode.dmPelsWidth != (DWORD)width)
1709 if(developer_extra.integer)
1710 Con_DPrintf("wrong width\n");
1713 if(thismode.dmPelsHeight != (DWORD)height)
1715 if(developer_extra.integer)
1716 Con_DPrintf("wrong height\n");
1722 // if we have a good mode, make sure this mode is better than the previous one, and allowed by the refreshrate
1723 if(thismode.dmDisplayFrequency > (DWORD)refreshrate)
1725 if(developer_extra.integer)
1726 Con_DPrintf("too high refresh rate\n");
1729 else if(thismode.dmDisplayFrequency <= gdevmode.dmDisplayFrequency)
1731 if(developer_extra.integer)
1732 Con_DPrintf("doesn't beat previous best match (too low)\n");
1738 // we do have one, but it isn't good... make sure it has a lower frequency than the previous one
1739 if(thismode.dmDisplayFrequency >= gdevmode.dmDisplayFrequency)
1741 if(developer_extra.integer)
1742 Con_DPrintf("doesn't beat previous best match (too high)\n");
1746 // otherwise, take anything
1748 memcpy(&gdevmode, &thismode, sizeof(gdevmode));
1749 if(thismode.dmDisplayFrequency <= (DWORD)refreshrate)
1750 foundgoodmode = true;
1753 if(developer_extra.integer)
1754 Con_DPrintf("(out of range)\n");
1757 if(developer_extra.integer)
1758 Con_DPrintf("accepted\n");
1765 Con_Printf("Unable to find the requested mode %dx%dx%dbpp\n", width, height, bpp);
1768 else if(ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
1771 Con_Printf("Unable to change to requested mode %dx%dx%dbpp\n", width, height, bpp);
1775 vid_isfullscreen = true;
1776 WindowStyle = WS_POPUP;
1777 ExWindowStyle = WS_EX_TOPMOST;
1782 i = GetDeviceCaps(hdc, RASTERCAPS);
1783 depth = GetDeviceCaps(hdc, PLANES) * GetDeviceCaps(hdc, BITSPIXEL);
1784 ReleaseDC (NULL, hdc);
1788 Con_Print("Can't run in non-RGB mode\n");
1794 Con_Print("A higher desktop depth is required to run this video mode\n");
1798 WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
1805 rect.bottom = height;
1806 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
1815 CenterX = (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) / 2;
1816 CenterY = (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) / 2;
1818 CenterX = max(0, CenterX);
1819 CenterY = max(0, CenterY);
1821 // x and y may be changed by WM_MOVE messages
1824 rect.left += CenterX;
1825 rect.right += CenterX;
1826 rect.top += CenterY;
1827 rect.bottom += CenterY;
1832 gl_platformextensions = "";
1834 mainwindow = CreateWindowEx (ExWindowStyle, "DarkPlacesWindowClass", gamename, WindowStyle, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, NULL, NULL, global_hInstance, NULL);
1837 Con_Printf("CreateWindowEx(%d, %s, %s, %d, %d, %d, %d, %d, %p, %p, %p, %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), (void *)NULL, (void *)NULL, (void *)global_hInstance, (void *)NULL);
1842 baseDC = GetDC(mainwindow);
1843 vid.softpixels = NULL;
1844 memset(&vid_softbmi, 0, sizeof(vid_softbmi));
1845 vid_softbmi.bmiHeader.biSize = sizeof(vid_softbmi.bmiHeader);
1846 vid_softbmi.bmiHeader.biWidth = width;
1847 vid_softbmi.bmiHeader.biHeight = -height; // negative to make a top-down bitmap
1848 vid_softbmi.bmiHeader.biPlanes = 1;
1849 vid_softbmi.bmiHeader.biBitCount = 32;
1850 vid_softbmi.bmiHeader.biCompression = BI_RGB;
1851 vid_softbmi.bmiHeader.biSizeImage = width*height*4;
1852 vid_softbmi.bmiHeader.biClrUsed = 256;
1853 vid_softbmi.bmiHeader.biClrImportant = 256;
1854 vid_softdibhandle = CreateDIBSection(baseDC, &vid_softbmi, DIB_RGB_COLORS, (void **)&vid.softpixels, NULL, 0);
1855 if (!vid_softdibhandle)
1857 Con_Printf("CreateDIBSection failed\n");
1862 vid_softhdc = CreateCompatibleDC(baseDC);
1863 vid_softhdc_backup = SelectObject(vid_softhdc, vid_softdibhandle);
1864 if (!vid_softhdc_backup)
1866 Con_Printf("SelectObject failed\n");
1870 // ReleaseDC(mainwindow, baseDC);
1873 vid.softdepthpixels = (unsigned int *)calloc(1, mode->width * mode->height * 4);
1874 if (DPSOFTRAST_Init(mode->width, mode->height, vid_soft_threads.integer, vid_soft_interlace.integer, (unsigned int *)vid.softpixels, (unsigned int *)vid.softdepthpixels) < 0)
1876 Con_Printf("Failed to initialize software rasterizer\n");
1881 VID_Soft_SharedSetup();
1883 ShowWindow (mainwindow, SW_SHOWDEFAULT);
1884 UpdateWindow (mainwindow);
1886 // now we try to make sure we get the focus on the mode switch, because
1887 // sometimes in some systems we don't. We grab the foreground, then
1888 // finish setting up, pump all our messages, and sleep for a little while
1889 // to let messages finish bouncing around the system, then we put
1890 // ourselves at the top of the z order, then grab the foreground again,
1891 // Who knows if it helps, but it probably doesn't hurt
1892 SetForegroundWindow (mainwindow);
1894 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
1896 TranslateMessage (&msg);
1897 DispatchMessage (&msg);
1902 SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS);
1904 SetForegroundWindow (mainwindow);
1906 // fix the leftover Alt from any Alt-Tab or the like that switched us away
1909 //vid_menudrawfn = VID_MenuDraw;
1910 //vid_menukeyfn = VID_MenuKey;
1911 vid_usingmouse = false;
1912 vid_usinghidecursor = false;
1913 vid_usingvsync = false;
1914 vid_reallyhidden = vid_hidden = false;
1915 vid_initialized = true;
1918 IN_StartupJoystick ();
1923 qboolean VID_InitMode(viddef_mode_t *mode)
1926 if (vid_soft.integer)
1927 return VID_InitModeSOFT(mode);
1930 // if (vid_dx11.integer)
1931 // return VID_InitModeDX(mode, 11);
1932 // if (vid_dx10.integer)
1933 // return VID_InitModeDX(mode, 10);
1934 if (vid_dx9.integer)
1935 return VID_InitModeDX(mode, 9);
1937 return VID_InitModeGL(mode);
1941 static void IN_Shutdown(void);
1942 void VID_Shutdown (void)
1945 if(vid_initialized == false)
1948 VID_SetMouse(false, false, false);
1949 VID_RestoreSystemGamma();
1951 vid_initialized = false;
1952 isgl = gldll != NULL;
1957 gl_platformextensions = "";
1960 SelectObject(vid_softhdc, vid_softhdc_backup);
1961 ReleaseDC(mainwindow, vid_softhdc);
1964 vid_softhdc_backup = NULL;
1965 if (vid_softdibhandle)
1966 DeleteObject(vid_softdibhandle);
1967 vid_softdibhandle = NULL;
1968 vid.softpixels = NULL;
1969 if (vid.softdepthpixels)
1970 free(vid.softdepthpixels);
1971 vid.softdepthpixels = NULL;
1976 IDirect3DDevice9_EndScene(vid_d3d9dev);
1977 vid_begunscene = false;
1978 // Cmd_ExecuteString("r_texturestats", src_command);
1979 // Cmd_ExecuteString("memlist", src_command);
1980 IDirect3DDevice9_Release(vid_d3d9dev);
1984 IDirect3D9_Release(vid_d3d9);
1987 if (qwglMakeCurrent)
1988 qwglMakeCurrent(NULL, NULL);
1989 qwglMakeCurrent = NULL;
1990 if (baseRC && qwglDeleteContext)
1991 qwglDeleteContext(baseRC);
1992 qwglDeleteContext = NULL;
1993 // close the library before we get rid of the window
1995 if (baseDC && mainwindow)
1996 ReleaseDC(mainwindow, baseDC);
1998 AppActivate(false, false);
2000 DestroyWindow(mainwindow);
2002 if (vid_isfullscreen && isgl)
2003 ChangeDisplaySettings (NULL, 0);
2004 vid_isfullscreen = false;
2007 void VID_SetMouse(qboolean fullscreengrab, qboolean relative, qboolean hidecursor)
2009 static qboolean restore_spi;
2010 static int originalmouseparms[3];
2012 if (!mouseinitialized)
2017 if (!vid_usingmouse)
2019 vid_usingmouse = true;
2020 cl_ignoremousemoves = 2;
2021 #ifdef SUPPORTDIRECTX
2022 if (dinput && g_pMouse)
2024 IDirectInputDevice_Acquire(g_pMouse);
2025 dinput_acquired = true;
2031 window_rect.left = window_x;
2032 window_rect.top = window_y;
2033 window_rect.right = window_x + vid.width;
2034 window_rect.bottom = window_y + vid.height;
2036 // change mouse settings to turn off acceleration
2037 // COMMANDLINEOPTION: Windows GDI Input: -noforcemparms disables setting of mouse parameters (not used with -dinput, windows only)
2038 if (!COM_CheckParm ("-noforcemparms") && SystemParametersInfo (SPI_GETMOUSE, 0, originalmouseparms, 0))
2040 int newmouseparms[3];
2041 newmouseparms[0] = 0; // threshold to double movement (only if accel level is >= 1)
2042 newmouseparms[1] = 0; // threshold to quadruple movement (only if accel level is >= 2)
2043 newmouseparms[2] = 0; // maximum level of acceleration (0 = off)
2044 restore_spi = SystemParametersInfo (SPI_SETMOUSE, 0, newmouseparms, 0) != FALSE;
2047 restore_spi = false;
2048 SetCursorPos ((window_x + vid.width / 2), (window_y + vid.height / 2));
2050 SetCapture (mainwindow);
2051 ClipCursor (&window_rect);
2059 vid_usingmouse = false;
2060 cl_ignoremousemoves = 2;
2061 #ifdef SUPPORTDIRECTX
2062 if (dinput_acquired)
2064 IDirectInputDevice_Unacquire(g_pMouse);
2065 dinput_acquired = false;
2070 // restore system mouseparms if we changed them
2072 SystemParametersInfo (SPI_SETMOUSE, 0, originalmouseparms, 0);
2073 restore_spi = false;
2080 if (vid_usinghidecursor != hidecursor)
2082 vid_usinghidecursor = hidecursor;
2083 ShowCursor (!hidecursor);
2088 #ifdef SUPPORTDIRECTX
2094 static qboolean IN_InitDInput (void)
2097 DIPROPDWORD dipdw = {
2099 sizeof(DIPROPDWORD), // diph.dwSize
2100 sizeof(DIPROPHEADER), // diph.dwHeaderSize
2102 DIPH_DEVICE, // diph.dwHow
2104 DINPUT_BUFFERSIZE, // dwData
2109 hInstDI = LoadLibrary("dinput.dll");
2111 if (hInstDI == NULL)
2113 Con_Print("Couldn't load dinput.dll\n");
2118 if (!pDirectInputCreate)
2120 pDirectInputCreate = (HRESULT (__stdcall *)(HINSTANCE,DWORD,LPDIRECTINPUT *,LPUNKNOWN))GetProcAddress(hInstDI,"DirectInputCreateA");
2122 if (!pDirectInputCreate)
2124 Con_Print("Couldn't get DI proc addr\n");
2129 // register with DirectInput and get an IDirectInput to play with.
2130 hr = iDirectInputCreate(global_hInstance, DIRECTINPUT_VERSION, &g_pdi, NULL);
2137 // obtain an interface to the system mouse device.
2139 hr = IDirectInput_CreateDevice(g_pdi, GUID_SysMouse, &g_pMouse, NULL);
2141 hr = IDirectInput_CreateDevice(g_pdi, &GUID_SysMouse, &g_pMouse, NULL);
2146 Con_Print("Couldn't open DI mouse device\n");
2150 // set the data format to "mouse format".
2151 hr = IDirectInputDevice_SetDataFormat(g_pMouse, &c_dfDIMouse);
2155 Con_Print("Couldn't set DI mouse format\n");
2159 // set the cooperativity level.
2160 hr = IDirectInputDevice_SetCooperativeLevel(g_pMouse, mainwindow,
2161 DISCL_EXCLUSIVE | DISCL_FOREGROUND);
2165 Con_Print("Couldn't set DI coop level\n");
2170 // set the buffer size to DINPUT_BUFFERSIZE elements.
2171 // the buffer size is a DWORD property associated with the device
2172 hr = IDirectInputDevice_SetProperty(g_pMouse, DIPROP_BUFFERSIZE, &dipdw.diph);
2176 Con_Print("Couldn't set DI buffersize\n");
2190 static void IN_StartupMouse (void)
2192 if (COM_CheckParm ("-nomouse"))
2195 mouseinitialized = true;
2197 #ifdef SUPPORTDIRECTX
2198 // COMMANDLINEOPTION: Windows Input: -dinput enables DirectInput for mouse/joystick input
2199 if (COM_CheckParm ("-dinput"))
2200 dinput = IN_InitDInput ();
2203 Con_Print("DirectInput initialized\n");
2205 Con_Print("DirectInput not initialized\n");
2217 static void IN_MouseMove (void)
2221 GetCursorPos (¤t_pos);
2222 in_windowmouse_x = current_pos.x - window_x;
2223 in_windowmouse_y = current_pos.y - window_y;
2225 if (!vid_usingmouse)
2228 #ifdef SUPPORTDIRECTX
2229 if (dinput_acquired)
2232 DIDEVICEOBJECTDATA od;
2240 hr = IDirectInputDevice_GetDeviceData(g_pMouse,
2241 sizeof(DIDEVICEOBJECTDATA), &od, &dwElements, 0);
2243 if ((hr == DIERR_INPUTLOST) || (hr == DIERR_NOTACQUIRED))
2245 IDirectInputDevice_Acquire(g_pMouse);
2249 /* Unable to read data or no data available */
2250 if (FAILED(hr) || dwElements == 0)
2253 /* Look at the element to see what happened */
2255 if ((int)od.dwOfs == DIMOFS_X)
2256 in_mouse_x += (LONG) od.dwData;
2257 if ((int)od.dwOfs == DIMOFS_Y)
2258 in_mouse_y += (LONG) od.dwData;
2259 if ((int)od.dwOfs == DIMOFS_Z)
2261 if((LONG)od.dwData < 0)
2263 Key_Event(K_MWHEELDOWN, 0, true);
2264 Key_Event(K_MWHEELDOWN, 0, false);
2266 else if((LONG)od.dwData > 0)
2268 Key_Event(K_MWHEELUP, 0, true);
2269 Key_Event(K_MWHEELUP, 0, false);
2272 if ((int)od.dwOfs == DIMOFS_BUTTON0)
2273 mstate_di = (mstate_di & ~1) | ((od.dwData & 0x80) >> 7);
2274 if ((int)od.dwOfs == DIMOFS_BUTTON1)
2275 mstate_di = (mstate_di & ~2) | ((od.dwData & 0x80) >> 6);
2276 if ((int)od.dwOfs == DIMOFS_BUTTON2)
2277 mstate_di = (mstate_di & ~4) | ((od.dwData & 0x80) >> 5);
2278 if ((int)od.dwOfs == DIMOFS_BUTTON3)
2279 mstate_di = (mstate_di & ~8) | ((od.dwData & 0x80) >> 4);
2282 // perform button actions
2283 for (i=0 ; i<mouse_buttons && i < 16 ; i++)
2284 if ((mstate_di ^ mouse_oldbuttonstate) & (1<<i))
2285 Key_Event (buttonremap[i], 0, (mstate_di & (1<<i)) != 0);
2286 mouse_oldbuttonstate = mstate_di;
2291 in_mouse_x += in_windowmouse_x - (int)(vid.width / 2);
2292 in_mouse_y += in_windowmouse_y - (int)(vid.height / 2);
2294 // if the mouse has moved, force it to the center, so there's room to move
2295 if (in_mouse_x || in_mouse_y)
2296 SetCursorPos ((window_x + vid.width / 2), (window_y + vid.height / 2));
2308 if (vid_activewindow && !vid_reallyhidden)
2321 static void IN_StartupJoystick (void)
2328 // assume no joystick
2331 // abort startup if user requests no joystick
2332 // COMMANDLINEOPTION: Windows Input: -nojoy disables joystick support, may be a small speed increase
2333 if (COM_CheckParm ("-nojoy"))
2336 // verify joystick driver is present
2337 if ((numdevs = joyGetNumDevs ()) == 0)
2339 Con_Print("\njoystick not found -- driver not present\n\n");
2343 // cycle through the joystick ids for the first valid one
2344 for (joy_id=0 ; joy_id<numdevs ; joy_id++)
2346 memset (&ji, 0, sizeof(ji));
2347 ji.dwSize = sizeof(ji);
2348 ji.dwFlags = JOY_RETURNCENTERED;
2350 if ((mmr = joyGetPosEx (joy_id, &ji)) == JOYERR_NOERROR)
2354 // abort startup if we didn't find a valid joystick
2355 if (mmr != JOYERR_NOERROR)
2357 Con_Printf("\njoystick not found -- no valid joysticks (%x)\n\n", mmr);
2361 // get the capabilities of the selected joystick
2362 // abort startup if command fails
2363 memset (&jc, 0, sizeof(jc));
2364 if ((mmr = joyGetDevCaps (joy_id, &jc, sizeof(jc))) != JOYERR_NOERROR)
2366 Con_Printf("\njoystick not found -- invalid joystick capabilities (%x)\n\n", mmr);
2370 // save the joystick's number of buttons and POV status
2371 joy_numbuttons = jc.wNumButtons;
2372 joy_haspov = (jc.wCaps & JOYCAPS_HASPOV) != 0;
2374 // old button and POV states default to no buttons pressed
2375 joy_oldbuttonstate = joy_oldpovstate = 0;
2377 // mark the joystick as available and advanced initialization not completed
2378 // this is needed as cvars are not available during initialization
2381 joy_advancedinit = false;
2383 Con_Print("\njoystick detected\n\n");
2392 static PDWORD RawValuePointer (int axis)
2409 return NULL; // LordHavoc: hush compiler warning
2415 Joy_AdvancedUpdate_f
2418 static void Joy_AdvancedUpdate_f (void)
2421 // called once by IN_ReadJoystick and by user whenever an update is needed
2422 // cvars are now available
2426 // initialize all the maps
2427 for (i = 0; i < JOY_MAX_AXES; i++)
2429 dwAxisMap[i] = AxisNada;
2430 dwControlMap[i] = JOY_ABSOLUTE_AXIS;
2431 pdwRawValue[i] = RawValuePointer(i);
2434 if( joy_advanced.integer == 0)
2436 // default joystick initialization
2437 // 2 axes only with joystick control
2438 dwAxisMap[JOY_AXIS_X] = AxisTurn;
2439 // dwControlMap[JOY_AXIS_X] = JOY_ABSOLUTE_AXIS;
2440 dwAxisMap[JOY_AXIS_Y] = AxisForward;
2441 // dwControlMap[JOY_AXIS_Y] = JOY_ABSOLUTE_AXIS;
2445 if (strcmp (joy_name.string, "joystick") != 0)
2447 // notify user of advanced controller
2448 Con_Printf("\n%s configured\n\n", joy_name.string);
2451 // advanced initialization here
2452 // data supplied by user via joy_axisn cvars
2453 dwTemp = (DWORD) joy_advaxisx.value;
2454 dwAxisMap[JOY_AXIS_X] = dwTemp & 0x0000000f;
2455 dwControlMap[JOY_AXIS_X] = dwTemp & JOY_RELATIVE_AXIS;
2456 dwTemp = (DWORD) joy_advaxisy.value;
2457 dwAxisMap[JOY_AXIS_Y] = dwTemp & 0x0000000f;
2458 dwControlMap[JOY_AXIS_Y] = dwTemp & JOY_RELATIVE_AXIS;
2459 dwTemp = (DWORD) joy_advaxisz.value;
2460 dwAxisMap[JOY_AXIS_Z] = dwTemp & 0x0000000f;
2461 dwControlMap[JOY_AXIS_Z] = dwTemp & JOY_RELATIVE_AXIS;
2462 dwTemp = (DWORD) joy_advaxisr.value;
2463 dwAxisMap[JOY_AXIS_R] = dwTemp & 0x0000000f;
2464 dwControlMap[JOY_AXIS_R] = dwTemp & JOY_RELATIVE_AXIS;
2465 dwTemp = (DWORD) joy_advaxisu.value;
2466 dwAxisMap[JOY_AXIS_U] = dwTemp & 0x0000000f;
2467 dwControlMap[JOY_AXIS_U] = dwTemp & JOY_RELATIVE_AXIS;
2468 dwTemp = (DWORD) joy_advaxisv.value;
2469 dwAxisMap[JOY_AXIS_V] = dwTemp & 0x0000000f;
2470 dwControlMap[JOY_AXIS_V] = dwTemp & JOY_RELATIVE_AXIS;
2473 // compute the axes to collect from DirectInput
2474 joy_flags = JOY_RETURNCENTERED | JOY_RETURNBUTTONS | JOY_RETURNPOV;
2475 for (i = 0; i < JOY_MAX_AXES; i++)
2477 if (dwAxisMap[i] != AxisNada)
2479 joy_flags |= dwAxisFlags[i];
2489 static qboolean IN_ReadJoystick (void)
2492 memset (&ji, 0, sizeof(ji));
2493 ji.dwSize = sizeof(ji);
2494 ji.dwFlags = joy_flags;
2496 if (joyGetPosEx (joy_id, &ji) == JOYERR_NOERROR)
2498 // this is a hack -- there is a bug in the Logitech WingMan Warrior DirectInput Driver
2499 // rather than having 32768 be the zero point, they have the zero point at 32668
2500 // go figure -- anyway, now we get the full resolution out of the device
2501 if (joy_wwhack1.integer != 0.0)
2509 // read error occurred
2510 // turning off the joystick seems too harsh for 1 read error,
2511 // but what should be done?
2518 IN_JoystickGetAxisNum
2522 int IN_JoystickGetAxisNum(int ControlListType)
2526 for (i = 0; i < JOY_MAX_AXES; i++)
2527 if (dwAxisMap[i] == (DWORD) ControlListType)
2537 static double IN_JoystickGetAxis(int axis, double sensitivity, double deadzone)
2539 float fAxisValue, fTemp;
2541 if (!joy_avail || axis < 0 || axis >= JOY_MAX_AXES)
2542 return 0; // no such axis on this joystick
2544 // get the floating point zero-centered, potentially-inverted data for the current axis
2545 fAxisValue = (float) *pdwRawValue[axis];
2547 // move centerpoint to zero
2548 fAxisValue -= 32768.0;
2550 if (joy_wwhack2.integer != 0.0)
2552 if (dwAxisMap[axis] == AxisTurn)
2554 // this is a special formula for the Logitech WingMan Warrior
2555 // y=ax^b; where a = 300 and b = 1.3
2556 // also x values are in increments of 800 (so this is factored out)
2557 // then bounds check result to level out excessively high spin rates
2558 fTemp = 300.0 * pow(abs(fAxisValue) / 800.0, 1.3);
2559 if (fTemp > 14000.0)
2561 // restore direction information
2562 fAxisValue = (fAxisValue > 0.0) ? fTemp : -fTemp;
2566 // convert range from -32768..32767 to -1..1
2567 fAxisValue /= 32768.0;
2569 // deadzone around center
2570 if (fabs(fAxisValue) < deadzone)
2573 // apply sensitivity
2574 return fAxisValue * sensitivity;
2579 IN_JoystickKeyeventForAxis
2583 static void IN_JoystickKeyeventForAxis(int axis, int key_pos, int key_neg)
2587 if (axis < 0 || axis >= JOY_MAX_AXES)
2588 return; // no such axis on this joystick
2590 joytime = Sys_DoubleTime();
2591 // no key event, continuous keydown event
2592 if (joy_axescache[axis].move == joy_axescache[axis].oldmove)
2594 if (joy_axescache[axis].move != 0 && joytime > joy_axescache[axis].keytime)
2596 //Con_Printf("joy %s %i %f\n", Key_KeynumToString((joy_axescache[axis].move > 0) ? key_pos : key_neg), 1, cl.time);
2597 Key_Event((joy_axescache[axis].move > 0) ? key_pos : key_neg, 0, 1);
2598 joy_axescache[axis].keytime = joytime + 0.5 / 20;
2602 // generate key up event
2603 if (joy_axescache[axis].oldmove)
2605 //Con_Printf("joy %s %i %f\n", Key_KeynumToString((joy_axescache[axis].oldmove > 0) ? key_pos : key_neg), 1, cl.time);
2606 Key_Event((joy_axescache[axis].oldmove > 0) ? key_pos : key_neg, 0, 0);
2608 // generate key down event
2609 if (joy_axescache[axis].move)
2611 //Con_Printf("joy %s %i %f\n", Key_KeynumToString((joy_axescache[axis].move > 0) ? key_pos : key_neg), 1, cl.time);
2612 Key_Event((joy_axescache[axis].move > 0) ? key_pos : key_neg, 0, 1);
2613 joy_axescache[axis].keytime = joytime + 0.5;
2619 IN_JoystickBlockDoubledKeyEvents
2623 static qboolean IN_ReadJoystick (void);
2624 static qboolean IN_JoystickBlockDoubledKeyEvents(int keycode)
2628 if (!joy_axiskeyevents.integer)
2631 // block keyevent if it's going to be provided by joystick keyevent system
2634 // collect the joystick data, if possible
2635 if (IN_ReadJoystick() != true)
2637 axis = IN_JoystickGetAxisNum(AxisForward);
2638 if (keycode == K_UPARROW || keycode == K_DOWNARROW)
2639 if (IN_JoystickGetAxis(axis, 1, joy_axiskeyevents_deadzone.value) || joy_axescache[axis].move || joy_axescache[axis].oldmove)
2641 axis = IN_JoystickGetAxisNum(AxisSide);
2642 if (keycode == K_RIGHTARROW || keycode == K_LEFTARROW)
2643 if (IN_JoystickGetAxis(axis, 1, joy_axiskeyevents_deadzone.value) || joy_axescache[axis].move || joy_axescache[axis].oldmove)
2655 static void IN_JoyMove (void)
2657 float speed, aspeed;
2659 int i, mouselook = (in_mlook.state & 1) || freelook.integer, AxisForwardIndex = -1, AxisSideIndex = -1;
2661 // complete initialization if first time in
2662 // this is needed as cvars are not available at initialization time
2663 if( joy_advancedinit != true )
2665 Joy_AdvancedUpdate_f();
2666 joy_advancedinit = true;
2672 DWORD buttonstate, povstate;
2674 // loop through the joystick buttons
2675 // key a joystick event or auxillary event for higher number buttons for each state change
2676 buttonstate = ji.dwButtons;
2677 for (i=0 ; i < (int) joy_numbuttons ; i++)
2679 if ( (buttonstate & (1<<i)) && !(joy_oldbuttonstate & (1<<i)) )
2681 key_index = (i < 16) ? K_JOY1 : K_AUX1;
2682 Key_Event (key_index + i, 0, true);
2684 if ( !(buttonstate & (1<<i)) && (joy_oldbuttonstate & (1<<i)) )
2686 key_index = (i < 16) ? K_JOY1 : K_AUX1;
2687 Key_Event (key_index + i, 0, false);
2690 joy_oldbuttonstate = buttonstate;
2694 // convert POV information into 4 bits of state information
2695 // this avoids any potential problems related to moving from one
2696 // direction to another without going through the center position
2698 if(ji.dwPOV != JOY_POVCENTERED)
2700 if (ji.dwPOV == JOY_POVFORWARD)
2702 if (ji.dwPOV == JOY_POVRIGHT)
2704 if (ji.dwPOV == JOY_POVBACKWARD)
2706 if (ji.dwPOV == JOY_POVLEFT)
2709 // determine which bits have changed and key an auxillary event for each change
2710 for (i=0 ; i < 4 ; i++)
2712 if ( (povstate & (1<<i)) && !(joy_oldpovstate & (1<<i)) )
2714 Key_Event (K_AUX29 + i, 0, true);
2717 if ( !(povstate & (1<<i)) && (joy_oldpovstate & (1<<i)) )
2719 Key_Event (K_AUX29 + i, 0, false);
2722 joy_oldpovstate = povstate;
2726 // verify joystick is available and that the user wants to use it
2727 if (!joy_avail || !in_joystick.integer)
2732 // collect the joystick data, if possible
2733 if (IN_ReadJoystick () != true)
2738 if (in_speed.state & 1)
2739 speed = cl_movespeedkey.value;
2742 // LordHavoc: viewzoom affects sensitivity for sniping
2743 aspeed = speed * cl.realframetime * cl.viewzoom;
2745 // loop through the axes
2746 for (i = 0; i < JOY_MAX_AXES; i++)
2748 // convert axis to real move
2749 switch (dwAxisMap[i])
2752 if (AxisForwardIndex < 0)
2753 AxisForwardIndex = i;
2754 if ((joy_advanced.integer == 0) && mouselook)
2756 // user wants forward control to become look control
2757 fAxisValue = IN_JoystickGetAxis(i, joy_pitchsensitivity.value, joy_pitchthreshold.value);
2758 if (fAxisValue != 0)
2760 // if mouse invert is on, invert the joystick pitch value
2761 // only absolute control support here (joy_advanced is false)
2762 if (m_pitch.value < 0.0)
2763 cl.viewangles[PITCH] -= fAxisValue * aspeed * cl_pitchspeed.value;
2765 cl.viewangles[PITCH] += fAxisValue * aspeed * cl_pitchspeed.value;
2770 // no pitch movement
2771 // disable pitch return-to-center unless requested by user
2772 // *** this code can be removed when the lookspring bug is fixed
2773 // *** the bug always has the lookspring feature on
2774 if (lookspring.value == 0.0)
2780 // user wants forward control to be forward control
2781 fAxisValue = IN_JoystickGetAxis(i, joy_forwardsensitivity.value, joy_forwardthreshold.value);
2782 cl.cmd.forwardmove += fAxisValue * speed * cl_forwardspeed.value;
2787 if (AxisSideIndex < 0)
2789 fAxisValue = IN_JoystickGetAxis(i, joy_sidesensitivity.value, joy_sidethreshold.value);
2790 cl.cmd.sidemove += fAxisValue * speed * cl_sidespeed.value;
2794 if ((in_strafe.state & 1) || (lookstrafe.integer && mouselook))
2796 // user wants turn control to become side control
2797 fAxisValue = IN_JoystickGetAxis(i, joy_sidesensitivity.value, joy_sidethreshold.value);
2798 cl.cmd.sidemove -= fAxisValue * speed * cl_sidespeed.value;
2802 // user wants turn control to be turn control
2803 fAxisValue = IN_JoystickGetAxis(i, joy_yawsensitivity.value, joy_yawthreshold.value);
2804 if (dwControlMap[i] == JOY_ABSOLUTE_AXIS)
2805 cl.viewangles[YAW] += fAxisValue * aspeed * cl_yawspeed.value;
2807 cl.viewangles[YAW] += fAxisValue * speed * 180.0;
2812 fAxisValue = IN_JoystickGetAxis(i, joy_pitchsensitivity.value, joy_pitchthreshold.value);
2815 if (fAxisValue != 0)
2817 // pitch movement detected and pitch movement desired by user
2818 if (dwControlMap[i] == JOY_ABSOLUTE_AXIS)
2819 cl.viewangles[PITCH] += fAxisValue * aspeed * cl_pitchspeed.value;
2821 cl.viewangles[PITCH] += fAxisValue * speed * 180.0;
2826 // no pitch movement
2827 // disable pitch return-to-center unless requested by user
2828 // *** this code can be removed when the lookspring bug is fixed
2829 // *** the bug always has the lookspring feature on
2830 if(lookspring.integer == 0)
2837 fAxisValue = IN_JoystickGetAxis(i, 1, 0.01);
2841 // cache for keyevents
2842 joy_axescache[i].oldmove = joy_axescache[i].move;
2843 joy_axescache[i].move = IN_JoystickGetAxis(i, 1, joy_axiskeyevents_deadzone.value);
2847 if (joy_axiskeyevents.integer)
2849 IN_JoystickKeyeventForAxis(AxisForwardIndex, K_DOWNARROW, K_UPARROW);
2850 IN_JoystickKeyeventForAxis(AxisSideIndex, K_RIGHTARROW, K_LEFTARROW);
2854 static void IN_Init(void)
2856 uiWheelMessage = RegisterWindowMessage ( "MSWHEEL_ROLLMSG" );
2858 // joystick variables
2859 Cvar_RegisterVariable (&in_joystick);
2860 Cvar_RegisterVariable (&joy_name);
2861 Cvar_RegisterVariable (&joy_advanced);
2862 Cvar_RegisterVariable (&joy_advaxisx);
2863 Cvar_RegisterVariable (&joy_advaxisy);
2864 Cvar_RegisterVariable (&joy_advaxisz);
2865 Cvar_RegisterVariable (&joy_advaxisr);
2866 Cvar_RegisterVariable (&joy_advaxisu);
2867 Cvar_RegisterVariable (&joy_advaxisv);
2868 Cvar_RegisterVariable (&joy_forwardthreshold);
2869 Cvar_RegisterVariable (&joy_sidethreshold);
2870 Cvar_RegisterVariable (&joy_pitchthreshold);
2871 Cvar_RegisterVariable (&joy_yawthreshold);
2872 Cvar_RegisterVariable (&joy_forwardsensitivity);
2873 Cvar_RegisterVariable (&joy_sidesensitivity);
2874 Cvar_RegisterVariable (&joy_pitchsensitivity);
2875 Cvar_RegisterVariable (&joy_yawsensitivity);
2876 Cvar_RegisterVariable (&joy_wwhack1);
2877 Cvar_RegisterVariable (&joy_wwhack2);
2878 Cvar_RegisterVariable (&joy_axiskeyevents);
2879 Cvar_RegisterVariable (&joy_axiskeyevents_deadzone);
2880 Cvar_RegisterVariable (&vid_forcerefreshrate);
2881 Cmd_AddCommand ("joyadvancedupdate", Joy_AdvancedUpdate_f, "applies current joyadv* cvar settings to the joystick driver");
2884 static void IN_Shutdown(void)
2886 #ifdef SUPPORTDIRECTX
2888 IDirectInputDevice_Release(g_pMouse);
2892 IDirectInput_Release(g_pdi);
2897 size_t VID_ListModes(vid_mode_t *modes, size_t maxcount)
2903 thismode.dmSize = sizeof(thismode);
2904 thismode.dmDriverExtra = 0;
2906 for(i = 0; EnumDisplaySettings(NULL, i, &thismode); ++i)
2908 if(~thismode.dmFields & (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY))
2910 Con_DPrintf("enumerating modes yielded a bogus item... please debug this\n");
2915 modes[k].width = thismode.dmPelsWidth;
2916 modes[k].height = thismode.dmPelsHeight;
2917 modes[k].bpp = thismode.dmBitsPerPel;
2918 modes[k].refreshrate = thismode.dmDisplayFrequency;
2919 modes[k].pixelheight_num = 1;
2920 modes[k].pixelheight_denom = 1; // Win32 apparently does not provide this (FIXME)