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 // gl_vidnt.c -- NT GL vid component
27 int cl_available = true;
29 int (WINAPI *qwglChoosePixelFormat)(HDC, CONST PIXELFORMATDESCRIPTOR *);
30 int (WINAPI *qwglDescribePixelFormat)(HDC, int, UINT, LPPIXELFORMATDESCRIPTOR);
31 //int (WINAPI *qwglGetPixelFormat)(HDC);
32 BOOL (WINAPI *qwglSetPixelFormat)(HDC, int, CONST PIXELFORMATDESCRIPTOR *);
33 BOOL (WINAPI *qwglSwapBuffers)(HDC);
34 HGLRC (WINAPI *qwglCreateContext)(HDC);
35 BOOL (WINAPI *qwglDeleteContext)(HGLRC);
36 HGLRC (WINAPI *qwglGetCurrentContext)(VOID);
37 HDC (WINAPI *qwglGetCurrentDC)(VOID);
38 PROC (WINAPI *qwglGetProcAddress)(LPCSTR);
39 BOOL (WINAPI *qwglMakeCurrent)(HDC, HGLRC);
40 BOOL (WINAPI *qwglSwapIntervalEXT)(int interval);
41 const char *(WINAPI *qwglGetExtensionsStringARB)(HDC hdc);
43 static gl_extensionfunctionlist_t getextensionsstringfuncs[] =
45 {"wglGetExtensionsString", (void **) &qwglGetExtensionsStringARB},
49 static gl_extensionfunctionlist_t wglfuncs[] =
51 {"wglChoosePixelFormat", (void **) &qwglChoosePixelFormat},
52 {"wglDescribePixelFormat", (void **) &qwglDescribePixelFormat},
53 // {"wglGetPixelFormat", (void **) &qwglGetPixelFormat},
54 {"wglSetPixelFormat", (void **) &qwglSetPixelFormat},
55 {"wglSwapBuffers", (void **) &qwglSwapBuffers},
56 {"wglCreateContext", (void **) &qwglCreateContext},
57 {"wglDeleteContext", (void **) &qwglDeleteContext},
58 {"wglGetProcAddress", (void **) &qwglGetProcAddress},
59 {"wglMakeCurrent", (void **) &qwglMakeCurrent},
60 {"wglGetCurrentContext", (void **) &qwglGetCurrentContext},
61 {"wglGetCurrentDC", (void **) &qwglGetCurrentDC},
65 static gl_extensionfunctionlist_t wglswapintervalfuncs[] =
67 {"wglSwapIntervalEXT", (void **) &qwglSwapIntervalEXT},
71 #define MAX_MODE_LIST 30
72 #define VID_ROW_SIZE 3
73 #define MAXWIDTH 10000
74 #define MAXHEIGHT 10000
76 #define MODE_WINDOWED 0
77 #define NO_MODE (MODE_WINDOWED - 1)
78 #define MODE_FULLSCREEN_DEFAULT (MODE_WINDOWED + 1)
96 lmode_t lowresmodes[] = {
103 qboolean scr_skipupdate;
105 static vmode_t modelist[MAX_MODE_LIST];
107 static vmode_t badmode;
109 static DEVMODE gdevmode;
110 static qboolean vid_initialized = false;
111 static qboolean windowed, leavecurrentmode;
112 static qboolean vid_wassuspended = false;
113 static int vid_usingmouse;
114 extern qboolean mouseactive; // from in_win.c
117 int DIBWidth, DIBHeight;
119 DWORD WindowStyle, ExWindowStyle;
123 int vid_modenum = NO_MODE;
125 int vid_default = MODE_WINDOWED;
126 static int windowed_default;
127 unsigned char vid_curpal[256*3];
132 HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow);
134 // global video state
137 modestate_t modestate = MS_UNINIT;
139 void VID_MenuDraw (void);
140 void VID_MenuKey (int key);
142 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
143 void AppActivate(BOOL fActive, BOOL minimize);
144 char *VID_GetModeDescription (int mode);
145 void ClearAllStates (void);
146 void VID_UpdateWindowStatus (void);
148 //====================================
150 int window_center_x, window_center_y, window_x, window_y, window_width, window_height;
153 // direct draw software compatability stuff
155 void CenterWindow(HWND hWndCenter, int width, int height, BOOL lefttopjustify)
157 int CenterX, CenterY;
159 CenterX = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
160 CenterY = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
161 if (CenterX > CenterY*2)
162 CenterX >>= 1; // dual screens
163 CenterX = (CenterX < 0) ? 0: CenterX;
164 CenterY = (CenterY < 0) ? 0: CenterY;
165 SetWindowPos (hWndCenter, NULL, CenterX, CenterY, 0, 0,
166 SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
169 qboolean VID_SetWindowedMode (int modenum)
171 int lastmodestate, width, height;
174 lastmodestate = modestate;
176 WindowRect.top = WindowRect.left = 0;
178 WindowRect.right = modelist[modenum].width;
179 WindowRect.bottom = modelist[modenum].height;
181 DIBWidth = modelist[modenum].width;
182 DIBHeight = modelist[modenum].height;
184 WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
188 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
190 width = rect.right - rect.left;
191 height = rect.bottom - rect.top;
193 // Create the DIB window
194 mainwindow = CreateWindowEx (ExWindowStyle, gamename, gamename, WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
197 Sys_Error ("Couldn't create DIB window");
199 // Center and show the DIB window
200 CenterWindow(mainwindow, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top, false);
202 ShowWindow (mainwindow, SW_SHOWDEFAULT);
203 UpdateWindow (mainwindow);
205 modestate = MS_WINDOWED;
207 SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
208 SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
214 qboolean VID_SetFullDIBMode (int modenum)
216 int lastmodestate, width, height;
219 if (!leavecurrentmode)
221 gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
222 gdevmode.dmBitsPerPel = modelist[modenum].bpp;
223 gdevmode.dmPelsWidth = modelist[modenum].width;
224 gdevmode.dmPelsHeight = modelist[modenum].height;
225 gdevmode.dmSize = sizeof (gdevmode);
227 if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
228 Sys_Error ("Couldn't set fullscreen DIB mode");
231 lastmodestate = modestate;
232 modestate = MS_FULLDIB;
234 WindowRect.top = WindowRect.left = 0;
236 WindowRect.right = modelist[modenum].width;
237 WindowRect.bottom = modelist[modenum].height;
239 DIBWidth = modelist[modenum].width;
240 DIBHeight = modelist[modenum].height;
242 WindowStyle = WS_POPUP;
246 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
248 width = rect.right - rect.left;
249 height = rect.bottom - rect.top;
251 // Create the DIB window
252 mainwindow = CreateWindowEx (ExWindowStyle, gamename, gamename, WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
255 Sys_Error ("Couldn't create DIB window");
257 ShowWindow (mainwindow, SW_SHOWDEFAULT);
258 UpdateWindow (mainwindow);
260 // needed because we're not getting WM_MOVE messages fullscreen on NT
264 SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
265 SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
271 int VID_SetMode (int modenum)
277 if ((windowed && (modenum != 0)) || (!windowed && (modenum < 1)) || (!windowed && (modenum >= nummodes)))
278 Sys_Error ("Bad video mode\n");
282 if (vid_modenum == NO_MODE)
283 original_mode = windowed_default;
285 original_mode = vid_modenum;
287 // Set either the fullscreen or windowed mode
288 if (modelist[modenum].type == MS_WINDOWED)
289 stat = VID_SetWindowedMode(modenum);
290 else if (modelist[modenum].type == MS_FULLDIB)
291 stat = VID_SetFullDIBMode(modenum);
293 Sys_Error ("VID_SetMode: Bad mode type in modelist");
295 window_width = DIBWidth;
296 window_height = DIBHeight;
297 VID_UpdateWindowStatus ();
302 Sys_Error ("Couldn't set video mode");
304 // now we try to make sure we get the focus on the mode switch, because
305 // sometimes in some systems we don't. We grab the foreground, then
306 // finish setting up, pump all our messages, and sleep for a little while
307 // to let messages finish bouncing around the system, then we put
308 // ourselves at the top of the z order, then grab the foreground again,
309 // Who knows if it helps, but it probably doesn't hurt
310 SetForegroundWindow (mainwindow);
311 vid_modenum = modenum;
312 Cvar_SetValue ("vid_mode", (float)vid_modenum);
314 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
316 TranslateMessage (&msg);
317 DispatchMessage (&msg);
322 SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS);
324 SetForegroundWindow (mainwindow);
326 // fix the leftover Alt from any Alt-Tab or the like that switched us away
330 Con_SafePrintf ("Video mode %s initialized.\n", VID_GetModeDescription (vid_modenum));
338 VID_UpdateWindowStatus
341 void VID_UpdateWindowStatus (void)
343 window_rect.left = window_x;
344 window_rect.top = window_y;
345 window_rect.right = window_x + window_width;
346 window_rect.bottom = window_y + window_height;
347 window_center_x = (window_rect.left + window_rect.right) / 2;
348 window_center_y = (window_rect.top + window_rect.bottom) / 2;
350 IN_UpdateClipCursor ();
354 //====================================
361 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
364 *width = WindowRect.right - WindowRect.left;
365 *height = WindowRect.bottom - WindowRect.top;
369 void VID_Finish (void)
372 if (r_render.integer && !scr_skipupdate)
378 // handle the mouse state when windowed if that's changed
379 vid_usemouse = false;
380 if (vid_mouse.integer && !key_consoleactive)
382 if (modestate == MS_FULLDIB)
384 if (!vid_activewindow)
385 vid_usemouse = false;
390 vid_usingmouse = true;
399 vid_usingmouse = false;
400 IN_DeactivateMouse ();
406 void VID_SetDefaultMode (void)
408 IN_DeactivateMouse ();
411 void VID_RestoreSystemGamma(void);
413 void VID_Shutdown (void)
420 if (qwglGetCurrentContext)
421 hRC = qwglGetCurrentContext();
422 if (qwglGetCurrentDC)
423 hDC = qwglGetCurrentDC();
426 qwglMakeCurrent(NULL, NULL);
428 if (hRC && qwglDeleteContext)
429 qwglDeleteContext(hRC);
431 // close the library before we get rid of the window
434 if (hDC && mainwindow)
435 ReleaseDC(mainwindow, hDC);
437 if (modestate == MS_FULLDIB)
438 ChangeDisplaySettings (NULL, 0);
440 if (maindc && mainwindow)
441 ReleaseDC (mainwindow, maindc);
443 AppActivate(false, false);
445 VID_RestoreSystemGamma();
450 //==========================================================================
453 BOOL bSetupPixelFormat(HDC hDC)
455 static PIXELFORMATDESCRIPTOR pfd = {
456 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
458 PFD_DRAW_TO_WINDOW // support window
459 | PFD_SUPPORT_OPENGL // support OpenGL
460 | PFD_DOUBLEBUFFER , // double buffered
461 PFD_TYPE_RGBA, // RGBA type
462 24, // 24-bit color depth
463 0, 0, 0, 0, 0, 0, // color bits ignored
464 0, // no alpha buffer
465 0, // shift bit ignored
466 0, // no accumulation buffer
467 0, 0, 0, 0, // accum bits ignored
468 32, // 32-bit z-buffer
469 0, // no stencil buffer
470 0, // no auxiliary buffer
471 PFD_MAIN_PLANE, // main layer
473 0, 0, 0 // layer masks ignored
477 if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
479 MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
483 if (SetPixelFormat(hDC, pixelformat, &pfd) == false)
485 MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
494 qbyte scantokey[128] =
496 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
497 0 ,27 ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'0' ,'-' ,'=' ,K_BACKSPACE,9 , // 0
498 'q' ,'w' ,'e' ,'r' ,'t' ,'y' ,'u' ,'i' ,'o' ,'p' ,'[' ,']' ,13 ,K_CTRL ,'a' ,'s' , // 1
499 'd' ,'f' ,'g' ,'h' ,'j' ,'k' ,'l' ,';' ,'\'' ,'`' ,K_SHIFT,'\\' ,'z' ,'x' ,'c' ,'v' , // 2
500 'b' ,'n' ,'m' ,',' ,'.' ,'/' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2 ,K_F3 ,K_F4 ,K_F5 , // 3
501 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
502 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
503 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
504 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
512 Map from windows to quake keynums
515 int MapKey (int key, int virtualkey)
520 if (scantokey[key] == 0)
521 Con_DPrintf("key 0x%02x has no translation\n", key);
522 return scantokey[key];
526 ===================================================================
530 ===================================================================
538 void ClearAllStates (void)
542 // send an up event for each key, to make sure the server clears them all
543 for (i=0 ; i<256 ; i++)
545 Key_Event (i, false);
552 void VID_RestoreGameGamma(void);
553 extern qboolean host_loopactive;
555 void AppActivate(BOOL fActive, BOOL minimize)
556 /****************************************************************************
558 * Function: AppActivate
559 * Parameters: fActive - True if app is activating
561 * Description: If the application is activating, then swap the system
562 * into SYSPAL_NOSTATIC mode so that our palettes will display
565 ****************************************************************************/
567 static BOOL sound_active;
569 vid_activewindow = fActive;
570 vid_hidden = minimize;
572 // enable/disable sound on focus gain/loss
573 if (!vid_activewindow && sound_active)
576 sound_active = false;
578 else if (vid_activewindow && !sound_active)
586 if (modestate == MS_FULLDIB)
588 if (vid_wassuspended)
590 vid_wassuspended = false;
591 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
592 ShowWindow(mainwindow, SW_SHOWNORMAL);
595 // LordHavoc: from dabb, fix for alt-tab bug in NVidia drivers
596 MoveWindow(mainwindow,0,0,gdevmode.dmPelsWidth,gdevmode.dmPelsHeight,false);
599 VID_RestoreGameGamma();
604 vid_usingmouse = false;
605 IN_DeactivateMouse ();
607 if (modestate == MS_FULLDIB)
609 ChangeDisplaySettings (NULL, 0);
610 vid_wassuspended = true;
612 VID_RestoreSystemGamma();
616 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
618 /* main window procedure */
619 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
622 int fActive, fMinimized, temp;
623 extern unsigned int uiWheelMessage;
625 if ( uMsg == uiWheelMessage )
626 uMsg = WM_MOUSEWHEEL;
631 if (modestate == MS_FULLDIB)
632 ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
639 window_x = (int) LOWORD(lParam);
640 window_y = (int) HIWORD(lParam);
641 VID_UpdateWindowStatus ();
646 Key_Event (MapKey(lParam, wParam), true);
651 Key_Event (MapKey(lParam, wParam), false);
655 // keep Alt-Space from happening
658 // this is complicated because Win32 seems to pack multiple mouse events into
659 // one update sometimes, so we always check all states and look for events
669 if (wParam & MK_LBUTTON)
672 if (wParam & MK_RBUTTON)
675 if (wParam & MK_MBUTTON)
678 IN_MouseEvent (temp);
682 // JACK: This is the mouse wheel with the Intellimouse
683 // Its delta is either positive or neg, and we generate the proper
686 if ((short) HIWORD(wParam) > 0) {
687 Key_Event(K_MWHEELUP, true);
688 Key_Event(K_MWHEELUP, false);
690 Key_Event(K_MWHEELDOWN, true);
691 Key_Event(K_MWHEELDOWN, false);
699 if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
705 fActive = LOWORD(wParam);
706 fMinimized = (BOOL) HIWORD(wParam);
707 AppActivate(!(fActive == WA_INACTIVE), fMinimized);
709 // fix the leftover Alt from any Alt-Tab or the like that switched us away
717 DestroyWindow (mainwindow);
724 lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
728 /* pass all unhandled messages to DefWindowProc */
729 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
733 /* return 1 if handled message, 0 if not */
743 int VID_NumModes (void)
754 vmode_t *VID_GetModePtr (int modenum)
757 if ((modenum >= 0) && (modenum < nummodes))
758 return &modelist[modenum];
766 VID_GetModeDescription
769 char *VID_GetModeDescription (int mode)
773 static char temp[100];
775 if ((mode < 0) || (mode >= nummodes))
778 if (!leavecurrentmode)
780 pv = VID_GetModePtr (mode);
781 pinfo = pv->modedesc;
785 sprintf (temp, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
793 // KJB: Added this to return the mode driver name in description for console
795 char *VID_GetExtModeDescription (int mode)
797 static char pinfo[40];
800 if ((mode < 0) || (mode >= nummodes))
803 pv = VID_GetModePtr (mode);
804 if (modelist[mode].type == MS_FULLDIB)
806 if (!leavecurrentmode)
807 sprintf(pinfo,"%s fullscreen", pv->modedesc);
809 sprintf (pinfo, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
813 if (modestate == MS_WINDOWED)
814 sprintf(pinfo, "%s windowed", pv->modedesc);
816 sprintf(pinfo, "windowed");
825 VID_DescribeCurrentMode_f
828 void VID_DescribeCurrentMode_f (void)
830 Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum));
839 void VID_NumModes_f (void)
842 Con_Printf ("%d video mode is available\n", nummodes);
844 Con_Printf ("%d video modes are available\n", nummodes);
853 void VID_DescribeMode_f (void)
857 modenum = atoi (Cmd_Argv(1));
859 t = leavecurrentmode;
860 leavecurrentmode = 0;
862 Con_Printf ("%s\n", VID_GetExtModeDescription (modenum));
864 leavecurrentmode = t;
873 void VID_DescribeModes_f (void)
879 lnummodes = VID_NumModes ();
881 t = leavecurrentmode;
882 leavecurrentmode = 0;
884 for (i=1 ; i<lnummodes ; i++)
886 pv = VID_GetModePtr (i);
887 pinfo = VID_GetExtModeDescription (i);
888 Con_Printf ("%2d: %s\n", i, pinfo);
891 leavecurrentmode = t;
894 void VID_AddMode(int type, int width, int height, int modenum, int dib, int fullscreen, int bpp)
897 if (nummodes >= MAX_MODE_LIST)
899 modelist[nummodes].type = type;
900 modelist[nummodes].width = width;
901 modelist[nummodes].height = height;
902 modelist[nummodes].modenum = modenum;
903 modelist[nummodes].dib = dib;
904 modelist[nummodes].fullscreen = fullscreen;
905 modelist[nummodes].bpp = bpp;
907 sprintf (modelist[nummodes].modedesc, "%dx%d", width, height);
909 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", width, height, bpp);
910 for (i = 0;i < nummodes;i++)
912 if (!memcmp(&modelist[i], &modelist[nummodes], sizeof(vmode_t)))
918 void VID_InitDIB (HINSTANCE hInstance)
923 // Register the frame class
925 wc.lpfnWndProc = (WNDPROC)MainWndProc;
928 wc.hInstance = hInstance;
930 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
931 wc.hbrBackground = NULL;
933 wc.lpszClassName = gamename;
935 if (!RegisterClass (&wc) )
936 Sys_Error ("Couldn't register window class");
938 if (COM_CheckParm("-width"))
939 w = atoi(com_argv[COM_CheckParm("-width")+1]);
946 if (COM_CheckParm("-height"))
947 h = atoi(com_argv[COM_CheckParm("-height")+1]);
954 VID_AddMode(MS_WINDOWED, w, h, 0, 1, 0, 0);
963 void VID_InitFullDIB (HINSTANCE hInstance)
967 int originalnummodes;
974 // enumerate >8 bpp modes
975 originalnummodes = nummodes;
980 stat = EnumDisplaySettings (NULL, modenum, &devmode);
982 if ((devmode.dmBitsPerPel >= 15) && (devmode.dmPelsWidth <= MAXWIDTH) && (devmode.dmPelsHeight <= MAXHEIGHT) && (nummodes < MAX_MODE_LIST))
984 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
986 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
987 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 1, 1, devmode.dmBitsPerPel);
994 // see if there are any low-res modes that aren't being reported
995 numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]);
1001 for (j=0 ; (j<numlowresmodes) && (nummodes < MAX_MODE_LIST) ; j++)
1003 devmode.dmBitsPerPel = bpp;
1004 devmode.dmPelsWidth = lowresmodes[j].width;
1005 devmode.dmPelsHeight = lowresmodes[j].height;
1006 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1008 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1009 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 1, 1, devmode.dmBitsPerPel);
1028 if (nummodes == originalnummodes)
1029 Con_SafePrintf ("No fullscreen DIB modes found\n");
1032 //static int grabsysgamma = true;
1033 WORD systemgammaramps[3][256], currentgammaramps[3][256];
1035 int VID_SetGamma(float prescale, float gamma, float scale, float base)
1041 BuildGammaTable16(prescale, gamma, scale, base, ¤tgammaramps[0][0]);
1042 for (i = 0;i < 256;i++)
1043 currentgammaramps[1][i] = currentgammaramps[2][i] = currentgammaramps[0][i];
1045 i = SetDeviceGammaRamp(hdc, ¤tgammaramps[0][0]);
1047 ReleaseDC (NULL, hdc);
1048 return i; // return success or failure
1051 void VID_RestoreGameGamma(void)
1053 VID_UpdateGamma(true);
1056 void VID_GetSystemGamma(void)
1061 GetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1063 ReleaseDC (NULL, hdc);
1066 void VID_RestoreSystemGamma(void)
1071 SetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1073 ReleaseDC (NULL, hdc);
1076 //========================================================
1078 //========================================================
1080 extern void M_Menu_Options_f (void);
1081 extern void M_Print (float cx, float cy, char *str);
1082 extern void M_PrintWhite (float cx, float cy, char *str);
1083 extern void M_DrawCharacter (float cx, float cy, int num);
1084 extern void M_DrawPic (float cx, float cy, char *picname);
1086 static int vid_wmodes;
1095 #define MAX_COLUMN_SIZE 9
1096 #define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 2)
1097 #define MAX_MODEDESCS (MAX_COLUMN_SIZE*3)
1099 static modedesc_t modedescs[MAX_MODEDESCS];
1106 void VID_MenuDraw (void)
1110 int lnummodes, i, k, column, row;
1113 p = Draw_CachePic ("gfx/vidmodes.lmp");
1114 M_DrawPic ( (320-p->width)/2, 4, "gfx/vidmodes.lmp");
1117 lnummodes = VID_NumModes ();
1119 for (i=1 ; (i<lnummodes) && (vid_wmodes < MAX_MODEDESCS) ; i++)
1121 ptr = VID_GetModeDescription (i);
1122 pv = VID_GetModePtr (i);
1126 modedescs[k].modenum = i;
1127 modedescs[k].desc = ptr;
1128 modedescs[k].iscur = 0;
1130 if (i == vid_modenum)
1131 modedescs[k].iscur = 1;
1139 M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)");
1144 for (i=0 ; i<vid_wmodes ; i++)
1146 if (modedescs[i].iscur)
1147 M_PrintWhite (column, row, modedescs[i].desc);
1149 M_Print (column, row, modedescs[i].desc);
1153 if ((i % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
1161 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*2, "Video modes must be set from the");
1162 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3, "command line with -width <width>");
1163 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4, "and -bpp <bits-per-pixel>");
1164 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6, "Select windowed mode with -window");
1173 void VID_MenuKey (int key)
1178 S_LocalSound ("misc/menu1.wav");
1179 M_Menu_Options_f ();
1187 static HINSTANCE gldll;
1189 int GL_OpenLibrary(const char *name)
1191 Con_Printf("Loading GL driver %s\n", name);
1193 if (!(gldll = LoadLibrary(name)))
1195 Con_Printf("Unable to LoadLibrary %s\n", name);
1198 strcpy(gl_driver, name);
1202 void GL_CloseLibrary(void)
1207 qwglGetProcAddress = NULL;
1210 gl_platformextensions = "";
1213 void *GL_GetProcAddress(const char *name)
1216 if (qwglGetProcAddress != NULL)
1217 p = (void *) qwglGetProcAddress(name);
1219 p = (void *) GetProcAddress(gldll, name);
1227 void VID_Init (int fullscreen, int width, int height, int bpp)
1230 double rating, bestrating;
1231 int basenummodes, done;
1235 if (!GL_OpenLibrary("opengl32.dll"))
1236 Sys_Error("Unable to load GL driver\n");
1238 memset(&devmode, 0, sizeof(devmode));
1240 Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);
1241 Cmd_AddCommand ("vid_describecurrentmode", VID_DescribeCurrentMode_f);
1242 Cmd_AddCommand ("vid_describemode", VID_DescribeMode_f);
1243 Cmd_AddCommand ("vid_describemodes", VID_DescribeModes_f);
1245 VID_GetSystemGamma();
1247 hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON2));
1249 InitCommonControls();
1251 VID_InitDIB (global_hInstance);
1252 basenummodes = nummodes = 1;
1254 VID_InitFullDIB (global_hInstance);
1260 if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
1261 Sys_Error ("Can't run in non-RGB mode");
1263 ReleaseDC (NULL, hdc);
1267 vid_default = MODE_WINDOWED;
1272 Sys_Error ("No RGB fullscreen modes available");
1279 bestrating = 1000000000;
1280 for (i = 0;i < nummodes;i++)
1282 if (fullscreen == modelist[i].fullscreen)
1284 rating = VID_CompareMode(width, height, bpp, modelist[i].width, modelist[i].height, modelist[i].bpp);
1285 if (bestrating > rating)
1287 bestrating = rating;
1294 Sys_Error ("Specified video mode not available");
1297 vid_initialized = true;
1299 VID_SetMode (vid_default);
1301 maindc = GetDC(mainwindow);
1302 bSetupPixelFormat(maindc);
1304 if (!GL_CheckExtension("wgl", wglfuncs, NULL, false))
1305 Sys_Error("wgl functions not found\n");
1307 baseRC = qwglCreateContext( maindc );
1309 Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.");
1310 if (!qwglMakeCurrent( maindc, baseRC ))
1311 Sys_Error ("wglMakeCurrent failed");
1313 qglGetString = GL_GetProcAddress("glGetString");
1314 if (qglGetString == NULL)
1315 Sys_Error ("glGetString does not exist");
1316 gl_renderer = qglGetString(GL_RENDERER);
1317 gl_vendor = qglGetString(GL_VENDOR);
1318 gl_version = qglGetString(GL_VERSION);
1319 gl_extensions = qglGetString(GL_EXTENSIONS);
1320 gl_platform = "WGL";
1321 gl_platformextensions = "";
1323 if (GL_CheckExtension("WGL_ARB_extensions_string", getextensionsstringfuncs, NULL, false))
1324 gl_platformextensions = qwglGetExtensionsStringARB(maindc);
1326 gl_videosyncavailable = GL_CheckExtension("WGL_EXT_swap_control", wglswapintervalfuncs, NULL, false);
1330 // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
1331 if (strncasecmp(gl_vendor,"ATI",3)==0)
1333 if (strncasecmp(gl_renderer,"Rage Pro",8)==0)
1336 if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
1339 vid_realmode = vid_modenum;
1341 vid_menudrawfn = VID_MenuDraw;
1342 vid_menukeyfn = VID_MenuKey;
1344 strcpy (badmode.modedesc, "Bad mode");