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 windowed_mouse;
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 // LordHavoc: ARB multitexture support
117 int gl_mtex_enum = 0;
119 qboolean isG200 = false; // LordHavoc: the Matrox G200 can't do per pixel alpha, and it uses a D3D driver for GL... ugh...
120 qboolean isRagePro = false; // LordHavoc: the ATI Rage Pro has limitations with per pixel alpha (the color scaler does not apply to per pixel alpha images...), although not as bad as a G200.
121 qboolean gl_mtexable = false;
122 qboolean gl_arrays = false;
124 //====================================
126 cvar_t vid_mode = {"vid_mode","0", false};
127 // Note that 0 is MODE_WINDOWED
128 //cvar_t _vid_default_mode = {"_vid_default_mode","0", true};
129 // Note that 3 is MODE_FULLSCREEN_DEFAULT
130 //cvar_t _vid_default_mode_win = {"_vid_default_mode_win","3", true};
131 cvar_t _windowed_mouse = {"_windowed_mouse","1", true};
133 int window_center_x, window_center_y, window_x, window_y, window_width, window_height;
136 // direct draw software compatability stuff
138 void CenterWindow(HWND hWndCenter, int width, int height, BOOL lefttopjustify)
140 int CenterX, CenterY;
142 CenterX = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
143 CenterY = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
144 if (CenterX > CenterY*2)
145 CenterX >>= 1; // dual screens
146 CenterX = (CenterX < 0) ? 0: CenterX;
147 CenterY = (CenterY < 0) ? 0: CenterY;
148 SetWindowPos (hWndCenter, NULL, CenterX, CenterY, 0, 0,
149 SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
152 qboolean VID_SetWindowedMode (int modenum)
154 int lastmodestate, width, height;
157 lastmodestate = modestate;
159 WindowRect.top = WindowRect.left = 0;
161 WindowRect.right = modelist[modenum].width;
162 WindowRect.bottom = modelist[modenum].height;
164 DIBWidth = modelist[modenum].width;
165 DIBHeight = modelist[modenum].height;
167 WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
171 AdjustWindowRectEx(&rect, WindowStyle, FALSE, 0);
173 width = rect.right - rect.left;
174 height = rect.bottom - rect.top;
176 // Create the DIB window
177 mainwindow = CreateWindowEx (ExWindowStyle, "DarkPlaces", "DarkPlacesGL", WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
180 Sys_Error ("Couldn't create DIB window");
182 // Center and show the DIB window
183 CenterWindow(mainwindow, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top, false);
185 ShowWindow (mainwindow, SW_SHOWDEFAULT);
186 UpdateWindow (mainwindow);
188 modestate = MS_WINDOWED;
190 if (vid.conheight > modelist[modenum].height)
191 vid.conheight = modelist[modenum].height;
192 if (vid.conwidth > modelist[modenum].width)
193 vid.conwidth = modelist[modenum].width;
194 vid.width = vid.conwidth;
195 vid.height = vid.conheight;
197 SendMessage (mainwindow, WM_SETICON, (WPARAM)TRUE, (LPARAM)hIcon);
198 SendMessage (mainwindow, WM_SETICON, (WPARAM)FALSE, (LPARAM)hIcon);
204 qboolean VID_SetFullDIBMode (int modenum)
206 int lastmodestate, width, height;
209 if (!leavecurrentmode)
211 gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
212 gdevmode.dmBitsPerPel = modelist[modenum].bpp;
213 gdevmode.dmPelsWidth = modelist[modenum].width <<
214 modelist[modenum].halfscreen;
215 gdevmode.dmPelsHeight = modelist[modenum].height;
216 gdevmode.dmSize = sizeof (gdevmode);
218 if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
219 Sys_Error ("Couldn't set fullscreen DIB mode");
222 lastmodestate = modestate;
223 modestate = MS_FULLDIB;
225 WindowRect.top = WindowRect.left = 0;
227 WindowRect.right = modelist[modenum].width;
228 WindowRect.bottom = modelist[modenum].height;
230 DIBWidth = modelist[modenum].width;
231 DIBHeight = modelist[modenum].height;
233 WindowStyle = WS_POPUP;
237 AdjustWindowRectEx(&rect, WindowStyle, FALSE, 0);
239 width = rect.right - rect.left;
240 height = rect.bottom - rect.top;
242 // Create the DIB window
243 mainwindow = CreateWindowEx (ExWindowStyle, "DarkPlaces", "DarkPlacesGL", WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
246 Sys_Error ("Couldn't create DIB window");
248 ShowWindow (mainwindow, SW_SHOWDEFAULT);
249 UpdateWindow (mainwindow);
251 if (vid.conheight > modelist[modenum].height)
252 vid.conheight = modelist[modenum].height;
253 if (vid.conwidth > modelist[modenum].width)
254 vid.conwidth = modelist[modenum].width;
255 vid.width = vid.conwidth;
256 vid.height = vid.conheight;
258 // needed because we're not getting WM_MOVE messages fullscreen on NT
262 SendMessage (mainwindow, WM_SETICON, (WPARAM)TRUE, (LPARAM)hIcon);
263 SendMessage (mainwindow, WM_SETICON, (WPARAM)FALSE, (LPARAM)hIcon);
269 int VID_SetMode (int modenum)
271 int original_mode, temp;
275 if ((windowed && (modenum != 0)) || (!windowed && (modenum < 1)) || (!windowed && (modenum >= nummodes)))
276 Sys_Error ("Bad video mode\n");
278 // so Con_Printfs don't mess us up by forcing vid and snd updates
279 temp = scr_disabled_for_loading;
280 scr_disabled_for_loading = true;
284 if (vid_modenum == NO_MODE)
285 original_mode = windowed_default;
287 original_mode = vid_modenum;
289 // Set either the fullscreen or windowed mode
290 if (modelist[modenum].type == MS_WINDOWED)
292 if (_windowed_mouse.value && key_dest == key_game)
294 stat = VID_SetWindowedMode(modenum);
300 IN_DeactivateMouse ();
302 stat = VID_SetWindowedMode(modenum);
305 else if (modelist[modenum].type == MS_FULLDIB)
307 stat = VID_SetFullDIBMode(modenum);
312 Sys_Error ("VID_SetMode: Bad mode type in modelist");
314 window_width = DIBWidth;
315 window_height = DIBHeight;
316 VID_UpdateWindowStatus ();
319 scr_disabled_for_loading = temp;
322 Sys_Error ("Couldn't set video mode");
324 // now we try to make sure we get the focus on the mode switch, because
325 // sometimes in some systems we don't. We grab the foreground, then
326 // finish setting up, pump all our messages, and sleep for a little while
327 // to let messages finish bouncing around the system, then we put
328 // ourselves at the top of the z order, then grab the foreground again,
329 // Who knows if it helps, but it probably doesn't hurt
330 SetForegroundWindow (mainwindow);
331 vid_modenum = modenum;
332 Cvar_SetValue ("vid_mode", (float)vid_modenum);
334 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
336 TranslateMessage (&msg);
337 DispatchMessage (&msg);
342 SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS);
344 SetForegroundWindow (mainwindow);
346 // fix the leftover Alt from any Alt-Tab or the like that switched us away
350 Con_SafePrintf ("Video mode %s initialized.\n", VID_GetModeDescription (vid_modenum));
352 vid.recalc_refdef = 1;
360 VID_UpdateWindowStatus
363 void VID_UpdateWindowStatus (void)
366 window_rect.left = window_x;
367 window_rect.top = window_y;
368 window_rect.right = window_x + window_width;
369 window_rect.bottom = window_y + window_height;
370 window_center_x = (window_rect.left + window_rect.right) / 2;
371 window_center_y = (window_rect.top + window_rect.bottom) / 2;
373 IN_UpdateClipCursor ();
377 //====================================
379 void (GLAPIENTRY *qglVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
380 void (GLAPIENTRY *qglColorPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
381 void (GLAPIENTRY *qglTexCoordPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
382 void (GLAPIENTRY *qglArrayElement)(GLint i);
383 void (GLAPIENTRY *qglDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
384 void (GLAPIENTRY *qglMTexCoord2f)(GLenum, GLfloat, GLfloat);
385 void (GLAPIENTRY *qglSelectTexture)(GLenum);
387 void VID_CheckVertexArrays (void)
389 if (COM_CheckParm("-novertex"))
391 Con_Printf("...vertex array support disabled\n");
394 if ((qglArrayElement = (void *) wglGetProcAddress("glArrayElement"))
395 && (qglColorPointer = (void *) wglGetProcAddress("glColorPointer"))
396 // && (qglDrawArrays = (void *) wglGetProcAddress("glDrawArrays"))
397 && (qglDrawElements = (void *) wglGetProcAddress("glDrawElements"))
398 // && (qglInterleavedArrays = (void *) wglGetProcAddress("glInterleavedArrays"))
399 && (qglTexCoordPointer = (void *) wglGetProcAddress("glTexCoordPointer"))
400 && (qglVertexPointer = (void *) wglGetProcAddress("glVertexPointer"))
403 Con_Printf("...vertex array support detected\n");
408 Con_Printf("...vertex array support disabled (not detected - get a better driver)\n");
411 int texture_extension_number = 1;
413 void VID_CheckMultitexture(void)
415 qglMTexCoord2f = NULL;
416 qglSelectTexture = NULL;
417 // Check to see if multitexture is disabled
418 if (COM_CheckParm("-nomtex"))
420 Con_Printf("...multitexture disabled\n");
423 // Test for ARB_multitexture
424 if (!COM_CheckParm("-SGISmtex") && strstr(gl_extensions, "GL_ARB_multitexture "))
426 Con_Printf("...using GL_ARB_multitexture\n");
427 qglMTexCoord2f = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
428 qglSelectTexture = (void *) wglGetProcAddress("glActiveTextureARB");
430 gl_mtex_enum = GL_TEXTURE0_ARB;
432 else if (strstr(gl_extensions, "GL_SGIS_multitexture ")) // Test for SGIS_multitexture (if ARB_multitexture not found)
434 Con_Printf("...using GL_SGIS_multitexture\n");
435 qglMTexCoord2f = (void *) wglGetProcAddress("glMTexCoord2fSGIS");
436 qglSelectTexture = (void *) wglGetProcAddress("glSelectTextureSGIS");
438 gl_mtex_enum = TEXTURE0_SGIS;
441 Con_Printf("...multitexture disabled (not detected)\n");
450 void GL_BeginRendering (int *x, int *y, int *width, int *height)
453 *width = WindowRect.right - WindowRect.left;
454 *height = WindowRect.bottom - WindowRect.top;
456 // if (!wglMakeCurrent( maindc, baseRC ))
457 // Sys_Error ("wglMakeCurrent failed");
459 // glViewport (*x, *y, *width, *height);
463 void GL_EndRendering (void)
465 if (r_render.value && !scr_skipupdate)
468 // handle the mouse state when windowed if that's changed
469 if (modestate == MS_WINDOWED)
471 if (!_windowed_mouse.value)
475 IN_DeactivateMouse ();
477 windowed_mouse = false;
482 windowed_mouse = true;
483 if (key_dest == key_game && !mouseactive && ActiveApp)
488 else if (mouseactive && key_dest != key_game)
490 IN_DeactivateMouse ();
497 void VID_SetDefaultMode (void)
499 IN_DeactivateMouse ();
503 void VID_Shutdown (void)
512 vid_canalttab = false;
513 hRC = wglGetCurrentContext();
514 hDC = wglGetCurrentDC();
516 wglMakeCurrent(NULL, NULL);
518 // LordHavoc: free textures before closing (may help NVIDIA)
519 for (i = 0;i < 8192;i++) temp[i] = i+1;
520 glDeleteTextures(8192, temp);
523 wglDeleteContext(hRC);
525 if (hDC && mainwindow)
526 ReleaseDC(mainwindow, hDC);
528 if (modestate == MS_FULLDIB)
529 ChangeDisplaySettings (NULL, 0);
531 if (maindc && mainwindow)
532 ReleaseDC (mainwindow, maindc);
534 AppActivate(false, false);
539 //==========================================================================
542 BOOL bSetupPixelFormat(HDC hDC)
544 static PIXELFORMATDESCRIPTOR pfd = {
545 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
547 PFD_DRAW_TO_WINDOW // support window
548 | PFD_SUPPORT_OPENGL // support OpenGL
549 | PFD_DOUBLEBUFFER , // double buffered
550 PFD_TYPE_RGBA, // RGBA type
551 24, // 24-bit color depth
552 0, 0, 0, 0, 0, 0, // color bits ignored
553 0, // no alpha buffer
554 0, // shift bit ignored
555 0, // no accumulation buffer
556 0, 0, 0, 0, // accum bits ignored
557 32, // 32-bit z-buffer
558 0, // no stencil buffer
559 0, // no auxiliary buffer
560 PFD_MAIN_PLANE, // main layer
562 0, 0, 0 // layer masks ignored
566 if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
568 MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
572 if (SetPixelFormat(hDC, pixelformat, &pfd) == FALSE)
574 MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
583 byte scantokey[128] =
585 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
586 0 ,27 ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'0' ,'-' ,'=' ,K_BACKSPACE,9 , // 0
587 'q' ,'w' ,'e' ,'r' ,'t' ,'y' ,'u' ,'i' ,'o' ,'p' ,'[' ,']' ,13 ,K_CTRL ,'a' ,'s' , // 1
588 'd' ,'f' ,'g' ,'h' ,'j' ,'k' ,'l' ,';' ,'\'' ,'`' ,K_SHIFT,'\\' ,'z' ,'x' ,'c' ,'v' , // 2
589 'b' ,'n' ,'m' ,',' ,'.' ,'/' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2 ,K_F3 ,K_F4 ,K_F5 , // 3
590 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
591 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
592 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
593 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
597 byte shiftscantokey[128] =
599 // 0 1 2 3 4 5 6 7 8 9 A B C D E F
600 0 ,27 ,'!' ,'@' ,'#' ,'$' ,'%' ,'^' ,'&' ,'*' ,'(' ,')' ,'_' ,'+' ,K_BACKSPACE,9 , // 0
601 'Q' ,'W' ,'E' ,'R' ,'T' ,'Y' ,'U' ,'I' ,'O' ,'P' ,'{' ,'}' ,13 ,K_CTRL ,'A' ,'S' , // 1
602 'D' ,'F' ,'G' ,'H' ,'J' ,'K' ,'L' ,':' ,'"' ,'~' ,K_SHIFT,'|' ,'Z' ,'X' ,'C' ,'V' , // 2
603 'B' ,'N' ,'M' ,'<' ,'>' ,'?' ,K_SHIFT,'*' ,K_ALT ,' ' ,0 ,K_F1 ,K_F2,K_F3 ,K_F4 ,K_F5 , // 3
604 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
605 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0 ,0 ,0 ,K_F11 ,K_F12 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 5
606 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 , // 6
607 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 // 7
615 Map from windows to quake keynums
618 int MapKey (int key, int virtualkey)
623 if (scantokey[key] == 0)
624 Con_DPrintf("key 0x%02x has no translation\n", key);
625 // if (scantokey[key] >= 0x20 && scantokey[key] < 0x7F)
627 return scantokey[key];
631 ===================================================================
635 ===================================================================
643 void ClearAllStates (void)
647 // send an up event for each key, to make sure the server clears them all
648 for (i=0 ; i<256 ; i++)
650 Key_Event (i, false);
657 void AppActivate(BOOL fActive, BOOL minimize)
658 /****************************************************************************
660 * Function: AppActivate
661 * Parameters: fActive - True if app is activating
663 * Description: If the application is activating, then swap the system
664 * into SYSPAL_NOSTATIC mode so that our palettes will display
667 ****************************************************************************/
669 static BOOL sound_active;
672 Minimized = minimize;
674 // enable/disable sound on focus gain/loss
675 if (!ActiveApp && sound_active)
678 sound_active = false;
680 else if (ActiveApp && !sound_active)
688 if (modestate == MS_FULLDIB)
692 if (vid_canalttab && vid_wassuspended) {
693 vid_wassuspended = false;
694 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
695 ShowWindow(mainwindow, SW_SHOWNORMAL);
698 else if ((modestate == MS_WINDOWED) && _windowed_mouse.value && key_dest == key_game)
707 if (modestate == MS_FULLDIB)
709 IN_DeactivateMouse ();
712 ChangeDisplaySettings (NULL, 0);
713 vid_wassuspended = true;
716 else if ((modestate == MS_WINDOWED) && _windowed_mouse.value)
718 IN_DeactivateMouse ();
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");
1252 // int existingmode;
1253 int basenummodes, width, height, bpp, findbpp, done;
1257 memset(&devmode, 0, sizeof(devmode));
1259 Cvar_RegisterVariable (&vid_mode);
1260 // Cvar_RegisterVariable (&_vid_default_mode);
1261 // Cvar_RegisterVariable (&_vid_default_mode_win);
1262 Cvar_RegisterVariable (&_windowed_mouse);
1264 Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);
1265 Cmd_AddCommand ("vid_describecurrentmode", VID_DescribeCurrentMode_f);
1266 Cmd_AddCommand ("vid_describemode", VID_DescribeMode_f);
1267 Cmd_AddCommand ("vid_describemodes", VID_DescribeModes_f);
1269 hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON2));
1271 InitCommonControls();
1273 VID_InitDIB (global_hInstance);
1274 basenummodes = nummodes = 1;
1276 VID_InitFullDIB (global_hInstance);
1278 if (COM_CheckParm("-window"))
1282 if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
1283 Sys_Error ("Can't run in non-RGB mode");
1285 ReleaseDC (NULL, hdc);
1289 vid_default = MODE_WINDOWED;
1294 Sys_Error ("No RGB fullscreen modes available");
1298 if (COM_CheckParm("-mode"))
1299 vid_default = atoi(com_argv[COM_CheckParm("-mode")+1]);
1302 if (COM_CheckParm("-current"))
1304 modelist[MODE_FULLSCREEN_DEFAULT].width = GetSystemMetrics (SM_CXSCREEN);
1305 modelist[MODE_FULLSCREEN_DEFAULT].height = GetSystemMetrics (SM_CYSCREEN);
1306 vid_default = MODE_FULLSCREEN_DEFAULT;
1307 leavecurrentmode = 1;
1311 if (COM_CheckParm("-width"))
1312 width = atoi(com_argv[COM_CheckParm("-width")+1]);
1316 if (COM_CheckParm("-bpp"))
1318 bpp = atoi(com_argv[COM_CheckParm("-bpp")+1]);
1327 if (COM_CheckParm("-height"))
1328 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1330 // if they want to force it, add the specified mode to the list
1331 if (COM_CheckParm("-force") && (nummodes < MAX_MODE_LIST))
1332 VID_AddMode(MS_FULLDIB, width, height, 0, 0, 1, 1, bpp);
1335 modelist[nummodes].type = MS_FULLDIB;
1336 modelist[nummodes].width = width;
1337 modelist[nummodes].height = height;
1338 modelist[nummodes].modenum = 0;
1339 modelist[nummodes].halfscreen = 0;
1340 modelist[nummodes].dib = 1;
1341 modelist[nummodes].fullscreen = 1;
1342 modelist[nummodes].bpp = bpp;
1343 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1345 for (i=nummodes, existingmode = 0 ; i<nummodes ; i++)
1347 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1363 if (COM_CheckParm("-height"))
1365 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1367 for (i=1, vid_default=0 ; i<nummodes ; i++)
1369 if ((modelist[i].width == width) && (modelist[i].height == height) && (modelist[i].bpp == bpp))
1379 for (i=1, vid_default=0 ; i<nummodes ; i++)
1381 if ((modelist[i].width == width) && (modelist[i].bpp == bpp))
1396 case 15: bpp = 16;break;
1397 case 16: bpp = 32;break;
1398 case 32: bpp = 24;break;
1399 case 24: done = 1;break;
1409 Sys_Error ("Specified video mode not available");
1414 vid_initialized = true;
1416 if ((i = COM_CheckParm("-conwidth")) != 0)
1417 vid.conwidth = atoi(com_argv[i+1]);
1421 vid.conwidth &= 0xfff8; // make it a multiple of eight
1423 if (vid.conwidth < 320)
1426 // pick a conheight that matches with correct aspect
1427 vid.conheight = vid.conwidth*3 / 4;
1429 if ((i = COM_CheckParm("-conheight")) != 0)
1430 vid.conheight = atoi(com_argv[i+1]);
1431 if (vid.conheight < 200)
1432 vid.conheight = 200;
1434 VID_SetMode (vid_default);
1436 maindc = GetDC(mainwindow);
1437 bSetupPixelFormat(maindc);
1439 baseRC = wglCreateContext( maindc );
1441 Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.");
1442 if (!wglMakeCurrent( maindc, baseRC ))
1443 Sys_Error ("wglMakeCurrent failed");
1447 // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
1448 if (strncasecmp(gl_vendor,"ATI",3)==0)
1450 if (strncasecmp(gl_renderer,"Rage Pro",8)==0)
1453 if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
1456 // sprintf (gldir, "%s/glquake", com_gamedir);
1457 // Sys_mkdir (gldir);
1459 vid_realmode = vid_modenum;
1461 vid_menudrawfn = VID_MenuDraw;
1462 vid_menukeyfn = VID_MenuKey;
1464 strcpy (badmode.modedesc, "Bad mode");
1465 vid_canalttab = true;
1469 //========================================================
1471 //========================================================
1473 extern void M_Menu_Options_f (void);
1474 extern void M_Print (int cx, int cy, char *str);
1475 extern void M_PrintWhite (int cx, int cy, char *str);
1476 extern void M_DrawCharacter (int cx, int line, int num);
1477 extern void M_DrawPic (int x, int y, qpic_t *pic);
1479 static int vid_line, vid_wmodes;
1488 #define MAX_COLUMN_SIZE 9
1489 #define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 2)
1490 #define MAX_MODEDESCS (MAX_COLUMN_SIZE*3)
1492 static modedesc_t modedescs[MAX_MODEDESCS];
1499 void VID_MenuDraw (void)
1503 int lnummodes, i, k, column, row;
1506 p = Draw_CachePic ("gfx/vidmodes.lmp");
1507 M_DrawPic ( (320-p->width)/2, 4, p);
1510 lnummodes = VID_NumModes ();
1512 for (i=1 ; (i<lnummodes) && (vid_wmodes < MAX_MODEDESCS) ; i++)
1514 ptr = VID_GetModeDescription (i);
1515 pv = VID_GetModePtr (i);
1519 modedescs[k].modenum = i;
1520 modedescs[k].desc = ptr;
1521 modedescs[k].iscur = 0;
1523 if (i == vid_modenum)
1524 modedescs[k].iscur = 1;
1532 M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)");
1537 for (i=0 ; i<vid_wmodes ; i++)
1539 if (modedescs[i].iscur)
1540 M_PrintWhite (column, row, modedescs[i].desc);
1542 M_Print (column, row, modedescs[i].desc);
1546 if ((i % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
1554 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*2,
1555 "Video modes must be set from the");
1556 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3,
1557 "command line with -width <width>");
1558 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4,
1559 "and -bpp <bits-per-pixel>");
1560 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6,
1561 "Select windowed mode with -window");
1570 void VID_MenuKey (int key)
1575 S_LocalSound ("misc/menu1.wav");
1576 M_Menu_Options_f ();