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];
96 HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow);
98 viddef_t vid; // global video state
100 float gldepthmin, gldepthmax;
102 modestate_t modestate = MS_UNINIT;
104 void VID_MenuDraw (void);
105 void VID_MenuKey (int key);
107 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
108 void AppActivate(BOOL fActive, BOOL minimize);
109 char *VID_GetModeDescription (int mode);
110 void ClearAllStates (void);
111 void VID_UpdateWindowStatus (void);
113 //====================================
115 // Note that 0 is MODE_WINDOWED
116 //cvar_t _vid_default_mode = {"_vid_default_mode","0", true};
117 // Note that 3 is MODE_FULLSCREEN_DEFAULT
118 //cvar_t _vid_default_mode_win = {"_vid_default_mode_win","3", true};
120 int window_center_x, window_center_y, window_x, window_y, window_width, window_height;
123 // direct draw software compatability stuff
125 void CenterWindow(HWND hWndCenter, int width, int height, BOOL lefttopjustify)
127 int CenterX, CenterY;
129 CenterX = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
130 CenterY = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
131 if (CenterX > CenterY*2)
132 CenterX >>= 1; // dual screens
133 CenterX = (CenterX < 0) ? 0: CenterX;
134 CenterY = (CenterY < 0) ? 0: CenterY;
135 SetWindowPos (hWndCenter, NULL, CenterX, CenterY, 0, 0,
136 SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
139 qboolean VID_SetWindowedMode (int modenum)
141 int lastmodestate, width, height;
144 lastmodestate = modestate;
146 WindowRect.top = WindowRect.left = 0;
148 WindowRect.right = modelist[modenum].width;
149 WindowRect.bottom = modelist[modenum].height;
151 DIBWidth = modelist[modenum].width;
152 DIBHeight = modelist[modenum].height;
154 WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
158 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
160 width = rect.right - rect.left;
161 height = rect.bottom - rect.top;
163 // Create the DIB window
164 mainwindow = CreateWindowEx (ExWindowStyle, gamename, gamename, WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
167 Sys_Error ("Couldn't create DIB window");
169 // Center and show the DIB window
170 CenterWindow(mainwindow, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top, false);
172 ShowWindow (mainwindow, SW_SHOWDEFAULT);
173 UpdateWindow (mainwindow);
175 modestate = MS_WINDOWED;
177 if (vid.conheight > modelist[modenum].height)
178 vid.conheight = modelist[modenum].height;
179 if (vid.conwidth > modelist[modenum].width)
180 vid.conwidth = modelist[modenum].width;
182 SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
183 SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
189 qboolean VID_SetFullDIBMode (int modenum)
191 int lastmodestate, width, height;
194 if (!leavecurrentmode)
196 gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
197 gdevmode.dmBitsPerPel = modelist[modenum].bpp;
198 gdevmode.dmPelsWidth = modelist[modenum].width <<
199 modelist[modenum].halfscreen;
200 gdevmode.dmPelsHeight = modelist[modenum].height;
201 gdevmode.dmSize = sizeof (gdevmode);
203 if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
204 Sys_Error ("Couldn't set fullscreen DIB mode");
207 lastmodestate = modestate;
208 modestate = MS_FULLDIB;
210 WindowRect.top = WindowRect.left = 0;
212 WindowRect.right = modelist[modenum].width;
213 WindowRect.bottom = modelist[modenum].height;
215 DIBWidth = modelist[modenum].width;
216 DIBHeight = modelist[modenum].height;
218 WindowStyle = WS_POPUP;
222 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
224 width = rect.right - rect.left;
225 height = rect.bottom - rect.top;
227 // Create the DIB window
228 mainwindow = CreateWindowEx (ExWindowStyle, gamename, gamename, WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
231 Sys_Error ("Couldn't create DIB window");
233 ShowWindow (mainwindow, SW_SHOWDEFAULT);
234 UpdateWindow (mainwindow);
236 if (vid.conheight > modelist[modenum].height)
237 vid.conheight = modelist[modenum].height;
238 if (vid.conwidth > modelist[modenum].width)
239 vid.conwidth = modelist[modenum].width;
241 // needed because we're not getting WM_MOVE messages fullscreen on NT
245 SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
246 SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
252 int VID_SetMode (int modenum)
254 int original_mode, temp;
258 if ((windowed && (modenum != 0)) || (!windowed && (modenum < 1)) || (!windowed && (modenum >= nummodes)))
259 Sys_Error ("Bad video mode\n");
261 // so Con_Printfs don't mess us up by forcing vid and snd updates
262 temp = scr_disabled_for_loading;
263 scr_disabled_for_loading = true;
267 if (vid_modenum == NO_MODE)
268 original_mode = windowed_default;
270 original_mode = vid_modenum;
272 // Set either the fullscreen or windowed mode
273 if (modelist[modenum].type == MS_WINDOWED)
275 // if (vid_mouse.integer && key_dest == key_game)
277 // stat = VID_SetWindowedMode(modenum);
278 // usingmouse = true;
279 // IN_ActivateMouse ();
284 // usingmouse = false;
285 // IN_DeactivateMouse ();
287 // stat = VID_SetWindowedMode(modenum);
289 stat = VID_SetWindowedMode(modenum);
291 else if (modelist[modenum].type == MS_FULLDIB)
293 stat = VID_SetFullDIBMode(modenum);
294 // usingmouse = true;
295 // IN_ActivateMouse ();
299 Sys_Error ("VID_SetMode: Bad mode type in modelist");
301 window_width = DIBWidth;
302 window_height = DIBHeight;
303 VID_UpdateWindowStatus ();
306 scr_disabled_for_loading = temp;
309 Sys_Error ("Couldn't set video mode");
311 // now we try to make sure we get the focus on the mode switch, because
312 // sometimes in some systems we don't. We grab the foreground, then
313 // finish setting up, pump all our messages, and sleep for a little while
314 // to let messages finish bouncing around the system, then we put
315 // ourselves at the top of the z order, then grab the foreground again,
316 // Who knows if it helps, but it probably doesn't hurt
317 SetForegroundWindow (mainwindow);
318 vid_modenum = modenum;
319 Cvar_SetValue ("vid_mode", (float)vid_modenum);
321 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
323 TranslateMessage (&msg);
324 DispatchMessage (&msg);
329 SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS);
331 SetForegroundWindow (mainwindow);
333 // fix the leftover Alt from any Alt-Tab or the like that switched us away
337 Con_SafePrintf ("Video mode %s initialized.\n", VID_GetModeDescription (vid_modenum));
339 // vid.recalc_refdef = 1;
347 VID_UpdateWindowStatus
350 void VID_UpdateWindowStatus (void)
353 window_rect.left = window_x;
354 window_rect.top = window_y;
355 window_rect.right = window_x + window_width;
356 window_rect.bottom = window_y + window_height;
357 window_center_x = (window_rect.left + window_rect.right) / 2;
358 window_center_y = (window_rect.top + window_rect.bottom) / 2;
360 IN_UpdateClipCursor ();
364 //====================================
371 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
374 *width = WindowRect.right - WindowRect.left;
375 *height = WindowRect.bottom - WindowRect.top;
379 void VID_Finish (void)
382 if (r_render.integer && !scr_skipupdate)
388 // handle the mouse state when windowed if that's changed
390 if (vid_mouse.integer && key_dest == key_game)
392 if (modestate == MS_FULLDIB)
410 IN_DeactivateMouse ();
416 void VID_SetDefaultMode (void)
418 IN_DeactivateMouse ();
421 void VID_RestoreSystemGamma(void);
423 void VID_Shutdown (void)
432 vid_canalttab = false;
433 hRC = wglGetCurrentContext();
434 hDC = wglGetCurrentDC();
436 wglMakeCurrent(NULL, NULL);
438 // LordHavoc: free textures before closing (may help NVIDIA)
439 for (i = 0;i < 8192;i++)
441 glDeleteTextures(8192, temp);
444 wglDeleteContext(hRC);
446 if (hDC && mainwindow)
447 ReleaseDC(mainwindow, hDC);
449 if (modestate == MS_FULLDIB)
450 ChangeDisplaySettings (NULL, 0);
452 if (maindc && mainwindow)
453 ReleaseDC (mainwindow, maindc);
455 AppActivate(false, false);
457 VID_RestoreSystemGamma();
462 //==========================================================================
465 BOOL bSetupPixelFormat(HDC hDC)
467 static PIXELFORMATDESCRIPTOR pfd = {
468 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
470 PFD_DRAW_TO_WINDOW // support window
471 | PFD_SUPPORT_OPENGL // support OpenGL
472 | PFD_DOUBLEBUFFER , // double buffered
473 PFD_TYPE_RGBA, // RGBA type
474 24, // 24-bit color depth
475 0, 0, 0, 0, 0, 0, // color bits ignored
476 0, // no alpha buffer
477 0, // shift bit ignored
478 0, // no accumulation buffer
479 0, 0, 0, 0, // accum bits ignored
480 32, // 32-bit z-buffer
481 0, // no stencil buffer
482 0, // no auxiliary buffer
483 PFD_MAIN_PLANE, // main layer
485 0, 0, 0 // layer masks ignored
489 if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
491 MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
495 if (SetPixelFormat(hDC, pixelformat, &pfd) == false)
497 MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
506 qbyte scantokey[128] =
508 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
509 0 ,27 ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'0' ,'-' ,'=' ,K_BACKSPACE,9 , // 0
510 'q' ,'w' ,'e' ,'r' ,'t' ,'y' ,'u' ,'i' ,'o' ,'p' ,'[' ,']' ,13 ,K_CTRL ,'a' ,'s' , // 1
511 'd' ,'f' ,'g' ,'h' ,'j' ,'k' ,'l' ,';' ,'\'' ,'`' ,K_SHIFT,'\\' ,'z' ,'x' ,'c' ,'v' , // 2
512 'b' ,'n' ,'m' ,',' ,'.' ,'/' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2 ,K_F3 ,K_F4 ,K_F5 , // 3
513 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
514 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
515 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
516 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
520 qbyte shiftscantokey[128] =
522 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
523 0 ,27 ,'!' ,'@' ,'#' ,'$' ,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'_' ,'+' ,K_BACKSPACE,9 , // 0
524 'Q' ,'W' ,'E' ,'R' ,'T' ,'Y' ,'U' ,'I' ,'O' ,'P' ,'{' ,'}' ,13 ,K_CTRL ,'A' ,'S' , // 1
525 'D' ,'F' ,'G' ,'H' ,'J' ,'K' ,'L' ,':' ,'"' ,'~' ,K_SHIFT,'|' ,'Z' ,'X' ,'C' ,'V' , // 2
526 'B' ,'N' ,'M' ,'<' ,'>' ,'?' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2,K_F3 ,K_F4 ,K_F5 , // 3
527 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
528 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
529 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
530 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
538 Map from windows to quake keynums
541 int MapKey (int key, int virtualkey)
546 if (scantokey[key] == 0)
547 Con_DPrintf("key 0x%02x has no translation\n", key);
548 // if (scantokey[key] >= 0x20 && scantokey[key] < 0x7F)
550 return scantokey[key];
554 ===================================================================
558 ===================================================================
566 void ClearAllStates (void)
570 // send an up event for each key, to make sure the server clears them all
571 for (i=0 ; i<256 ; i++)
573 Key_Event (i, false);
580 void VID_RestoreGameGamma(void);
581 extern qboolean host_loopactive;
583 void AppActivate(BOOL fActive, BOOL minimize)
584 /****************************************************************************
586 * Function: AppActivate
587 * Parameters: fActive - True if app is activating
589 * Description: If the application is activating, then swap the system
590 * into SYSPAL_NOSTATIC mode so that our palettes will display
593 ****************************************************************************/
595 static BOOL sound_active;
598 Minimized = minimize;
600 // enable/disable sound on focus gain/loss
601 if (!ActiveApp && sound_active)
604 sound_active = false;
606 else if (ActiveApp && !sound_active)
614 if (modestate == MS_FULLDIB)
616 // usingmouse = true;
617 // IN_ActivateMouse ();
619 if (vid_canalttab && vid_wassuspended)
621 vid_wassuspended = false;
622 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
623 ShowWindow(mainwindow, SW_SHOWNORMAL);
626 // LordHavoc: from dabb, fix for alt-tab bug in NVidia drivers
627 MoveWindow(mainwindow,0,0,gdevmode.dmPelsWidth,gdevmode.dmPelsHeight,false);
629 // else if ((modestate == MS_WINDOWED) && vid_mouse.integer && key_dest == key_game)
631 // usingmouse = true;
632 // IN_ActivateMouse ();
636 VID_RestoreGameGamma();
642 IN_DeactivateMouse ();
644 if (modestate == MS_FULLDIB)
646 // usingmouse = false;
647 // IN_DeactivateMouse ();
651 ChangeDisplaySettings (NULL, 0);
652 vid_wassuspended = true;
655 // else if ((modestate == MS_WINDOWED) && vid_mouse.integer)
657 // usingmouse = false;
658 // IN_DeactivateMouse ();
661 VID_RestoreSystemGamma();
665 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
667 /* main window procedure */
668 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
671 int fActive, fMinimized, temp;
672 extern unsigned int uiWheelMessage;
674 if ( uMsg == uiWheelMessage )
675 uMsg = WM_MOUSEWHEEL;
680 if (modestate == MS_FULLDIB)
681 ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
688 window_x = (int) LOWORD(lParam);
689 window_y = (int) HIWORD(lParam);
690 VID_UpdateWindowStatus ();
695 Key_Event (MapKey(lParam, wParam), true);
700 Key_Event (MapKey(lParam, wParam), false);
704 // keep Alt-Space from happening
707 // this is complicated because Win32 seems to pack multiple mouse events into
708 // one update sometimes, so we always check all states and look for events
718 if (wParam & MK_LBUTTON)
721 if (wParam & MK_RBUTTON)
724 if (wParam & MK_MBUTTON)
727 IN_MouseEvent (temp);
731 // JACK: This is the mouse wheel with the Intellimouse
732 // Its delta is either positive or neg, and we generate the proper
735 if ((short) HIWORD(wParam) > 0) {
736 Key_Event(K_MWHEELUP, true);
737 Key_Event(K_MWHEELUP, false);
739 Key_Event(K_MWHEELDOWN, true);
740 Key_Event(K_MWHEELDOWN, false);
748 if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
754 fActive = LOWORD(wParam);
755 fMinimized = (BOOL) HIWORD(wParam);
756 AppActivate(!(fActive == WA_INACTIVE), fMinimized);
758 // fix the leftover Alt from any Alt-Tab or the like that switched us away
766 DestroyWindow (mainwindow);
773 lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
777 /* pass all unhandled messages to DefWindowProc */
778 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
782 /* return 1 if handled message, 0 if not */
792 int VID_NumModes (void)
803 vmode_t *VID_GetModePtr (int modenum)
806 if ((modenum >= 0) && (modenum < nummodes))
807 return &modelist[modenum];
815 VID_GetModeDescription
818 char *VID_GetModeDescription (int mode)
822 static char temp[100];
824 if ((mode < 0) || (mode >= nummodes))
827 if (!leavecurrentmode)
829 pv = VID_GetModePtr (mode);
830 pinfo = pv->modedesc;
834 sprintf (temp, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
842 // KJB: Added this to return the mode driver name in description for console
844 char *VID_GetExtModeDescription (int mode)
846 static char pinfo[40];
849 if ((mode < 0) || (mode >= nummodes))
852 pv = VID_GetModePtr (mode);
853 if (modelist[mode].type == MS_FULLDIB)
855 if (!leavecurrentmode)
856 sprintf(pinfo,"%s fullscreen", pv->modedesc);
858 sprintf (pinfo, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
862 if (modestate == MS_WINDOWED)
863 sprintf(pinfo, "%s windowed", pv->modedesc);
865 sprintf(pinfo, "windowed");
874 VID_DescribeCurrentMode_f
877 void VID_DescribeCurrentMode_f (void)
879 Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum));
888 void VID_NumModes_f (void)
892 Con_Printf ("%d video mode is available\n", nummodes);
894 Con_Printf ("%d video modes are available\n", nummodes);
903 void VID_DescribeMode_f (void)
907 modenum = atoi (Cmd_Argv(1));
909 t = leavecurrentmode;
910 leavecurrentmode = 0;
912 Con_Printf ("%s\n", VID_GetExtModeDescription (modenum));
914 leavecurrentmode = t;
923 void VID_DescribeModes_f (void)
929 lnummodes = VID_NumModes ();
931 t = leavecurrentmode;
932 leavecurrentmode = 0;
934 for (i=1 ; i<lnummodes ; i++)
936 pv = VID_GetModePtr (i);
937 pinfo = VID_GetExtModeDescription (i);
938 Con_Printf ("%2d: %s\n", i, pinfo);
941 leavecurrentmode = t;
944 void VID_AddMode(int type, int width, int height, int modenum, int halfscreen, int dib, int fullscreen, int bpp)
947 if (nummodes >= MAX_MODE_LIST)
949 modelist[nummodes].type = type;
950 modelist[nummodes].width = width;
951 modelist[nummodes].height = height;
952 modelist[nummodes].modenum = modenum;
953 modelist[nummodes].halfscreen = halfscreen;
954 modelist[nummodes].dib = dib;
955 modelist[nummodes].fullscreen = fullscreen;
956 modelist[nummodes].bpp = bpp;
958 sprintf (modelist[nummodes].modedesc, "%dx%d", width, height);
960 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", width, height, bpp);
961 for (i = 0;i < nummodes;i++)
963 if (!memcmp(&modelist[i], &modelist[nummodes], sizeof(vmode_t)))
969 void VID_InitDIB (HINSTANCE hInstance)
974 // Register the frame class
976 wc.lpfnWndProc = (WNDPROC)MainWndProc;
979 wc.hInstance = hInstance;
981 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
982 wc.hbrBackground = NULL;
984 wc.lpszClassName = gamename;
986 if (!RegisterClass (&wc) )
987 Sys_Error ("Couldn't register window class");
990 modelist[0].type = MS_WINDOWED;
992 if (COM_CheckParm("-width"))
993 modelist[0].width = atoi(com_argv[COM_CheckParm("-width")+1]);
995 modelist[0].width = 640;
997 if (modelist[0].width < 320)
998 modelist[0].width = 320;
1000 if (COM_CheckParm("-height"))
1001 modelist[0].height= atoi(com_argv[COM_CheckParm("-height")+1]);
1003 modelist[0].height = modelist[0].width * 240/320;
1005 if (modelist[0].height < 240)
1006 modelist[0].height = 240;
1008 sprintf (modelist[0].modedesc, "%dx%d", modelist[0].width, modelist[0].height);
1010 modelist[0].modenum = MODE_WINDOWED;
1011 modelist[0].dib = 1;
1012 modelist[0].fullscreen = 0;
1013 modelist[0].halfscreen = 0;
1014 modelist[0].bpp = 0;
1018 if (COM_CheckParm("-width"))
1019 w = atoi(com_argv[COM_CheckParm("-width")+1]);
1026 if (COM_CheckParm("-height"))
1027 h = atoi(com_argv[COM_CheckParm("-height")+1]);
1034 VID_AddMode(MS_WINDOWED, w, h, 0, 0, 1, 0, 0);
1043 void VID_InitFullDIB (HINSTANCE hInstance)
1048 int originalnummodes;
1049 // int existingmode;
1056 // enumerate >8 bpp modes
1057 originalnummodes = nummodes;
1062 stat = EnumDisplaySettings (NULL, modenum, &devmode);
1064 if ((devmode.dmBitsPerPel >= 15) && (devmode.dmPelsWidth <= MAXWIDTH) && (devmode.dmPelsHeight <= MAXHEIGHT) && (nummodes < MAX_MODE_LIST))
1066 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1068 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1070 // if the width is more than twice the height, reduce it by half because this
1071 // is probably a dual-screen monitor
1072 if ((!COM_CheckParm("-noadjustaspect")) && (devmode.dmPelsWidth > (devmode.dmPelsHeight << 1)))
1073 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth >> 1, devmode.dmPelsHeight, 0, 1, 1, 1, devmode.dmBitsPerPel);
1075 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1077 modelist[nummodes].type = MS_FULLDIB;
1078 modelist[nummodes].width = devmode.dmPelsWidth;
1079 modelist[nummodes].height = devmode.dmPelsHeight;
1080 modelist[nummodes].modenum = 0;
1081 modelist[nummodes].halfscreen = 0;
1082 modelist[nummodes].dib = 1;
1083 modelist[nummodes].fullscreen = 1;
1084 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1085 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1087 // if the width is more than twice the height, reduce it by half because this
1088 // is probably a dual-screen monitor
1089 if (!COM_CheckParm("-noadjustaspect"))
1091 if (modelist[nummodes].width > (modelist[nummodes].height << 1))
1093 modelist[nummodes].width >>= 1;
1094 modelist[nummodes].halfscreen = 1;
1095 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", modelist[nummodes].width, modelist[nummodes].height, modelist[nummodes].bpp);
1099 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1101 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1118 // see if there are any low-res modes that aren't being reported
1119 numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]);
1125 for (j=0 ; (j<numlowresmodes) && (nummodes < MAX_MODE_LIST) ; j++)
1127 devmode.dmBitsPerPel = bpp;
1128 devmode.dmPelsWidth = lowresmodes[j].width;
1129 devmode.dmPelsHeight = lowresmodes[j].height;
1130 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1132 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1133 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1136 modelist[nummodes].type = MS_FULLDIB;
1137 modelist[nummodes].width = devmode.dmPelsWidth;
1138 modelist[nummodes].height = devmode.dmPelsHeight;
1139 modelist[nummodes].modenum = 0;
1140 modelist[nummodes].halfscreen = 0;
1141 modelist[nummodes].dib = 1;
1142 modelist[nummodes].fullscreen = 1;
1143 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1144 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1146 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1148 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1177 if (nummodes == originalnummodes)
1178 Con_SafePrintf ("No fullscreen DIB modes found\n");
1181 //static int grabsysgamma = true;
1182 WORD systemgammaramps[3][256], currentgammaramps[3][256];
1184 int VID_SetGamma(float prescale, float gamma, float scale, float base)
1190 BuildGammaTable16(prescale, gamma, scale, base, ¤tgammaramps[0][0]);
1191 for (i = 0;i < 256;i++)
1192 currentgammaramps[1][i] = currentgammaramps[2][i] = currentgammaramps[0][i];
1194 i = SetDeviceGammaRamp(hdc, ¤tgammaramps[0][0]);
1196 ReleaseDC (NULL, hdc);
1197 return i; // return success or failure
1200 void VID_RestoreGameGamma(void)
1202 VID_UpdateGamma(true);
1205 void VID_GetSystemGamma(void)
1210 GetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1212 ReleaseDC (NULL, hdc);
1215 void VID_RestoreSystemGamma(void)
1220 SetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1222 ReleaseDC (NULL, hdc);
1230 void VID_Init (void)
1233 // int existingmode;
1234 int basenummodes, width, height = 0, bpp, findbpp, done;
1238 memset(&devmode, 0, sizeof(devmode));
1240 // Cvar_RegisterVariable (&_vid_default_mode);
1241 // Cvar_RegisterVariable (&_vid_default_mode_win);
1243 Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);
1244 Cmd_AddCommand ("vid_describecurrentmode", VID_DescribeCurrentMode_f);
1245 Cmd_AddCommand ("vid_describemode", VID_DescribeMode_f);
1246 Cmd_AddCommand ("vid_describemodes", VID_DescribeModes_f);
1248 VID_GetSystemGamma();
1250 hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON2));
1252 InitCommonControls();
1254 VID_InitDIB (global_hInstance);
1255 basenummodes = nummodes = 1;
1257 VID_InitFullDIB (global_hInstance);
1259 if (COM_CheckParm("-window"))
1263 if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
1264 Sys_Error ("Can't run in non-RGB mode");
1266 ReleaseDC (NULL, hdc);
1270 vid_default = MODE_WINDOWED;
1275 Sys_Error ("No RGB fullscreen modes available");
1279 if (COM_CheckParm("-mode"))
1280 vid_default = atoi(com_argv[COM_CheckParm("-mode")+1]);
1283 if (COM_CheckParm("-current"))
1285 modelist[MODE_FULLSCREEN_DEFAULT].width = GetSystemMetrics (SM_CXSCREEN);
1286 modelist[MODE_FULLSCREEN_DEFAULT].height = GetSystemMetrics (SM_CYSCREEN);
1287 vid_default = MODE_FULLSCREEN_DEFAULT;
1288 leavecurrentmode = 1;
1292 if (COM_CheckParm("-width"))
1293 width = atoi(com_argv[COM_CheckParm("-width")+1]);
1297 if (COM_CheckParm("-bpp"))
1299 bpp = atoi(com_argv[COM_CheckParm("-bpp")+1]);
1308 if (COM_CheckParm("-height"))
1309 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1311 // if they want to force it, add the specified mode to the list
1312 if (COM_CheckParm("-force") && (nummodes < MAX_MODE_LIST))
1313 VID_AddMode(MS_FULLDIB, width, height, 0, 0, 1, 1, bpp);
1316 modelist[nummodes].type = MS_FULLDIB;
1317 modelist[nummodes].width = width;
1318 modelist[nummodes].height = height;
1319 modelist[nummodes].modenum = 0;
1320 modelist[nummodes].halfscreen = 0;
1321 modelist[nummodes].dib = 1;
1322 modelist[nummodes].fullscreen = 1;
1323 modelist[nummodes].bpp = bpp;
1324 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1326 for (i=nummodes, existingmode = 0 ; i<nummodes ; i++)
1328 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1344 if (COM_CheckParm("-height"))
1346 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1348 for (i=1, vid_default=0 ; i<nummodes ; i++)
1350 if ((modelist[i].width == width) && (modelist[i].height == height) && (modelist[i].bpp == bpp))
1360 for (i=1, vid_default=0 ; i<nummodes ; i++)
1362 if ((modelist[i].width == width) && (modelist[i].bpp == bpp))
1377 case 15: bpp = 16;break;
1378 case 16: bpp = 32;break;
1379 case 32: bpp = 24;break;
1380 case 24: done = 1;break;
1390 Sys_Error ("Specified video mode not available");
1395 vid_initialized = true;
1397 if ((i = COM_CheckParm("-conwidth")) != 0)
1398 vid.conwidth = atoi(com_argv[i+1]);
1402 vid.conwidth &= 0xfff8; // make it a multiple of eight
1404 if (vid.conwidth < 320)
1407 // pick a conheight that matches with correct aspect
1408 vid.conheight = vid.conwidth*3 / 4;
1410 if ((i = COM_CheckParm("-conheight")) != 0)
1411 vid.conheight = atoi(com_argv[i+1]);
1412 if (vid.conheight < 200)
1413 vid.conheight = 200;
1415 VID_SetMode (vid_default);
1417 maindc = GetDC(mainwindow);
1418 bSetupPixelFormat(maindc);
1420 baseRC = wglCreateContext( maindc );
1422 Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.");
1423 if (!wglMakeCurrent( maindc, baseRC ))
1424 Sys_Error ("wglMakeCurrent failed");
1428 // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
1429 if (strncasecmp(gl_vendor,"ATI",3)==0)
1431 if (strncasecmp(gl_renderer,"Rage Pro",8)==0)
1434 if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
1437 // sprintf (gldir, "%s/glquake", com_gamedir);
1438 // Sys_mkdir (gldir);
1440 vid_realmode = vid_modenum;
1442 vid_menudrawfn = VID_MenuDraw;
1443 vid_menukeyfn = VID_MenuKey;
1445 strcpy (badmode.modedesc, "Bad mode");
1446 vid_canalttab = true;
1450 //========================================================
1452 //========================================================
1454 extern void M_Menu_Options_f (void);
1455 extern void M_Print (int cx, int cy, char *str);
1456 extern void M_PrintWhite (int cx, int cy, char *str);
1457 extern void M_DrawCharacter (int cx, int line, int num);
1458 extern void M_DrawPic (int x, int y, char *picname);
1460 static int vid_wmodes;
1469 #define MAX_COLUMN_SIZE 9
1470 #define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 2)
1471 #define MAX_MODEDESCS (MAX_COLUMN_SIZE*3)
1473 static modedesc_t modedescs[MAX_MODEDESCS];
1480 void VID_MenuDraw (void)
1484 int lnummodes, i, k, column, row;
1487 p = Draw_CachePic ("gfx/vidmodes.lmp");
1488 M_DrawPic ( (320-p->width)/2, 4, "gfx/vidmodes.lmp");
1491 lnummodes = VID_NumModes ();
1493 for (i=1 ; (i<lnummodes) && (vid_wmodes < MAX_MODEDESCS) ; i++)
1495 ptr = VID_GetModeDescription (i);
1496 pv = VID_GetModePtr (i);
1500 modedescs[k].modenum = i;
1501 modedescs[k].desc = ptr;
1502 modedescs[k].iscur = 0;
1504 if (i == vid_modenum)
1505 modedescs[k].iscur = 1;
1513 M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)");
1518 for (i=0 ; i<vid_wmodes ; i++)
1520 if (modedescs[i].iscur)
1521 M_PrintWhite (column, row, modedescs[i].desc);
1523 M_Print (column, row, modedescs[i].desc);
1527 if ((i % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
1535 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*2, "Video modes must be set from the");
1536 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3, "command line with -width <width>");
1537 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4, "and -bpp <bits-per-pixel>");
1538 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6, "Select windowed mode with -window");
1547 void VID_MenuKey (int key)
1552 S_LocalSound ("misc/menu1.wav");
1553 M_Menu_Options_f ();