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 //====================================
372 void GL_BeginRendering (int *x, int *y, int *width, int *height)
375 *width = WindowRect.right - WindowRect.left;
376 *height = WindowRect.bottom - WindowRect.top;
378 // if (!wglMakeCurrent( maindc, baseRC ))
379 // Sys_Error ("wglMakeCurrent failed");
381 // glViewport (*x, *y, *width, *height);
385 void GL_EndRendering (void)
388 if (r_render.integer && !scr_skipupdate)
391 // handle the mouse state when windowed if that's changed
393 if (vid_mouse.integer && key_dest == key_game)
395 if (modestate == MS_FULLDIB)
413 IN_DeactivateMouse ();
419 void VID_SetDefaultMode (void)
421 IN_DeactivateMouse ();
424 void VID_RestoreSystemGamma(void);
426 void VID_Shutdown (void)
435 vid_canalttab = false;
436 hRC = wglGetCurrentContext();
437 hDC = wglGetCurrentDC();
439 wglMakeCurrent(NULL, NULL);
441 // LordHavoc: free textures before closing (may help NVIDIA)
442 for (i = 0;i < 8192;i++)
444 glDeleteTextures(8192, temp);
447 wglDeleteContext(hRC);
449 if (hDC && mainwindow)
450 ReleaseDC(mainwindow, hDC);
452 if (modestate == MS_FULLDIB)
453 ChangeDisplaySettings (NULL, 0);
455 if (maindc && mainwindow)
456 ReleaseDC (mainwindow, maindc);
458 AppActivate(false, false);
460 VID_RestoreSystemGamma();
465 //==========================================================================
468 BOOL bSetupPixelFormat(HDC hDC)
470 static PIXELFORMATDESCRIPTOR pfd = {
471 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
473 PFD_DRAW_TO_WINDOW // support window
474 | PFD_SUPPORT_OPENGL // support OpenGL
475 | PFD_DOUBLEBUFFER , // double buffered
476 PFD_TYPE_RGBA, // RGBA type
477 24, // 24-bit color depth
478 0, 0, 0, 0, 0, 0, // color bits ignored
479 0, // no alpha buffer
480 0, // shift bit ignored
481 0, // no accumulation buffer
482 0, 0, 0, 0, // accum bits ignored
483 32, // 32-bit z-buffer
484 0, // no stencil buffer
485 0, // no auxiliary buffer
486 PFD_MAIN_PLANE, // main layer
488 0, 0, 0 // layer masks ignored
492 if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
494 MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
498 if (SetPixelFormat(hDC, pixelformat, &pfd) == false)
500 MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
509 byte scantokey[128] =
511 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
512 0 ,27 ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'0' ,'-' ,'=' ,K_BACKSPACE,9 , // 0
513 'q' ,'w' ,'e' ,'r' ,'t' ,'y' ,'u' ,'i' ,'o' ,'p' ,'[' ,']' ,13 ,K_CTRL ,'a' ,'s' , // 1
514 'd' ,'f' ,'g' ,'h' ,'j' ,'k' ,'l' ,';' ,'\'' ,'`' ,K_SHIFT,'\\' ,'z' ,'x' ,'c' ,'v' , // 2
515 'b' ,'n' ,'m' ,',' ,'.' ,'/' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2 ,K_F3 ,K_F4 ,K_F5 , // 3
516 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
517 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
518 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
519 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
523 byte shiftscantokey[128] =
525 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
526 0 ,27 ,'!' ,'@' ,'#' ,'$' ,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'_' ,'+' ,K_BACKSPACE,9 , // 0
527 'Q' ,'W' ,'E' ,'R' ,'T' ,'Y' ,'U' ,'I' ,'O' ,'P' ,'{' ,'}' ,13 ,K_CTRL ,'A' ,'S' , // 1
528 'D' ,'F' ,'G' ,'H' ,'J' ,'K' ,'L' ,':' ,'"' ,'~' ,K_SHIFT,'|' ,'Z' ,'X' ,'C' ,'V' , // 2
529 'B' ,'N' ,'M' ,'<' ,'>' ,'?' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2,K_F3 ,K_F4 ,K_F5 , // 3
530 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
531 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
532 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
533 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
541 Map from windows to quake keynums
544 int MapKey (int key, int virtualkey)
549 if (scantokey[key] == 0)
550 Con_DPrintf("key 0x%02x has no translation\n", key);
551 // if (scantokey[key] >= 0x20 && scantokey[key] < 0x7F)
553 return scantokey[key];
557 ===================================================================
561 ===================================================================
569 void ClearAllStates (void)
573 // send an up event for each key, to make sure the server clears them all
574 for (i=0 ; i<256 ; i++)
576 Key_Event (i, false);
583 void VID_RestoreGameGamma(void);
584 extern qboolean host_loopactive;
586 void AppActivate(BOOL fActive, BOOL minimize)
587 /****************************************************************************
589 * Function: AppActivate
590 * Parameters: fActive - True if app is activating
592 * Description: If the application is activating, then swap the system
593 * into SYSPAL_NOSTATIC mode so that our palettes will display
596 ****************************************************************************/
598 static BOOL sound_active;
601 Minimized = minimize;
603 // enable/disable sound on focus gain/loss
604 if (!ActiveApp && sound_active)
607 sound_active = false;
609 else if (ActiveApp && !sound_active)
617 if (modestate == MS_FULLDIB)
619 // usingmouse = true;
620 // IN_ActivateMouse ();
622 if (vid_canalttab && vid_wassuspended)
624 vid_wassuspended = false;
625 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
626 ShowWindow(mainwindow, SW_SHOWNORMAL);
629 // LordHavoc: from dabb, fix for alt-tab bug in NVidia drivers
630 MoveWindow(mainwindow,0,0,gdevmode.dmPelsWidth,gdevmode.dmPelsHeight,false);
632 // else if ((modestate == MS_WINDOWED) && vid_mouse.integer && key_dest == key_game)
634 // usingmouse = true;
635 // IN_ActivateMouse ();
639 VID_RestoreGameGamma();
645 IN_DeactivateMouse ();
647 if (modestate == MS_FULLDIB)
649 // usingmouse = false;
650 // IN_DeactivateMouse ();
654 ChangeDisplaySettings (NULL, 0);
655 vid_wassuspended = true;
658 // else if ((modestate == MS_WINDOWED) && vid_mouse.integer)
660 // usingmouse = false;
661 // IN_DeactivateMouse ();
664 VID_RestoreSystemGamma();
668 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
670 /* main window procedure */
671 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
674 int fActive, fMinimized, temp;
675 extern unsigned int uiWheelMessage;
677 if ( uMsg == uiWheelMessage )
678 uMsg = WM_MOUSEWHEEL;
683 if (modestate == MS_FULLDIB)
684 ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
691 window_x = (int) LOWORD(lParam);
692 window_y = (int) HIWORD(lParam);
693 VID_UpdateWindowStatus ();
698 Key_Event (MapKey(lParam, wParam), true);
703 Key_Event (MapKey(lParam, wParam), false);
707 // keep Alt-Space from happening
710 // this is complicated because Win32 seems to pack multiple mouse events into
711 // one update sometimes, so we always check all states and look for events
721 if (wParam & MK_LBUTTON)
724 if (wParam & MK_RBUTTON)
727 if (wParam & MK_MBUTTON)
730 IN_MouseEvent (temp);
734 // JACK: This is the mouse wheel with the Intellimouse
735 // Its delta is either positive or neg, and we generate the proper
738 if ((short) HIWORD(wParam) > 0) {
739 Key_Event(K_MWHEELUP, true);
740 Key_Event(K_MWHEELUP, false);
742 Key_Event(K_MWHEELDOWN, true);
743 Key_Event(K_MWHEELDOWN, false);
751 if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
757 fActive = LOWORD(wParam);
758 fMinimized = (BOOL) HIWORD(wParam);
759 AppActivate(!(fActive == WA_INACTIVE), fMinimized);
761 // fix the leftover Alt from any Alt-Tab or the like that switched us away
769 DestroyWindow (mainwindow);
776 lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
780 /* pass all unhandled messages to DefWindowProc */
781 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
785 /* return 1 if handled message, 0 if not */
795 int VID_NumModes (void)
806 vmode_t *VID_GetModePtr (int modenum)
809 if ((modenum >= 0) && (modenum < nummodes))
810 return &modelist[modenum];
818 VID_GetModeDescription
821 char *VID_GetModeDescription (int mode)
825 static char temp[100];
827 if ((mode < 0) || (mode >= nummodes))
830 if (!leavecurrentmode)
832 pv = VID_GetModePtr (mode);
833 pinfo = pv->modedesc;
837 sprintf (temp, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
845 // KJB: Added this to return the mode driver name in description for console
847 char *VID_GetExtModeDescription (int mode)
849 static char pinfo[40];
852 if ((mode < 0) || (mode >= nummodes))
855 pv = VID_GetModePtr (mode);
856 if (modelist[mode].type == MS_FULLDIB)
858 if (!leavecurrentmode)
859 sprintf(pinfo,"%s fullscreen", pv->modedesc);
861 sprintf (pinfo, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
865 if (modestate == MS_WINDOWED)
866 sprintf(pinfo, "%s windowed", pv->modedesc);
868 sprintf(pinfo, "windowed");
877 VID_DescribeCurrentMode_f
880 void VID_DescribeCurrentMode_f (void)
882 Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum));
891 void VID_NumModes_f (void)
895 Con_Printf ("%d video mode is available\n", nummodes);
897 Con_Printf ("%d video modes are available\n", nummodes);
906 void VID_DescribeMode_f (void)
910 modenum = atoi (Cmd_Argv(1));
912 t = leavecurrentmode;
913 leavecurrentmode = 0;
915 Con_Printf ("%s\n", VID_GetExtModeDescription (modenum));
917 leavecurrentmode = t;
926 void VID_DescribeModes_f (void)
932 lnummodes = VID_NumModes ();
934 t = leavecurrentmode;
935 leavecurrentmode = 0;
937 for (i=1 ; i<lnummodes ; i++)
939 pv = VID_GetModePtr (i);
940 pinfo = VID_GetExtModeDescription (i);
941 Con_Printf ("%2d: %s\n", i, pinfo);
944 leavecurrentmode = t;
947 void VID_AddMode(int type, int width, int height, int modenum, int halfscreen, int dib, int fullscreen, int bpp)
950 if (nummodes >= MAX_MODE_LIST)
952 modelist[nummodes].type = type;
953 modelist[nummodes].width = width;
954 modelist[nummodes].height = height;
955 modelist[nummodes].modenum = modenum;
956 modelist[nummodes].halfscreen = halfscreen;
957 modelist[nummodes].dib = dib;
958 modelist[nummodes].fullscreen = fullscreen;
959 modelist[nummodes].bpp = bpp;
961 sprintf (modelist[nummodes].modedesc, "%dx%d", width, height);
963 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", width, height, bpp);
964 for (i = 0;i < nummodes;i++)
966 if (!memcmp(&modelist[i], &modelist[nummodes], sizeof(vmode_t)))
972 void VID_InitDIB (HINSTANCE hInstance)
977 // Register the frame class
979 wc.lpfnWndProc = (WNDPROC)MainWndProc;
982 wc.hInstance = hInstance;
984 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
985 wc.hbrBackground = NULL;
987 wc.lpszClassName = gamename;
989 if (!RegisterClass (&wc) )
990 Sys_Error ("Couldn't register window class");
993 modelist[0].type = MS_WINDOWED;
995 if (COM_CheckParm("-width"))
996 modelist[0].width = atoi(com_argv[COM_CheckParm("-width")+1]);
998 modelist[0].width = 640;
1000 if (modelist[0].width < 320)
1001 modelist[0].width = 320;
1003 if (COM_CheckParm("-height"))
1004 modelist[0].height= atoi(com_argv[COM_CheckParm("-height")+1]);
1006 modelist[0].height = modelist[0].width * 240/320;
1008 if (modelist[0].height < 240)
1009 modelist[0].height = 240;
1011 sprintf (modelist[0].modedesc, "%dx%d", modelist[0].width, modelist[0].height);
1013 modelist[0].modenum = MODE_WINDOWED;
1014 modelist[0].dib = 1;
1015 modelist[0].fullscreen = 0;
1016 modelist[0].halfscreen = 0;
1017 modelist[0].bpp = 0;
1021 if (COM_CheckParm("-width"))
1022 w = atoi(com_argv[COM_CheckParm("-width")+1]);
1029 if (COM_CheckParm("-height"))
1030 h = atoi(com_argv[COM_CheckParm("-height")+1]);
1037 VID_AddMode(MS_WINDOWED, w, h, 0, 0, 1, 0, 0);
1046 void VID_InitFullDIB (HINSTANCE hInstance)
1051 int originalnummodes;
1052 // int existingmode;
1059 // enumerate >8 bpp modes
1060 originalnummodes = nummodes;
1065 stat = EnumDisplaySettings (NULL, modenum, &devmode);
1067 if ((devmode.dmBitsPerPel >= 15) && (devmode.dmPelsWidth <= MAXWIDTH) && (devmode.dmPelsHeight <= MAXHEIGHT) && (nummodes < MAX_MODE_LIST))
1069 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1071 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1073 // if the width is more than twice the height, reduce it by half because this
1074 // is probably a dual-screen monitor
1075 if ((!COM_CheckParm("-noadjustaspect")) && (devmode.dmPelsWidth > (devmode.dmPelsHeight << 1)))
1076 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth >> 1, devmode.dmPelsHeight, 0, 1, 1, 1, devmode.dmBitsPerPel);
1078 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1080 modelist[nummodes].type = MS_FULLDIB;
1081 modelist[nummodes].width = devmode.dmPelsWidth;
1082 modelist[nummodes].height = devmode.dmPelsHeight;
1083 modelist[nummodes].modenum = 0;
1084 modelist[nummodes].halfscreen = 0;
1085 modelist[nummodes].dib = 1;
1086 modelist[nummodes].fullscreen = 1;
1087 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1088 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1090 // if the width is more than twice the height, reduce it by half because this
1091 // is probably a dual-screen monitor
1092 if (!COM_CheckParm("-noadjustaspect"))
1094 if (modelist[nummodes].width > (modelist[nummodes].height << 1))
1096 modelist[nummodes].width >>= 1;
1097 modelist[nummodes].halfscreen = 1;
1098 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", modelist[nummodes].width, modelist[nummodes].height, modelist[nummodes].bpp);
1102 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1104 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1121 // see if there are any low-res modes that aren't being reported
1122 numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]);
1128 for (j=0 ; (j<numlowresmodes) && (nummodes < MAX_MODE_LIST) ; j++)
1130 devmode.dmBitsPerPel = bpp;
1131 devmode.dmPelsWidth = lowresmodes[j].width;
1132 devmode.dmPelsHeight = lowresmodes[j].height;
1133 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1135 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1136 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1139 modelist[nummodes].type = MS_FULLDIB;
1140 modelist[nummodes].width = devmode.dmPelsWidth;
1141 modelist[nummodes].height = devmode.dmPelsHeight;
1142 modelist[nummodes].modenum = 0;
1143 modelist[nummodes].halfscreen = 0;
1144 modelist[nummodes].dib = 1;
1145 modelist[nummodes].fullscreen = 1;
1146 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1147 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1149 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1151 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1180 if (nummodes == originalnummodes)
1181 Con_SafePrintf ("No fullscreen DIB modes found\n");
1184 static int grabsysgamma = true;
1185 WORD systemgammaramps[3][256], currentgammaramps[3][256];
1187 int VID_SetGamma(float prescale, float gamma, float scale, float base)
1193 BuildGammaTable16(prescale, gamma, scale, base, ¤tgammaramps[0][0]);
1194 for (i = 0;i < 256;i++)
1195 currentgammaramps[1][i] = currentgammaramps[2][i] = currentgammaramps[0][i];
1197 i = SetDeviceGammaRamp(hdc, ¤tgammaramps[0][0]);
1199 ReleaseDC (NULL, hdc);
1200 return i; // return success or failure
1203 void VID_RestoreGameGamma(void)
1205 VID_UpdateGamma(true);
1208 void VID_GetSystemGamma(void)
1213 GetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1215 ReleaseDC (NULL, hdc);
1218 void VID_RestoreSystemGamma(void)
1223 SetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1225 ReleaseDC (NULL, hdc);
1233 void VID_Init (void)
1236 // int existingmode;
1237 int basenummodes, width, height, bpp, findbpp, done;
1241 memset(&devmode, 0, sizeof(devmode));
1243 // Cvar_RegisterVariable (&_vid_default_mode);
1244 // Cvar_RegisterVariable (&_vid_default_mode_win);
1246 Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);
1247 Cmd_AddCommand ("vid_describecurrentmode", VID_DescribeCurrentMode_f);
1248 Cmd_AddCommand ("vid_describemode", VID_DescribeMode_f);
1249 Cmd_AddCommand ("vid_describemodes", VID_DescribeModes_f);
1251 VID_GetSystemGamma();
1253 hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON2));
1255 InitCommonControls();
1257 VID_InitDIB (global_hInstance);
1258 basenummodes = nummodes = 1;
1260 VID_InitFullDIB (global_hInstance);
1262 if (COM_CheckParm("-window"))
1266 if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
1267 Sys_Error ("Can't run in non-RGB mode");
1269 ReleaseDC (NULL, hdc);
1273 vid_default = MODE_WINDOWED;
1278 Sys_Error ("No RGB fullscreen modes available");
1282 if (COM_CheckParm("-mode"))
1283 vid_default = atoi(com_argv[COM_CheckParm("-mode")+1]);
1286 if (COM_CheckParm("-current"))
1288 modelist[MODE_FULLSCREEN_DEFAULT].width = GetSystemMetrics (SM_CXSCREEN);
1289 modelist[MODE_FULLSCREEN_DEFAULT].height = GetSystemMetrics (SM_CYSCREEN);
1290 vid_default = MODE_FULLSCREEN_DEFAULT;
1291 leavecurrentmode = 1;
1295 if (COM_CheckParm("-width"))
1296 width = atoi(com_argv[COM_CheckParm("-width")+1]);
1300 if (COM_CheckParm("-bpp"))
1302 bpp = atoi(com_argv[COM_CheckParm("-bpp")+1]);
1311 if (COM_CheckParm("-height"))
1312 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1314 // if they want to force it, add the specified mode to the list
1315 if (COM_CheckParm("-force") && (nummodes < MAX_MODE_LIST))
1316 VID_AddMode(MS_FULLDIB, width, height, 0, 0, 1, 1, bpp);
1319 modelist[nummodes].type = MS_FULLDIB;
1320 modelist[nummodes].width = width;
1321 modelist[nummodes].height = height;
1322 modelist[nummodes].modenum = 0;
1323 modelist[nummodes].halfscreen = 0;
1324 modelist[nummodes].dib = 1;
1325 modelist[nummodes].fullscreen = 1;
1326 modelist[nummodes].bpp = bpp;
1327 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1329 for (i=nummodes, existingmode = 0 ; i<nummodes ; i++)
1331 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1347 if (COM_CheckParm("-height"))
1349 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1351 for (i=1, vid_default=0 ; i<nummodes ; i++)
1353 if ((modelist[i].width == width) && (modelist[i].height == height) && (modelist[i].bpp == bpp))
1363 for (i=1, vid_default=0 ; i<nummodes ; i++)
1365 if ((modelist[i].width == width) && (modelist[i].bpp == bpp))
1380 case 15: bpp = 16;break;
1381 case 16: bpp = 32;break;
1382 case 32: bpp = 24;break;
1383 case 24: done = 1;break;
1393 Sys_Error ("Specified video mode not available");
1398 vid_initialized = true;
1400 if ((i = COM_CheckParm("-conwidth")) != 0)
1401 vid.conwidth = atoi(com_argv[i+1]);
1405 vid.conwidth &= 0xfff8; // make it a multiple of eight
1407 if (vid.conwidth < 320)
1410 // pick a conheight that matches with correct aspect
1411 vid.conheight = vid.conwidth*3 / 4;
1413 if ((i = COM_CheckParm("-conheight")) != 0)
1414 vid.conheight = atoi(com_argv[i+1]);
1415 if (vid.conheight < 200)
1416 vid.conheight = 200;
1418 VID_SetMode (vid_default);
1420 maindc = GetDC(mainwindow);
1421 bSetupPixelFormat(maindc);
1423 baseRC = wglCreateContext( maindc );
1425 Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.");
1426 if (!wglMakeCurrent( maindc, baseRC ))
1427 Sys_Error ("wglMakeCurrent failed");
1431 // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
1432 if (strncasecmp(gl_vendor,"ATI",3)==0)
1434 if (strncasecmp(gl_renderer,"Rage Pro",8)==0)
1437 if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
1440 // sprintf (gldir, "%s/glquake", com_gamedir);
1441 // Sys_mkdir (gldir);
1443 vid_realmode = vid_modenum;
1445 vid_menudrawfn = VID_MenuDraw;
1446 vid_menukeyfn = VID_MenuKey;
1448 strcpy (badmode.modedesc, "Bad mode");
1449 vid_canalttab = true;
1453 //========================================================
1455 //========================================================
1457 extern void M_Menu_Options_f (void);
1458 extern void M_Print (int cx, int cy, char *str);
1459 extern void M_PrintWhite (int cx, int cy, char *str);
1460 extern void M_DrawCharacter (int cx, int line, int num);
1461 extern void M_DrawPic (int x, int y, qpic_t *pic);
1463 static int vid_line, vid_wmodes;
1472 #define MAX_COLUMN_SIZE 9
1473 #define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 2)
1474 #define MAX_MODEDESCS (MAX_COLUMN_SIZE*3)
1476 static modedesc_t modedescs[MAX_MODEDESCS];
1483 void VID_MenuDraw (void)
1487 int lnummodes, i, k, column, row;
1490 p = Draw_CachePic ("gfx/vidmodes.lmp");
1491 M_DrawPic ( (320-p->width)/2, 4, p);
1494 lnummodes = VID_NumModes ();
1496 for (i=1 ; (i<lnummodes) && (vid_wmodes < MAX_MODEDESCS) ; i++)
1498 ptr = VID_GetModeDescription (i);
1499 pv = VID_GetModePtr (i);
1503 modedescs[k].modenum = i;
1504 modedescs[k].desc = ptr;
1505 modedescs[k].iscur = 0;
1507 if (i == vid_modenum)
1508 modedescs[k].iscur = 1;
1516 M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)");
1521 for (i=0 ; i<vid_wmodes ; i++)
1523 if (modedescs[i].iscur)
1524 M_PrintWhite (column, row, modedescs[i].desc);
1526 M_Print (column, row, modedescs[i].desc);
1530 if ((i % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
1538 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*2, "Video modes must be set from the");
1539 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3, "command line with -width <width>");
1540 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4, "and -bpp <bits-per-pixel>");
1541 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6, "Select windowed mode with -window");
1550 void VID_MenuKey (int key)
1555 S_LocalSound ("misc/menu1.wav");
1556 M_Menu_Options_f ();