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)
252 int original_mode, temp;
256 if ((windowed && (modenum != 0)) || (!windowed && (modenum < 1)) || (!windowed && (modenum >= nummodes)))
257 Sys_Error ("Bad video mode\n");
259 // so Con_Printfs don't mess us up by forcing vid and snd updates
260 temp = scr_disabled_for_loading;
261 scr_disabled_for_loading = true;
265 if (vid_modenum == NO_MODE)
266 original_mode = windowed_default;
268 original_mode = vid_modenum;
270 // Set either the fullscreen or windowed mode
271 if (modelist[modenum].type == MS_WINDOWED)
273 // if (vid_mouse.integer && key_dest == key_game)
275 // stat = VID_SetWindowedMode(modenum);
276 // usingmouse = true;
277 // IN_ActivateMouse ();
282 // usingmouse = false;
283 // IN_DeactivateMouse ();
285 // stat = VID_SetWindowedMode(modenum);
287 stat = VID_SetWindowedMode(modenum);
289 else if (modelist[modenum].type == MS_FULLDIB)
291 stat = VID_SetFullDIBMode(modenum);
292 // usingmouse = true;
293 // IN_ActivateMouse ();
297 Sys_Error ("VID_SetMode: Bad mode type in modelist");
299 window_width = DIBWidth;
300 window_height = DIBHeight;
301 VID_UpdateWindowStatus ();
304 scr_disabled_for_loading = temp;
307 Sys_Error ("Couldn't set video mode");
309 // now we try to make sure we get the focus on the mode switch, because
310 // sometimes in some systems we don't. We grab the foreground, then
311 // finish setting up, pump all our messages, and sleep for a little while
312 // to let messages finish bouncing around the system, then we put
313 // ourselves at the top of the z order, then grab the foreground again,
314 // Who knows if it helps, but it probably doesn't hurt
315 SetForegroundWindow (mainwindow);
316 vid_modenum = modenum;
317 Cvar_SetValue ("vid_mode", (float)vid_modenum);
319 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
321 TranslateMessage (&msg);
322 DispatchMessage (&msg);
327 SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS);
329 SetForegroundWindow (mainwindow);
331 // fix the leftover Alt from any Alt-Tab or the like that switched us away
335 Con_SafePrintf ("Video mode %s initialized.\n", VID_GetModeDescription (vid_modenum));
337 // vid.recalc_refdef = 1;
345 VID_UpdateWindowStatus
348 void VID_UpdateWindowStatus (void)
351 window_rect.left = window_x;
352 window_rect.top = window_y;
353 window_rect.right = window_x + window_width;
354 window_rect.bottom = window_y + window_height;
355 window_center_x = (window_rect.left + window_rect.right) / 2;
356 window_center_y = (window_rect.top + window_rect.bottom) / 2;
358 IN_UpdateClipCursor ();
362 //====================================
369 void VID_GetWindowSize (int *x, int *y, int *width, int *height)
372 *width = WindowRect.right - WindowRect.left;
373 *height = WindowRect.bottom - WindowRect.top;
377 void VID_Finish (void)
380 if (r_render.integer && !scr_skipupdate)
386 // handle the mouse state when windowed if that's changed
388 if (vid_mouse.integer && key_dest == key_game)
390 if (modestate == MS_FULLDIB)
408 IN_DeactivateMouse ();
414 void VID_SetDefaultMode (void)
416 IN_DeactivateMouse ();
419 void VID_RestoreSystemGamma(void);
421 void VID_Shutdown (void)
430 vid_canalttab = false;
431 hRC = wglGetCurrentContext();
432 hDC = wglGetCurrentDC();
434 wglMakeCurrent(NULL, NULL);
436 // LordHavoc: free textures before closing (may help NVIDIA)
437 for (i = 0;i < 8192;i++)
439 glDeleteTextures(8192, temp);
442 wglDeleteContext(hRC);
444 if (hDC && mainwindow)
445 ReleaseDC(mainwindow, hDC);
447 if (modestate == MS_FULLDIB)
448 ChangeDisplaySettings (NULL, 0);
450 if (maindc && mainwindow)
451 ReleaseDC (mainwindow, maindc);
453 AppActivate(false, false);
455 VID_RestoreSystemGamma();
460 //==========================================================================
463 BOOL bSetupPixelFormat(HDC hDC)
465 static PIXELFORMATDESCRIPTOR pfd = {
466 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
468 PFD_DRAW_TO_WINDOW // support window
469 | PFD_SUPPORT_OPENGL // support OpenGL
470 | PFD_DOUBLEBUFFER , // double buffered
471 PFD_TYPE_RGBA, // RGBA type
472 24, // 24-bit color depth
473 0, 0, 0, 0, 0, 0, // color bits ignored
474 0, // no alpha buffer
475 0, // shift bit ignored
476 0, // no accumulation buffer
477 0, 0, 0, 0, // accum bits ignored
478 32, // 32-bit z-buffer
479 0, // no stencil buffer
480 0, // no auxiliary buffer
481 PFD_MAIN_PLANE, // main layer
483 0, 0, 0 // layer masks ignored
487 if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
489 MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
493 if (SetPixelFormat(hDC, pixelformat, &pfd) == false)
495 MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
504 qbyte scantokey[128] =
506 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
507 0 ,27 ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'0' ,'-' ,'=' ,K_BACKSPACE,9 , // 0
508 'q' ,'w' ,'e' ,'r' ,'t' ,'y' ,'u' ,'i' ,'o' ,'p' ,'[' ,']' ,13 ,K_CTRL ,'a' ,'s' , // 1
509 'd' ,'f' ,'g' ,'h' ,'j' ,'k' ,'l' ,';' ,'\'' ,'`' ,K_SHIFT,'\\' ,'z' ,'x' ,'c' ,'v' , // 2
510 'b' ,'n' ,'m' ,',' ,'.' ,'/' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2 ,K_F3 ,K_F4 ,K_F5 , // 3
511 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
512 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
513 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
514 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
518 qbyte shiftscantokey[128] =
520 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
521 0 ,27 ,'!' ,'@' ,'#' ,'$' ,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'_' ,'+' ,K_BACKSPACE,9 , // 0
522 'Q' ,'W' ,'E' ,'R' ,'T' ,'Y' ,'U' ,'I' ,'O' ,'P' ,'{' ,'}' ,13 ,K_CTRL ,'A' ,'S' , // 1
523 'D' ,'F' ,'G' ,'H' ,'J' ,'K' ,'L' ,':' ,'"' ,'~' ,K_SHIFT,'|' ,'Z' ,'X' ,'C' ,'V' , // 2
524 'B' ,'N' ,'M' ,'<' ,'>' ,'?' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2,K_F3 ,K_F4 ,K_F5 , // 3
525 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
526 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
527 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
528 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
536 Map from windows to quake keynums
539 int MapKey (int key, int virtualkey)
544 if (scantokey[key] == 0)
545 Con_DPrintf("key 0x%02x has no translation\n", key);
546 // if (scantokey[key] >= 0x20 && scantokey[key] < 0x7F)
548 return scantokey[key];
552 ===================================================================
556 ===================================================================
564 void ClearAllStates (void)
568 // send an up event for each key, to make sure the server clears them all
569 for (i=0 ; i<256 ; i++)
571 Key_Event (i, false);
578 void VID_RestoreGameGamma(void);
579 extern qboolean host_loopactive;
581 void AppActivate(BOOL fActive, BOOL minimize)
582 /****************************************************************************
584 * Function: AppActivate
585 * Parameters: fActive - True if app is activating
587 * Description: If the application is activating, then swap the system
588 * into SYSPAL_NOSTATIC mode so that our palettes will display
591 ****************************************************************************/
593 static BOOL sound_active;
596 Minimized = minimize;
598 // enable/disable sound on focus gain/loss
599 if (!ActiveApp && sound_active)
602 sound_active = false;
604 else if (ActiveApp && !sound_active)
612 if (modestate == MS_FULLDIB)
614 // usingmouse = true;
615 // IN_ActivateMouse ();
617 if (vid_canalttab && vid_wassuspended)
619 vid_wassuspended = false;
620 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
621 ShowWindow(mainwindow, SW_SHOWNORMAL);
624 // LordHavoc: from dabb, fix for alt-tab bug in NVidia drivers
625 MoveWindow(mainwindow,0,0,gdevmode.dmPelsWidth,gdevmode.dmPelsHeight,false);
627 // else if ((modestate == MS_WINDOWED) && vid_mouse.integer && key_dest == key_game)
629 // usingmouse = true;
630 // IN_ActivateMouse ();
634 VID_RestoreGameGamma();
640 IN_DeactivateMouse ();
642 if (modestate == MS_FULLDIB)
644 // usingmouse = false;
645 // IN_DeactivateMouse ();
649 ChangeDisplaySettings (NULL, 0);
650 vid_wassuspended = true;
653 // else if ((modestate == MS_WINDOWED) && vid_mouse.integer)
655 // usingmouse = false;
656 // IN_DeactivateMouse ();
659 VID_RestoreSystemGamma();
663 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
665 /* main window procedure */
666 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
669 int fActive, fMinimized, temp;
670 extern unsigned int uiWheelMessage;
672 if ( uMsg == uiWheelMessage )
673 uMsg = WM_MOUSEWHEEL;
678 if (modestate == MS_FULLDIB)
679 ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
686 window_x = (int) LOWORD(lParam);
687 window_y = (int) HIWORD(lParam);
688 VID_UpdateWindowStatus ();
693 Key_Event (MapKey(lParam, wParam), true);
698 Key_Event (MapKey(lParam, wParam), false);
702 // keep Alt-Space from happening
705 // this is complicated because Win32 seems to pack multiple mouse events into
706 // one update sometimes, so we always check all states and look for events
716 if (wParam & MK_LBUTTON)
719 if (wParam & MK_RBUTTON)
722 if (wParam & MK_MBUTTON)
725 IN_MouseEvent (temp);
729 // JACK: This is the mouse wheel with the Intellimouse
730 // Its delta is either positive or neg, and we generate the proper
733 if ((short) HIWORD(wParam) > 0) {
734 Key_Event(K_MWHEELUP, true);
735 Key_Event(K_MWHEELUP, false);
737 Key_Event(K_MWHEELDOWN, true);
738 Key_Event(K_MWHEELDOWN, false);
746 if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
752 fActive = LOWORD(wParam);
753 fMinimized = (BOOL) HIWORD(wParam);
754 AppActivate(!(fActive == WA_INACTIVE), fMinimized);
756 // fix the leftover Alt from any Alt-Tab or the like that switched us away
764 DestroyWindow (mainwindow);
771 lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
775 /* pass all unhandled messages to DefWindowProc */
776 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
780 /* return 1 if handled message, 0 if not */
790 int VID_NumModes (void)
801 vmode_t *VID_GetModePtr (int modenum)
804 if ((modenum >= 0) && (modenum < nummodes))
805 return &modelist[modenum];
813 VID_GetModeDescription
816 char *VID_GetModeDescription (int mode)
820 static char temp[100];
822 if ((mode < 0) || (mode >= nummodes))
825 if (!leavecurrentmode)
827 pv = VID_GetModePtr (mode);
828 pinfo = pv->modedesc;
832 sprintf (temp, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
840 // KJB: Added this to return the mode driver name in description for console
842 char *VID_GetExtModeDescription (int mode)
844 static char pinfo[40];
847 if ((mode < 0) || (mode >= nummodes))
850 pv = VID_GetModePtr (mode);
851 if (modelist[mode].type == MS_FULLDIB)
853 if (!leavecurrentmode)
854 sprintf(pinfo,"%s fullscreen", pv->modedesc);
856 sprintf (pinfo, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
860 if (modestate == MS_WINDOWED)
861 sprintf(pinfo, "%s windowed", pv->modedesc);
863 sprintf(pinfo, "windowed");
872 VID_DescribeCurrentMode_f
875 void VID_DescribeCurrentMode_f (void)
877 Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum));
886 void VID_NumModes_f (void)
890 Con_Printf ("%d video mode is available\n", nummodes);
892 Con_Printf ("%d video modes are available\n", nummodes);
901 void VID_DescribeMode_f (void)
905 modenum = atoi (Cmd_Argv(1));
907 t = leavecurrentmode;
908 leavecurrentmode = 0;
910 Con_Printf ("%s\n", VID_GetExtModeDescription (modenum));
912 leavecurrentmode = t;
921 void VID_DescribeModes_f (void)
927 lnummodes = VID_NumModes ();
929 t = leavecurrentmode;
930 leavecurrentmode = 0;
932 for (i=1 ; i<lnummodes ; i++)
934 pv = VID_GetModePtr (i);
935 pinfo = VID_GetExtModeDescription (i);
936 Con_Printf ("%2d: %s\n", i, pinfo);
939 leavecurrentmode = t;
942 void VID_AddMode(int type, int width, int height, int modenum, int halfscreen, int dib, int fullscreen, int bpp)
945 if (nummodes >= MAX_MODE_LIST)
947 modelist[nummodes].type = type;
948 modelist[nummodes].width = width;
949 modelist[nummodes].height = height;
950 modelist[nummodes].modenum = modenum;
951 modelist[nummodes].halfscreen = halfscreen;
952 modelist[nummodes].dib = dib;
953 modelist[nummodes].fullscreen = fullscreen;
954 modelist[nummodes].bpp = bpp;
956 sprintf (modelist[nummodes].modedesc, "%dx%d", width, height);
958 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", width, height, bpp);
959 for (i = 0;i < nummodes;i++)
961 if (!memcmp(&modelist[i], &modelist[nummodes], sizeof(vmode_t)))
967 void VID_InitDIB (HINSTANCE hInstance)
972 // Register the frame class
974 wc.lpfnWndProc = (WNDPROC)MainWndProc;
977 wc.hInstance = hInstance;
979 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
980 wc.hbrBackground = NULL;
982 wc.lpszClassName = gamename;
984 if (!RegisterClass (&wc) )
985 Sys_Error ("Couldn't register window class");
988 modelist[0].type = MS_WINDOWED;
990 if (COM_CheckParm("-width"))
991 modelist[0].width = atoi(com_argv[COM_CheckParm("-width")+1]);
993 modelist[0].width = 640;
995 if (modelist[0].width < 320)
996 modelist[0].width = 320;
998 if (COM_CheckParm("-height"))
999 modelist[0].height= atoi(com_argv[COM_CheckParm("-height")+1]);
1001 modelist[0].height = modelist[0].width * 240/320;
1003 if (modelist[0].height < 240)
1004 modelist[0].height = 240;
1006 sprintf (modelist[0].modedesc, "%dx%d", modelist[0].width, modelist[0].height);
1008 modelist[0].modenum = MODE_WINDOWED;
1009 modelist[0].dib = 1;
1010 modelist[0].fullscreen = 0;
1011 modelist[0].halfscreen = 0;
1012 modelist[0].bpp = 0;
1016 if (COM_CheckParm("-width"))
1017 w = atoi(com_argv[COM_CheckParm("-width")+1]);
1024 if (COM_CheckParm("-height"))
1025 h = atoi(com_argv[COM_CheckParm("-height")+1]);
1032 VID_AddMode(MS_WINDOWED, w, h, 0, 0, 1, 0, 0);
1041 void VID_InitFullDIB (HINSTANCE hInstance)
1046 int originalnummodes;
1047 // int existingmode;
1054 // enumerate >8 bpp modes
1055 originalnummodes = nummodes;
1060 stat = EnumDisplaySettings (NULL, modenum, &devmode);
1062 if ((devmode.dmBitsPerPel >= 15) && (devmode.dmPelsWidth <= MAXWIDTH) && (devmode.dmPelsHeight <= MAXHEIGHT) && (nummodes < MAX_MODE_LIST))
1064 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1066 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1068 // if the width is more than twice the height, reduce it by half because this
1069 // is probably a dual-screen monitor
1070 if ((!COM_CheckParm("-noadjustaspect")) && (devmode.dmPelsWidth > (devmode.dmPelsHeight << 1)))
1071 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth >> 1, devmode.dmPelsHeight, 0, 1, 1, 1, devmode.dmBitsPerPel);
1073 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1075 modelist[nummodes].type = MS_FULLDIB;
1076 modelist[nummodes].width = devmode.dmPelsWidth;
1077 modelist[nummodes].height = devmode.dmPelsHeight;
1078 modelist[nummodes].modenum = 0;
1079 modelist[nummodes].halfscreen = 0;
1080 modelist[nummodes].dib = 1;
1081 modelist[nummodes].fullscreen = 1;
1082 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1083 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1085 // if the width is more than twice the height, reduce it by half because this
1086 // is probably a dual-screen monitor
1087 if (!COM_CheckParm("-noadjustaspect"))
1089 if (modelist[nummodes].width > (modelist[nummodes].height << 1))
1091 modelist[nummodes].width >>= 1;
1092 modelist[nummodes].halfscreen = 1;
1093 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", modelist[nummodes].width, modelist[nummodes].height, modelist[nummodes].bpp);
1097 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1099 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1116 // see if there are any low-res modes that aren't being reported
1117 numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]);
1123 for (j=0 ; (j<numlowresmodes) && (nummodes < MAX_MODE_LIST) ; j++)
1125 devmode.dmBitsPerPel = bpp;
1126 devmode.dmPelsWidth = lowresmodes[j].width;
1127 devmode.dmPelsHeight = lowresmodes[j].height;
1128 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1130 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1131 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1134 modelist[nummodes].type = MS_FULLDIB;
1135 modelist[nummodes].width = devmode.dmPelsWidth;
1136 modelist[nummodes].height = devmode.dmPelsHeight;
1137 modelist[nummodes].modenum = 0;
1138 modelist[nummodes].halfscreen = 0;
1139 modelist[nummodes].dib = 1;
1140 modelist[nummodes].fullscreen = 1;
1141 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1142 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1144 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1146 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1175 if (nummodes == originalnummodes)
1176 Con_SafePrintf ("No fullscreen DIB modes found\n");
1179 //static int grabsysgamma = true;
1180 WORD systemgammaramps[3][256], currentgammaramps[3][256];
1182 int VID_SetGamma(float prescale, float gamma, float scale, float base)
1188 BuildGammaTable16(prescale, gamma, scale, base, ¤tgammaramps[0][0]);
1189 for (i = 0;i < 256;i++)
1190 currentgammaramps[1][i] = currentgammaramps[2][i] = currentgammaramps[0][i];
1192 i = SetDeviceGammaRamp(hdc, ¤tgammaramps[0][0]);
1194 ReleaseDC (NULL, hdc);
1195 return i; // return success or failure
1198 void VID_RestoreGameGamma(void)
1200 VID_UpdateGamma(true);
1203 void VID_GetSystemGamma(void)
1208 GetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1210 ReleaseDC (NULL, hdc);
1213 void VID_RestoreSystemGamma(void)
1218 SetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1220 ReleaseDC (NULL, hdc);
1228 void VID_Init (void)
1231 // int existingmode;
1232 int basenummodes, width, height = 0, bpp, findbpp, done;
1236 memset(&devmode, 0, sizeof(devmode));
1238 // Cvar_RegisterVariable (&_vid_default_mode);
1239 // Cvar_RegisterVariable (&_vid_default_mode_win);
1241 Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);
1242 Cmd_AddCommand ("vid_describecurrentmode", VID_DescribeCurrentMode_f);
1243 Cmd_AddCommand ("vid_describemode", VID_DescribeMode_f);
1244 Cmd_AddCommand ("vid_describemodes", VID_DescribeModes_f);
1246 VID_GetSystemGamma();
1248 hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON2));
1250 InitCommonControls();
1252 VID_InitDIB (global_hInstance);
1253 basenummodes = nummodes = 1;
1255 VID_InitFullDIB (global_hInstance);
1257 if (COM_CheckParm("-window"))
1261 if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
1262 Sys_Error ("Can't run in non-RGB mode");
1264 ReleaseDC (NULL, hdc);
1268 vid_default = MODE_WINDOWED;
1273 Sys_Error ("No RGB fullscreen modes available");
1277 if (COM_CheckParm("-mode"))
1278 vid_default = atoi(com_argv[COM_CheckParm("-mode")+1]);
1281 if (COM_CheckParm("-current"))
1283 modelist[MODE_FULLSCREEN_DEFAULT].width = GetSystemMetrics (SM_CXSCREEN);
1284 modelist[MODE_FULLSCREEN_DEFAULT].height = GetSystemMetrics (SM_CYSCREEN);
1285 vid_default = MODE_FULLSCREEN_DEFAULT;
1286 leavecurrentmode = 1;
1290 if (COM_CheckParm("-width"))
1291 width = atoi(com_argv[COM_CheckParm("-width")+1]);
1295 if (COM_CheckParm("-bpp"))
1297 bpp = atoi(com_argv[COM_CheckParm("-bpp")+1]);
1306 if (COM_CheckParm("-height"))
1307 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1309 // if they want to force it, add the specified mode to the list
1310 if (COM_CheckParm("-force") && (nummodes < MAX_MODE_LIST))
1311 VID_AddMode(MS_FULLDIB, width, height, 0, 0, 1, 1, bpp);
1314 modelist[nummodes].type = MS_FULLDIB;
1315 modelist[nummodes].width = width;
1316 modelist[nummodes].height = height;
1317 modelist[nummodes].modenum = 0;
1318 modelist[nummodes].halfscreen = 0;
1319 modelist[nummodes].dib = 1;
1320 modelist[nummodes].fullscreen = 1;
1321 modelist[nummodes].bpp = bpp;
1322 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1324 for (i=nummodes, existingmode = 0 ; i<nummodes ; i++)
1326 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1342 if (COM_CheckParm("-height"))
1344 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1346 for (i=1, vid_default=0 ; i<nummodes ; i++)
1348 if ((modelist[i].width == width) && (modelist[i].height == height) && (modelist[i].bpp == bpp))
1358 for (i=1, vid_default=0 ; i<nummodes ; i++)
1360 if ((modelist[i].width == width) && (modelist[i].bpp == bpp))
1375 case 15: bpp = 16;break;
1376 case 16: bpp = 32;break;
1377 case 32: bpp = 24;break;
1378 case 24: done = 1;break;
1388 Sys_Error ("Specified video mode not available");
1393 vid_initialized = true;
1395 if ((i = COM_CheckParm("-conwidth")) != 0)
1396 vid.conwidth = atoi(com_argv[i+1]);
1400 vid.conwidth &= 0xfff8; // make it a multiple of eight
1402 if (vid.conwidth < 320)
1405 // pick a conheight that matches with correct aspect
1406 vid.conheight = vid.conwidth*3 / 4;
1408 if ((i = COM_CheckParm("-conheight")) != 0)
1409 vid.conheight = atoi(com_argv[i+1]);
1410 if (vid.conheight < 200)
1411 vid.conheight = 200;
1413 VID_SetMode (vid_default);
1415 maindc = GetDC(mainwindow);
1416 bSetupPixelFormat(maindc);
1418 baseRC = wglCreateContext( maindc );
1420 Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.");
1421 if (!wglMakeCurrent( maindc, baseRC ))
1422 Sys_Error ("wglMakeCurrent failed");
1426 // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
1427 if (strncasecmp(gl_vendor,"ATI",3)==0)
1429 if (strncasecmp(gl_renderer,"Rage Pro",8)==0)
1432 if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
1435 // sprintf (gldir, "%s/glquake", com_gamedir);
1436 // Sys_mkdir (gldir);
1438 vid_realmode = vid_modenum;
1440 vid_menudrawfn = VID_MenuDraw;
1441 vid_menukeyfn = VID_MenuKey;
1443 strcpy (badmode.modedesc, "Bad mode");
1444 vid_canalttab = true;
1448 //========================================================
1450 //========================================================
1452 extern void M_Menu_Options_f (void);
1453 extern void M_Print (int cx, int cy, char *str);
1454 extern void M_PrintWhite (int cx, int cy, char *str);
1455 extern void M_DrawCharacter (int cx, int line, int num);
1456 extern void M_DrawPic (int x, int y, char *picname);
1458 static int vid_wmodes;
1467 #define MAX_COLUMN_SIZE 9
1468 #define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 2)
1469 #define MAX_MODEDESCS (MAX_COLUMN_SIZE*3)
1471 static modedesc_t modedescs[MAX_MODEDESCS];
1478 void VID_MenuDraw (void)
1482 int lnummodes, i, k, column, row;
1485 p = Draw_CachePic ("gfx/vidmodes.lmp");
1486 M_DrawPic ( (320-p->width)/2, 4, "gfx/vidmodes.lmp");
1489 lnummodes = VID_NumModes ();
1491 for (i=1 ; (i<lnummodes) && (vid_wmodes < MAX_MODEDESCS) ; i++)
1493 ptr = VID_GetModeDescription (i);
1494 pv = VID_GetModePtr (i);
1498 modedescs[k].modenum = i;
1499 modedescs[k].desc = ptr;
1500 modedescs[k].iscur = 0;
1502 if (i == vid_modenum)
1503 modedescs[k].iscur = 1;
1511 M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)");
1516 for (i=0 ; i<vid_wmodes ; i++)
1518 if (modedescs[i].iscur)
1519 M_PrintWhite (column, row, modedescs[i].desc);
1521 M_Print (column, row, modedescs[i].desc);
1525 if ((i % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
1533 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*2, "Video modes must be set from the");
1534 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3, "command line with -width <width>");
1535 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4, "and -bpp <bits-per-pixel>");
1536 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6, "Select windowed mode with -window");
1545 void VID_MenuKey (int key)
1550 S_LocalSound ("misc/menu1.wav");
1551 M_Menu_Options_f ();