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 #define MAX_MODE_LIST 30
28 #define VID_ROW_SIZE 3
29 #define MAXWIDTH 10000
30 #define MAXHEIGHT 10000
32 #define MODE_WINDOWED 0
33 #define NO_MODE (MODE_WINDOWED - 1)
34 #define MODE_FULLSCREEN_DEFAULT (MODE_WINDOWED + 1)
53 lmode_t lowresmodes[] = {
60 const char *gl_vendor;
61 const char *gl_renderer;
62 const char *gl_version;
63 const char *gl_extensions;
65 qboolean scr_skipupdate;
67 static vmode_t modelist[MAX_MODE_LIST];
69 static vmode_t *pcurrentmode;
70 static vmode_t badmode;
72 static DEVMODE gdevmode;
73 static qboolean vid_initialized = false;
74 static qboolean windowed, leavecurrentmode;
75 static qboolean vid_canalttab = false;
76 static qboolean vid_wassuspended = false;
77 static int usingmouse;
78 extern qboolean mouseactive; // from in_win.c
81 int DIBWidth, DIBHeight;
83 DWORD WindowStyle, ExWindowStyle;
87 int vid_modenum = NO_MODE;
89 int vid_default = MODE_WINDOWED;
90 static int windowed_default;
91 unsigned char vid_curpal[256*3];
98 HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow);
100 viddef_t vid; // global video state
102 float gldepthmin, gldepthmax;
104 modestate_t modestate = MS_UNINIT;
106 void VID_MenuDraw (void);
107 void VID_MenuKey (int key);
109 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
110 void AppActivate(BOOL fActive, BOOL minimize);
111 char *VID_GetModeDescription (int mode);
112 void ClearAllStates (void);
113 void VID_UpdateWindowStatus (void);
115 //====================================
117 // Note that 0 is MODE_WINDOWED
118 //cvar_t _vid_default_mode = {"_vid_default_mode","0", true};
119 // Note that 3 is MODE_FULLSCREEN_DEFAULT
120 //cvar_t _vid_default_mode_win = {"_vid_default_mode_win","3", true};
122 int window_center_x, window_center_y, window_x, window_y, window_width, window_height;
125 // direct draw software compatability stuff
127 void CenterWindow(HWND hWndCenter, int width, int height, BOOL lefttopjustify)
129 int CenterX, CenterY;
131 CenterX = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
132 CenterY = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
133 if (CenterX > CenterY*2)
134 CenterX >>= 1; // dual screens
135 CenterX = (CenterX < 0) ? 0: CenterX;
136 CenterY = (CenterY < 0) ? 0: CenterY;
137 SetWindowPos (hWndCenter, NULL, CenterX, CenterY, 0, 0,
138 SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
141 qboolean VID_SetWindowedMode (int modenum)
143 int lastmodestate, width, height;
146 lastmodestate = modestate;
148 WindowRect.top = WindowRect.left = 0;
150 WindowRect.right = modelist[modenum].width;
151 WindowRect.bottom = modelist[modenum].height;
153 DIBWidth = modelist[modenum].width;
154 DIBHeight = modelist[modenum].height;
156 WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
160 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
162 width = rect.right - rect.left;
163 height = rect.bottom - rect.top;
165 // Create the DIB window
166 mainwindow = CreateWindowEx (ExWindowStyle, gamename, gamename, WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
169 Sys_Error ("Couldn't create DIB window");
171 // Center and show the DIB window
172 CenterWindow(mainwindow, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top, false);
174 ShowWindow (mainwindow, SW_SHOWDEFAULT);
175 UpdateWindow (mainwindow);
177 modestate = MS_WINDOWED;
179 if (vid.conheight > modelist[modenum].height)
180 vid.conheight = modelist[modenum].height;
181 if (vid.conwidth > modelist[modenum].width)
182 vid.conwidth = modelist[modenum].width;
184 SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
185 SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
191 qboolean VID_SetFullDIBMode (int modenum)
193 int lastmodestate, width, height;
196 if (!leavecurrentmode)
198 gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
199 gdevmode.dmBitsPerPel = modelist[modenum].bpp;
200 gdevmode.dmPelsWidth = modelist[modenum].width <<
201 modelist[modenum].halfscreen;
202 gdevmode.dmPelsHeight = modelist[modenum].height;
203 gdevmode.dmSize = sizeof (gdevmode);
205 if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
206 Sys_Error ("Couldn't set fullscreen DIB mode");
209 lastmodestate = modestate;
210 modestate = MS_FULLDIB;
212 WindowRect.top = WindowRect.left = 0;
214 WindowRect.right = modelist[modenum].width;
215 WindowRect.bottom = modelist[modenum].height;
217 DIBWidth = modelist[modenum].width;
218 DIBHeight = modelist[modenum].height;
220 WindowStyle = WS_POPUP;
224 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
226 width = rect.right - rect.left;
227 height = rect.bottom - rect.top;
229 // Create the DIB window
230 mainwindow = CreateWindowEx (ExWindowStyle, gamename, gamename, WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
233 Sys_Error ("Couldn't create DIB window");
235 ShowWindow (mainwindow, SW_SHOWDEFAULT);
236 UpdateWindow (mainwindow);
238 if (vid.conheight > modelist[modenum].height)
239 vid.conheight = modelist[modenum].height;
240 if (vid.conwidth > modelist[modenum].width)
241 vid.conwidth = modelist[modenum].width;
243 // needed because we're not getting WM_MOVE messages fullscreen on NT
247 SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
248 SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
254 int VID_SetMode (int modenum)
256 int original_mode, temp;
260 if ((windowed && (modenum != 0)) || (!windowed && (modenum < 1)) || (!windowed && (modenum >= nummodes)))
261 Sys_Error ("Bad video mode\n");
263 // so Con_Printfs don't mess us up by forcing vid and snd updates
264 temp = scr_disabled_for_loading;
265 scr_disabled_for_loading = true;
269 if (vid_modenum == NO_MODE)
270 original_mode = windowed_default;
272 original_mode = vid_modenum;
274 // Set either the fullscreen or windowed mode
275 if (modelist[modenum].type == MS_WINDOWED)
277 // if (vid_mouse.value && key_dest == key_game)
279 // stat = VID_SetWindowedMode(modenum);
280 // usingmouse = true;
281 // IN_ActivateMouse ();
286 // usingmouse = false;
287 // IN_DeactivateMouse ();
289 // stat = VID_SetWindowedMode(modenum);
291 stat = VID_SetWindowedMode(modenum);
293 else if (modelist[modenum].type == MS_FULLDIB)
295 stat = VID_SetFullDIBMode(modenum);
296 // usingmouse = true;
297 // IN_ActivateMouse ();
301 Sys_Error ("VID_SetMode: Bad mode type in modelist");
303 window_width = DIBWidth;
304 window_height = DIBHeight;
305 VID_UpdateWindowStatus ();
308 scr_disabled_for_loading = temp;
311 Sys_Error ("Couldn't set video mode");
313 // now we try to make sure we get the focus on the mode switch, because
314 // sometimes in some systems we don't. We grab the foreground, then
315 // finish setting up, pump all our messages, and sleep for a little while
316 // to let messages finish bouncing around the system, then we put
317 // ourselves at the top of the z order, then grab the foreground again,
318 // Who knows if it helps, but it probably doesn't hurt
319 SetForegroundWindow (mainwindow);
320 vid_modenum = modenum;
321 Cvar_SetValue ("vid_mode", (float)vid_modenum);
323 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
325 TranslateMessage (&msg);
326 DispatchMessage (&msg);
331 SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS);
333 SetForegroundWindow (mainwindow);
335 // fix the leftover Alt from any Alt-Tab or the like that switched us away
339 Con_SafePrintf ("Video mode %s initialized.\n", VID_GetModeDescription (vid_modenum));
341 // vid.recalc_refdef = 1;
349 VID_UpdateWindowStatus
352 void VID_UpdateWindowStatus (void)
355 window_rect.left = window_x;
356 window_rect.top = window_y;
357 window_rect.right = window_x + window_width;
358 window_rect.bottom = window_y + window_height;
359 window_center_x = (window_rect.left + window_rect.right) / 2;
360 window_center_y = (window_rect.top + window_rect.bottom) / 2;
362 IN_UpdateClipCursor ();
366 //====================================
374 void GL_BeginRendering (int *x, int *y, int *width, int *height)
377 *width = WindowRect.right - WindowRect.left;
378 *height = WindowRect.bottom - WindowRect.top;
380 // if (!wglMakeCurrent( maindc, baseRC ))
381 // Sys_Error ("wglMakeCurrent failed");
383 // glViewport (*x, *y, *width, *height);
387 void GL_EndRendering (void)
390 if (r_render.value && !scr_skipupdate)
393 // handle the mouse state when windowed if that's changed
395 if (vid_mouse.value && key_dest == key_game)
397 if (modestate == MS_FULLDIB)
415 IN_DeactivateMouse ();
421 void VID_SetDefaultMode (void)
423 IN_DeactivateMouse ();
426 void VID_RestoreSystemGamma(void);
428 void VID_Shutdown (void)
437 vid_canalttab = false;
438 hRC = wglGetCurrentContext();
439 hDC = wglGetCurrentDC();
441 wglMakeCurrent(NULL, NULL);
443 // LordHavoc: free textures before closing (may help NVIDIA)
444 for (i = 0;i < 8192;i++)
446 glDeleteTextures(8192, temp);
449 wglDeleteContext(hRC);
451 if (hDC && mainwindow)
452 ReleaseDC(mainwindow, hDC);
454 if (modestate == MS_FULLDIB)
455 ChangeDisplaySettings (NULL, 0);
457 if (maindc && mainwindow)
458 ReleaseDC (mainwindow, maindc);
460 AppActivate(false, false);
462 VID_RestoreSystemGamma();
467 //==========================================================================
470 BOOL bSetupPixelFormat(HDC hDC)
472 static PIXELFORMATDESCRIPTOR pfd = {
473 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
475 PFD_DRAW_TO_WINDOW // support window
476 | PFD_SUPPORT_OPENGL // support OpenGL
477 | PFD_DOUBLEBUFFER , // double buffered
478 PFD_TYPE_RGBA, // RGBA type
479 24, // 24-bit color depth
480 0, 0, 0, 0, 0, 0, // color bits ignored
481 0, // no alpha buffer
482 0, // shift bit ignored
483 0, // no accumulation buffer
484 0, 0, 0, 0, // accum bits ignored
485 32, // 32-bit z-buffer
486 0, // no stencil buffer
487 0, // no auxiliary buffer
488 PFD_MAIN_PLANE, // main layer
490 0, 0, 0 // layer masks ignored
494 if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
496 MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
500 if (SetPixelFormat(hDC, pixelformat, &pfd) == false)
502 MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
511 byte scantokey[128] =
513 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
514 0 ,27 ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'0' ,'-' ,'=' ,K_BACKSPACE,9 , // 0
515 'q' ,'w' ,'e' ,'r' ,'t' ,'y' ,'u' ,'i' ,'o' ,'p' ,'[' ,']' ,13 ,K_CTRL ,'a' ,'s' , // 1
516 'd' ,'f' ,'g' ,'h' ,'j' ,'k' ,'l' ,';' ,'\'' ,'`' ,K_SHIFT,'\\' ,'z' ,'x' ,'c' ,'v' , // 2
517 'b' ,'n' ,'m' ,',' ,'.' ,'/' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2 ,K_F3 ,K_F4 ,K_F5 , // 3
518 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
519 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
520 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
521 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
525 byte shiftscantokey[128] =
527 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
528 0 ,27 ,'!' ,'@' ,'#' ,'$' ,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'_' ,'+' ,K_BACKSPACE,9 , // 0
529 'Q' ,'W' ,'E' ,'R' ,'T' ,'Y' ,'U' ,'I' ,'O' ,'P' ,'{' ,'}' ,13 ,K_CTRL ,'A' ,'S' , // 1
530 'D' ,'F' ,'G' ,'H' ,'J' ,'K' ,'L' ,':' ,'"' ,'~' ,K_SHIFT,'|' ,'Z' ,'X' ,'C' ,'V' , // 2
531 'B' ,'N' ,'M' ,'<' ,'>' ,'?' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2,K_F3 ,K_F4 ,K_F5 , // 3
532 K_F6 ,K_F7 ,K_F8 ,K_F9 ,K_F10,K_PAUSE,0 ,K_HOME,K_UPARROW,K_PGUP,'_' ,K_LEFTARROW,'%' ,K_RIGHTARROW,'+' ,K_END, // 4
533 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
534 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
535 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
543 Map from windows to quake keynums
546 int MapKey (int key, int virtualkey)
551 if (scantokey[key] == 0)
552 Con_DPrintf("key 0x%02x has no translation\n", key);
553 // if (scantokey[key] >= 0x20 && scantokey[key] < 0x7F)
555 return scantokey[key];
559 ===================================================================
563 ===================================================================
571 void ClearAllStates (void)
575 // send an up event for each key, to make sure the server clears them all
576 for (i=0 ; i<256 ; i++)
578 Key_Event (i, false);
585 void VID_RestoreGameGamma(void);
586 extern qboolean hostloopactive;
588 void AppActivate(BOOL fActive, BOOL minimize)
589 /****************************************************************************
591 * Function: AppActivate
592 * Parameters: fActive - True if app is activating
594 * Description: If the application is activating, then swap the system
595 * into SYSPAL_NOSTATIC mode so that our palettes will display
598 ****************************************************************************/
600 static BOOL sound_active;
603 Minimized = minimize;
605 // enable/disable sound on focus gain/loss
606 if (!ActiveApp && sound_active)
609 sound_active = false;
611 else if (ActiveApp && !sound_active)
619 if (modestate == MS_FULLDIB)
621 // usingmouse = true;
622 // IN_ActivateMouse ();
624 if (vid_canalttab && vid_wassuspended)
626 vid_wassuspended = false;
627 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
628 ShowWindow(mainwindow, SW_SHOWNORMAL);
631 // LordHavoc: from dabb, fix for alt-tab bug in NVidia drivers
632 MoveWindow(mainwindow,0,0,gdevmode.dmPelsWidth,gdevmode.dmPelsHeight,false);
634 // else if ((modestate == MS_WINDOWED) && vid_mouse.value && key_dest == key_game)
636 // usingmouse = true;
637 // IN_ActivateMouse ();
641 VID_RestoreGameGamma();
647 IN_DeactivateMouse ();
649 if (modestate == MS_FULLDIB)
651 // usingmouse = false;
652 // IN_DeactivateMouse ();
656 ChangeDisplaySettings (NULL, 0);
657 vid_wassuspended = true;
660 // else if ((modestate == MS_WINDOWED) && vid_mouse.value)
662 // usingmouse = false;
663 // IN_DeactivateMouse ();
666 VID_RestoreSystemGamma();
670 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
672 /* main window procedure */
673 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
676 int fActive, fMinimized, temp;
677 extern unsigned int uiWheelMessage;
679 if ( uMsg == uiWheelMessage )
680 uMsg = WM_MOUSEWHEEL;
685 if (modestate == MS_FULLDIB)
686 ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
693 window_x = (int) LOWORD(lParam);
694 window_y = (int) HIWORD(lParam);
695 VID_UpdateWindowStatus ();
700 Key_Event (MapKey(lParam, wParam), true);
705 Key_Event (MapKey(lParam, wParam), false);
709 // keep Alt-Space from happening
712 // this is complicated because Win32 seems to pack multiple mouse events into
713 // one update sometimes, so we always check all states and look for events
723 if (wParam & MK_LBUTTON)
726 if (wParam & MK_RBUTTON)
729 if (wParam & MK_MBUTTON)
732 IN_MouseEvent (temp);
736 // JACK: This is the mouse wheel with the Intellimouse
737 // Its delta is either positive or neg, and we generate the proper
740 if ((short) HIWORD(wParam) > 0) {
741 Key_Event(K_MWHEELUP, true);
742 Key_Event(K_MWHEELUP, false);
744 Key_Event(K_MWHEELDOWN, true);
745 Key_Event(K_MWHEELDOWN, false);
753 if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
759 fActive = LOWORD(wParam);
760 fMinimized = (BOOL) HIWORD(wParam);
761 AppActivate(!(fActive == WA_INACTIVE), fMinimized);
763 // fix the leftover Alt from any Alt-Tab or the like that switched us away
771 DestroyWindow (mainwindow);
778 lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
782 /* pass all unhandled messages to DefWindowProc */
783 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
787 /* return 1 if handled message, 0 if not */
797 int VID_NumModes (void)
808 vmode_t *VID_GetModePtr (int modenum)
811 if ((modenum >= 0) && (modenum < nummodes))
812 return &modelist[modenum];
820 VID_GetModeDescription
823 char *VID_GetModeDescription (int mode)
827 static char temp[100];
829 if ((mode < 0) || (mode >= nummodes))
832 if (!leavecurrentmode)
834 pv = VID_GetModePtr (mode);
835 pinfo = pv->modedesc;
839 sprintf (temp, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
847 // KJB: Added this to return the mode driver name in description for console
849 char *VID_GetExtModeDescription (int mode)
851 static char pinfo[40];
854 if ((mode < 0) || (mode >= nummodes))
857 pv = VID_GetModePtr (mode);
858 if (modelist[mode].type == MS_FULLDIB)
860 if (!leavecurrentmode)
861 sprintf(pinfo,"%s fullscreen", pv->modedesc);
863 sprintf (pinfo, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
867 if (modestate == MS_WINDOWED)
868 sprintf(pinfo, "%s windowed", pv->modedesc);
870 sprintf(pinfo, "windowed");
879 VID_DescribeCurrentMode_f
882 void VID_DescribeCurrentMode_f (void)
884 Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum));
893 void VID_NumModes_f (void)
897 Con_Printf ("%d video mode is available\n", nummodes);
899 Con_Printf ("%d video modes are available\n", nummodes);
908 void VID_DescribeMode_f (void)
912 modenum = atoi (Cmd_Argv(1));
914 t = leavecurrentmode;
915 leavecurrentmode = 0;
917 Con_Printf ("%s\n", VID_GetExtModeDescription (modenum));
919 leavecurrentmode = t;
928 void VID_DescribeModes_f (void)
934 lnummodes = VID_NumModes ();
936 t = leavecurrentmode;
937 leavecurrentmode = 0;
939 for (i=1 ; i<lnummodes ; i++)
941 pv = VID_GetModePtr (i);
942 pinfo = VID_GetExtModeDescription (i);
943 Con_Printf ("%2d: %s\n", i, pinfo);
946 leavecurrentmode = t;
949 void VID_AddMode(int type, int width, int height, int modenum, int halfscreen, int dib, int fullscreen, int bpp)
952 if (nummodes >= MAX_MODE_LIST)
954 modelist[nummodes].type = type;
955 modelist[nummodes].width = width;
956 modelist[nummodes].height = height;
957 modelist[nummodes].modenum = modenum;
958 modelist[nummodes].halfscreen = halfscreen;
959 modelist[nummodes].dib = dib;
960 modelist[nummodes].fullscreen = fullscreen;
961 modelist[nummodes].bpp = bpp;
963 sprintf (modelist[nummodes].modedesc, "%dx%d", width, height);
965 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", width, height, bpp);
966 for (i = 0;i < nummodes;i++)
968 if (!memcmp(&modelist[i], &modelist[nummodes], sizeof(vmode_t)))
974 void VID_InitDIB (HINSTANCE hInstance)
979 // Register the frame class
981 wc.lpfnWndProc = (WNDPROC)MainWndProc;
984 wc.hInstance = hInstance;
986 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
987 wc.hbrBackground = NULL;
989 wc.lpszClassName = gamename;
991 if (!RegisterClass (&wc) )
992 Sys_Error ("Couldn't register window class");
995 modelist[0].type = MS_WINDOWED;
997 if (COM_CheckParm("-width"))
998 modelist[0].width = atoi(com_argv[COM_CheckParm("-width")+1]);
1000 modelist[0].width = 640;
1002 if (modelist[0].width < 320)
1003 modelist[0].width = 320;
1005 if (COM_CheckParm("-height"))
1006 modelist[0].height= atoi(com_argv[COM_CheckParm("-height")+1]);
1008 modelist[0].height = modelist[0].width * 240/320;
1010 if (modelist[0].height < 240)
1011 modelist[0].height = 240;
1013 sprintf (modelist[0].modedesc, "%dx%d", modelist[0].width, modelist[0].height);
1015 modelist[0].modenum = MODE_WINDOWED;
1016 modelist[0].dib = 1;
1017 modelist[0].fullscreen = 0;
1018 modelist[0].halfscreen = 0;
1019 modelist[0].bpp = 0;
1023 if (COM_CheckParm("-width"))
1024 w = atoi(com_argv[COM_CheckParm("-width")+1]);
1031 if (COM_CheckParm("-height"))
1032 h = atoi(com_argv[COM_CheckParm("-height")+1]);
1039 VID_AddMode(MS_WINDOWED, w, h, 0, 0, 1, 0, 0);
1048 void VID_InitFullDIB (HINSTANCE hInstance)
1053 int originalnummodes;
1054 // int existingmode;
1061 // enumerate >8 bpp modes
1062 originalnummodes = nummodes;
1067 stat = EnumDisplaySettings (NULL, modenum, &devmode);
1069 if ((devmode.dmBitsPerPel >= 15) && (devmode.dmPelsWidth <= MAXWIDTH) && (devmode.dmPelsHeight <= MAXHEIGHT) && (nummodes < MAX_MODE_LIST))
1071 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1073 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1075 // if the width is more than twice the height, reduce it by half because this
1076 // is probably a dual-screen monitor
1077 if ((!COM_CheckParm("-noadjustaspect")) && (devmode.dmPelsWidth > (devmode.dmPelsHeight << 1)))
1078 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth >> 1, devmode.dmPelsHeight, 0, 1, 1, 1, devmode.dmBitsPerPel);
1080 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1082 modelist[nummodes].type = MS_FULLDIB;
1083 modelist[nummodes].width = devmode.dmPelsWidth;
1084 modelist[nummodes].height = devmode.dmPelsHeight;
1085 modelist[nummodes].modenum = 0;
1086 modelist[nummodes].halfscreen = 0;
1087 modelist[nummodes].dib = 1;
1088 modelist[nummodes].fullscreen = 1;
1089 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1090 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1092 // if the width is more than twice the height, reduce it by half because this
1093 // is probably a dual-screen monitor
1094 if (!COM_CheckParm("-noadjustaspect"))
1096 if (modelist[nummodes].width > (modelist[nummodes].height << 1))
1098 modelist[nummodes].width >>= 1;
1099 modelist[nummodes].halfscreen = 1;
1100 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", modelist[nummodes].width, modelist[nummodes].height, modelist[nummodes].bpp);
1104 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1106 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1123 // see if there are any low-res modes that aren't being reported
1124 numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]);
1130 for (j=0 ; (j<numlowresmodes) && (nummodes < MAX_MODE_LIST) ; j++)
1132 devmode.dmBitsPerPel = bpp;
1133 devmode.dmPelsWidth = lowresmodes[j].width;
1134 devmode.dmPelsHeight = lowresmodes[j].height;
1135 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1137 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1138 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1141 modelist[nummodes].type = MS_FULLDIB;
1142 modelist[nummodes].width = devmode.dmPelsWidth;
1143 modelist[nummodes].height = devmode.dmPelsHeight;
1144 modelist[nummodes].modenum = 0;
1145 modelist[nummodes].halfscreen = 0;
1146 modelist[nummodes].dib = 1;
1147 modelist[nummodes].fullscreen = 1;
1148 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1149 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1151 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1153 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1182 if (nummodes == originalnummodes)
1183 Con_SafePrintf ("No fullscreen DIB modes found\n");
1186 static int grabsysgamma = true;
1187 WORD systemgammaramps[3][256], currentgammaramps[3][256];
1189 int VID_SetGamma(float prescale, float gamma, float scale, float base)
1195 BuildGammaTable16(prescale, gamma, scale, base, ¤tgammaramps[0][0]);
1196 for (i = 0;i < 256;i++)
1197 currentgammaramps[1][i] = currentgammaramps[2][i] = currentgammaramps[0][i];
1199 i = SetDeviceGammaRamp(hdc, ¤tgammaramps[0][0]);
1201 ReleaseDC (NULL, hdc);
1202 return i; // return success or failure
1205 void VID_RestoreGameGamma(void)
1207 VID_UpdateGamma(true);
1210 void VID_GetSystemGamma(void)
1215 GetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1217 ReleaseDC (NULL, hdc);
1220 void VID_RestoreSystemGamma(void)
1225 SetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1227 ReleaseDC (NULL, hdc);
1235 void VID_Init (void)
1238 // int existingmode;
1239 int basenummodes, width, height, bpp, findbpp, done;
1243 memset(&devmode, 0, sizeof(devmode));
1245 // Cvar_RegisterVariable (&_vid_default_mode);
1246 // Cvar_RegisterVariable (&_vid_default_mode_win);
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);
1264 if (COM_CheckParm("-window"))
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");
1284 if (COM_CheckParm("-mode"))
1285 vid_default = atoi(com_argv[COM_CheckParm("-mode")+1]);
1288 if (COM_CheckParm("-current"))
1290 modelist[MODE_FULLSCREEN_DEFAULT].width = GetSystemMetrics (SM_CXSCREEN);
1291 modelist[MODE_FULLSCREEN_DEFAULT].height = GetSystemMetrics (SM_CYSCREEN);
1292 vid_default = MODE_FULLSCREEN_DEFAULT;
1293 leavecurrentmode = 1;
1297 if (COM_CheckParm("-width"))
1298 width = atoi(com_argv[COM_CheckParm("-width")+1]);
1302 if (COM_CheckParm("-bpp"))
1304 bpp = atoi(com_argv[COM_CheckParm("-bpp")+1]);
1313 if (COM_CheckParm("-height"))
1314 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1316 // if they want to force it, add the specified mode to the list
1317 if (COM_CheckParm("-force") && (nummodes < MAX_MODE_LIST))
1318 VID_AddMode(MS_FULLDIB, width, height, 0, 0, 1, 1, bpp);
1321 modelist[nummodes].type = MS_FULLDIB;
1322 modelist[nummodes].width = width;
1323 modelist[nummodes].height = height;
1324 modelist[nummodes].modenum = 0;
1325 modelist[nummodes].halfscreen = 0;
1326 modelist[nummodes].dib = 1;
1327 modelist[nummodes].fullscreen = 1;
1328 modelist[nummodes].bpp = bpp;
1329 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1331 for (i=nummodes, existingmode = 0 ; i<nummodes ; i++)
1333 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1349 if (COM_CheckParm("-height"))
1351 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1353 for (i=1, vid_default=0 ; i<nummodes ; i++)
1355 if ((modelist[i].width == width) && (modelist[i].height == height) && (modelist[i].bpp == bpp))
1365 for (i=1, vid_default=0 ; i<nummodes ; i++)
1367 if ((modelist[i].width == width) && (modelist[i].bpp == bpp))
1382 case 15: bpp = 16;break;
1383 case 16: bpp = 32;break;
1384 case 32: bpp = 24;break;
1385 case 24: done = 1;break;
1395 Sys_Error ("Specified video mode not available");
1400 vid_initialized = true;
1402 if ((i = COM_CheckParm("-conwidth")) != 0)
1403 vid.conwidth = atoi(com_argv[i+1]);
1407 vid.conwidth &= 0xfff8; // make it a multiple of eight
1409 if (vid.conwidth < 320)
1412 // pick a conheight that matches with correct aspect
1413 vid.conheight = vid.conwidth*3 / 4;
1415 if ((i = COM_CheckParm("-conheight")) != 0)
1416 vid.conheight = atoi(com_argv[i+1]);
1417 if (vid.conheight < 200)
1418 vid.conheight = 200;
1420 VID_SetMode (vid_default);
1422 maindc = GetDC(mainwindow);
1423 bSetupPixelFormat(maindc);
1425 baseRC = wglCreateContext( maindc );
1427 Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.");
1428 if (!wglMakeCurrent( maindc, baseRC ))
1429 Sys_Error ("wglMakeCurrent failed");
1433 // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
1434 if (strncasecmp(gl_vendor,"ATI",3)==0)
1436 if (strncasecmp(gl_renderer,"Rage Pro",8)==0)
1439 if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
1442 // sprintf (gldir, "%s/glquake", com_gamedir);
1443 // Sys_mkdir (gldir);
1445 vid_realmode = vid_modenum;
1447 vid_menudrawfn = VID_MenuDraw;
1448 vid_menukeyfn = VID_MenuKey;
1450 strcpy (badmode.modedesc, "Bad mode");
1451 vid_canalttab = true;
1455 //========================================================
1457 //========================================================
1459 extern void M_Menu_Options_f (void);
1460 extern void M_Print (int cx, int cy, char *str);
1461 extern void M_PrintWhite (int cx, int cy, char *str);
1462 extern void M_DrawCharacter (int cx, int line, int num);
1463 extern void M_DrawPic (int x, int y, qpic_t *pic);
1465 static int vid_line, vid_wmodes;
1474 #define MAX_COLUMN_SIZE 9
1475 #define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 2)
1476 #define MAX_MODEDESCS (MAX_COLUMN_SIZE*3)
1478 static modedesc_t modedescs[MAX_MODEDESCS];
1485 void VID_MenuDraw (void)
1489 int lnummodes, i, k, column, row;
1492 p = Draw_CachePic ("gfx/vidmodes.lmp");
1493 M_DrawPic ( (320-p->width)/2, 4, p);
1496 lnummodes = VID_NumModes ();
1498 for (i=1 ; (i<lnummodes) && (vid_wmodes < MAX_MODEDESCS) ; i++)
1500 ptr = VID_GetModeDescription (i);
1501 pv = VID_GetModePtr (i);
1505 modedescs[k].modenum = i;
1506 modedescs[k].desc = ptr;
1507 modedescs[k].iscur = 0;
1509 if (i == vid_modenum)
1510 modedescs[k].iscur = 1;
1518 M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)");
1523 for (i=0 ; i<vid_wmodes ; i++)
1525 if (modedescs[i].iscur)
1526 M_PrintWhite (column, row, modedescs[i].desc);
1528 M_Print (column, row, modedescs[i].desc);
1532 if ((i % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
1540 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*2, "Video modes must be set from the");
1541 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3, "command line with -width <width>");
1542 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4, "and -bpp <bits-per-pixel>");
1543 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6, "Select windowed mode with -window");
1552 void VID_MenuKey (int key)
1557 S_LocalSound ("misc/menu1.wav");
1558 M_Menu_Options_f ();