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];
98 HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow);
100 viddef_t vid; // global video state
102 float gldepthmin, gldepthmax;
104 modestate_t modestate = MS_UNINIT;
106 void VID_MenuDraw (void);
107 void VID_MenuKey (int key);
109 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
110 void AppActivate(BOOL fActive, BOOL minimize);
111 char *VID_GetModeDescription (int mode);
112 void ClearAllStates (void);
113 void VID_UpdateWindowStatus (void);
115 //====================================
117 // Note that 0 is MODE_WINDOWED
118 //cvar_t _vid_default_mode = {"_vid_default_mode","0", true};
119 // Note that 3 is MODE_FULLSCREEN_DEFAULT
120 //cvar_t _vid_default_mode_win = {"_vid_default_mode_win","3", true};
122 int window_center_x, window_center_y, window_x, window_y, window_width, window_height;
125 // direct draw software compatability stuff
127 void CenterWindow(HWND hWndCenter, int width, int height, BOOL lefttopjustify)
129 int CenterX, CenterY;
131 CenterX = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
132 CenterY = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
133 if (CenterX > CenterY*2)
134 CenterX >>= 1; // dual screens
135 CenterX = (CenterX < 0) ? 0: CenterX;
136 CenterY = (CenterY < 0) ? 0: CenterY;
137 SetWindowPos (hWndCenter, NULL, CenterX, CenterY, 0, 0,
138 SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
141 qboolean VID_SetWindowedMode (int modenum)
143 int lastmodestate, width, height;
146 lastmodestate = modestate;
148 WindowRect.top = WindowRect.left = 0;
150 WindowRect.right = modelist[modenum].width;
151 WindowRect.bottom = modelist[modenum].height;
153 DIBWidth = modelist[modenum].width;
154 DIBHeight = modelist[modenum].height;
156 WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
160 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
162 width = rect.right - rect.left;
163 height = rect.bottom - rect.top;
165 // Create the DIB window
166 mainwindow = CreateWindowEx (ExWindowStyle, "DarkPlaces", "DarkPlacesGL", WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
169 Sys_Error ("Couldn't create DIB window");
171 // Center and show the DIB window
172 CenterWindow(mainwindow, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top, false);
174 ShowWindow (mainwindow, SW_SHOWDEFAULT);
175 UpdateWindow (mainwindow);
177 modestate = MS_WINDOWED;
179 if (vid.conheight > modelist[modenum].height)
180 vid.conheight = modelist[modenum].height;
181 if (vid.conwidth > modelist[modenum].width)
182 vid.conwidth = modelist[modenum].width;
183 vid.width = vid.conwidth;
184 vid.height = vid.conheight;
186 SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
187 SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
193 qboolean VID_SetFullDIBMode (int modenum)
195 int lastmodestate, width, height;
198 if (!leavecurrentmode)
200 gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
201 gdevmode.dmBitsPerPel = modelist[modenum].bpp;
202 gdevmode.dmPelsWidth = modelist[modenum].width <<
203 modelist[modenum].halfscreen;
204 gdevmode.dmPelsHeight = modelist[modenum].height;
205 gdevmode.dmSize = sizeof (gdevmode);
207 if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
208 Sys_Error ("Couldn't set fullscreen DIB mode");
211 lastmodestate = modestate;
212 modestate = MS_FULLDIB;
214 WindowRect.top = WindowRect.left = 0;
216 WindowRect.right = modelist[modenum].width;
217 WindowRect.bottom = modelist[modenum].height;
219 DIBWidth = modelist[modenum].width;
220 DIBHeight = modelist[modenum].height;
222 WindowStyle = WS_POPUP;
226 AdjustWindowRectEx(&rect, WindowStyle, false, 0);
228 width = rect.right - rect.left;
229 height = rect.bottom - rect.top;
231 // Create the DIB window
232 mainwindow = CreateWindowEx (ExWindowStyle, "DarkPlaces", "DarkPlacesGL", WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
235 Sys_Error ("Couldn't create DIB window");
237 ShowWindow (mainwindow, SW_SHOWDEFAULT);
238 UpdateWindow (mainwindow);
240 if (vid.conheight > modelist[modenum].height)
241 vid.conheight = modelist[modenum].height;
242 if (vid.conwidth > modelist[modenum].width)
243 vid.conwidth = modelist[modenum].width;
244 vid.width = vid.conwidth;
245 vid.height = vid.conheight;
247 // needed because we're not getting WM_MOVE messages fullscreen on NT
251 SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
252 SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
258 int VID_SetMode (int modenum)
260 int original_mode, temp;
264 if ((windowed && (modenum != 0)) || (!windowed && (modenum < 1)) || (!windowed && (modenum >= nummodes)))
265 Sys_Error ("Bad video mode\n");
267 // so Con_Printfs don't mess us up by forcing vid and snd updates
268 temp = scr_disabled_for_loading;
269 scr_disabled_for_loading = true;
273 if (vid_modenum == NO_MODE)
274 original_mode = windowed_default;
276 original_mode = vid_modenum;
278 // Set either the fullscreen or windowed mode
279 if (modelist[modenum].type == MS_WINDOWED)
281 // if (vid_mouse.value && key_dest == key_game)
283 // stat = VID_SetWindowedMode(modenum);
284 // usingmouse = true;
285 // IN_ActivateMouse ();
290 // usingmouse = false;
291 // IN_DeactivateMouse ();
293 // stat = VID_SetWindowedMode(modenum);
295 stat = VID_SetWindowedMode(modenum);
297 else if (modelist[modenum].type == MS_FULLDIB)
299 stat = VID_SetFullDIBMode(modenum);
300 // usingmouse = true;
301 // IN_ActivateMouse ();
305 Sys_Error ("VID_SetMode: Bad mode type in modelist");
307 window_width = DIBWidth;
308 window_height = DIBHeight;
309 VID_UpdateWindowStatus ();
312 scr_disabled_for_loading = temp;
315 Sys_Error ("Couldn't set video mode");
317 // now we try to make sure we get the focus on the mode switch, because
318 // sometimes in some systems we don't. We grab the foreground, then
319 // finish setting up, pump all our messages, and sleep for a little while
320 // to let messages finish bouncing around the system, then we put
321 // ourselves at the top of the z order, then grab the foreground again,
322 // Who knows if it helps, but it probably doesn't hurt
323 SetForegroundWindow (mainwindow);
324 vid_modenum = modenum;
325 Cvar_SetValue ("vid_mode", (float)vid_modenum);
327 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
329 TranslateMessage (&msg);
330 DispatchMessage (&msg);
335 SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS);
337 SetForegroundWindow (mainwindow);
339 // fix the leftover Alt from any Alt-Tab or the like that switched us away
343 Con_SafePrintf ("Video mode %s initialized.\n", VID_GetModeDescription (vid_modenum));
345 vid.recalc_refdef = 1;
353 VID_UpdateWindowStatus
356 void VID_UpdateWindowStatus (void)
359 window_rect.left = window_x;
360 window_rect.top = window_y;
361 window_rect.right = window_x + window_width;
362 window_rect.bottom = window_y + window_height;
363 window_center_x = (window_rect.left + window_rect.right) / 2;
364 window_center_y = (window_rect.top + window_rect.bottom) / 2;
366 IN_UpdateClipCursor ();
370 //====================================
372 void VID_CheckMultitexture(void)
374 qglMTexCoord2f = NULL;
375 qglSelectTexture = NULL;
377 // Check to see if multitexture is disabled
378 if (COM_CheckParm("-nomtex"))
380 Con_Printf("...multitexture disabled\n");
383 // Test for ARB_multitexture
384 if (!COM_CheckParm("-SGISmtex") && strstr(gl_extensions, "GL_ARB_multitexture "))
386 Con_Printf("...using GL_ARB_multitexture\n");
387 qglMTexCoord2f = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
388 qglSelectTexture = (void *) wglGetProcAddress("glActiveTextureARB");
390 gl_mtex_enum = GL_TEXTURE0_ARB;
392 else if (strstr(gl_extensions, "GL_SGIS_multitexture ")) // Test for SGIS_multitexture (if ARB_multitexture not found)
394 Con_Printf("...using GL_SGIS_multitexture\n");
395 qglMTexCoord2f = (void *) wglGetProcAddress("glMTexCoord2fSGIS");
396 qglSelectTexture = (void *) wglGetProcAddress("glSelectTextureSGIS");
398 gl_mtex_enum = TEXTURE0_SGIS;
401 Con_Printf("...multitexture disabled (not detected)\n");
404 void VID_CheckCVA(void)
406 qglLockArraysEXT = NULL;
407 qglUnlockArraysEXT = NULL;
408 gl_supportslockarrays = false;
409 if (COM_CheckParm("-nocva"))
411 Con_Printf("...compiled vertex arrays disabled\n");
414 if (strstr(gl_extensions, "GL_EXT_compiled_vertex_array"))
416 Con_Printf("...using compiled vertex arrays\n");
417 qglLockArraysEXT = (void *) wglGetProcAddress("glLockArraysEXT");
418 qglUnlockArraysEXT = (void *) wglGetProcAddress("glUnlockArraysEXT");
419 gl_supportslockarrays = true;
429 void GL_BeginRendering (int *x, int *y, int *width, int *height)
432 *width = WindowRect.right - WindowRect.left;
433 *height = WindowRect.bottom - WindowRect.top;
435 // if (!wglMakeCurrent( maindc, baseRC ))
436 // Sys_Error ("wglMakeCurrent failed");
438 // glViewport (*x, *y, *width, *height);
442 void GL_EndRendering (void)
445 if (r_render.value && !scr_skipupdate)
448 // handle the mouse state when windowed if that's changed
450 if (vid_mouse.value && key_dest == key_game)
452 if (modestate == MS_FULLDIB)
470 IN_DeactivateMouse ();
476 void VID_SetDefaultMode (void)
478 IN_DeactivateMouse ();
481 void VID_RestoreSystemGamma(void);
483 void VID_Shutdown (void)
492 vid_canalttab = false;
493 hRC = wglGetCurrentContext();
494 hDC = wglGetCurrentDC();
496 wglMakeCurrent(NULL, NULL);
498 // LordHavoc: free textures before closing (may help NVIDIA)
499 for (i = 0;i < 8192;i++)
501 glDeleteTextures(8192, temp);
504 wglDeleteContext(hRC);
506 if (hDC && mainwindow)
507 ReleaseDC(mainwindow, hDC);
509 if (modestate == MS_FULLDIB)
510 ChangeDisplaySettings (NULL, 0);
512 if (maindc && mainwindow)
513 ReleaseDC (mainwindow, maindc);
515 AppActivate(false, false);
517 VID_RestoreSystemGamma();
522 //==========================================================================
525 BOOL bSetupPixelFormat(HDC hDC)
527 static PIXELFORMATDESCRIPTOR pfd = {
528 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
530 PFD_DRAW_TO_WINDOW // support window
531 | PFD_SUPPORT_OPENGL // support OpenGL
532 | PFD_DOUBLEBUFFER , // double buffered
533 PFD_TYPE_RGBA, // RGBA type
534 24, // 24-bit color depth
535 0, 0, 0, 0, 0, 0, // color bits ignored
536 0, // no alpha buffer
537 0, // shift bit ignored
538 0, // no accumulation buffer
539 0, 0, 0, 0, // accum bits ignored
540 32, // 32-bit z-buffer
541 0, // no stencil buffer
542 0, // no auxiliary buffer
543 PFD_MAIN_PLANE, // main layer
545 0, 0, 0 // layer masks ignored
549 if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
551 MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
555 if (SetPixelFormat(hDC, pixelformat, &pfd) == false)
557 MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
566 byte scantokey[128] =
568 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
569 0 ,27 ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'0' ,'-' ,'=' ,K_BACKSPACE,9 , // 0
570 'q' ,'w' ,'e' ,'r' ,'t' ,'y' ,'u' ,'i' ,'o' ,'p' ,'[' ,']' ,13 ,K_CTRL ,'a' ,'s' , // 1
571 'd' ,'f' ,'g' ,'h' ,'j' ,'k' ,'l' ,';' ,'\'' ,'`' ,K_SHIFT,'\\' ,'z' ,'x' ,'c' ,'v' , // 2
572 'b' ,'n' ,'m' ,',' ,'.' ,'/' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2 ,K_F3 ,K_F4 ,K_F5 , // 3
573 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
574 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
575 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
576 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
580 byte shiftscantokey[128] =
582 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
583 0 ,27 ,'!' ,'@' ,'#' ,'$' ,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'_' ,'+' ,K_BACKSPACE,9 , // 0
584 'Q' ,'W' ,'E' ,'R' ,'T' ,'Y' ,'U' ,'I' ,'O' ,'P' ,'{' ,'}' ,13 ,K_CTRL ,'A' ,'S' , // 1
585 'D' ,'F' ,'G' ,'H' ,'J' ,'K' ,'L' ,':' ,'"' ,'~' ,K_SHIFT,'|' ,'Z' ,'X' ,'C' ,'V' , // 2
586 'B' ,'N' ,'M' ,'<' ,'>' ,'?' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2,K_F3 ,K_F4 ,K_F5 , // 3
587 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
588 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
589 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
590 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
598 Map from windows to quake keynums
601 int MapKey (int key, int virtualkey)
606 if (scantokey[key] == 0)
607 Con_DPrintf("key 0x%02x has no translation\n", key);
608 // if (scantokey[key] >= 0x20 && scantokey[key] < 0x7F)
610 return scantokey[key];
614 ===================================================================
618 ===================================================================
626 void ClearAllStates (void)
630 // send an up event for each key, to make sure the server clears them all
631 for (i=0 ; i<256 ; i++)
633 Key_Event (i, false);
640 void VID_RestoreGameGamma(void);
642 void AppActivate(BOOL fActive, BOOL minimize)
643 /****************************************************************************
645 * Function: AppActivate
646 * Parameters: fActive - True if app is activating
648 * Description: If the application is activating, then swap the system
649 * into SYSPAL_NOSTATIC mode so that our palettes will display
652 ****************************************************************************/
654 static BOOL sound_active;
657 Minimized = minimize;
659 // enable/disable sound on focus gain/loss
660 if (!ActiveApp && sound_active)
663 sound_active = false;
665 else if (ActiveApp && !sound_active)
673 if (modestate == MS_FULLDIB)
675 // usingmouse = true;
676 // IN_ActivateMouse ();
678 if (vid_canalttab && vid_wassuspended)
680 vid_wassuspended = false;
681 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
682 ShowWindow(mainwindow, SW_SHOWNORMAL);
685 // LordHavoc: from dabb, fix for alt-tab bug in NVidia drivers
686 MoveWindow(mainwindow,0,0,gdevmode.dmPelsWidth,gdevmode.dmPelsHeight,false);
688 // else if ((modestate == MS_WINDOWED) && vid_mouse.value && key_dest == key_game)
690 // usingmouse = true;
691 // IN_ActivateMouse ();
694 VID_RestoreGameGamma();
700 IN_DeactivateMouse ();
702 if (modestate == MS_FULLDIB)
704 // usingmouse = false;
705 // IN_DeactivateMouse ();
709 ChangeDisplaySettings (NULL, 0);
710 vid_wassuspended = true;
713 // else if ((modestate == MS_WINDOWED) && vid_mouse.value)
715 // usingmouse = false;
716 // IN_DeactivateMouse ();
719 VID_RestoreSystemGamma();
723 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
725 /* main window procedure */
726 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
729 int fActive, fMinimized, temp;
730 extern unsigned int uiWheelMessage;
732 if ( uMsg == uiWheelMessage )
733 uMsg = WM_MOUSEWHEEL;
738 if (modestate == MS_FULLDIB)
739 ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
746 window_x = (int) LOWORD(lParam);
747 window_y = (int) HIWORD(lParam);
748 VID_UpdateWindowStatus ();
753 Key_Event (MapKey(lParam, wParam), true);
758 Key_Event (MapKey(lParam, wParam), false);
762 // keep Alt-Space from happening
765 // this is complicated because Win32 seems to pack multiple mouse events into
766 // one update sometimes, so we always check all states and look for events
776 if (wParam & MK_LBUTTON)
779 if (wParam & MK_RBUTTON)
782 if (wParam & MK_MBUTTON)
785 IN_MouseEvent (temp);
789 // JACK: This is the mouse wheel with the Intellimouse
790 // Its delta is either positive or neg, and we generate the proper
793 if ((short) HIWORD(wParam) > 0) {
794 Key_Event(K_MWHEELUP, true);
795 Key_Event(K_MWHEELUP, false);
797 Key_Event(K_MWHEELDOWN, true);
798 Key_Event(K_MWHEELDOWN, false);
806 if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
812 fActive = LOWORD(wParam);
813 fMinimized = (BOOL) HIWORD(wParam);
814 AppActivate(!(fActive == WA_INACTIVE), fMinimized);
816 // fix the leftover Alt from any Alt-Tab or the like that switched us away
824 DestroyWindow (mainwindow);
831 lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
835 /* pass all unhandled messages to DefWindowProc */
836 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
840 /* return 1 if handled message, 0 if not */
850 int VID_NumModes (void)
861 vmode_t *VID_GetModePtr (int modenum)
864 if ((modenum >= 0) && (modenum < nummodes))
865 return &modelist[modenum];
873 VID_GetModeDescription
876 char *VID_GetModeDescription (int mode)
880 static char temp[100];
882 if ((mode < 0) || (mode >= nummodes))
885 if (!leavecurrentmode)
887 pv = VID_GetModePtr (mode);
888 pinfo = pv->modedesc;
892 sprintf (temp, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
900 // KJB: Added this to return the mode driver name in description for console
902 char *VID_GetExtModeDescription (int mode)
904 static char pinfo[40];
907 if ((mode < 0) || (mode >= nummodes))
910 pv = VID_GetModePtr (mode);
911 if (modelist[mode].type == MS_FULLDIB)
913 if (!leavecurrentmode)
914 sprintf(pinfo,"%s fullscreen", pv->modedesc);
916 sprintf (pinfo, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
920 if (modestate == MS_WINDOWED)
921 sprintf(pinfo, "%s windowed", pv->modedesc);
923 sprintf(pinfo, "windowed");
932 VID_DescribeCurrentMode_f
935 void VID_DescribeCurrentMode_f (void)
937 Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum));
946 void VID_NumModes_f (void)
950 Con_Printf ("%d video mode is available\n", nummodes);
952 Con_Printf ("%d video modes are available\n", nummodes);
961 void VID_DescribeMode_f (void)
965 modenum = atoi (Cmd_Argv(1));
967 t = leavecurrentmode;
968 leavecurrentmode = 0;
970 Con_Printf ("%s\n", VID_GetExtModeDescription (modenum));
972 leavecurrentmode = t;
981 void VID_DescribeModes_f (void)
987 lnummodes = VID_NumModes ();
989 t = leavecurrentmode;
990 leavecurrentmode = 0;
992 for (i=1 ; i<lnummodes ; i++)
994 pv = VID_GetModePtr (i);
995 pinfo = VID_GetExtModeDescription (i);
996 Con_Printf ("%2d: %s\n", i, pinfo);
999 leavecurrentmode = t;
1002 void VID_AddMode(int type, int width, int height, int modenum, int halfscreen, int dib, int fullscreen, int bpp)
1005 if (nummodes >= MAX_MODE_LIST)
1007 modelist[nummodes].type = type;
1008 modelist[nummodes].width = width;
1009 modelist[nummodes].height = height;
1010 modelist[nummodes].modenum = modenum;
1011 modelist[nummodes].halfscreen = halfscreen;
1012 modelist[nummodes].dib = dib;
1013 modelist[nummodes].fullscreen = fullscreen;
1014 modelist[nummodes].bpp = bpp;
1016 sprintf (modelist[nummodes].modedesc, "%dx%d", width, height);
1018 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", width, height, bpp);
1019 for (i = 0;i < nummodes;i++)
1021 if (!memcmp(&modelist[i], &modelist[nummodes], sizeof(vmode_t)))
1027 void VID_InitDIB (HINSTANCE hInstance)
1032 // Register the frame class
1034 wc.lpfnWndProc = (WNDPROC)MainWndProc;
1037 wc.hInstance = hInstance;
1039 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
1040 wc.hbrBackground = NULL;
1041 wc.lpszMenuName = 0;
1042 wc.lpszClassName = "DarkPlaces";
1044 if (!RegisterClass (&wc) )
1045 Sys_Error ("Couldn't register window class");
1048 modelist[0].type = MS_WINDOWED;
1050 if (COM_CheckParm("-width"))
1051 modelist[0].width = atoi(com_argv[COM_CheckParm("-width")+1]);
1053 modelist[0].width = 640;
1055 if (modelist[0].width < 320)
1056 modelist[0].width = 320;
1058 if (COM_CheckParm("-height"))
1059 modelist[0].height= atoi(com_argv[COM_CheckParm("-height")+1]);
1061 modelist[0].height = modelist[0].width * 240/320;
1063 if (modelist[0].height < 240)
1064 modelist[0].height = 240;
1066 sprintf (modelist[0].modedesc, "%dx%d", modelist[0].width, modelist[0].height);
1068 modelist[0].modenum = MODE_WINDOWED;
1069 modelist[0].dib = 1;
1070 modelist[0].fullscreen = 0;
1071 modelist[0].halfscreen = 0;
1072 modelist[0].bpp = 0;
1076 if (COM_CheckParm("-width"))
1077 w = atoi(com_argv[COM_CheckParm("-width")+1]);
1084 if (COM_CheckParm("-height"))
1085 h = atoi(com_argv[COM_CheckParm("-height")+1]);
1092 VID_AddMode(MS_WINDOWED, w, h, 0, 0, 1, 0, 0);
1101 void VID_InitFullDIB (HINSTANCE hInstance)
1106 int originalnummodes;
1107 // int existingmode;
1114 // enumerate >8 bpp modes
1115 originalnummodes = nummodes;
1120 stat = EnumDisplaySettings (NULL, modenum, &devmode);
1122 if ((devmode.dmBitsPerPel >= 15) && (devmode.dmPelsWidth <= MAXWIDTH) && (devmode.dmPelsHeight <= MAXHEIGHT) && (nummodes < MAX_MODE_LIST))
1124 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1126 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1128 // if the width is more than twice the height, reduce it by half because this
1129 // is probably a dual-screen monitor
1130 if ((!COM_CheckParm("-noadjustaspect")) && (devmode.dmPelsWidth > (devmode.dmPelsHeight << 1)))
1131 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth >> 1, devmode.dmPelsHeight, 0, 1, 1, 1, devmode.dmBitsPerPel);
1133 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1135 modelist[nummodes].type = MS_FULLDIB;
1136 modelist[nummodes].width = devmode.dmPelsWidth;
1137 modelist[nummodes].height = devmode.dmPelsHeight;
1138 modelist[nummodes].modenum = 0;
1139 modelist[nummodes].halfscreen = 0;
1140 modelist[nummodes].dib = 1;
1141 modelist[nummodes].fullscreen = 1;
1142 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1143 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1145 // if the width is more than twice the height, reduce it by half because this
1146 // is probably a dual-screen monitor
1147 if (!COM_CheckParm("-noadjustaspect"))
1149 if (modelist[nummodes].width > (modelist[nummodes].height << 1))
1151 modelist[nummodes].width >>= 1;
1152 modelist[nummodes].halfscreen = 1;
1153 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", modelist[nummodes].width, modelist[nummodes].height, modelist[nummodes].bpp);
1157 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1159 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1176 // see if there are any low-res modes that aren't being reported
1177 numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]);
1183 for (j=0 ; (j<numlowresmodes) && (nummodes < MAX_MODE_LIST) ; j++)
1185 devmode.dmBitsPerPel = bpp;
1186 devmode.dmPelsWidth = lowresmodes[j].width;
1187 devmode.dmPelsHeight = lowresmodes[j].height;
1188 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1190 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1191 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1194 modelist[nummodes].type = MS_FULLDIB;
1195 modelist[nummodes].width = devmode.dmPelsWidth;
1196 modelist[nummodes].height = devmode.dmPelsHeight;
1197 modelist[nummodes].modenum = 0;
1198 modelist[nummodes].halfscreen = 0;
1199 modelist[nummodes].dib = 1;
1200 modelist[nummodes].fullscreen = 1;
1201 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1202 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1204 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1206 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1235 if (nummodes == originalnummodes)
1236 Con_SafePrintf ("No fullscreen DIB modes found\n");
1239 static int grabsysgamma = true;
1240 WORD systemgammaramps[3][256], currentgammaramps[3][256];
1242 int VID_SetGamma(float prescale, float gamma, float scale, float base)
1248 BuildGammaTable16(prescale, gamma, scale, base, ¤tgammaramps[0][0]);
1249 for (i = 0;i < 256;i++)
1250 currentgammaramps[1][i] = currentgammaramps[2][i] = currentgammaramps[0][i];
1252 i = SetDeviceGammaRamp(hdc, ¤tgammaramps[0][0]);
1254 ReleaseDC (NULL, hdc);
1255 return i; // return success or failure
1258 void VID_RestoreGameGamma(void)
1260 VID_UpdateGamma(true);
1263 void VID_GetSystemGamma(void)
1268 GetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1270 ReleaseDC (NULL, hdc);
1273 void VID_RestoreSystemGamma(void)
1278 SetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1280 ReleaseDC (NULL, hdc);
1288 void VID_Init (void)
1291 // int existingmode;
1292 int basenummodes, width, height, bpp, findbpp, done;
1296 memset(&devmode, 0, sizeof(devmode));
1298 // Cvar_RegisterVariable (&_vid_default_mode);
1299 // Cvar_RegisterVariable (&_vid_default_mode_win);
1301 Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);
1302 Cmd_AddCommand ("vid_describecurrentmode", VID_DescribeCurrentMode_f);
1303 Cmd_AddCommand ("vid_describemode", VID_DescribeMode_f);
1304 Cmd_AddCommand ("vid_describemodes", VID_DescribeModes_f);
1306 VID_GetSystemGamma();
1308 hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON2));
1310 InitCommonControls();
1312 VID_InitDIB (global_hInstance);
1313 basenummodes = nummodes = 1;
1315 VID_InitFullDIB (global_hInstance);
1317 if (COM_CheckParm("-window"))
1321 if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
1322 Sys_Error ("Can't run in non-RGB mode");
1324 ReleaseDC (NULL, hdc);
1328 vid_default = MODE_WINDOWED;
1333 Sys_Error ("No RGB fullscreen modes available");
1337 if (COM_CheckParm("-mode"))
1338 vid_default = atoi(com_argv[COM_CheckParm("-mode")+1]);
1341 if (COM_CheckParm("-current"))
1343 modelist[MODE_FULLSCREEN_DEFAULT].width = GetSystemMetrics (SM_CXSCREEN);
1344 modelist[MODE_FULLSCREEN_DEFAULT].height = GetSystemMetrics (SM_CYSCREEN);
1345 vid_default = MODE_FULLSCREEN_DEFAULT;
1346 leavecurrentmode = 1;
1350 if (COM_CheckParm("-width"))
1351 width = atoi(com_argv[COM_CheckParm("-width")+1]);
1355 if (COM_CheckParm("-bpp"))
1357 bpp = atoi(com_argv[COM_CheckParm("-bpp")+1]);
1366 if (COM_CheckParm("-height"))
1367 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1369 // if they want to force it, add the specified mode to the list
1370 if (COM_CheckParm("-force") && (nummodes < MAX_MODE_LIST))
1371 VID_AddMode(MS_FULLDIB, width, height, 0, 0, 1, 1, bpp);
1374 modelist[nummodes].type = MS_FULLDIB;
1375 modelist[nummodes].width = width;
1376 modelist[nummodes].height = height;
1377 modelist[nummodes].modenum = 0;
1378 modelist[nummodes].halfscreen = 0;
1379 modelist[nummodes].dib = 1;
1380 modelist[nummodes].fullscreen = 1;
1381 modelist[nummodes].bpp = bpp;
1382 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1384 for (i=nummodes, existingmode = 0 ; i<nummodes ; i++)
1386 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1402 if (COM_CheckParm("-height"))
1404 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1406 for (i=1, vid_default=0 ; i<nummodes ; i++)
1408 if ((modelist[i].width == width) && (modelist[i].height == height) && (modelist[i].bpp == bpp))
1418 for (i=1, vid_default=0 ; i<nummodes ; i++)
1420 if ((modelist[i].width == width) && (modelist[i].bpp == bpp))
1435 case 15: bpp = 16;break;
1436 case 16: bpp = 32;break;
1437 case 32: bpp = 24;break;
1438 case 24: done = 1;break;
1448 Sys_Error ("Specified video mode not available");
1453 vid_initialized = true;
1455 if ((i = COM_CheckParm("-conwidth")) != 0)
1456 vid.conwidth = atoi(com_argv[i+1]);
1460 vid.conwidth &= 0xfff8; // make it a multiple of eight
1462 if (vid.conwidth < 320)
1465 // pick a conheight that matches with correct aspect
1466 vid.conheight = vid.conwidth*3 / 4;
1468 if ((i = COM_CheckParm("-conheight")) != 0)
1469 vid.conheight = atoi(com_argv[i+1]);
1470 if (vid.conheight < 200)
1471 vid.conheight = 200;
1473 VID_SetMode (vid_default);
1475 maindc = GetDC(mainwindow);
1476 bSetupPixelFormat(maindc);
1478 baseRC = wglCreateContext( maindc );
1480 Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.");
1481 if (!wglMakeCurrent( maindc, baseRC ))
1482 Sys_Error ("wglMakeCurrent failed");
1486 // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
1487 if (strncasecmp(gl_vendor,"ATI",3)==0)
1489 if (strncasecmp(gl_renderer,"Rage Pro",8)==0)
1492 if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
1495 // sprintf (gldir, "%s/glquake", com_gamedir);
1496 // Sys_mkdir (gldir);
1498 vid_realmode = vid_modenum;
1500 vid_menudrawfn = VID_MenuDraw;
1501 vid_menukeyfn = VID_MenuKey;
1503 strcpy (badmode.modedesc, "Bad mode");
1504 vid_canalttab = true;
1508 //========================================================
1510 //========================================================
1512 extern void M_Menu_Options_f (void);
1513 extern void M_Print (int cx, int cy, char *str);
1514 extern void M_PrintWhite (int cx, int cy, char *str);
1515 extern void M_DrawCharacter (int cx, int line, int num);
1516 extern void M_DrawPic (int x, int y, qpic_t *pic);
1518 static int vid_line, vid_wmodes;
1527 #define MAX_COLUMN_SIZE 9
1528 #define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 2)
1529 #define MAX_MODEDESCS (MAX_COLUMN_SIZE*3)
1531 static modedesc_t modedescs[MAX_MODEDESCS];
1538 void VID_MenuDraw (void)
1542 int lnummodes, i, k, column, row;
1545 p = Draw_CachePic ("gfx/vidmodes.lmp");
1546 M_DrawPic ( (320-p->width)/2, 4, p);
1549 lnummodes = VID_NumModes ();
1551 for (i=1 ; (i<lnummodes) && (vid_wmodes < MAX_MODEDESCS) ; i++)
1553 ptr = VID_GetModeDescription (i);
1554 pv = VID_GetModePtr (i);
1558 modedescs[k].modenum = i;
1559 modedescs[k].desc = ptr;
1560 modedescs[k].iscur = 0;
1562 if (i == vid_modenum)
1563 modedescs[k].iscur = 1;
1571 M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)");
1576 for (i=0 ; i<vid_wmodes ; i++)
1578 if (modedescs[i].iscur)
1579 M_PrintWhite (column, row, modedescs[i].desc);
1581 M_Print (column, row, modedescs[i].desc);
1585 if ((i % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
1593 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*2, "Video modes must be set from the");
1594 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3, "command line with -width <width>");
1595 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4, "and -bpp <bits-per-pixel>");
1596 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6, "Select windowed mode with -window");
1605 void VID_MenuKey (int key)
1610 S_LocalSound ("misc/menu1.wav");
1611 M_Menu_Options_f ();