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 //====================================
378 void GL_BeginRendering (int *x, int *y, int *width, int *height)
381 *width = WindowRect.right - WindowRect.left;
382 *height = WindowRect.bottom - WindowRect.top;
384 // if (!wglMakeCurrent( maindc, baseRC ))
385 // Sys_Error ("wglMakeCurrent failed");
387 // glViewport (*x, *y, *width, *height);
391 void GL_EndRendering (void)
394 if (r_render.value && !scr_skipupdate)
397 // handle the mouse state when windowed if that's changed
399 if (vid_mouse.value && key_dest == key_game)
401 if (modestate == MS_FULLDIB)
419 IN_DeactivateMouse ();
425 void VID_SetDefaultMode (void)
427 IN_DeactivateMouse ();
430 void VID_RestoreSystemGamma(void);
432 void VID_Shutdown (void)
441 vid_canalttab = false;
442 hRC = wglGetCurrentContext();
443 hDC = wglGetCurrentDC();
445 wglMakeCurrent(NULL, NULL);
447 // LordHavoc: free textures before closing (may help NVIDIA)
448 for (i = 0;i < 8192;i++)
450 glDeleteTextures(8192, temp);
453 wglDeleteContext(hRC);
455 if (hDC && mainwindow)
456 ReleaseDC(mainwindow, hDC);
458 if (modestate == MS_FULLDIB)
459 ChangeDisplaySettings (NULL, 0);
461 if (maindc && mainwindow)
462 ReleaseDC (mainwindow, maindc);
464 AppActivate(false, false);
466 VID_RestoreSystemGamma();
471 //==========================================================================
474 BOOL bSetupPixelFormat(HDC hDC)
476 static PIXELFORMATDESCRIPTOR pfd = {
477 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
479 PFD_DRAW_TO_WINDOW // support window
480 | PFD_SUPPORT_OPENGL // support OpenGL
481 | PFD_DOUBLEBUFFER , // double buffered
482 PFD_TYPE_RGBA, // RGBA type
483 24, // 24-bit color depth
484 0, 0, 0, 0, 0, 0, // color bits ignored
485 0, // no alpha buffer
486 0, // shift bit ignored
487 0, // no accumulation buffer
488 0, 0, 0, 0, // accum bits ignored
489 32, // 32-bit z-buffer
490 0, // no stencil buffer
491 0, // no auxiliary buffer
492 PFD_MAIN_PLANE, // main layer
494 0, 0, 0 // layer masks ignored
498 if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
500 MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
504 if (SetPixelFormat(hDC, pixelformat, &pfd) == false)
506 MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
515 byte scantokey[128] =
517 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
518 0 ,27 ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'0' ,'-' ,'=' ,K_BACKSPACE,9 , // 0
519 'q' ,'w' ,'e' ,'r' ,'t' ,'y' ,'u' ,'i' ,'o' ,'p' ,'[' ,']' ,13 ,K_CTRL ,'a' ,'s' , // 1
520 'd' ,'f' ,'g' ,'h' ,'j' ,'k' ,'l' ,';' ,'\'' ,'`' ,K_SHIFT,'\\' ,'z' ,'x' ,'c' ,'v' , // 2
521 'b' ,'n' ,'m' ,',' ,'.' ,'/' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2 ,K_F3 ,K_F4 ,K_F5 , // 3
522 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
523 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
524 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
525 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
529 byte shiftscantokey[128] =
531 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
532 0 ,27 ,'!' ,'@' ,'#' ,'$' ,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'_' ,'+' ,K_BACKSPACE,9 , // 0
533 'Q' ,'W' ,'E' ,'R' ,'T' ,'Y' ,'U' ,'I' ,'O' ,'P' ,'{' ,'}' ,13 ,K_CTRL ,'A' ,'S' , // 1
534 'D' ,'F' ,'G' ,'H' ,'J' ,'K' ,'L' ,':' ,'"' ,'~' ,K_SHIFT,'|' ,'Z' ,'X' ,'C' ,'V' , // 2
535 'B' ,'N' ,'M' ,'<' ,'>' ,'?' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2,K_F3 ,K_F4 ,K_F5 , // 3
536 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
537 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
538 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
539 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
547 Map from windows to quake keynums
550 int MapKey (int key, int virtualkey)
555 if (scantokey[key] == 0)
556 Con_DPrintf("key 0x%02x has no translation\n", key);
557 // if (scantokey[key] >= 0x20 && scantokey[key] < 0x7F)
559 return scantokey[key];
563 ===================================================================
567 ===================================================================
575 void ClearAllStates (void)
579 // send an up event for each key, to make sure the server clears them all
580 for (i=0 ; i<256 ; i++)
582 Key_Event (i, false);
589 void VID_RestoreGameGamma(void);
591 void AppActivate(BOOL fActive, BOOL minimize)
592 /****************************************************************************
594 * Function: AppActivate
595 * Parameters: fActive - True if app is activating
597 * Description: If the application is activating, then swap the system
598 * into SYSPAL_NOSTATIC mode so that our palettes will display
601 ****************************************************************************/
603 static BOOL sound_active;
606 Minimized = minimize;
608 // enable/disable sound on focus gain/loss
609 if (!ActiveApp && sound_active)
612 sound_active = false;
614 else if (ActiveApp && !sound_active)
622 if (modestate == MS_FULLDIB)
624 // usingmouse = true;
625 // IN_ActivateMouse ();
627 if (vid_canalttab && vid_wassuspended)
629 vid_wassuspended = false;
630 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
631 ShowWindow(mainwindow, SW_SHOWNORMAL);
634 // LordHavoc: from dabb, fix for alt-tab bug in NVidia drivers
635 MoveWindow(mainwindow,0,0,gdevmode.dmPelsWidth,gdevmode.dmPelsHeight,false);
637 // else if ((modestate == MS_WINDOWED) && vid_mouse.value && key_dest == key_game)
639 // usingmouse = true;
640 // IN_ActivateMouse ();
643 VID_RestoreGameGamma();
649 IN_DeactivateMouse ();
651 if (modestate == MS_FULLDIB)
653 // usingmouse = false;
654 // IN_DeactivateMouse ();
658 ChangeDisplaySettings (NULL, 0);
659 vid_wassuspended = true;
662 // else if ((modestate == MS_WINDOWED) && vid_mouse.value)
664 // usingmouse = false;
665 // IN_DeactivateMouse ();
668 VID_RestoreSystemGamma();
672 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
674 /* main window procedure */
675 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
678 int fActive, fMinimized, temp;
679 extern unsigned int uiWheelMessage;
681 if ( uMsg == uiWheelMessage )
682 uMsg = WM_MOUSEWHEEL;
687 if (modestate == MS_FULLDIB)
688 ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
695 window_x = (int) LOWORD(lParam);
696 window_y = (int) HIWORD(lParam);
697 VID_UpdateWindowStatus ();
702 Key_Event (MapKey(lParam, wParam), true);
707 Key_Event (MapKey(lParam, wParam), false);
711 // keep Alt-Space from happening
714 // this is complicated because Win32 seems to pack multiple mouse events into
715 // one update sometimes, so we always check all states and look for events
725 if (wParam & MK_LBUTTON)
728 if (wParam & MK_RBUTTON)
731 if (wParam & MK_MBUTTON)
734 IN_MouseEvent (temp);
738 // JACK: This is the mouse wheel with the Intellimouse
739 // Its delta is either positive or neg, and we generate the proper
742 if ((short) HIWORD(wParam) > 0) {
743 Key_Event(K_MWHEELUP, true);
744 Key_Event(K_MWHEELUP, false);
746 Key_Event(K_MWHEELDOWN, true);
747 Key_Event(K_MWHEELDOWN, false);
755 if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
761 fActive = LOWORD(wParam);
762 fMinimized = (BOOL) HIWORD(wParam);
763 AppActivate(!(fActive == WA_INACTIVE), fMinimized);
765 // fix the leftover Alt from any Alt-Tab or the like that switched us away
773 DestroyWindow (mainwindow);
780 lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
784 /* pass all unhandled messages to DefWindowProc */
785 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
789 /* return 1 if handled message, 0 if not */
799 int VID_NumModes (void)
810 vmode_t *VID_GetModePtr (int modenum)
813 if ((modenum >= 0) && (modenum < nummodes))
814 return &modelist[modenum];
822 VID_GetModeDescription
825 char *VID_GetModeDescription (int mode)
829 static char temp[100];
831 if ((mode < 0) || (mode >= nummodes))
834 if (!leavecurrentmode)
836 pv = VID_GetModePtr (mode);
837 pinfo = pv->modedesc;
841 sprintf (temp, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
849 // KJB: Added this to return the mode driver name in description for console
851 char *VID_GetExtModeDescription (int mode)
853 static char pinfo[40];
856 if ((mode < 0) || (mode >= nummodes))
859 pv = VID_GetModePtr (mode);
860 if (modelist[mode].type == MS_FULLDIB)
862 if (!leavecurrentmode)
863 sprintf(pinfo,"%s fullscreen", pv->modedesc);
865 sprintf (pinfo, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
869 if (modestate == MS_WINDOWED)
870 sprintf(pinfo, "%s windowed", pv->modedesc);
872 sprintf(pinfo, "windowed");
881 VID_DescribeCurrentMode_f
884 void VID_DescribeCurrentMode_f (void)
886 Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum));
895 void VID_NumModes_f (void)
899 Con_Printf ("%d video mode is available\n", nummodes);
901 Con_Printf ("%d video modes are available\n", nummodes);
910 void VID_DescribeMode_f (void)
914 modenum = atoi (Cmd_Argv(1));
916 t = leavecurrentmode;
917 leavecurrentmode = 0;
919 Con_Printf ("%s\n", VID_GetExtModeDescription (modenum));
921 leavecurrentmode = t;
930 void VID_DescribeModes_f (void)
936 lnummodes = VID_NumModes ();
938 t = leavecurrentmode;
939 leavecurrentmode = 0;
941 for (i=1 ; i<lnummodes ; i++)
943 pv = VID_GetModePtr (i);
944 pinfo = VID_GetExtModeDescription (i);
945 Con_Printf ("%2d: %s\n", i, pinfo);
948 leavecurrentmode = t;
951 void VID_AddMode(int type, int width, int height, int modenum, int halfscreen, int dib, int fullscreen, int bpp)
954 if (nummodes >= MAX_MODE_LIST)
956 modelist[nummodes].type = type;
957 modelist[nummodes].width = width;
958 modelist[nummodes].height = height;
959 modelist[nummodes].modenum = modenum;
960 modelist[nummodes].halfscreen = halfscreen;
961 modelist[nummodes].dib = dib;
962 modelist[nummodes].fullscreen = fullscreen;
963 modelist[nummodes].bpp = bpp;
965 sprintf (modelist[nummodes].modedesc, "%dx%d", width, height);
967 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", width, height, bpp);
968 for (i = 0;i < nummodes;i++)
970 if (!memcmp(&modelist[i], &modelist[nummodes], sizeof(vmode_t)))
976 void VID_InitDIB (HINSTANCE hInstance)
981 // Register the frame class
983 wc.lpfnWndProc = (WNDPROC)MainWndProc;
986 wc.hInstance = hInstance;
988 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
989 wc.hbrBackground = NULL;
991 wc.lpszClassName = "DarkPlaces";
993 if (!RegisterClass (&wc) )
994 Sys_Error ("Couldn't register window class");
997 modelist[0].type = MS_WINDOWED;
999 if (COM_CheckParm("-width"))
1000 modelist[0].width = atoi(com_argv[COM_CheckParm("-width")+1]);
1002 modelist[0].width = 640;
1004 if (modelist[0].width < 320)
1005 modelist[0].width = 320;
1007 if (COM_CheckParm("-height"))
1008 modelist[0].height= atoi(com_argv[COM_CheckParm("-height")+1]);
1010 modelist[0].height = modelist[0].width * 240/320;
1012 if (modelist[0].height < 240)
1013 modelist[0].height = 240;
1015 sprintf (modelist[0].modedesc, "%dx%d", modelist[0].width, modelist[0].height);
1017 modelist[0].modenum = MODE_WINDOWED;
1018 modelist[0].dib = 1;
1019 modelist[0].fullscreen = 0;
1020 modelist[0].halfscreen = 0;
1021 modelist[0].bpp = 0;
1025 if (COM_CheckParm("-width"))
1026 w = atoi(com_argv[COM_CheckParm("-width")+1]);
1033 if (COM_CheckParm("-height"))
1034 h = atoi(com_argv[COM_CheckParm("-height")+1]);
1041 VID_AddMode(MS_WINDOWED, w, h, 0, 0, 1, 0, 0);
1050 void VID_InitFullDIB (HINSTANCE hInstance)
1055 int originalnummodes;
1056 // int existingmode;
1063 // enumerate >8 bpp modes
1064 originalnummodes = nummodes;
1069 stat = EnumDisplaySettings (NULL, modenum, &devmode);
1071 if ((devmode.dmBitsPerPel >= 15) && (devmode.dmPelsWidth <= MAXWIDTH) && (devmode.dmPelsHeight <= MAXHEIGHT) && (nummodes < MAX_MODE_LIST))
1073 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1075 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1077 // if the width is more than twice the height, reduce it by half because this
1078 // is probably a dual-screen monitor
1079 if ((!COM_CheckParm("-noadjustaspect")) && (devmode.dmPelsWidth > (devmode.dmPelsHeight << 1)))
1080 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth >> 1, devmode.dmPelsHeight, 0, 1, 1, 1, devmode.dmBitsPerPel);
1082 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1084 modelist[nummodes].type = MS_FULLDIB;
1085 modelist[nummodes].width = devmode.dmPelsWidth;
1086 modelist[nummodes].height = devmode.dmPelsHeight;
1087 modelist[nummodes].modenum = 0;
1088 modelist[nummodes].halfscreen = 0;
1089 modelist[nummodes].dib = 1;
1090 modelist[nummodes].fullscreen = 1;
1091 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1092 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1094 // if the width is more than twice the height, reduce it by half because this
1095 // is probably a dual-screen monitor
1096 if (!COM_CheckParm("-noadjustaspect"))
1098 if (modelist[nummodes].width > (modelist[nummodes].height << 1))
1100 modelist[nummodes].width >>= 1;
1101 modelist[nummodes].halfscreen = 1;
1102 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", modelist[nummodes].width, modelist[nummodes].height, modelist[nummodes].bpp);
1106 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1108 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1125 // see if there are any low-res modes that aren't being reported
1126 numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]);
1132 for (j=0 ; (j<numlowresmodes) && (nummodes < MAX_MODE_LIST) ; j++)
1134 devmode.dmBitsPerPel = bpp;
1135 devmode.dmPelsWidth = lowresmodes[j].width;
1136 devmode.dmPelsHeight = lowresmodes[j].height;
1137 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1139 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1140 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1143 modelist[nummodes].type = MS_FULLDIB;
1144 modelist[nummodes].width = devmode.dmPelsWidth;
1145 modelist[nummodes].height = devmode.dmPelsHeight;
1146 modelist[nummodes].modenum = 0;
1147 modelist[nummodes].halfscreen = 0;
1148 modelist[nummodes].dib = 1;
1149 modelist[nummodes].fullscreen = 1;
1150 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1151 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1153 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1155 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1184 if (nummodes == originalnummodes)
1185 Con_SafePrintf ("No fullscreen DIB modes found\n");
1188 static int grabsysgamma = true;
1189 WORD systemgammaramps[3][256], currentgammaramps[3][256];
1191 int VID_SetGamma(float prescale, float gamma, float scale, float base)
1197 BuildGammaTable16(prescale, gamma, scale, base, ¤tgammaramps[0][0]);
1198 for (i = 0;i < 256;i++)
1199 currentgammaramps[1][i] = currentgammaramps[2][i] = currentgammaramps[0][i];
1201 i = SetDeviceGammaRamp(hdc, ¤tgammaramps[0][0]);
1203 ReleaseDC (NULL, hdc);
1204 return i; // return success or failure
1207 void VID_RestoreGameGamma(void)
1209 VID_UpdateGamma(true);
1212 void VID_GetSystemGamma(void)
1217 GetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1219 ReleaseDC (NULL, hdc);
1222 void VID_RestoreSystemGamma(void)
1227 SetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1229 ReleaseDC (NULL, hdc);
1237 void VID_Init (void)
1240 // int existingmode;
1241 int basenummodes, width, height, bpp, findbpp, done;
1245 memset(&devmode, 0, sizeof(devmode));
1247 // Cvar_RegisterVariable (&_vid_default_mode);
1248 // Cvar_RegisterVariable (&_vid_default_mode_win);
1250 Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);
1251 Cmd_AddCommand ("vid_describecurrentmode", VID_DescribeCurrentMode_f);
1252 Cmd_AddCommand ("vid_describemode", VID_DescribeMode_f);
1253 Cmd_AddCommand ("vid_describemodes", VID_DescribeModes_f);
1255 VID_GetSystemGamma();
1257 hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON2));
1259 InitCommonControls();
1261 VID_InitDIB (global_hInstance);
1262 basenummodes = nummodes = 1;
1264 VID_InitFullDIB (global_hInstance);
1266 if (COM_CheckParm("-window"))
1270 if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
1271 Sys_Error ("Can't run in non-RGB mode");
1273 ReleaseDC (NULL, hdc);
1277 vid_default = MODE_WINDOWED;
1282 Sys_Error ("No RGB fullscreen modes available");
1286 if (COM_CheckParm("-mode"))
1287 vid_default = atoi(com_argv[COM_CheckParm("-mode")+1]);
1290 if (COM_CheckParm("-current"))
1292 modelist[MODE_FULLSCREEN_DEFAULT].width = GetSystemMetrics (SM_CXSCREEN);
1293 modelist[MODE_FULLSCREEN_DEFAULT].height = GetSystemMetrics (SM_CYSCREEN);
1294 vid_default = MODE_FULLSCREEN_DEFAULT;
1295 leavecurrentmode = 1;
1299 if (COM_CheckParm("-width"))
1300 width = atoi(com_argv[COM_CheckParm("-width")+1]);
1304 if (COM_CheckParm("-bpp"))
1306 bpp = atoi(com_argv[COM_CheckParm("-bpp")+1]);
1315 if (COM_CheckParm("-height"))
1316 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1318 // if they want to force it, add the specified mode to the list
1319 if (COM_CheckParm("-force") && (nummodes < MAX_MODE_LIST))
1320 VID_AddMode(MS_FULLDIB, width, height, 0, 0, 1, 1, bpp);
1323 modelist[nummodes].type = MS_FULLDIB;
1324 modelist[nummodes].width = width;
1325 modelist[nummodes].height = height;
1326 modelist[nummodes].modenum = 0;
1327 modelist[nummodes].halfscreen = 0;
1328 modelist[nummodes].dib = 1;
1329 modelist[nummodes].fullscreen = 1;
1330 modelist[nummodes].bpp = bpp;
1331 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1333 for (i=nummodes, existingmode = 0 ; i<nummodes ; i++)
1335 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1351 if (COM_CheckParm("-height"))
1353 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1355 for (i=1, vid_default=0 ; i<nummodes ; i++)
1357 if ((modelist[i].width == width) && (modelist[i].height == height) && (modelist[i].bpp == bpp))
1367 for (i=1, vid_default=0 ; i<nummodes ; i++)
1369 if ((modelist[i].width == width) && (modelist[i].bpp == bpp))
1384 case 15: bpp = 16;break;
1385 case 16: bpp = 32;break;
1386 case 32: bpp = 24;break;
1387 case 24: done = 1;break;
1397 Sys_Error ("Specified video mode not available");
1402 vid_initialized = true;
1404 if ((i = COM_CheckParm("-conwidth")) != 0)
1405 vid.conwidth = atoi(com_argv[i+1]);
1409 vid.conwidth &= 0xfff8; // make it a multiple of eight
1411 if (vid.conwidth < 320)
1414 // pick a conheight that matches with correct aspect
1415 vid.conheight = vid.conwidth*3 / 4;
1417 if ((i = COM_CheckParm("-conheight")) != 0)
1418 vid.conheight = atoi(com_argv[i+1]);
1419 if (vid.conheight < 200)
1420 vid.conheight = 200;
1422 VID_SetMode (vid_default);
1424 maindc = GetDC(mainwindow);
1425 bSetupPixelFormat(maindc);
1427 baseRC = wglCreateContext( maindc );
1429 Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.");
1430 if (!wglMakeCurrent( maindc, baseRC ))
1431 Sys_Error ("wglMakeCurrent failed");
1435 // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
1436 if (strncasecmp(gl_vendor,"ATI",3)==0)
1438 if (strncasecmp(gl_renderer,"Rage Pro",8)==0)
1441 if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
1444 // sprintf (gldir, "%s/glquake", com_gamedir);
1445 // Sys_mkdir (gldir);
1447 vid_realmode = vid_modenum;
1449 vid_menudrawfn = VID_MenuDraw;
1450 vid_menukeyfn = VID_MenuKey;
1452 strcpy (badmode.modedesc, "Bad mode");
1453 vid_canalttab = true;
1457 //========================================================
1459 //========================================================
1461 extern void M_Menu_Options_f (void);
1462 extern void M_Print (int cx, int cy, char *str);
1463 extern void M_PrintWhite (int cx, int cy, char *str);
1464 extern void M_DrawCharacter (int cx, int line, int num);
1465 extern void M_DrawPic (int x, int y, qpic_t *pic);
1467 static int vid_line, vid_wmodes;
1476 #define MAX_COLUMN_SIZE 9
1477 #define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 2)
1478 #define MAX_MODEDESCS (MAX_COLUMN_SIZE*3)
1480 static modedesc_t modedescs[MAX_MODEDESCS];
1487 void VID_MenuDraw (void)
1491 int lnummodes, i, k, column, row;
1494 p = Draw_CachePic ("gfx/vidmodes.lmp");
1495 M_DrawPic ( (320-p->width)/2, 4, p);
1498 lnummodes = VID_NumModes ();
1500 for (i=1 ; (i<lnummodes) && (vid_wmodes < MAX_MODEDESCS) ; i++)
1502 ptr = VID_GetModeDescription (i);
1503 pv = VID_GetModePtr (i);
1507 modedescs[k].modenum = i;
1508 modedescs[k].desc = ptr;
1509 modedescs[k].iscur = 0;
1511 if (i == vid_modenum)
1512 modedescs[k].iscur = 1;
1520 M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)");
1525 for (i=0 ; i<vid_wmodes ; i++)
1527 if (modedescs[i].iscur)
1528 M_PrintWhite (column, row, modedescs[i].desc);
1530 M_Print (column, row, modedescs[i].desc);
1534 if ((i % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
1542 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*2, "Video modes must be set from the");
1543 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3, "command line with -width <width>");
1544 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4, "and -bpp <bits-per-pixel>");
1545 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6, "Select windowed mode with -window");
1554 void VID_MenuKey (int key)
1559 S_LocalSound ("misc/menu1.wav");
1560 M_Menu_Options_f ();