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;
66 qboolean scr_skipupdate;
68 static vmode_t modelist[MAX_MODE_LIST];
70 static vmode_t *pcurrentmode;
71 static vmode_t badmode;
73 static DEVMODE gdevmode;
74 static qboolean vid_initialized = false;
75 static qboolean windowed, leavecurrentmode;
76 static qboolean vid_canalttab = false;
77 static qboolean vid_wassuspended = false;
78 static int usingmouse;
79 extern qboolean mouseactive; // from in_win.c
82 int DIBWidth, DIBHeight;
84 DWORD WindowStyle, ExWindowStyle;
88 int vid_modenum = NO_MODE;
90 int vid_default = MODE_WINDOWED;
91 static int windowed_default;
92 unsigned char vid_curpal[256*3];
99 HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow);
101 viddef_t vid; // global video state
103 float gldepthmin, gldepthmax;
105 modestate_t modestate = MS_UNINIT;
107 void VID_MenuDraw (void);
108 void VID_MenuKey (int key);
110 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
111 void AppActivate(BOOL fActive, BOOL minimize);
112 char *VID_GetModeDescription (int mode);
113 void ClearAllStates (void);
114 void VID_UpdateWindowStatus (void);
116 //====================================
118 // Note that 0 is MODE_WINDOWED
119 //cvar_t _vid_default_mode = {"_vid_default_mode","0", true};
120 // Note that 3 is MODE_FULLSCREEN_DEFAULT
121 //cvar_t _vid_default_mode_win = {"_vid_default_mode_win","3", true};
123 int window_center_x, window_center_y, window_x, window_y, window_width, window_height;
126 // direct draw software compatability stuff
128 void CenterWindow(HWND hWndCenter, int width, int height, BOOL lefttopjustify)
130 int CenterX, CenterY;
132 CenterX = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
133 CenterY = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
134 if (CenterX > CenterY*2)
135 CenterX >>= 1; // dual screens
136 CenterX = (CenterX < 0) ? 0: CenterX;
137 CenterY = (CenterY < 0) ? 0: CenterY;
138 SetWindowPos (hWndCenter, NULL, CenterX, CenterY, 0, 0,
139 SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
142 qboolean VID_SetWindowedMode (int modenum)
144 int lastmodestate, width, height;
147 lastmodestate = modestate;
149 WindowRect.top = WindowRect.left = 0;
151 WindowRect.right = modelist[modenum].width;
152 WindowRect.bottom = modelist[modenum].height;
154 DIBWidth = modelist[modenum].width;
155 DIBHeight = modelist[modenum].height;
157 WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
161 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
163 width = rect.right - rect.left;
164 height = rect.bottom - rect.top;
166 // Create the DIB window
167 mainwindow = CreateWindowEx (ExWindowStyle, "DarkPlaces", "DarkPlacesGL", WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
170 Sys_Error ("Couldn't create DIB window");
172 // Center and show the DIB window
173 CenterWindow(mainwindow, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top, false);
175 ShowWindow (mainwindow, SW_SHOWDEFAULT);
176 UpdateWindow (mainwindow);
178 modestate = MS_WINDOWED;
180 if (vid.conheight > modelist[modenum].height)
181 vid.conheight = modelist[modenum].height;
182 if (vid.conwidth > modelist[modenum].width)
183 vid.conwidth = modelist[modenum].width;
184 vid.width = vid.conwidth;
185 vid.height = vid.conheight;
187 SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
188 SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
194 qboolean VID_SetFullDIBMode (int modenum)
196 int lastmodestate, width, height;
199 if (!leavecurrentmode)
201 gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
202 gdevmode.dmBitsPerPel = modelist[modenum].bpp;
203 gdevmode.dmPelsWidth = modelist[modenum].width <<
204 modelist[modenum].halfscreen;
205 gdevmode.dmPelsHeight = modelist[modenum].height;
206 gdevmode.dmSize = sizeof (gdevmode);
208 if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
209 Sys_Error ("Couldn't set fullscreen DIB mode");
212 lastmodestate = modestate;
213 modestate = MS_FULLDIB;
215 WindowRect.top = WindowRect.left = 0;
217 WindowRect.right = modelist[modenum].width;
218 WindowRect.bottom = modelist[modenum].height;
220 DIBWidth = modelist[modenum].width;
221 DIBHeight = modelist[modenum].height;
223 WindowStyle = WS_POPUP;
227 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
229 width = rect.right - rect.left;
230 height = rect.bottom - rect.top;
232 // Create the DIB window
233 mainwindow = CreateWindowEx (ExWindowStyle, "DarkPlaces", "DarkPlacesGL", WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
236 Sys_Error ("Couldn't create DIB window");
238 ShowWindow (mainwindow, SW_SHOWDEFAULT);
239 UpdateWindow (mainwindow);
241 if (vid.conheight > modelist[modenum].height)
242 vid.conheight = modelist[modenum].height;
243 if (vid.conwidth > modelist[modenum].width)
244 vid.conwidth = modelist[modenum].width;
245 vid.width = vid.conwidth;
246 vid.height = vid.conheight;
248 // needed because we're not getting WM_MOVE messages fullscreen on NT
252 SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
253 SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
259 int VID_SetMode (int modenum)
261 int original_mode, temp;
265 if ((windowed && (modenum != 0)) || (!windowed && (modenum < 1)) || (!windowed && (modenum >= nummodes)))
266 Sys_Error ("Bad video mode\n");
268 // so Con_Printfs don't mess us up by forcing vid and snd updates
269 temp = scr_disabled_for_loading;
270 scr_disabled_for_loading = true;
274 if (vid_modenum == NO_MODE)
275 original_mode = windowed_default;
277 original_mode = vid_modenum;
279 // Set either the fullscreen or windowed mode
280 if (modelist[modenum].type == MS_WINDOWED)
282 // if (vid_mouse.value && key_dest == key_game)
284 // stat = VID_SetWindowedMode(modenum);
285 // usingmouse = true;
286 // IN_ActivateMouse ();
291 // usingmouse = false;
292 // IN_DeactivateMouse ();
294 // stat = VID_SetWindowedMode(modenum);
296 stat = VID_SetWindowedMode(modenum);
298 else if (modelist[modenum].type == MS_FULLDIB)
300 stat = VID_SetFullDIBMode(modenum);
301 // usingmouse = true;
302 // IN_ActivateMouse ();
306 Sys_Error ("VID_SetMode: Bad mode type in modelist");
308 window_width = DIBWidth;
309 window_height = DIBHeight;
310 VID_UpdateWindowStatus ();
313 scr_disabled_for_loading = temp;
316 Sys_Error ("Couldn't set video mode");
318 // now we try to make sure we get the focus on the mode switch, because
319 // sometimes in some systems we don't. We grab the foreground, then
320 // finish setting up, pump all our messages, and sleep for a little while
321 // to let messages finish bouncing around the system, then we put
322 // ourselves at the top of the z order, then grab the foreground again,
323 // Who knows if it helps, but it probably doesn't hurt
324 SetForegroundWindow (mainwindow);
325 vid_modenum = modenum;
326 Cvar_SetValue ("vid_mode", (float)vid_modenum);
328 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
330 TranslateMessage (&msg);
331 DispatchMessage (&msg);
336 SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS);
338 SetForegroundWindow (mainwindow);
340 // fix the leftover Alt from any Alt-Tab or the like that switched us away
344 Con_SafePrintf ("Video mode %s initialized.\n", VID_GetModeDescription (vid_modenum));
346 vid.recalc_refdef = 1;
354 VID_UpdateWindowStatus
357 void VID_UpdateWindowStatus (void)
360 window_rect.left = window_x;
361 window_rect.top = window_y;
362 window_rect.right = window_x + window_width;
363 window_rect.bottom = window_y + window_height;
364 window_center_x = (window_rect.left + window_rect.right) / 2;
365 window_center_y = (window_rect.top + window_rect.bottom) / 2;
367 IN_UpdateClipCursor ();
371 //====================================
373 void VID_CheckMultitexture(void)
375 qglMTexCoord2f = NULL;
376 qglSelectTexture = NULL;
378 // Check to see if multitexture is disabled
379 if (COM_CheckParm("-nomtex"))
381 Con_Printf("...multitexture disabled\n");
384 // Test for ARB_multitexture
385 if (!COM_CheckParm("-SGISmtex") && strstr(gl_extensions, "GL_ARB_multitexture "))
387 Con_Printf("...using GL_ARB_multitexture\n");
388 qglMTexCoord2f = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
389 qglSelectTexture = (void *) wglGetProcAddress("glActiveTextureARB");
391 gl_mtex_enum = GL_TEXTURE0_ARB;
393 else if (strstr(gl_extensions, "GL_SGIS_multitexture ")) // Test for SGIS_multitexture (if ARB_multitexture not found)
395 Con_Printf("...using GL_SGIS_multitexture\n");
396 qglMTexCoord2f = (void *) wglGetProcAddress("glMTexCoord2fSGIS");
397 qglSelectTexture = (void *) wglGetProcAddress("glSelectTextureSGIS");
399 gl_mtex_enum = TEXTURE0_SGIS;
402 Con_Printf("...multitexture disabled (not detected)\n");
405 void VID_CheckCVA(void)
407 qglLockArraysEXT = NULL;
408 qglUnlockArraysEXT = NULL;
409 gl_supportslockarrays = false;
410 if (COM_CheckParm("-nocva"))
412 Con_Printf("...compiled vertex arrays disabled\n");
415 if (strstr(gl_extensions, "GL_EXT_compiled_vertex_array"))
417 Con_Printf("...using compiled vertex arrays\n");
418 qglLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT");
419 qglUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
420 gl_supportslockarrays = true;
430 void GL_BeginRendering (int *x, int *y, int *width, int *height)
433 *width = WindowRect.right - WindowRect.left;
434 *height = WindowRect.bottom - WindowRect.top;
436 // if (!wglMakeCurrent( maindc, baseRC ))
437 // Sys_Error ("wglMakeCurrent failed");
439 // glViewport (*x, *y, *width, *height);
443 void GL_EndRendering (void)
446 if (r_render.value && !scr_skipupdate)
449 // handle the mouse state when windowed if that's changed
451 if (vid_mouse.value && key_dest == key_game)
453 if (modestate == MS_FULLDIB)
471 IN_DeactivateMouse ();
477 void VID_SetDefaultMode (void)
479 IN_DeactivateMouse ();
482 void VID_RestoreSystemGamma(void);
484 void VID_Shutdown (void)
493 vid_canalttab = false;
494 hRC = wglGetCurrentContext();
495 hDC = wglGetCurrentDC();
497 wglMakeCurrent(NULL, NULL);
499 // LordHavoc: free textures before closing (may help NVIDIA)
500 for (i = 0;i < 8192;i++)
502 glDeleteTextures(8192, temp);
505 wglDeleteContext(hRC);
507 if (hDC && mainwindow)
508 ReleaseDC(mainwindow, hDC);
510 if (modestate == MS_FULLDIB)
511 ChangeDisplaySettings (NULL, 0);
513 if (maindc && mainwindow)
514 ReleaseDC (mainwindow, maindc);
516 AppActivate(false, false);
518 VID_RestoreSystemGamma();
523 //==========================================================================
526 BOOL bSetupPixelFormat(HDC hDC)
528 static PIXELFORMATDESCRIPTOR pfd = {
529 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
531 PFD_DRAW_TO_WINDOW // support window
532 | PFD_SUPPORT_OPENGL // support OpenGL
533 | PFD_DOUBLEBUFFER , // double buffered
534 PFD_TYPE_RGBA, // RGBA type
535 24, // 24-bit color depth
536 0, 0, 0, 0, 0, 0, // color bits ignored
537 0, // no alpha buffer
538 0, // shift bit ignored
539 0, // no accumulation buffer
540 0, 0, 0, 0, // accum bits ignored
541 32, // 32-bit z-buffer
542 0, // no stencil buffer
543 0, // no auxiliary buffer
544 PFD_MAIN_PLANE, // main layer
546 0, 0, 0 // layer masks ignored
550 if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
552 MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
556 if (SetPixelFormat(hDC, pixelformat, &pfd) == false)
558 MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
567 byte scantokey[128] =
569 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
570 0 ,27 ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'0' ,'-' ,'=' ,K_BACKSPACE,9 , // 0
571 'q' ,'w' ,'e' ,'r' ,'t' ,'y' ,'u' ,'i' ,'o' ,'p' ,'[' ,']' ,13 ,K_CTRL ,'a' ,'s' , // 1
572 'd' ,'f' ,'g' ,'h' ,'j' ,'k' ,'l' ,';' ,'\'' ,'`' ,K_SHIFT,'\\' ,'z' ,'x' ,'c' ,'v' , // 2
573 'b' ,'n' ,'m' ,',' ,'.' ,'/' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2 ,K_F3 ,K_F4 ,K_F5 , // 3
574 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
575 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
576 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
577 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
581 byte shiftscantokey[128] =
583 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
584 0 ,27 ,'!' ,'@' ,'#' ,'$' ,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'_' ,'+' ,K_BACKSPACE,9 , // 0
585 'Q' ,'W' ,'E' ,'R' ,'T' ,'Y' ,'U' ,'I' ,'O' ,'P' ,'{' ,'}' ,13 ,K_CTRL ,'A' ,'S' , // 1
586 'D' ,'F' ,'G' ,'H' ,'J' ,'K' ,'L' ,':' ,'"' ,'~' ,K_SHIFT,'|' ,'Z' ,'X' ,'C' ,'V' , // 2
587 'B' ,'N' ,'M' ,'<' ,'>' ,'?' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2,K_F3 ,K_F4 ,K_F5 , // 3
588 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
589 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
590 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
591 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
599 Map from windows to quake keynums
602 int MapKey (int key, int virtualkey)
607 if (scantokey[key] == 0)
608 Con_DPrintf("key 0x%02x has no translation\n", key);
609 // if (scantokey[key] >= 0x20 && scantokey[key] < 0x7F)
611 return scantokey[key];
615 ===================================================================
619 ===================================================================
627 void ClearAllStates (void)
631 // send an up event for each key, to make sure the server clears them all
632 for (i=0 ; i<256 ; i++)
634 Key_Event (i, false);
641 void VID_RestoreGameGamma(void);
643 void AppActivate(BOOL fActive, BOOL minimize)
644 /****************************************************************************
646 * Function: AppActivate
647 * Parameters: fActive - True if app is activating
649 * Description: If the application is activating, then swap the system
650 * into SYSPAL_NOSTATIC mode so that our palettes will display
653 ****************************************************************************/
655 static BOOL sound_active;
658 Minimized = minimize;
660 // enable/disable sound on focus gain/loss
661 if (!ActiveApp && sound_active)
664 sound_active = false;
666 else if (ActiveApp && !sound_active)
674 if (modestate == MS_FULLDIB)
676 // usingmouse = true;
677 // IN_ActivateMouse ();
679 if (vid_canalttab && vid_wassuspended)
681 vid_wassuspended = false;
682 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
683 ShowWindow(mainwindow, SW_SHOWNORMAL);
686 // LordHavoc: from dabb, fix for alt-tab bug in NVidia drivers
687 MoveWindow(mainwindow,0,0,gdevmode.dmPelsWidth,gdevmode.dmPelsHeight,false);
689 // else if ((modestate == MS_WINDOWED) && vid_mouse.value && key_dest == key_game)
691 // usingmouse = true;
692 // IN_ActivateMouse ();
695 VID_RestoreGameGamma();
701 IN_DeactivateMouse ();
703 if (modestate == MS_FULLDIB)
705 // usingmouse = false;
706 // IN_DeactivateMouse ();
710 ChangeDisplaySettings (NULL, 0);
711 vid_wassuspended = true;
714 // else if ((modestate == MS_WINDOWED) && vid_mouse.value)
716 // usingmouse = false;
717 // IN_DeactivateMouse ();
720 VID_RestoreSystemGamma();
724 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
726 /* main window procedure */
727 LONG WINAPI MainWndProc (
734 int fActive, fMinimized, temp;
735 extern unsigned int uiWheelMessage;
737 if ( uMsg == uiWheelMessage )
738 uMsg = WM_MOUSEWHEEL;
743 if (modestate == MS_FULLDIB)
744 ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
751 window_x = (int) LOWORD(lParam);
752 window_y = (int) HIWORD(lParam);
753 VID_UpdateWindowStatus ();
758 Key_Event (MapKey(lParam, wParam), true);
763 Key_Event (MapKey(lParam, wParam), false);
767 // keep Alt-Space from happening
770 // this is complicated because Win32 seems to pack multiple mouse events into
771 // one update sometimes, so we always check all states and look for events
781 if (wParam & MK_LBUTTON)
784 if (wParam & MK_RBUTTON)
787 if (wParam & MK_MBUTTON)
790 IN_MouseEvent (temp);
794 // JACK: This is the mouse wheel with the Intellimouse
795 // Its delta is either positive or neg, and we generate the proper
798 if ((short) HIWORD(wParam) > 0) {
799 Key_Event(K_MWHEELUP, true);
800 Key_Event(K_MWHEELUP, false);
802 Key_Event(K_MWHEELDOWN, true);
803 Key_Event(K_MWHEELDOWN, false);
811 if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
817 fActive = LOWORD(wParam);
818 fMinimized = (BOOL) HIWORD(wParam);
819 AppActivate(!(fActive == WA_INACTIVE), fMinimized);
821 // fix the leftover Alt from any Alt-Tab or the like that switched us away
829 DestroyWindow (mainwindow);
836 lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
840 /* pass all unhandled messages to DefWindowProc */
841 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
845 /* return 1 if handled message, 0 if not */
855 int VID_NumModes (void)
866 vmode_t *VID_GetModePtr (int modenum)
869 if ((modenum >= 0) && (modenum < nummodes))
870 return &modelist[modenum];
878 VID_GetModeDescription
881 char *VID_GetModeDescription (int mode)
885 static char temp[100];
887 if ((mode < 0) || (mode >= nummodes))
890 if (!leavecurrentmode)
892 pv = VID_GetModePtr (mode);
893 pinfo = pv->modedesc;
897 sprintf (temp, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
905 // KJB: Added this to return the mode driver name in description for console
907 char *VID_GetExtModeDescription (int mode)
909 static char pinfo[40];
912 if ((mode < 0) || (mode >= nummodes))
915 pv = VID_GetModePtr (mode);
916 if (modelist[mode].type == MS_FULLDIB)
918 if (!leavecurrentmode)
919 sprintf(pinfo,"%s fullscreen", pv->modedesc);
921 sprintf (pinfo, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
925 if (modestate == MS_WINDOWED)
926 sprintf(pinfo, "%s windowed", pv->modedesc);
928 sprintf(pinfo, "windowed");
937 VID_DescribeCurrentMode_f
940 void VID_DescribeCurrentMode_f (void)
942 Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum));
951 void VID_NumModes_f (void)
955 Con_Printf ("%d video mode is available\n", nummodes);
957 Con_Printf ("%d video modes are available\n", nummodes);
966 void VID_DescribeMode_f (void)
970 modenum = atoi (Cmd_Argv(1));
972 t = leavecurrentmode;
973 leavecurrentmode = 0;
975 Con_Printf ("%s\n", VID_GetExtModeDescription (modenum));
977 leavecurrentmode = t;
986 void VID_DescribeModes_f (void)
992 lnummodes = VID_NumModes ();
994 t = leavecurrentmode;
995 leavecurrentmode = 0;
997 for (i=1 ; i<lnummodes ; i++)
999 pv = VID_GetModePtr (i);
1000 pinfo = VID_GetExtModeDescription (i);
1001 Con_Printf ("%2d: %s\n", i, pinfo);
1004 leavecurrentmode = t;
1007 void VID_AddMode(int type, int width, int height, int modenum, int halfscreen, int dib, int fullscreen, int bpp)
1010 if (nummodes >= MAX_MODE_LIST)
1012 modelist[nummodes].type = type;
1013 modelist[nummodes].width = width;
1014 modelist[nummodes].height = height;
1015 modelist[nummodes].modenum = modenum;
1016 modelist[nummodes].halfscreen = halfscreen;
1017 modelist[nummodes].dib = dib;
1018 modelist[nummodes].fullscreen = fullscreen;
1019 modelist[nummodes].bpp = bpp;
1021 sprintf (modelist[nummodes].modedesc, "%dx%d", width, height);
1023 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", width, height, bpp);
1024 for (i = 0;i < nummodes;i++)
1026 if (!memcmp(&modelist[i], &modelist[nummodes], sizeof(vmode_t)))
1032 void VID_InitDIB (HINSTANCE hInstance)
1037 // Register the frame class
1039 wc.lpfnWndProc = (WNDPROC)MainWndProc;
1042 wc.hInstance = hInstance;
1044 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
1045 wc.hbrBackground = NULL;
1046 wc.lpszMenuName = 0;
1047 wc.lpszClassName = "DarkPlaces";
1049 if (!RegisterClass (&wc) )
1050 Sys_Error ("Couldn't register window class");
1053 modelist[0].type = MS_WINDOWED;
1055 if (COM_CheckParm("-width"))
1056 modelist[0].width = atoi(com_argv[COM_CheckParm("-width")+1]);
1058 modelist[0].width = 640;
1060 if (modelist[0].width < 320)
1061 modelist[0].width = 320;
1063 if (COM_CheckParm("-height"))
1064 modelist[0].height= atoi(com_argv[COM_CheckParm("-height")+1]);
1066 modelist[0].height = modelist[0].width * 240/320;
1068 if (modelist[0].height < 240)
1069 modelist[0].height = 240;
1071 sprintf (modelist[0].modedesc, "%dx%d", modelist[0].width, modelist[0].height);
1073 modelist[0].modenum = MODE_WINDOWED;
1074 modelist[0].dib = 1;
1075 modelist[0].fullscreen = 0;
1076 modelist[0].halfscreen = 0;
1077 modelist[0].bpp = 0;
1081 if (COM_CheckParm("-width"))
1082 w = atoi(com_argv[COM_CheckParm("-width")+1]);
1089 if (COM_CheckParm("-height"))
1090 h = atoi(com_argv[COM_CheckParm("-height")+1]);
1097 VID_AddMode(MS_WINDOWED, w, h, 0, 0, 1, 0, 0);
1106 void VID_InitFullDIB (HINSTANCE hInstance)
1111 int originalnummodes;
1112 // int existingmode;
1119 // enumerate >8 bpp modes
1120 originalnummodes = nummodes;
1125 stat = EnumDisplaySettings (NULL, modenum, &devmode);
1127 if ((devmode.dmBitsPerPel >= 15) && (devmode.dmPelsWidth <= MAXWIDTH) && (devmode.dmPelsHeight <= MAXHEIGHT) && (nummodes < MAX_MODE_LIST))
1129 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1131 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1133 // if the width is more than twice the height, reduce it by half because this
1134 // is probably a dual-screen monitor
1135 if ((!COM_CheckParm("-noadjustaspect")) && (devmode.dmPelsWidth > (devmode.dmPelsHeight << 1)))
1136 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth >> 1, devmode.dmPelsHeight, 0, 1, 1, 1, devmode.dmBitsPerPel);
1138 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1140 modelist[nummodes].type = MS_FULLDIB;
1141 modelist[nummodes].width = devmode.dmPelsWidth;
1142 modelist[nummodes].height = devmode.dmPelsHeight;
1143 modelist[nummodes].modenum = 0;
1144 modelist[nummodes].halfscreen = 0;
1145 modelist[nummodes].dib = 1;
1146 modelist[nummodes].fullscreen = 1;
1147 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1148 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1150 // if the width is more than twice the height, reduce it by half because this
1151 // is probably a dual-screen monitor
1152 if (!COM_CheckParm("-noadjustaspect"))
1154 if (modelist[nummodes].width > (modelist[nummodes].height << 1))
1156 modelist[nummodes].width >>= 1;
1157 modelist[nummodes].halfscreen = 1;
1158 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", modelist[nummodes].width, modelist[nummodes].height, modelist[nummodes].bpp);
1162 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1164 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1181 // see if there are any low-res modes that aren't being reported
1182 numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]);
1188 for (j=0 ; (j<numlowresmodes) && (nummodes < MAX_MODE_LIST) ; j++)
1190 devmode.dmBitsPerPel = bpp;
1191 devmode.dmPelsWidth = lowresmodes[j].width;
1192 devmode.dmPelsHeight = lowresmodes[j].height;
1193 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1195 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1196 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1199 modelist[nummodes].type = MS_FULLDIB;
1200 modelist[nummodes].width = devmode.dmPelsWidth;
1201 modelist[nummodes].height = devmode.dmPelsHeight;
1202 modelist[nummodes].modenum = 0;
1203 modelist[nummodes].halfscreen = 0;
1204 modelist[nummodes].dib = 1;
1205 modelist[nummodes].fullscreen = 1;
1206 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1207 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1209 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1211 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1240 if (nummodes == originalnummodes)
1241 Con_SafePrintf ("No fullscreen DIB modes found\n");
1244 static int grabsysgamma = true;
1245 WORD systemgammaramps[3][256], currentgammaramps[3][256];
1247 int VID_SetGamma(float prescale, float gamma, float scale, float base)
1253 BuildGammaTable16(prescale, gamma, scale, base, ¤tgammaramps[0][0]);
1254 for (i = 0;i < 256;i++)
1255 currentgammaramps[1][i] = currentgammaramps[2][i] = currentgammaramps[0][i];
1257 i = SetDeviceGammaRamp(hdc, ¤tgammaramps[0][0]);
1259 ReleaseDC (NULL, hdc);
1260 return i; // return success or failure
1263 void VID_RestoreGameGamma(void)
1265 VID_UpdateGamma(true);
1268 void VID_GetSystemGamma(void)
1273 GetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1275 ReleaseDC (NULL, hdc);
1278 void VID_RestoreSystemGamma(void)
1283 SetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1285 ReleaseDC (NULL, hdc);
1293 void VID_Init (void)
1296 // int existingmode;
1297 int basenummodes, width, height, bpp, findbpp, done;
1301 memset(&devmode, 0, sizeof(devmode));
1303 // Cvar_RegisterVariable (&_vid_default_mode);
1304 // Cvar_RegisterVariable (&_vid_default_mode_win);
1306 Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);
1307 Cmd_AddCommand ("vid_describecurrentmode", VID_DescribeCurrentMode_f);
1308 Cmd_AddCommand ("vid_describemode", VID_DescribeMode_f);
1309 Cmd_AddCommand ("vid_describemodes", VID_DescribeModes_f);
1311 VID_GetSystemGamma();
1313 hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON2));
1315 InitCommonControls();
1317 VID_InitDIB (global_hInstance);
1318 basenummodes = nummodes = 1;
1320 VID_InitFullDIB (global_hInstance);
1322 if (COM_CheckParm("-window"))
1326 if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
1327 Sys_Error ("Can't run in non-RGB mode");
1329 ReleaseDC (NULL, hdc);
1333 vid_default = MODE_WINDOWED;
1338 Sys_Error ("No RGB fullscreen modes available");
1342 if (COM_CheckParm("-mode"))
1343 vid_default = atoi(com_argv[COM_CheckParm("-mode")+1]);
1346 if (COM_CheckParm("-current"))
1348 modelist[MODE_FULLSCREEN_DEFAULT].width = GetSystemMetrics (SM_CXSCREEN);
1349 modelist[MODE_FULLSCREEN_DEFAULT].height = GetSystemMetrics (SM_CYSCREEN);
1350 vid_default = MODE_FULLSCREEN_DEFAULT;
1351 leavecurrentmode = 1;
1355 if (COM_CheckParm("-width"))
1356 width = atoi(com_argv[COM_CheckParm("-width")+1]);
1360 if (COM_CheckParm("-bpp"))
1362 bpp = atoi(com_argv[COM_CheckParm("-bpp")+1]);
1371 if (COM_CheckParm("-height"))
1372 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1374 // if they want to force it, add the specified mode to the list
1375 if (COM_CheckParm("-force") && (nummodes < MAX_MODE_LIST))
1376 VID_AddMode(MS_FULLDIB, width, height, 0, 0, 1, 1, bpp);
1379 modelist[nummodes].type = MS_FULLDIB;
1380 modelist[nummodes].width = width;
1381 modelist[nummodes].height = height;
1382 modelist[nummodes].modenum = 0;
1383 modelist[nummodes].halfscreen = 0;
1384 modelist[nummodes].dib = 1;
1385 modelist[nummodes].fullscreen = 1;
1386 modelist[nummodes].bpp = bpp;
1387 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1389 for (i=nummodes, existingmode = 0 ; i<nummodes ; i++)
1391 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1407 if (COM_CheckParm("-height"))
1409 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1411 for (i=1, vid_default=0 ; i<nummodes ; i++)
1413 if ((modelist[i].width == width) && (modelist[i].height == height) && (modelist[i].bpp == bpp))
1423 for (i=1, vid_default=0 ; i<nummodes ; i++)
1425 if ((modelist[i].width == width) && (modelist[i].bpp == bpp))
1440 case 15: bpp = 16;break;
1441 case 16: bpp = 32;break;
1442 case 32: bpp = 24;break;
1443 case 24: done = 1;break;
1453 Sys_Error ("Specified video mode not available");
1458 vid_initialized = true;
1460 if ((i = COM_CheckParm("-conwidth")) != 0)
1461 vid.conwidth = atoi(com_argv[i+1]);
1465 vid.conwidth &= 0xfff8; // make it a multiple of eight
1467 if (vid.conwidth < 320)
1470 // pick a conheight that matches with correct aspect
1471 vid.conheight = vid.conwidth*3 / 4;
1473 if ((i = COM_CheckParm("-conheight")) != 0)
1474 vid.conheight = atoi(com_argv[i+1]);
1475 if (vid.conheight < 200)
1476 vid.conheight = 200;
1478 VID_SetMode (vid_default);
1480 maindc = GetDC(mainwindow);
1481 bSetupPixelFormat(maindc);
1483 baseRC = wglCreateContext( maindc );
1485 Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.");
1486 if (!wglMakeCurrent( maindc, baseRC ))
1487 Sys_Error ("wglMakeCurrent failed");
1491 // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
1492 if (strncasecmp(gl_vendor,"ATI",3)==0)
1494 if (strncasecmp(gl_renderer,"Rage Pro",8)==0)
1497 if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
1500 // sprintf (gldir, "%s/glquake", com_gamedir);
1501 // Sys_mkdir (gldir);
1503 vid_realmode = vid_modenum;
1505 vid_menudrawfn = VID_MenuDraw;
1506 vid_menukeyfn = VID_MenuKey;
1508 strcpy (badmode.modedesc, "Bad mode");
1509 vid_canalttab = true;
1513 //========================================================
1515 //========================================================
1517 extern void M_Menu_Options_f (void);
1518 extern void M_Print (int cx, int cy, char *str);
1519 extern void M_PrintWhite (int cx, int cy, char *str);
1520 extern void M_DrawCharacter (int cx, int line, int num);
1521 extern void M_DrawPic (int x, int y, qpic_t *pic);
1523 static int vid_line, vid_wmodes;
1532 #define MAX_COLUMN_SIZE 9
1533 #define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 2)
1534 #define MAX_MODEDESCS (MAX_COLUMN_SIZE*3)
1536 static modedesc_t modedescs[MAX_MODEDESCS];
1543 void VID_MenuDraw (void)
1547 int lnummodes, i, k, column, row;
1550 p = Draw_CachePic ("gfx/vidmodes.lmp");
1551 M_DrawPic ( (320-p->width)/2, 4, p);
1554 lnummodes = VID_NumModes ();
1556 for (i=1 ; (i<lnummodes) && (vid_wmodes < MAX_MODEDESCS) ; i++)
1558 ptr = VID_GetModeDescription (i);
1559 pv = VID_GetModePtr (i);
1563 modedescs[k].modenum = i;
1564 modedescs[k].desc = ptr;
1565 modedescs[k].iscur = 0;
1567 if (i == vid_modenum)
1568 modedescs[k].iscur = 1;
1576 M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)");
1581 for (i=0 ; i<vid_wmodes ; i++)
1583 if (modedescs[i].iscur)
1584 M_PrintWhite (column, row, modedescs[i].desc);
1586 M_Print (column, row, modedescs[i].desc);
1590 if ((i % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
1598 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*2, "Video modes must be set from the");
1599 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3, "command line with -width <width>");
1600 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4, "and -bpp <bits-per-pixel>");
1601 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6, "Select windowed mode with -window");
1610 void VID_MenuKey (int key)
1615 S_LocalSound ("misc/menu1.wav");
1616 M_Menu_Options_f ();