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 PROC (WINAPI *qwglGetProcAddress)(LPCSTR);
37 BOOL (WINAPI *qwglMakeCurrent)(HDC, HGLRC);
38 BOOL (WINAPI *qwglSwapIntervalEXT)(int interval);
39 const char *(WINAPI *wglGetExtensionsStringARB)(HDC hdc);
41 static gl_extensionfunctionlist_t getextensionsstringfuncs[] =
43 {"wglGetExtensionsString", (void **) &qwglGetExtensionsString},
47 static gl_extensionfunctionlist_t wglfuncs[] =
49 {"wglChoosePixelFormat", (void **) &qwglChoosePixelFormat},
50 {"wglDescribePixelFormat", (void **) &qwglDescribePixelFormat},
51 // {"wglGetPixelFormat", (void **) &qwglGetPixelFormat},
52 {"wglSetPixelFormat", (void **) &qwglSetPixelFormat},
53 {"wglSwapBuffers", (void **) &qwglSwapBuffers},
54 {"wglCreateContext", (void **) &qwglCreateContext},
55 {"wglDeleteContext", (void **) &qwglDeleteContext},
56 {"wglGetProcAddress", (void **) &qwglGetProcAddress},
57 {"wglMakeCurrent", (void **) &qwglMakeCurrent},
61 static gl_extensionfunctionlist_t wglswapintervalfuncs[] =
63 {"wglSwapIntervalEXT", (void **) &qwglSwapIntervalEXT},
67 #define MAX_MODE_LIST 30
68 #define VID_ROW_SIZE 3
69 #define MAXWIDTH 10000
70 #define MAXHEIGHT 10000
72 #define MODE_WINDOWED 0
73 #define NO_MODE (MODE_WINDOWED - 1)
74 #define MODE_FULLSCREEN_DEFAULT (MODE_WINDOWED + 1)
92 lmode_t lowresmodes[] = {
99 qboolean scr_skipupdate;
101 static vmode_t modelist[MAX_MODE_LIST];
103 static vmode_t badmode;
105 static DEVMODE gdevmode;
106 static qboolean vid_initialized = false;
107 static qboolean windowed, leavecurrentmode;
108 static qboolean vid_canalttab = false;
109 static qboolean vid_wassuspended = false;
110 static int vid_usingmouse;
111 extern qboolean mouseactive; // from in_win.c
114 int DIBWidth, DIBHeight;
116 DWORD WindowStyle, ExWindowStyle;
120 int vid_modenum = NO_MODE;
122 int vid_default = MODE_WINDOWED;
123 static int windowed_default;
124 unsigned char vid_curpal[256*3];
129 HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow);
131 // global video state
134 modestate_t modestate = MS_UNINIT;
136 void VID_MenuDraw (void);
137 void VID_MenuKey (int key);
139 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
140 void AppActivate(BOOL fActive, BOOL minimize);
141 char *VID_GetModeDescription (int mode);
142 void ClearAllStates (void);
143 void VID_UpdateWindowStatus (void);
145 //====================================
147 int window_center_x, window_center_y, window_x, window_y, window_width, window_height;
150 // direct draw software compatability stuff
152 void CenterWindow(HWND hWndCenter, int width, int height, BOOL lefttopjustify)
154 int CenterX, CenterY;
156 CenterX = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
157 CenterY = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
158 if (CenterX > CenterY*2)
159 CenterX >>= 1; // dual screens
160 CenterX = (CenterX < 0) ? 0: CenterX;
161 CenterY = (CenterY < 0) ? 0: CenterY;
162 SetWindowPos (hWndCenter, NULL, CenterX, CenterY, 0, 0,
163 SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
166 qboolean VID_SetWindowedMode (int modenum)
168 int lastmodestate, width, height;
171 lastmodestate = modestate;
173 WindowRect.top = WindowRect.left = 0;
175 WindowRect.right = modelist[modenum].width;
176 WindowRect.bottom = modelist[modenum].height;
178 DIBWidth = modelist[modenum].width;
179 DIBHeight = modelist[modenum].height;
181 WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
185 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
187 width = rect.right - rect.left;
188 height = rect.bottom - rect.top;
190 // Create the DIB window
191 mainwindow = CreateWindowEx (ExWindowStyle, gamename, gamename, WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
194 Sys_Error ("Couldn't create DIB window");
196 // Center and show the DIB window
197 CenterWindow(mainwindow, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top, false);
199 ShowWindow (mainwindow, SW_SHOWDEFAULT);
200 UpdateWindow (mainwindow);
202 modestate = MS_WINDOWED;
204 SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
205 SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
211 qboolean VID_SetFullDIBMode (int modenum)
213 int lastmodestate, width, height;
216 if (!leavecurrentmode)
218 gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
219 gdevmode.dmBitsPerPel = modelist[modenum].bpp;
220 gdevmode.dmPelsWidth = modelist[modenum].width;
221 gdevmode.dmPelsHeight = modelist[modenum].height;
222 gdevmode.dmSize = sizeof (gdevmode);
224 if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
225 Sys_Error ("Couldn't set fullscreen DIB mode");
228 lastmodestate = modestate;
229 modestate = MS_FULLDIB;
231 WindowRect.top = WindowRect.left = 0;
233 WindowRect.right = modelist[modenum].width;
234 WindowRect.bottom = modelist[modenum].height;
236 DIBWidth = modelist[modenum].width;
237 DIBHeight = modelist[modenum].height;
239 WindowStyle = WS_POPUP;
243 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
245 width = rect.right - rect.left;
246 height = rect.bottom - rect.top;
248 // Create the DIB window
249 mainwindow = CreateWindowEx (ExWindowStyle, gamename, gamename, WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
252 Sys_Error ("Couldn't create DIB window");
254 ShowWindow (mainwindow, SW_SHOWDEFAULT);
255 UpdateWindow (mainwindow);
257 // needed because we're not getting WM_MOVE messages fullscreen on NT
261 SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
262 SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
268 int VID_SetMode (int modenum)
274 if ((windowed && (modenum != 0)) || (!windowed && (modenum < 1)) || (!windowed && (modenum >= nummodes)))
275 Sys_Error ("Bad video mode\n");
279 if (vid_modenum == NO_MODE)
280 original_mode = windowed_default;
282 original_mode = vid_modenum;
284 // Set either the fullscreen or windowed mode
285 if (modelist[modenum].type == MS_WINDOWED)
286 stat = VID_SetWindowedMode(modenum);
287 else if (modelist[modenum].type == MS_FULLDIB)
288 stat = VID_SetFullDIBMode(modenum);
290 Sys_Error ("VID_SetMode: Bad mode type in modelist");
292 window_width = DIBWidth;
293 window_height = DIBHeight;
294 VID_UpdateWindowStatus ();
299 Sys_Error ("Couldn't set video mode");
301 // now we try to make sure we get the focus on the mode switch, because
302 // sometimes in some systems we don't. We grab the foreground, then
303 // finish setting up, pump all our messages, and sleep for a little while
304 // to let messages finish bouncing around the system, then we put
305 // ourselves at the top of the z order, then grab the foreground again,
306 // Who knows if it helps, but it probably doesn't hurt
307 SetForegroundWindow (mainwindow);
308 vid_modenum = modenum;
309 Cvar_SetValue ("vid_mode", (float)vid_modenum);
311 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
313 TranslateMessage (&msg);
314 DispatchMessage (&msg);
319 SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS);
321 SetForegroundWindow (mainwindow);
323 // fix the leftover Alt from any Alt-Tab or the like that switched us away
327 Con_SafePrintf ("Video mode %s initialized.\n", VID_GetModeDescription (vid_modenum));
335 VID_UpdateWindowStatus
338 void VID_UpdateWindowStatus (void)
340 window_rect.left = window_x;
341 window_rect.top = window_y;
342 window_rect.right = window_x + window_width;
343 window_rect.bottom = window_y + window_height;
344 window_center_x = (window_rect.left + window_rect.right) / 2;
345 window_center_y = (window_rect.top + window_rect.bottom) / 2;
347 IN_UpdateClipCursor ();
351 //====================================
358 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
361 *width = WindowRect.right - WindowRect.left;
362 *height = WindowRect.bottom - WindowRect.top;
366 void VID_Finish (void)
369 if (r_render.integer && !scr_skipupdate)
375 // handle the mouse state when windowed if that's changed
376 vid_usemouse = false;
377 if (vid_mouse.integer && !key_consoleactive)
379 if (modestate == MS_FULLDIB)
381 if (!vid_activewindow)
382 vid_usemouse = false;
387 vid_usingmouse = true;
396 vid_usingmouse = false;
397 IN_DeactivateMouse ();
403 void VID_SetDefaultMode (void)
405 IN_DeactivateMouse ();
408 void VID_RestoreSystemGamma(void);
410 void VID_Shutdown (void)
419 vid_canalttab = false;
420 hRC = qwglGetCurrentContext();
421 hDC = qwglGetCurrentDC();
423 qwglMakeCurrent(NULL, NULL);
425 // LordHavoc: free textures before closing (may help NVIDIA)
426 for (i = 0;i < 8192;i++)
428 qglDeleteTextures(8192, temp);
431 qwglDeleteContext(hRC);
433 // close the library before we get rid of the window
436 if (hDC && mainwindow)
437 ReleaseDC(mainwindow, hDC);
439 if (modestate == MS_FULLDIB)
440 ChangeDisplaySettings (NULL, 0);
442 if (maindc && mainwindow)
443 ReleaseDC (mainwindow, maindc);
445 AppActivate(false, false);
447 VID_RestoreSystemGamma();
452 //==========================================================================
455 BOOL bSetupPixelFormat(HDC hDC)
457 static PIXELFORMATDESCRIPTOR pfd = {
458 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
460 PFD_DRAW_TO_WINDOW // support window
461 | PFD_SUPPORT_OPENGL // support OpenGL
462 | PFD_DOUBLEBUFFER , // double buffered
463 PFD_TYPE_RGBA, // RGBA type
464 24, // 24-bit color depth
465 0, 0, 0, 0, 0, 0, // color bits ignored
466 0, // no alpha buffer
467 0, // shift bit ignored
468 0, // no accumulation buffer
469 0, 0, 0, 0, // accum bits ignored
470 32, // 32-bit z-buffer
471 0, // no stencil buffer
472 0, // no auxiliary buffer
473 PFD_MAIN_PLANE, // main layer
475 0, 0, 0 // layer masks ignored
479 if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
481 MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
485 if (SetPixelFormat(hDC, pixelformat, &pfd) == false)
487 MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
496 qbyte scantokey[128] =
498 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
499 0 ,27 ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'0' ,'-' ,'=' ,K_BACKSPACE,9 , // 0
500 'q' ,'w' ,'e' ,'r' ,'t' ,'y' ,'u' ,'i' ,'o' ,'p' ,'[' ,']' ,13 ,K_CTRL ,'a' ,'s' , // 1
501 'd' ,'f' ,'g' ,'h' ,'j' ,'k' ,'l' ,';' ,'\'' ,'`' ,K_SHIFT,'\\' ,'z' ,'x' ,'c' ,'v' , // 2
502 'b' ,'n' ,'m' ,',' ,'.' ,'/' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2 ,K_F3 ,K_F4 ,K_F5 , // 3
503 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
504 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
505 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
506 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
514 Map from windows to quake keynums
517 int MapKey (int key, int virtualkey)
522 if (scantokey[key] == 0)
523 Con_DPrintf("key 0x%02x has no translation\n", key);
524 return scantokey[key];
528 ===================================================================
532 ===================================================================
540 void ClearAllStates (void)
544 // send an up event for each key, to make sure the server clears them all
545 for (i=0 ; i<256 ; i++)
547 Key_Event (i, false);
554 void VID_RestoreGameGamma(void);
555 extern qboolean host_loopactive;
557 void AppActivate(BOOL fActive, BOOL minimize)
558 /****************************************************************************
560 * Function: AppActivate
561 * Parameters: fActive - True if app is activating
563 * Description: If the application is activating, then swap the system
564 * into SYSPAL_NOSTATIC mode so that our palettes will display
567 ****************************************************************************/
569 static BOOL sound_active;
571 vid_activewindow = fActive;
572 vid_hidden = minimize;
574 // enable/disable sound on focus gain/loss
575 if (!vid_activewindow && sound_active)
578 sound_active = false;
580 else if (vid_activewindow && !sound_active)
588 if (modestate == MS_FULLDIB)
590 if (vid_canalttab && vid_wassuspended)
592 vid_wassuspended = false;
593 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
594 ShowWindow(mainwindow, SW_SHOWNORMAL);
597 // LordHavoc: from dabb, fix for alt-tab bug in NVidia drivers
598 MoveWindow(mainwindow,0,0,gdevmode.dmPelsWidth,gdevmode.dmPelsHeight,false);
601 VID_RestoreGameGamma();
606 vid_usingmouse = false;
607 IN_DeactivateMouse ();
609 if (modestate == MS_FULLDIB && vid_canalttab)
611 ChangeDisplaySettings (NULL, 0);
612 vid_wassuspended = true;
614 VID_RestoreSystemGamma();
618 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
620 /* main window procedure */
621 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
624 int fActive, fMinimized, temp;
625 extern unsigned int uiWheelMessage;
627 if ( uMsg == uiWheelMessage )
628 uMsg = WM_MOUSEWHEEL;
633 if (modestate == MS_FULLDIB)
634 ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
641 window_x = (int) LOWORD(lParam);
642 window_y = (int) HIWORD(lParam);
643 VID_UpdateWindowStatus ();
648 Key_Event (MapKey(lParam, wParam), true);
653 Key_Event (MapKey(lParam, wParam), false);
657 // keep Alt-Space from happening
660 // this is complicated because Win32 seems to pack multiple mouse events into
661 // one update sometimes, so we always check all states and look for events
671 if (wParam & MK_LBUTTON)
674 if (wParam & MK_RBUTTON)
677 if (wParam & MK_MBUTTON)
680 IN_MouseEvent (temp);
684 // JACK: This is the mouse wheel with the Intellimouse
685 // Its delta is either positive or neg, and we generate the proper
688 if ((short) HIWORD(wParam) > 0) {
689 Key_Event(K_MWHEELUP, true);
690 Key_Event(K_MWHEELUP, false);
692 Key_Event(K_MWHEELDOWN, true);
693 Key_Event(K_MWHEELDOWN, false);
701 if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
707 fActive = LOWORD(wParam);
708 fMinimized = (BOOL) HIWORD(wParam);
709 AppActivate(!(fActive == WA_INACTIVE), fMinimized);
711 // fix the leftover Alt from any Alt-Tab or the like that switched us away
719 DestroyWindow (mainwindow);
726 lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
730 /* pass all unhandled messages to DefWindowProc */
731 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
735 /* return 1 if handled message, 0 if not */
745 int VID_NumModes (void)
756 vmode_t *VID_GetModePtr (int modenum)
759 if ((modenum >= 0) && (modenum < nummodes))
760 return &modelist[modenum];
768 VID_GetModeDescription
771 char *VID_GetModeDescription (int mode)
775 static char temp[100];
777 if ((mode < 0) || (mode >= nummodes))
780 if (!leavecurrentmode)
782 pv = VID_GetModePtr (mode);
783 pinfo = pv->modedesc;
787 sprintf (temp, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
795 // KJB: Added this to return the mode driver name in description for console
797 char *VID_GetExtModeDescription (int mode)
799 static char pinfo[40];
802 if ((mode < 0) || (mode >= nummodes))
805 pv = VID_GetModePtr (mode);
806 if (modelist[mode].type == MS_FULLDIB)
808 if (!leavecurrentmode)
809 sprintf(pinfo,"%s fullscreen", pv->modedesc);
811 sprintf (pinfo, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
815 if (modestate == MS_WINDOWED)
816 sprintf(pinfo, "%s windowed", pv->modedesc);
818 sprintf(pinfo, "windowed");
827 VID_DescribeCurrentMode_f
830 void VID_DescribeCurrentMode_f (void)
832 Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum));
841 void VID_NumModes_f (void)
844 Con_Printf ("%d video mode is available\n", nummodes);
846 Con_Printf ("%d video modes are available\n", nummodes);
855 void VID_DescribeMode_f (void)
859 modenum = atoi (Cmd_Argv(1));
861 t = leavecurrentmode;
862 leavecurrentmode = 0;
864 Con_Printf ("%s\n", VID_GetExtModeDescription (modenum));
866 leavecurrentmode = t;
875 void VID_DescribeModes_f (void)
881 lnummodes = VID_NumModes ();
883 t = leavecurrentmode;
884 leavecurrentmode = 0;
886 for (i=1 ; i<lnummodes ; i++)
888 pv = VID_GetModePtr (i);
889 pinfo = VID_GetExtModeDescription (i);
890 Con_Printf ("%2d: %s\n", i, pinfo);
893 leavecurrentmode = t;
896 void VID_AddMode(int type, int width, int height, int modenum, int dib, int fullscreen, int bpp)
899 if (nummodes >= MAX_MODE_LIST)
901 modelist[nummodes].type = type;
902 modelist[nummodes].width = width;
903 modelist[nummodes].height = height;
904 modelist[nummodes].modenum = modenum;
905 modelist[nummodes].dib = dib;
906 modelist[nummodes].fullscreen = fullscreen;
907 modelist[nummodes].bpp = bpp;
909 sprintf (modelist[nummodes].modedesc, "%dx%d", width, height);
911 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", width, height, bpp);
912 for (i = 0;i < nummodes;i++)
914 if (!memcmp(&modelist[i], &modelist[nummodes], sizeof(vmode_t)))
920 void VID_InitDIB (HINSTANCE hInstance)
925 // Register the frame class
927 wc.lpfnWndProc = (WNDPROC)MainWndProc;
930 wc.hInstance = hInstance;
932 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
933 wc.hbrBackground = NULL;
935 wc.lpszClassName = gamename;
937 if (!RegisterClass (&wc) )
938 Sys_Error ("Couldn't register window class");
940 if (COM_CheckParm("-width"))
941 w = atoi(com_argv[COM_CheckParm("-width")+1]);
948 if (COM_CheckParm("-height"))
949 h = atoi(com_argv[COM_CheckParm("-height")+1]);
956 VID_AddMode(MS_WINDOWED, w, h, 0, 1, 0, 0);
965 void VID_InitFullDIB (HINSTANCE hInstance)
969 int originalnummodes;
976 // enumerate >8 bpp modes
977 originalnummodes = nummodes;
982 stat = EnumDisplaySettings (NULL, modenum, &devmode);
984 if ((devmode.dmBitsPerPel >= 15) && (devmode.dmPelsWidth <= MAXWIDTH) && (devmode.dmPelsHeight <= MAXHEIGHT) && (nummodes < MAX_MODE_LIST))
986 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
988 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
990 // if the width is more than twice the height, reduce it by half because this
991 // is probably a dual-screen monitor
992 if ((!COM_CheckParm("-noadjustaspect")) && (devmode.dmPelsWidth > (devmode.dmPelsHeight << 1)))
993 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth >> 1, devmode.dmPelsHeight, 0, 1, 1, 1, devmode.dmBitsPerPel);
995 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1003 // see if there are any low-res modes that aren't being reported
1004 numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]);
1010 for (j=0 ; (j<numlowresmodes) && (nummodes < MAX_MODE_LIST) ; j++)
1012 devmode.dmBitsPerPel = bpp;
1013 devmode.dmPelsWidth = lowresmodes[j].width;
1014 devmode.dmPelsHeight = lowresmodes[j].height;
1015 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1017 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1018 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1037 if (nummodes == originalnummodes)
1038 Con_SafePrintf ("No fullscreen DIB modes found\n");
1041 //static int grabsysgamma = true;
1042 WORD systemgammaramps[3][256], currentgammaramps[3][256];
1044 int VID_SetGamma(float prescale, float gamma, float scale, float base)
1050 BuildGammaTable16(prescale, gamma, scale, base, ¤tgammaramps[0][0]);
1051 for (i = 0;i < 256;i++)
1052 currentgammaramps[1][i] = currentgammaramps[2][i] = currentgammaramps[0][i];
1054 i = SetDeviceGammaRamp(hdc, ¤tgammaramps[0][0]);
1056 ReleaseDC (NULL, hdc);
1057 return i; // return success or failure
1060 void VID_RestoreGameGamma(void)
1062 VID_UpdateGamma(true);
1065 void VID_GetSystemGamma(void)
1070 GetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1072 ReleaseDC (NULL, hdc);
1075 void VID_RestoreSystemGamma(void)
1080 SetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1082 ReleaseDC (NULL, hdc);
1085 //========================================================
1087 //========================================================
1089 extern void M_Menu_Options_f (void);
1090 extern void M_Print (float cx, float cy, char *str);
1091 extern void M_PrintWhite (float cx, float cy, char *str);
1092 extern void M_DrawCharacter (float cx, float cy, int num);
1093 extern void M_DrawPic (float cx, float cy, char *picname);
1095 static int vid_wmodes;
1104 #define MAX_COLUMN_SIZE 9
1105 #define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 2)
1106 #define MAX_MODEDESCS (MAX_COLUMN_SIZE*3)
1108 static modedesc_t modedescs[MAX_MODEDESCS];
1115 void VID_MenuDraw (void)
1119 int lnummodes, i, k, column, row;
1122 p = Draw_CachePic ("gfx/vidmodes.lmp");
1123 M_DrawPic ( (320-p->width)/2, 4, "gfx/vidmodes.lmp");
1126 lnummodes = VID_NumModes ();
1128 for (i=1 ; (i<lnummodes) && (vid_wmodes < MAX_MODEDESCS) ; i++)
1130 ptr = VID_GetModeDescription (i);
1131 pv = VID_GetModePtr (i);
1135 modedescs[k].modenum = i;
1136 modedescs[k].desc = ptr;
1137 modedescs[k].iscur = 0;
1139 if (i == vid_modenum)
1140 modedescs[k].iscur = 1;
1148 M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)");
1153 for (i=0 ; i<vid_wmodes ; i++)
1155 if (modedescs[i].iscur)
1156 M_PrintWhite (column, row, modedescs[i].desc);
1158 M_Print (column, row, modedescs[i].desc);
1162 if ((i % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
1170 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*2, "Video modes must be set from the");
1171 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3, "command line with -width <width>");
1172 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4, "and -bpp <bits-per-pixel>");
1173 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6, "Select windowed mode with -window");
1182 void VID_MenuKey (int key)
1187 S_LocalSound ("misc/menu1.wav");
1188 M_Menu_Options_f ();
1196 static HINSTANCE gldll;
1198 int GL_OpenLibrary(const char *name)
1200 Con_Printf("Loading GL driver %s\n", name);
1202 if (!(gldll = LoadLibrary(name)))
1204 Con_Printf("Unable to LoadLibrary %s\n", name);
1207 strcpy(gl_driver, name);
1211 void GL_CloseLibrary(void)
1216 qwglGetProcAddress = NULL;
1219 gl_platformextensions = "";
1222 void *GL_GetProcAddress(const char *name)
1225 if (qwglGetProcAddress != NULL)
1226 p = (void *) qwglGetProcAddress(name);
1228 p = (void *) GetProcAddress(gldll, name);
1236 void VID_Init (int fullscreen, int width, int height)
1239 int basenummodes, bpp, findbpp, done;
1243 if (!GL_OpenLibrary("opengl32.dll"))
1244 Sys_Error("Unable to load GL driver\n");
1246 memset(&devmode, 0, sizeof(devmode));
1248 Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);
1249 Cmd_AddCommand ("vid_describecurrentmode", VID_DescribeCurrentMode_f);
1250 Cmd_AddCommand ("vid_describemode", VID_DescribeMode_f);
1251 Cmd_AddCommand ("vid_describemodes", VID_DescribeModes_f);
1253 VID_GetSystemGamma();
1255 hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON2));
1257 InitCommonControls();
1259 VID_InitDIB (global_hInstance);
1260 basenummodes = nummodes = 1;
1262 VID_InitFullDIB (global_hInstance);
1268 if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
1269 Sys_Error ("Can't run in non-RGB mode");
1271 ReleaseDC (NULL, hdc);
1275 vid_default = MODE_WINDOWED;
1280 Sys_Error ("No RGB fullscreen modes available");
1287 bestrating = 1000000000;
1288 for (i = 0;i < nummodes;i++)
1290 rating = VID_CompareMode(fullscreen, width, height, bpp, modelist[i].fullscreen, modelist[i].width, modelist[i].height, modelist[i].bpp);
1291 if (bestrating > rating)
1293 bestrating = rating;
1299 Sys_Error ("Specified video mode not available");
1302 vid_initialized = true;
1304 VID_SetMode (vid_default);
1306 maindc = GetDC(mainwindow);
1307 bSetupPixelFormat(maindc);
1309 if (!gl_checkextension("wgl", wglfuncs, NULL, false))
1310 Sys_Error("wgl functions not found\n");
1312 baseRC = qwglCreateContext( maindc );
1314 Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.");
1315 if (!qwglMakeCurrent( maindc, baseRC ))
1316 Sys_Error ("wglMakeCurrent failed");
1318 gl_renderer = qglGetString(GL_RENDERER);
1319 gl_vendor = qglGetString(GL_VENDOR);
1320 gl_version = qglGetString(GL_VERSION);
1321 gl_extensions = qglGetString(GL_EXTENSIONS);
1322 gl_platformname = "WGL";
1323 gl_platformextensions = "";
1325 if (gl_checkextension("WGL_ARB_extensions_string", extensionsstringfuncs, NULL, false))
1326 gl_platformextensions = qwglGetExtensionsStringARB(maindc);
1328 gl_videosyncavailable = gl_checkextension("WGL_EXT_swap_control", wglswapintervalfuncs, NULL, false);
1332 // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
1333 if (strncasecmp(gl_vendor,"ATI",3)==0)
1335 if (strncasecmp(gl_renderer,"Rage Pro",8)==0)
1338 if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
1341 vid_realmode = vid_modenum;
1343 vid_menudrawfn = VID_MenuDraw;
1344 vid_menukeyfn = VID_MenuKey;
1346 strcpy (badmode.modedesc, "Bad mode");
1347 vid_canalttab = true;