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 modestate_t modestate = MS_UNINIT;
102 void VID_MenuDraw (void);
103 void VID_MenuKey (int key);
105 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
106 void AppActivate(BOOL fActive, BOOL minimize);
107 char *VID_GetModeDescription (int mode);
108 void ClearAllStates (void);
109 void VID_UpdateWindowStatus (void);
111 //====================================
113 // Note that 0 is MODE_WINDOWED
114 //cvar_t _vid_default_mode = {"_vid_default_mode","0", true};
115 // Note that 3 is MODE_FULLSCREEN_DEFAULT
116 //cvar_t _vid_default_mode_win = {"_vid_default_mode_win","3", true};
118 int window_center_x, window_center_y, window_x, window_y, window_width, window_height;
121 // direct draw software compatability stuff
123 void CenterWindow(HWND hWndCenter, int width, int height, BOOL lefttopjustify)
125 int CenterX, CenterY;
127 CenterX = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
128 CenterY = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
129 if (CenterX > CenterY*2)
130 CenterX >>= 1; // dual screens
131 CenterX = (CenterX < 0) ? 0: CenterX;
132 CenterY = (CenterY < 0) ? 0: CenterY;
133 SetWindowPos (hWndCenter, NULL, CenterX, CenterY, 0, 0,
134 SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
137 qboolean VID_SetWindowedMode (int modenum)
139 int lastmodestate, width, height;
142 lastmodestate = modestate;
144 WindowRect.top = WindowRect.left = 0;
146 WindowRect.right = modelist[modenum].width;
147 WindowRect.bottom = modelist[modenum].height;
149 DIBWidth = modelist[modenum].width;
150 DIBHeight = modelist[modenum].height;
152 WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
156 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
158 width = rect.right - rect.left;
159 height = rect.bottom - rect.top;
161 // Create the DIB window
162 mainwindow = CreateWindowEx (ExWindowStyle, gamename, gamename, WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
165 Sys_Error ("Couldn't create DIB window");
167 // Center and show the DIB window
168 CenterWindow(mainwindow, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top, false);
170 ShowWindow (mainwindow, SW_SHOWDEFAULT);
171 UpdateWindow (mainwindow);
173 modestate = MS_WINDOWED;
175 if (vid.conheight > modelist[modenum].height)
176 vid.conheight = modelist[modenum].height;
177 if (vid.conwidth > modelist[modenum].width)
178 vid.conwidth = modelist[modenum].width;
180 SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
181 SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
187 qboolean VID_SetFullDIBMode (int modenum)
189 int lastmodestate, width, height;
192 if (!leavecurrentmode)
194 gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
195 gdevmode.dmBitsPerPel = modelist[modenum].bpp;
196 gdevmode.dmPelsWidth = modelist[modenum].width <<
197 modelist[modenum].halfscreen;
198 gdevmode.dmPelsHeight = modelist[modenum].height;
199 gdevmode.dmSize = sizeof (gdevmode);
201 if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
202 Sys_Error ("Couldn't set fullscreen DIB mode");
205 lastmodestate = modestate;
206 modestate = MS_FULLDIB;
208 WindowRect.top = WindowRect.left = 0;
210 WindowRect.right = modelist[modenum].width;
211 WindowRect.bottom = modelist[modenum].height;
213 DIBWidth = modelist[modenum].width;
214 DIBHeight = modelist[modenum].height;
216 WindowStyle = WS_POPUP;
220 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
222 width = rect.right - rect.left;
223 height = rect.bottom - rect.top;
225 // Create the DIB window
226 mainwindow = CreateWindowEx (ExWindowStyle, gamename, gamename, WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
229 Sys_Error ("Couldn't create DIB window");
231 ShowWindow (mainwindow, SW_SHOWDEFAULT);
232 UpdateWindow (mainwindow);
234 if (vid.conheight > modelist[modenum].height)
235 vid.conheight = modelist[modenum].height;
236 if (vid.conwidth > modelist[modenum].width)
237 vid.conwidth = modelist[modenum].width;
239 // needed because we're not getting WM_MOVE messages fullscreen on NT
243 SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
244 SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
250 int VID_SetMode (int modenum)
257 if ((windowed && (modenum != 0)) || (!windowed && (modenum < 1)) || (!windowed && (modenum >= nummodes)))
258 Sys_Error ("Bad video mode\n");
260 // so Con_Printfs don't mess us up by forcing vid and snd updates
261 // temp = scr_disabled_for_loading;
262 // scr_disabled_for_loading = true;
266 if (vid_modenum == NO_MODE)
267 original_mode = windowed_default;
269 original_mode = vid_modenum;
271 // Set either the fullscreen or windowed mode
272 if (modelist[modenum].type == MS_WINDOWED)
274 // if (vid_mouse.integer && key_dest == key_game)
276 // stat = VID_SetWindowedMode(modenum);
277 // usingmouse = true;
278 // IN_ActivateMouse ();
283 // usingmouse = false;
284 // IN_DeactivateMouse ();
286 // stat = VID_SetWindowedMode(modenum);
288 stat = VID_SetWindowedMode(modenum);
290 else if (modelist[modenum].type == MS_FULLDIB)
292 stat = VID_SetFullDIBMode(modenum);
293 // usingmouse = true;
294 // IN_ActivateMouse ();
298 Sys_Error ("VID_SetMode: Bad mode type in modelist");
300 window_width = DIBWidth;
301 window_height = DIBHeight;
302 VID_UpdateWindowStatus ();
305 // scr_disabled_for_loading = temp;
308 Sys_Error ("Couldn't set video mode");
310 // now we try to make sure we get the focus on the mode switch, because
311 // sometimes in some systems we don't. We grab the foreground, then
312 // finish setting up, pump all our messages, and sleep for a little while
313 // to let messages finish bouncing around the system, then we put
314 // ourselves at the top of the z order, then grab the foreground again,
315 // Who knows if it helps, but it probably doesn't hurt
316 SetForegroundWindow (mainwindow);
317 vid_modenum = modenum;
318 Cvar_SetValue ("vid_mode", (float)vid_modenum);
320 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
322 TranslateMessage (&msg);
323 DispatchMessage (&msg);
328 SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS);
330 SetForegroundWindow (mainwindow);
332 // fix the leftover Alt from any Alt-Tab or the like that switched us away
336 Con_SafePrintf ("Video mode %s initialized.\n", VID_GetModeDescription (vid_modenum));
338 // vid.recalc_refdef = 1;
346 VID_UpdateWindowStatus
349 void VID_UpdateWindowStatus (void)
352 window_rect.left = window_x;
353 window_rect.top = window_y;
354 window_rect.right = window_x + window_width;
355 window_rect.bottom = window_y + window_height;
356 window_center_x = (window_rect.left + window_rect.right) / 2;
357 window_center_y = (window_rect.top + window_rect.bottom) / 2;
359 IN_UpdateClipCursor ();
363 //====================================
370 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
373 *width = WindowRect.right - WindowRect.left;
374 *height = WindowRect.bottom - WindowRect.top;
378 void VID_Finish (void)
381 if (r_render.integer && !scr_skipupdate)
387 // handle the mouse state when windowed if that's changed
389 if (vid_mouse.integer && key_dest == key_game)
391 if (modestate == MS_FULLDIB)
409 IN_DeactivateMouse ();
415 void VID_SetDefaultMode (void)
417 IN_DeactivateMouse ();
420 void VID_RestoreSystemGamma(void);
422 void VID_Shutdown (void)
431 vid_canalttab = false;
432 hRC = wglGetCurrentContext();
433 hDC = wglGetCurrentDC();
435 wglMakeCurrent(NULL, NULL);
437 // LordHavoc: free textures before closing (may help NVIDIA)
438 for (i = 0;i < 8192;i++)
440 glDeleteTextures(8192, temp);
443 wglDeleteContext(hRC);
445 if (hDC && mainwindow)
446 ReleaseDC(mainwindow, hDC);
448 if (modestate == MS_FULLDIB)
449 ChangeDisplaySettings (NULL, 0);
451 if (maindc && mainwindow)
452 ReleaseDC (mainwindow, maindc);
454 AppActivate(false, false);
456 VID_RestoreSystemGamma();
461 //==========================================================================
464 BOOL bSetupPixelFormat(HDC hDC)
466 static PIXELFORMATDESCRIPTOR pfd = {
467 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
469 PFD_DRAW_TO_WINDOW // support window
470 | PFD_SUPPORT_OPENGL // support OpenGL
471 | PFD_DOUBLEBUFFER , // double buffered
472 PFD_TYPE_RGBA, // RGBA type
473 24, // 24-bit color depth
474 0, 0, 0, 0, 0, 0, // color bits ignored
475 0, // no alpha buffer
476 0, // shift bit ignored
477 0, // no accumulation buffer
478 0, 0, 0, 0, // accum bits ignored
479 32, // 32-bit z-buffer
480 0, // no stencil buffer
481 0, // no auxiliary buffer
482 PFD_MAIN_PLANE, // main layer
484 0, 0, 0 // layer masks ignored
488 if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
490 MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
494 if (SetPixelFormat(hDC, pixelformat, &pfd) == false)
496 MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
505 qbyte scantokey[128] =
507 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
508 0 ,27 ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'0' ,'-' ,'=' ,K_BACKSPACE,9 , // 0
509 'q' ,'w' ,'e' ,'r' ,'t' ,'y' ,'u' ,'i' ,'o' ,'p' ,'[' ,']' ,13 ,K_CTRL ,'a' ,'s' , // 1
510 'd' ,'f' ,'g' ,'h' ,'j' ,'k' ,'l' ,';' ,'\'' ,'`' ,K_SHIFT,'\\' ,'z' ,'x' ,'c' ,'v' , // 2
511 'b' ,'n' ,'m' ,',' ,'.' ,'/' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2 ,K_F3 ,K_F4 ,K_F5 , // 3
512 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
513 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
514 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
515 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
519 qbyte shiftscantokey[128] =
521 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
522 0 ,27 ,'!' ,'@' ,'#' ,'$' ,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'_' ,'+' ,K_BACKSPACE,9 , // 0
523 'Q' ,'W' ,'E' ,'R' ,'T' ,'Y' ,'U' ,'I' ,'O' ,'P' ,'{' ,'}' ,13 ,K_CTRL ,'A' ,'S' , // 1
524 'D' ,'F' ,'G' ,'H' ,'J' ,'K' ,'L' ,':' ,'"' ,'~' ,K_SHIFT,'|' ,'Z' ,'X' ,'C' ,'V' , // 2
525 'B' ,'N' ,'M' ,'<' ,'>' ,'?' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2,K_F3 ,K_F4 ,K_F5 , // 3
526 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
527 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
528 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
529 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
537 Map from windows to quake keynums
540 int MapKey (int key, int virtualkey)
545 if (scantokey[key] == 0)
546 Con_DPrintf("key 0x%02x has no translation\n", key);
547 // if (scantokey[key] >= 0x20 && scantokey[key] < 0x7F)
549 return scantokey[key];
553 ===================================================================
557 ===================================================================
565 void ClearAllStates (void)
569 // send an up event for each key, to make sure the server clears them all
570 for (i=0 ; i<256 ; i++)
572 Key_Event (i, false);
579 void VID_RestoreGameGamma(void);
580 extern qboolean host_loopactive;
582 void AppActivate(BOOL fActive, BOOL minimize)
583 /****************************************************************************
585 * Function: AppActivate
586 * Parameters: fActive - True if app is activating
588 * Description: If the application is activating, then swap the system
589 * into SYSPAL_NOSTATIC mode so that our palettes will display
592 ****************************************************************************/
594 static BOOL sound_active;
597 Minimized = minimize;
599 // enable/disable sound on focus gain/loss
600 if (!ActiveApp && sound_active)
603 sound_active = false;
605 else if (ActiveApp && !sound_active)
613 if (modestate == MS_FULLDIB)
615 // usingmouse = true;
616 // IN_ActivateMouse ();
618 if (vid_canalttab && vid_wassuspended)
620 vid_wassuspended = false;
621 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
622 ShowWindow(mainwindow, SW_SHOWNORMAL);
625 // LordHavoc: from dabb, fix for alt-tab bug in NVidia drivers
626 MoveWindow(mainwindow,0,0,gdevmode.dmPelsWidth,gdevmode.dmPelsHeight,false);
628 // else if ((modestate == MS_WINDOWED) && vid_mouse.integer && key_dest == key_game)
630 // usingmouse = true;
631 // IN_ActivateMouse ();
635 VID_RestoreGameGamma();
641 IN_DeactivateMouse ();
643 if (modestate == MS_FULLDIB)
645 // usingmouse = false;
646 // IN_DeactivateMouse ();
650 ChangeDisplaySettings (NULL, 0);
651 vid_wassuspended = true;
654 // else if ((modestate == MS_WINDOWED) && vid_mouse.integer)
656 // usingmouse = false;
657 // IN_DeactivateMouse ();
660 VID_RestoreSystemGamma();
664 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
666 /* main window procedure */
667 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
670 int fActive, fMinimized, temp;
671 extern unsigned int uiWheelMessage;
673 if ( uMsg == uiWheelMessage )
674 uMsg = WM_MOUSEWHEEL;
679 if (modestate == MS_FULLDIB)
680 ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
687 window_x = (int) LOWORD(lParam);
688 window_y = (int) HIWORD(lParam);
689 VID_UpdateWindowStatus ();
694 Key_Event (MapKey(lParam, wParam), true);
699 Key_Event (MapKey(lParam, wParam), false);
703 // keep Alt-Space from happening
706 // this is complicated because Win32 seems to pack multiple mouse events into
707 // one update sometimes, so we always check all states and look for events
717 if (wParam & MK_LBUTTON)
720 if (wParam & MK_RBUTTON)
723 if (wParam & MK_MBUTTON)
726 IN_MouseEvent (temp);
730 // JACK: This is the mouse wheel with the Intellimouse
731 // Its delta is either positive or neg, and we generate the proper
734 if ((short) HIWORD(wParam) > 0) {
735 Key_Event(K_MWHEELUP, true);
736 Key_Event(K_MWHEELUP, false);
738 Key_Event(K_MWHEELDOWN, true);
739 Key_Event(K_MWHEELDOWN, false);
747 if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
753 fActive = LOWORD(wParam);
754 fMinimized = (BOOL) HIWORD(wParam);
755 AppActivate(!(fActive == WA_INACTIVE), fMinimized);
757 // fix the leftover Alt from any Alt-Tab or the like that switched us away
765 DestroyWindow (mainwindow);
772 lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
776 /* pass all unhandled messages to DefWindowProc */
777 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
781 /* return 1 if handled message, 0 if not */
791 int VID_NumModes (void)
802 vmode_t *VID_GetModePtr (int modenum)
805 if ((modenum >= 0) && (modenum < nummodes))
806 return &modelist[modenum];
814 VID_GetModeDescription
817 char *VID_GetModeDescription (int mode)
821 static char temp[100];
823 if ((mode < 0) || (mode >= nummodes))
826 if (!leavecurrentmode)
828 pv = VID_GetModePtr (mode);
829 pinfo = pv->modedesc;
833 sprintf (temp, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
841 // KJB: Added this to return the mode driver name in description for console
843 char *VID_GetExtModeDescription (int mode)
845 static char pinfo[40];
848 if ((mode < 0) || (mode >= nummodes))
851 pv = VID_GetModePtr (mode);
852 if (modelist[mode].type == MS_FULLDIB)
854 if (!leavecurrentmode)
855 sprintf(pinfo,"%s fullscreen", pv->modedesc);
857 sprintf (pinfo, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
861 if (modestate == MS_WINDOWED)
862 sprintf(pinfo, "%s windowed", pv->modedesc);
864 sprintf(pinfo, "windowed");
873 VID_DescribeCurrentMode_f
876 void VID_DescribeCurrentMode_f (void)
878 Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum));
887 void VID_NumModes_f (void)
891 Con_Printf ("%d video mode is available\n", nummodes);
893 Con_Printf ("%d video modes are available\n", nummodes);
902 void VID_DescribeMode_f (void)
906 modenum = atoi (Cmd_Argv(1));
908 t = leavecurrentmode;
909 leavecurrentmode = 0;
911 Con_Printf ("%s\n", VID_GetExtModeDescription (modenum));
913 leavecurrentmode = t;
922 void VID_DescribeModes_f (void)
928 lnummodes = VID_NumModes ();
930 t = leavecurrentmode;
931 leavecurrentmode = 0;
933 for (i=1 ; i<lnummodes ; i++)
935 pv = VID_GetModePtr (i);
936 pinfo = VID_GetExtModeDescription (i);
937 Con_Printf ("%2d: %s\n", i, pinfo);
940 leavecurrentmode = t;
943 void VID_AddMode(int type, int width, int height, int modenum, int halfscreen, int dib, int fullscreen, int bpp)
946 if (nummodes >= MAX_MODE_LIST)
948 modelist[nummodes].type = type;
949 modelist[nummodes].width = width;
950 modelist[nummodes].height = height;
951 modelist[nummodes].modenum = modenum;
952 modelist[nummodes].halfscreen = halfscreen;
953 modelist[nummodes].dib = dib;
954 modelist[nummodes].fullscreen = fullscreen;
955 modelist[nummodes].bpp = bpp;
957 sprintf (modelist[nummodes].modedesc, "%dx%d", width, height);
959 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", width, height, bpp);
960 for (i = 0;i < nummodes;i++)
962 if (!memcmp(&modelist[i], &modelist[nummodes], sizeof(vmode_t)))
968 void VID_InitDIB (HINSTANCE hInstance)
973 // Register the frame class
975 wc.lpfnWndProc = (WNDPROC)MainWndProc;
978 wc.hInstance = hInstance;
980 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
981 wc.hbrBackground = NULL;
983 wc.lpszClassName = gamename;
985 if (!RegisterClass (&wc) )
986 Sys_Error ("Couldn't register window class");
989 modelist[0].type = MS_WINDOWED;
991 if (COM_CheckParm("-width"))
992 modelist[0].width = atoi(com_argv[COM_CheckParm("-width")+1]);
994 modelist[0].width = 640;
996 if (modelist[0].width < 320)
997 modelist[0].width = 320;
999 if (COM_CheckParm("-height"))
1000 modelist[0].height= atoi(com_argv[COM_CheckParm("-height")+1]);
1002 modelist[0].height = modelist[0].width * 240/320;
1004 if (modelist[0].height < 240)
1005 modelist[0].height = 240;
1007 sprintf (modelist[0].modedesc, "%dx%d", modelist[0].width, modelist[0].height);
1009 modelist[0].modenum = MODE_WINDOWED;
1010 modelist[0].dib = 1;
1011 modelist[0].fullscreen = 0;
1012 modelist[0].halfscreen = 0;
1013 modelist[0].bpp = 0;
1017 if (COM_CheckParm("-width"))
1018 w = atoi(com_argv[COM_CheckParm("-width")+1]);
1025 if (COM_CheckParm("-height"))
1026 h = atoi(com_argv[COM_CheckParm("-height")+1]);
1033 VID_AddMode(MS_WINDOWED, w, h, 0, 0, 1, 0, 0);
1042 void VID_InitFullDIB (HINSTANCE hInstance)
1047 int originalnummodes;
1048 // int existingmode;
1055 // enumerate >8 bpp modes
1056 originalnummodes = nummodes;
1061 stat = EnumDisplaySettings (NULL, modenum, &devmode);
1063 if ((devmode.dmBitsPerPel >= 15) && (devmode.dmPelsWidth <= MAXWIDTH) && (devmode.dmPelsHeight <= MAXHEIGHT) && (nummodes < MAX_MODE_LIST))
1065 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1067 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1069 // if the width is more than twice the height, reduce it by half because this
1070 // is probably a dual-screen monitor
1071 if ((!COM_CheckParm("-noadjustaspect")) && (devmode.dmPelsWidth > (devmode.dmPelsHeight << 1)))
1072 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth >> 1, devmode.dmPelsHeight, 0, 1, 1, 1, devmode.dmBitsPerPel);
1074 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1076 modelist[nummodes].type = MS_FULLDIB;
1077 modelist[nummodes].width = devmode.dmPelsWidth;
1078 modelist[nummodes].height = devmode.dmPelsHeight;
1079 modelist[nummodes].modenum = 0;
1080 modelist[nummodes].halfscreen = 0;
1081 modelist[nummodes].dib = 1;
1082 modelist[nummodes].fullscreen = 1;
1083 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1084 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1086 // if the width is more than twice the height, reduce it by half because this
1087 // is probably a dual-screen monitor
1088 if (!COM_CheckParm("-noadjustaspect"))
1090 if (modelist[nummodes].width > (modelist[nummodes].height << 1))
1092 modelist[nummodes].width >>= 1;
1093 modelist[nummodes].halfscreen = 1;
1094 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", modelist[nummodes].width, modelist[nummodes].height, modelist[nummodes].bpp);
1098 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1100 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1117 // see if there are any low-res modes that aren't being reported
1118 numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]);
1124 for (j=0 ; (j<numlowresmodes) && (nummodes < MAX_MODE_LIST) ; j++)
1126 devmode.dmBitsPerPel = bpp;
1127 devmode.dmPelsWidth = lowresmodes[j].width;
1128 devmode.dmPelsHeight = lowresmodes[j].height;
1129 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1131 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1132 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1135 modelist[nummodes].type = MS_FULLDIB;
1136 modelist[nummodes].width = devmode.dmPelsWidth;
1137 modelist[nummodes].height = devmode.dmPelsHeight;
1138 modelist[nummodes].modenum = 0;
1139 modelist[nummodes].halfscreen = 0;
1140 modelist[nummodes].dib = 1;
1141 modelist[nummodes].fullscreen = 1;
1142 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1143 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1145 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1147 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1176 if (nummodes == originalnummodes)
1177 Con_SafePrintf ("No fullscreen DIB modes found\n");
1180 //static int grabsysgamma = true;
1181 WORD systemgammaramps[3][256], currentgammaramps[3][256];
1183 int VID_SetGamma(float prescale, float gamma, float scale, float base)
1189 BuildGammaTable16(prescale, gamma, scale, base, ¤tgammaramps[0][0]);
1190 for (i = 0;i < 256;i++)
1191 currentgammaramps[1][i] = currentgammaramps[2][i] = currentgammaramps[0][i];
1193 i = SetDeviceGammaRamp(hdc, ¤tgammaramps[0][0]);
1195 ReleaseDC (NULL, hdc);
1196 return i; // return success or failure
1199 void VID_RestoreGameGamma(void)
1201 VID_UpdateGamma(true);
1204 void VID_GetSystemGamma(void)
1209 GetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1211 ReleaseDC (NULL, hdc);
1214 void VID_RestoreSystemGamma(void)
1219 SetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1221 ReleaseDC (NULL, hdc);
1229 void VID_Init (void)
1232 // int existingmode;
1233 int basenummodes, width, height = 0, bpp, findbpp, done;
1237 memset(&devmode, 0, sizeof(devmode));
1239 // Cvar_RegisterVariable (&_vid_default_mode);
1240 // Cvar_RegisterVariable (&_vid_default_mode_win);
1242 Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);
1243 Cmd_AddCommand ("vid_describecurrentmode", VID_DescribeCurrentMode_f);
1244 Cmd_AddCommand ("vid_describemode", VID_DescribeMode_f);
1245 Cmd_AddCommand ("vid_describemodes", VID_DescribeModes_f);
1247 VID_GetSystemGamma();
1249 hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON2));
1251 InitCommonControls();
1253 VID_InitDIB (global_hInstance);
1254 basenummodes = nummodes = 1;
1256 VID_InitFullDIB (global_hInstance);
1258 if (COM_CheckParm("-window"))
1262 if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
1263 Sys_Error ("Can't run in non-RGB mode");
1265 ReleaseDC (NULL, hdc);
1269 vid_default = MODE_WINDOWED;
1274 Sys_Error ("No RGB fullscreen modes available");
1278 if (COM_CheckParm("-mode"))
1279 vid_default = atoi(com_argv[COM_CheckParm("-mode")+1]);
1282 if (COM_CheckParm("-current"))
1284 modelist[MODE_FULLSCREEN_DEFAULT].width = GetSystemMetrics (SM_CXSCREEN);
1285 modelist[MODE_FULLSCREEN_DEFAULT].height = GetSystemMetrics (SM_CYSCREEN);
1286 vid_default = MODE_FULLSCREEN_DEFAULT;
1287 leavecurrentmode = 1;
1291 if (COM_CheckParm("-width"))
1292 width = atoi(com_argv[COM_CheckParm("-width")+1]);
1296 if (COM_CheckParm("-bpp"))
1298 bpp = atoi(com_argv[COM_CheckParm("-bpp")+1]);
1307 if (COM_CheckParm("-height"))
1308 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1310 // if they want to force it, add the specified mode to the list
1311 if (COM_CheckParm("-force") && (nummodes < MAX_MODE_LIST))
1312 VID_AddMode(MS_FULLDIB, width, height, 0, 0, 1, 1, bpp);
1315 modelist[nummodes].type = MS_FULLDIB;
1316 modelist[nummodes].width = width;
1317 modelist[nummodes].height = height;
1318 modelist[nummodes].modenum = 0;
1319 modelist[nummodes].halfscreen = 0;
1320 modelist[nummodes].dib = 1;
1321 modelist[nummodes].fullscreen = 1;
1322 modelist[nummodes].bpp = bpp;
1323 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1325 for (i=nummodes, existingmode = 0 ; i<nummodes ; i++)
1327 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1343 if (COM_CheckParm("-height"))
1345 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1347 for (i=1, vid_default=0 ; i<nummodes ; i++)
1349 if ((modelist[i].width == width) && (modelist[i].height == height) && (modelist[i].bpp == bpp))
1359 for (i=1, vid_default=0 ; i<nummodes ; i++)
1361 if ((modelist[i].width == width) && (modelist[i].bpp == bpp))
1376 case 15: bpp = 16;break;
1377 case 16: bpp = 32;break;
1378 case 32: bpp = 24;break;
1379 case 24: done = 1;break;
1389 Sys_Error ("Specified video mode not available");
1394 vid_initialized = true;
1396 if ((i = COM_CheckParm("-conwidth")) != 0)
1397 vid.conwidth = atoi(com_argv[i+1]);
1401 vid.conwidth &= 0xfff8; // make it a multiple of eight
1403 if (vid.conwidth < 320)
1406 // pick a conheight that matches with correct aspect
1407 vid.conheight = vid.conwidth*3 / 4;
1409 if ((i = COM_CheckParm("-conheight")) != 0)
1410 vid.conheight = atoi(com_argv[i+1]);
1411 if (vid.conheight < 200)
1412 vid.conheight = 200;
1414 VID_SetMode (vid_default);
1416 maindc = GetDC(mainwindow);
1417 bSetupPixelFormat(maindc);
1419 baseRC = wglCreateContext( maindc );
1421 Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.");
1422 if (!wglMakeCurrent( maindc, baseRC ))
1423 Sys_Error ("wglMakeCurrent failed");
1427 // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
1428 if (strncasecmp(gl_vendor,"ATI",3)==0)
1430 if (strncasecmp(gl_renderer,"Rage Pro",8)==0)
1433 if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
1436 // sprintf (gldir, "%s/glquake", com_gamedir);
1437 // Sys_mkdir (gldir);
1439 vid_realmode = vid_modenum;
1441 vid_menudrawfn = VID_MenuDraw;
1442 vid_menukeyfn = VID_MenuKey;
1444 strcpy (badmode.modedesc, "Bad mode");
1445 vid_canalttab = true;
1449 //========================================================
1451 //========================================================
1453 extern void M_Menu_Options_f (void);
1454 extern void M_Print (int cx, int cy, char *str);
1455 extern void M_PrintWhite (int cx, int cy, char *str);
1456 extern void M_DrawCharacter (int cx, int line, int num);
1457 extern void M_DrawPic (int x, int y, char *picname);
1459 static int vid_wmodes;
1468 #define MAX_COLUMN_SIZE 9
1469 #define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 2)
1470 #define MAX_MODEDESCS (MAX_COLUMN_SIZE*3)
1472 static modedesc_t modedescs[MAX_MODEDESCS];
1479 void VID_MenuDraw (void)
1483 int lnummodes, i, k, column, row;
1486 p = Draw_CachePic ("gfx/vidmodes.lmp");
1487 M_DrawPic ( (320-p->width)/2, 4, "gfx/vidmodes.lmp");
1490 lnummodes = VID_NumModes ();
1492 for (i=1 ; (i<lnummodes) && (vid_wmodes < MAX_MODEDESCS) ; i++)
1494 ptr = VID_GetModeDescription (i);
1495 pv = VID_GetModePtr (i);
1499 modedescs[k].modenum = i;
1500 modedescs[k].desc = ptr;
1501 modedescs[k].iscur = 0;
1503 if (i == vid_modenum)
1504 modedescs[k].iscur = 1;
1512 M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)");
1517 for (i=0 ; i<vid_wmodes ; i++)
1519 if (modedescs[i].iscur)
1520 M_PrintWhite (column, row, modedescs[i].desc);
1522 M_Print (column, row, modedescs[i].desc);
1526 if ((i % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
1534 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*2, "Video modes must be set from the");
1535 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3, "command line with -width <width>");
1536 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4, "and -bpp <bits-per-pixel>");
1537 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6, "Select windowed mode with -window");
1546 void VID_MenuKey (int key)
1551 S_LocalSound ("misc/menu1.wav");
1552 M_Menu_Options_f ();