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 WARP_WIDTH 320
30 #define WARP_HEIGHT 200
31 #define MAXWIDTH 10000
32 #define MAXHEIGHT 10000
34 #define BASEHEIGHT 200
36 #define MODE_WINDOWED 0
37 #define NO_MODE (MODE_WINDOWED - 1)
38 #define MODE_FULLSCREEN_DEFAULT (MODE_WINDOWED + 1)
57 lmode_t lowresmodes[] = {
64 const char *gl_vendor;
65 const char *gl_renderer;
66 const char *gl_version;
67 const char *gl_extensions;
70 qboolean scr_skipupdate;
72 static vmode_t modelist[MAX_MODE_LIST];
74 static vmode_t *pcurrentmode;
75 static vmode_t badmode;
77 static DEVMODE gdevmode;
78 static qboolean vid_initialized = false;
79 static qboolean windowed, leavecurrentmode;
80 static qboolean vid_canalttab = false;
81 static qboolean vid_wassuspended = false;
82 static int windowed_mouse;
83 extern qboolean mouseactive; // from in_win.c
86 int DIBWidth, DIBHeight;
88 DWORD WindowStyle, ExWindowStyle;
90 HWND mainwindow, dibwindow;
92 int vid_modenum = NO_MODE;
94 int vid_default = MODE_WINDOWED;
95 static int windowed_default;
96 unsigned char vid_curpal[256*3];
103 HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow);
105 viddef_t vid; // global video state
107 float gldepthmin, gldepthmax;
109 modestate_t modestate = MS_UNINIT;
111 void VID_MenuDraw (void);
112 void VID_MenuKey (int key);
114 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
115 void AppActivate(BOOL fActive, BOOL minimize);
116 char *VID_GetModeDescription (int mode);
117 void ClearAllStates (void);
118 void VID_UpdateWindowStatus (void);
121 // LordHavoc: ARB multitexture support
122 int gl_mtex_enum = 0;
124 qboolean is8bit = false;
125 qboolean isPermedia = false;
126 qboolean isATI = false; // LordHavoc: special differences for ATI's broken drivers
127 qboolean isG200 = false; // LordHavoc: the Matrox G200 can't do per pixel alpha, and it uses a D3D driver for GL... ugh...
128 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.
129 qboolean gl_mtexable = false;
130 qboolean gl_arrays = false;
132 //====================================
134 cvar_t vid_mode = {"vid_mode","0", false};
135 // Note that 0 is MODE_WINDOWED
136 cvar_t _vid_default_mode = {"_vid_default_mode","0", true};
137 // Note that 3 is MODE_FULLSCREEN_DEFAULT
138 cvar_t _vid_default_mode_win = {"_vid_default_mode_win","3", true};
139 cvar_t vid_wait = {"vid_wait","0"};
140 cvar_t vid_nopageflip = {"vid_nopageflip","0", true};
141 cvar_t _vid_wait_override = {"_vid_wait_override", "0", true};
142 cvar_t vid_config_x = {"vid_config_x","800", true};
143 cvar_t vid_config_y = {"vid_config_y","600", true};
144 cvar_t vid_stretch_by_2 = {"vid_stretch_by_2","1", true};
145 cvar_t _windowed_mouse = {"_windowed_mouse","1", true};
147 int window_center_x, window_center_y, window_x, window_y, window_width, window_height;
150 // direct draw software compatability stuff
152 void CenterWindow(HWND hWndCenter, int width, int height, BOOL lefttopjustify)
154 int CenterX, CenterY;
156 CenterX = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
157 CenterY = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
158 if (CenterX > CenterY*2)
159 CenterX >>= 1; // dual screens
160 CenterX = (CenterX < 0) ? 0: CenterX;
161 CenterY = (CenterY < 0) ? 0: CenterY;
162 SetWindowPos (hWndCenter, NULL, CenterX, CenterY, 0, 0,
163 SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
166 qboolean VID_SetWindowedMode (int modenum)
168 int lastmodestate, width, height;
171 lastmodestate = modestate;
173 WindowRect.top = WindowRect.left = 0;
175 WindowRect.right = modelist[modenum].width;
176 WindowRect.bottom = modelist[modenum].height;
178 DIBWidth = modelist[modenum].width;
179 DIBHeight = modelist[modenum].height;
181 WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU |
186 AdjustWindowRectEx(&rect, WindowStyle, FALSE, 0);
188 width = rect.right - rect.left;
189 height = rect.bottom - rect.top;
191 // Create the DIB window
192 dibwindow = CreateWindowEx (
211 Sys_Error ("Couldn't create DIB window");
213 // Center and show the DIB window
214 CenterWindow(dibwindow, WindowRect.right - WindowRect.left,
215 WindowRect.bottom - WindowRect.top, false);
217 ShowWindow (dibwindow, SW_SHOWDEFAULT);
218 UpdateWindow (dibwindow);
220 modestate = MS_WINDOWED;
222 // LordHavoc: using GDI functions on an OpenGL window? bad idea
224 // because we have set the background brush for the window to NULL
225 // (to avoid flickering when re-sizing the window on the desktop),
226 // we clear the window to black when created, otherwise it will be
227 // empty while Quake starts up.
228 hdc = GetDC(dibwindow);
229 PatBlt(hdc,0,0,WindowRect.right,WindowRect.bottom,BLACKNESS);
230 ReleaseDC(dibwindow, hdc);
233 if (vid.conheight > modelist[modenum].height)
234 vid.conheight = modelist[modenum].height;
235 if (vid.conwidth > modelist[modenum].width)
236 vid.conwidth = modelist[modenum].width;
237 vid.width = vid.conwidth;
238 vid.height = vid.conheight;
240 mainwindow = dibwindow;
242 SendMessage (mainwindow, WM_SETICON, (WPARAM)TRUE, (LPARAM)hIcon);
243 SendMessage (mainwindow, WM_SETICON, (WPARAM)FALSE, (LPARAM)hIcon);
249 qboolean VID_SetFullDIBMode (int modenum)
251 int lastmodestate, width, height;
254 if (!leavecurrentmode)
256 gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
257 gdevmode.dmBitsPerPel = modelist[modenum].bpp;
258 gdevmode.dmPelsWidth = modelist[modenum].width <<
259 modelist[modenum].halfscreen;
260 gdevmode.dmPelsHeight = modelist[modenum].height;
261 gdevmode.dmSize = sizeof (gdevmode);
263 if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
264 Sys_Error ("Couldn't set fullscreen DIB mode");
267 lastmodestate = modestate;
268 modestate = MS_FULLDIB;
270 WindowRect.top = WindowRect.left = 0;
272 WindowRect.right = modelist[modenum].width;
273 WindowRect.bottom = modelist[modenum].height;
275 DIBWidth = modelist[modenum].width;
276 DIBHeight = modelist[modenum].height;
278 WindowStyle = WS_POPUP;
282 AdjustWindowRectEx(&rect, WindowStyle, FALSE, 0);
284 width = rect.right - rect.left;
285 height = rect.bottom - rect.top;
287 // Create the DIB window
288 dibwindow = CreateWindowEx (
307 Sys_Error ("Couldn't create DIB window");
309 ShowWindow (dibwindow, SW_SHOWDEFAULT);
310 UpdateWindow (dibwindow);
312 // LordHavoc: using GDI functions on an OpenGL window? bad idea
314 // Because we have set the background brush for the window to NULL
315 // (to avoid flickering when re-sizing the window on the desktop), we
316 // clear the window to black when created, otherwise it will be
317 // empty while Quake starts up.
318 hdc = GetDC(dibwindow);
319 PatBlt(hdc,0,0,WindowRect.right,WindowRect.bottom,BLACKNESS);
320 ReleaseDC(dibwindow, hdc);
323 if (vid.conheight > modelist[modenum].height)
324 vid.conheight = modelist[modenum].height;
325 if (vid.conwidth > modelist[modenum].width)
326 vid.conwidth = modelist[modenum].width;
327 vid.width = vid.conwidth;
328 vid.height = vid.conheight;
330 // needed because we're not getting WM_MOVE messages fullscreen on NT
334 mainwindow = dibwindow;
336 SendMessage (mainwindow, WM_SETICON, (WPARAM)TRUE, (LPARAM)hIcon);
337 SendMessage (mainwindow, WM_SETICON, (WPARAM)FALSE, (LPARAM)hIcon);
343 int VID_SetMode (int modenum, unsigned char *palette)
345 int original_mode, temp;
349 if ((windowed && (modenum != 0)) ||
350 (!windowed && (modenum < 1)) ||
351 (!windowed && (modenum >= nummodes)))
353 Sys_Error ("Bad video mode\n");
356 // so Con_Printfs don't mess us up by forcing vid and snd updates
357 temp = scr_disabled_for_loading;
358 scr_disabled_for_loading = true;
362 if (vid_modenum == NO_MODE)
363 original_mode = windowed_default;
365 original_mode = vid_modenum;
367 // Set either the fullscreen or windowed mode
368 if (modelist[modenum].type == MS_WINDOWED)
370 if (_windowed_mouse.value && key_dest == key_game)
372 stat = VID_SetWindowedMode(modenum);
378 IN_DeactivateMouse ();
380 stat = VID_SetWindowedMode(modenum);
383 else if (modelist[modenum].type == MS_FULLDIB)
385 stat = VID_SetFullDIBMode(modenum);
391 Sys_Error ("VID_SetMode: Bad mode type in modelist");
394 window_width = DIBWidth;
395 window_height = DIBHeight;
396 VID_UpdateWindowStatus ();
399 scr_disabled_for_loading = temp;
403 Sys_Error ("Couldn't set video mode");
406 // now we try to make sure we get the focus on the mode switch, because
407 // sometimes in some systems we don't. We grab the foreground, then
408 // finish setting up, pump all our messages, and sleep for a little while
409 // to let messages finish bouncing around the system, then we put
410 // ourselves at the top of the z order, then grab the foreground again,
411 // Who knows if it helps, but it probably doesn't hurt
412 SetForegroundWindow (mainwindow);
413 VID_SetPalette (palette);
414 vid_modenum = modenum;
415 Cvar_SetValue ("vid_mode", (float)vid_modenum);
417 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
419 TranslateMessage (&msg);
420 DispatchMessage (&msg);
425 SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0,
426 SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW |
429 SetForegroundWindow (mainwindow);
431 // fix the leftover Alt from any Alt-Tab or the like that switched us away
435 Con_SafePrintf ("Video mode %s initialized.\n", VID_GetModeDescription (vid_modenum));
437 VID_SetPalette (palette);
439 vid.recalc_refdef = 1;
447 VID_UpdateWindowStatus
450 void VID_UpdateWindowStatus (void)
453 window_rect.left = window_x;
454 window_rect.top = window_y;
455 window_rect.right = window_x + window_width;
456 window_rect.bottom = window_y + window_height;
457 window_center_x = (window_rect.left + window_rect.right) / 2;
458 window_center_y = (window_rect.top + window_rect.bottom) / 2;
460 IN_UpdateClipCursor ();
464 //====================================
466 void (APIENTRY *qglVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
467 void (APIENTRY *qglColorPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
468 void (APIENTRY *qglTexCoordPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
469 void (APIENTRY *qglArrayElement)(GLint i);
470 void (APIENTRY *qglDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
471 void (APIENTRY *qglMTexCoord2f)(GLenum, GLfloat, GLfloat);
472 void (APIENTRY *qglSelectTexture)(GLenum);
473 void (APIENTRY *glColorTableEXT)(int, int, int, int, int, const void*);
475 void CheckVertexArrays (void)
477 if (COM_CheckParm("-novertex"))
479 Con_Printf("...vertex array support disabled\n");
482 if ((qglArrayElement = (void *) wglGetProcAddress("glArrayElement"))
483 && (qglColorPointer = (void *) wglGetProcAddress("glColorPointer"))
484 // && (qglDrawArrays = (void *) wglGetProcAddress("glDrawArrays"))
485 && (qglDrawElements = (void *) wglGetProcAddress("glDrawElements"))
486 // && (qglInterleavedArrays = (void *) wglGetProcAddress("glInterleavedArrays"))
487 && (qglTexCoordPointer = (void *) wglGetProcAddress("glTexCoordPointer"))
488 && (qglVertexPointer = (void *) wglGetProcAddress("glVertexPointer"))
491 Con_Printf("...vertex array support detected\n");
496 Con_Printf("...vertex array support disabled (not detected - get a better driver)\n");
499 int texture_extension_number = 1;
501 void CheckMultiTexture(void)
503 qglMTexCoord2f = NULL;
504 qglSelectTexture = NULL;
505 // Check to see if multitexture is disabled
506 if (COM_CheckParm("-nomtex"))
508 Con_Printf("...multitexture disabled\n");
511 // Test for ARB_multitexture
512 if (!COM_CheckParm("-SGISmtex") && strstr(gl_extensions, "GL_ARB_multitexture "))
514 Con_Printf("...using GL_ARB_multitexture\n");
515 qglMTexCoord2f = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
516 qglSelectTexture = (void *) wglGetProcAddress("glActiveTextureARB");
518 gl_mtex_enum = GL_TEXTURE0_ARB;
520 else if (strstr(gl_extensions, "GL_SGIS_multitexture ")) // Test for SGIS_multitexture (if ARB_multitexture not found)
522 Con_Printf("...using GL_SGIS_multitexture\n");
523 qglMTexCoord2f = (void *) wglGetProcAddress("glMTexCoord2fSGIS");
524 qglSelectTexture = (void *) wglGetProcAddress("glSelectTextureSGIS");
526 gl_mtex_enum = TEXTURE0_SGIS;
529 Con_Printf("...multitexture disabled (not detected)\n");
537 extern char *QSG_EXTENSIONS;
540 gl_vendor = glGetString (GL_VENDOR);
541 Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
542 gl_renderer = glGetString (GL_RENDERER);
543 Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
545 gl_version = glGetString (GL_VERSION);
546 Con_Printf ("GL_VERSION: %s\n", gl_version);
547 gl_extensions = glGetString (GL_EXTENSIONS);
548 Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
550 // Con_Printf ("%s %s\n", gl_renderer, gl_version);
552 if (strncasecmp(gl_renderer,"Permedia",8)==0)
555 // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
556 if (strncasecmp(gl_vendor,"ATI",3)==0)
559 if (strncasecmp(gl_renderer,"Rage Pro",8)==0)
562 if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
565 CheckMultiTexture ();
566 CheckVertexArrays ();
568 // LordHavoc: report supported extensions
569 Con_Printf ("\nQSG extensions: %s\n", QSG_EXTENSIONS);
571 // LordHavoc: set up state
572 // glEnable(GL_DEPTH_TEST);
573 // glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
574 // glShadeModel(GL_SMOOTH);
575 // glEnable(GL_TEXTURE_2D);
576 glAlphaFunc(GL_GREATER, 0.5);
577 // if (isRagePro || isG200)
579 // glEnable(GL_ALPHA_TEST);
580 // glAlphaFunc(GL_GREATER, 0.5);
583 // glDisable(GL_ALPHA_TEST);
585 // glDisable(GL_DITHER); // LordHavoc: disable dithering
586 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
587 // glEnable(GL_BLEND);
589 // glClearColor (0,0,0,0); // LordHavoc: changed from red to black
590 glCullFace(GL_FRONT);
592 // glAlphaFunc(GL_GREATER, 0.666);
601 void GL_BeginRendering (int *x, int *y, int *width, int *height)
603 // extern cvar_t gl_clear;
606 *width = WindowRect.right - WindowRect.left;
607 *height = WindowRect.bottom - WindowRect.top;
609 // if (!wglMakeCurrent( maindc, baseRC ))
610 // Sys_Error ("wglMakeCurrent failed");
612 // glViewport (*x, *y, *width, *height);
616 void GL_EndRendering (void)
621 // handle the mouse state when windowed if that's changed
622 if (modestate == MS_WINDOWED)
624 if (!_windowed_mouse.value)
628 IN_DeactivateMouse ();
630 windowed_mouse = false;
635 windowed_mouse = true;
636 if (key_dest == key_game && !mouseactive && ActiveApp)
641 else if (mouseactive && key_dest != key_game)
643 IN_DeactivateMouse ();
650 void VID_SetDefaultMode (void)
652 IN_DeactivateMouse ();
656 void VID_Shutdown (void)
665 vid_canalttab = false;
666 hRC = wglGetCurrentContext();
667 hDC = wglGetCurrentDC();
669 wglMakeCurrent(NULL, NULL);
671 // LordHavoc: free textures before closing (may help NVIDIA)
672 for (i = 0;i < 8192;i++) temp[i] = i;
673 glDeleteTextures(8192, temp);
676 wglDeleteContext(hRC);
678 if (hDC && dibwindow)
679 ReleaseDC(dibwindow, hDC);
681 if (modestate == MS_FULLDIB)
682 ChangeDisplaySettings (NULL, 0);
684 if (maindc && dibwindow)
685 ReleaseDC (dibwindow, maindc);
687 AppActivate(false, false);
692 //==========================================================================
695 BOOL bSetupPixelFormat(HDC hDC)
697 static PIXELFORMATDESCRIPTOR pfd = {
698 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
700 PFD_DRAW_TO_WINDOW // support window
701 | PFD_SUPPORT_OPENGL // support OpenGL
702 | PFD_DOUBLEBUFFER , // double buffered
703 PFD_TYPE_RGBA, // RGBA type
704 24, // 24-bit color depth
705 0, 0, 0, 0, 0, 0, // color bits ignored
706 0, // no alpha buffer
707 0, // shift bit ignored
708 0, // no accumulation buffer
709 0, 0, 0, 0, // accum bits ignored
710 32, // 32-bit z-buffer
711 0, // no stencil buffer
712 0, // no auxiliary buffer
713 PFD_MAIN_PLANE, // main layer
715 0, 0, 0 // layer masks ignored
719 if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
721 MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
725 if (SetPixelFormat(hDC, pixelformat, &pfd) == FALSE)
727 MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
736 byte scantokey[128] =
740 0 , 27, '1', '2', '3', '4', '5', '6',
741 '7', '8', '9', '0', '-', '=', K_BACKSPACE, 9, // 0
742 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
743 'o', 'p', '[', ']', 13 , K_CTRL,'a', 's', // 1
744 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
745 '\'' , '`', K_SHIFT,'\\', 'z', 'x', 'c', 'v', // 2
746 'b', 'n', 'm', ',', '.', '/', K_SHIFT,'*',
747 K_ALT,' ', 0 , K_F1, K_F2, K_F3, K_F4, K_F5, // 3
748 K_F6, K_F7, K_F8, K_F9, K_F10, K_PAUSE , 0 , K_HOME,
749 K_UPARROW,K_PGUP,'-',K_LEFTARROW,'5',K_RIGHTARROW,'+',K_END, //4
750 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0,0, 0, K_F11,
751 K_F12,0 , 0 , 0 , 0 , 0 , 0 , 0, // 5
752 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
753 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, // 6
754 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
755 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 7
758 byte shiftscantokey[128] =
762 0 , 27, '!', '@', '#', '$', '%', '^',
763 '&', '*', '(', ')', '_', '+', K_BACKSPACE, 9, // 0
764 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
765 'O', 'P', '{', '}', 13 , K_CTRL,'A', 'S', // 1
766 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
767 '"' , '~', K_SHIFT,'|', 'Z', 'X', 'C', 'V', // 2
768 'B', 'N', 'M', '<', '>', '?', K_SHIFT,'*',
769 K_ALT,' ', 0 , K_F1, K_F2, K_F3, K_F4, K_F5, // 3
770 K_F6, K_F7, K_F8, K_F9, K_F10, K_PAUSE , 0 , K_HOME,
771 K_UPARROW,K_PGUP,'_',K_LEFTARROW,'%',K_RIGHTARROW,'+',K_END, //4
772 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0,0, 0, K_F11,
773 K_F12,0 , 0 , 0 , 0 , 0 , 0 , 0, // 5
774 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
775 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, // 6
776 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
777 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 7
785 Map from windows to quake keynums
793 if (scantokey[key] == 0)
794 Con_DPrintf("key 0x%02x has no translation\n", key);
795 return scantokey[key];
799 ===================================================================
803 ===================================================================
811 void ClearAllStates (void)
815 // send an up event for each key, to make sure the server clears them all
816 for (i=0 ; i<256 ; i++)
818 Key_Event (i, false);
825 void AppActivate(BOOL fActive, BOOL minimize)
826 /****************************************************************************
828 * Function: AppActivate
829 * Parameters: fActive - True if app is activating
831 * Description: If the application is activating, then swap the system
832 * into SYSPAL_NOSTATIC mode so that our palettes will display
835 ****************************************************************************/
837 static BOOL sound_active;
840 Minimized = minimize;
842 // enable/disable sound on focus gain/loss
843 if (!ActiveApp && sound_active)
846 sound_active = false;
848 else if (ActiveApp && !sound_active)
856 if (modestate == MS_FULLDIB)
860 if (vid_canalttab && vid_wassuspended) {
861 vid_wassuspended = false;
862 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
863 ShowWindow(mainwindow, SW_SHOWNORMAL);
866 else if ((modestate == MS_WINDOWED) && _windowed_mouse.value && key_dest == key_game)
875 if (modestate == MS_FULLDIB)
877 IN_DeactivateMouse ();
880 ChangeDisplaySettings (NULL, 0);
881 vid_wassuspended = true;
884 else if ((modestate == MS_WINDOWED) && _windowed_mouse.value)
886 IN_DeactivateMouse ();
892 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
894 /* main window procedure */
895 LONG WINAPI MainWndProc (
902 int fActive, fMinimized, temp;
903 extern unsigned int uiWheelMessage;
905 if ( uMsg == uiWheelMessage )
906 uMsg = WM_MOUSEWHEEL;
911 if (modestate == MS_FULLDIB)
912 ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
919 window_x = (int) LOWORD(lParam);
920 window_y = (int) HIWORD(lParam);
921 VID_UpdateWindowStatus ();
926 Key_Event (MapKey(lParam), true);
931 Key_Event (MapKey(lParam), false);
935 // keep Alt-Space from happening
938 // this is complicated because Win32 seems to pack multiple mouse events into
939 // one update sometimes, so we always check all states and look for events
949 if (wParam & MK_LBUTTON)
952 if (wParam & MK_RBUTTON)
955 if (wParam & MK_MBUTTON)
958 IN_MouseEvent (temp);
962 // JACK: This is the mouse wheel with the Intellimouse
963 // Its delta is either positive or neg, and we generate the proper
966 if ((short) HIWORD(wParam) > 0) {
967 Key_Event(K_MWHEELUP, true);
968 Key_Event(K_MWHEELUP, false);
970 Key_Event(K_MWHEELDOWN, true);
971 Key_Event(K_MWHEELDOWN, false);
979 if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit",
980 MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
988 fActive = LOWORD(wParam);
989 fMinimized = (BOOL) HIWORD(wParam);
990 AppActivate(!(fActive == WA_INACTIVE), fMinimized);
992 // fix the leftover Alt from any Alt-Tab or the like that switched us away
1000 DestroyWindow (dibwindow);
1002 PostQuitMessage (0);
1007 lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
1011 /* pass all unhandled messages to DefWindowProc */
1012 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
1016 /* return 1 if handled message, 0 if not */
1026 int VID_NumModes (void)
1037 vmode_t *VID_GetModePtr (int modenum)
1040 if ((modenum >= 0) && (modenum < nummodes))
1041 return &modelist[modenum];
1049 VID_GetModeDescription
1052 char *VID_GetModeDescription (int mode)
1056 static char temp[100];
1058 if ((mode < 0) || (mode >= nummodes))
1061 if (!leavecurrentmode)
1063 pv = VID_GetModePtr (mode);
1064 pinfo = pv->modedesc;
1068 sprintf (temp, "Desktop resolution (%dx%d)",
1069 modelist[MODE_FULLSCREEN_DEFAULT].width,
1070 modelist[MODE_FULLSCREEN_DEFAULT].height);
1078 // KJB: Added this to return the mode driver name in description for console
1080 char *VID_GetExtModeDescription (int mode)
1082 static char pinfo[40];
1085 if ((mode < 0) || (mode >= nummodes))
1088 pv = VID_GetModePtr (mode);
1089 if (modelist[mode].type == MS_FULLDIB)
1091 if (!leavecurrentmode)
1093 sprintf(pinfo,"%s fullscreen", pv->modedesc);
1097 sprintf (pinfo, "Desktop resolution (%dx%d)",
1098 modelist[MODE_FULLSCREEN_DEFAULT].width,
1099 modelist[MODE_FULLSCREEN_DEFAULT].height);
1104 if (modestate == MS_WINDOWED)
1105 sprintf(pinfo, "%s windowed", pv->modedesc);
1107 sprintf(pinfo, "windowed");
1116 VID_DescribeCurrentMode_f
1119 void VID_DescribeCurrentMode_f (void)
1121 Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum));
1130 void VID_NumModes_f (void)
1134 Con_Printf ("%d video mode is available\n", nummodes);
1136 Con_Printf ("%d video modes are available\n", nummodes);
1145 void VID_DescribeMode_f (void)
1149 modenum = atoi (Cmd_Argv(1));
1151 t = leavecurrentmode;
1152 leavecurrentmode = 0;
1154 Con_Printf ("%s\n", VID_GetExtModeDescription (modenum));
1156 leavecurrentmode = t;
1165 void VID_DescribeModes_f (void)
1167 int i, lnummodes, t;
1171 lnummodes = VID_NumModes ();
1173 t = leavecurrentmode;
1174 leavecurrentmode = 0;
1176 for (i=1 ; i<lnummodes ; i++)
1178 pv = VID_GetModePtr (i);
1179 pinfo = VID_GetExtModeDescription (i);
1180 Con_Printf ("%2d: %s\n", i, pinfo);
1183 leavecurrentmode = t;
1187 void VID_InitDIB (HINSTANCE hInstance)
1191 /* Register the frame class */
1193 wc.lpfnWndProc = (WNDPROC)MainWndProc;
1196 wc.hInstance = hInstance;
1198 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
1199 wc.hbrBackground = NULL;
1200 wc.lpszMenuName = 0;
1202 wc.lpszClassName = "DPNehahraQuake";
1204 wc.lpszClassName = "DarkPlacesQuake";
1207 if (!RegisterClass (&wc) )
1208 Sys_Error ("Couldn't register window class");
1210 modelist[0].type = MS_WINDOWED;
1212 if (COM_CheckParm("-width"))
1213 modelist[0].width = atoi(com_argv[COM_CheckParm("-width")+1]);
1215 modelist[0].width = 640;
1217 if (modelist[0].width < 320)
1218 modelist[0].width = 320;
1220 if (COM_CheckParm("-height"))
1221 modelist[0].height= atoi(com_argv[COM_CheckParm("-height")+1]);
1223 modelist[0].height = modelist[0].width * 240/320;
1225 if (modelist[0].height < 240)
1226 modelist[0].height = 240;
1228 sprintf (modelist[0].modedesc, "%dx%d",
1229 modelist[0].width, modelist[0].height);
1231 modelist[0].modenum = MODE_WINDOWED;
1232 modelist[0].dib = 1;
1233 modelist[0].fullscreen = 0;
1234 modelist[0].halfscreen = 0;
1235 modelist[0].bpp = 0;
1246 void VID_InitFullDIB (HINSTANCE hInstance)
1249 int i, modenum, originalnummodes, existingmode, numlowresmodes;
1253 // enumerate >8 bpp modes
1254 originalnummodes = nummodes;
1259 stat = EnumDisplaySettings (NULL, modenum, &devmode);
1261 if ((devmode.dmBitsPerPel >= 15) &&
1262 (devmode.dmPelsWidth <= MAXWIDTH) &&
1263 (devmode.dmPelsHeight <= MAXHEIGHT) &&
1264 (nummodes < MAX_MODE_LIST))
1266 devmode.dmFields = DM_BITSPERPEL |
1270 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) ==
1271 DISP_CHANGE_SUCCESSFUL)
1273 modelist[nummodes].type = MS_FULLDIB;
1274 modelist[nummodes].width = devmode.dmPelsWidth;
1275 modelist[nummodes].height = devmode.dmPelsHeight;
1276 modelist[nummodes].modenum = 0;
1277 modelist[nummodes].halfscreen = 0;
1278 modelist[nummodes].dib = 1;
1279 modelist[nummodes].fullscreen = 1;
1280 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1281 sprintf (modelist[nummodes].modedesc, "%dx%dx%d",
1282 devmode.dmPelsWidth, devmode.dmPelsHeight,
1283 devmode.dmBitsPerPel);
1285 // if the width is more than twice the height, reduce it by half because this
1286 // is probably a dual-screen monitor
1287 if (!COM_CheckParm("-noadjustaspect"))
1289 if (modelist[nummodes].width > (modelist[nummodes].height << 1))
1291 modelist[nummodes].width >>= 1;
1292 modelist[nummodes].halfscreen = 1;
1293 sprintf (modelist[nummodes].modedesc, "%dx%dx%d",
1294 modelist[nummodes].width,
1295 modelist[nummodes].height,
1296 modelist[nummodes].bpp);
1300 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1302 if ((modelist[nummodes].width == modelist[i].width) &&
1303 (modelist[nummodes].height == modelist[i].height) &&
1304 (modelist[nummodes].bpp == modelist[i].bpp))
1321 // see if there are any low-res modes that aren't being reported
1322 numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]);
1328 for (j=0 ; (j<numlowresmodes) && (nummodes < MAX_MODE_LIST) ; j++)
1330 devmode.dmBitsPerPel = bpp;
1331 devmode.dmPelsWidth = lowresmodes[j].width;
1332 devmode.dmPelsHeight = lowresmodes[j].height;
1333 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1335 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) ==
1336 DISP_CHANGE_SUCCESSFUL)
1338 modelist[nummodes].type = MS_FULLDIB;
1339 modelist[nummodes].width = devmode.dmPelsWidth;
1340 modelist[nummodes].height = devmode.dmPelsHeight;
1341 modelist[nummodes].modenum = 0;
1342 modelist[nummodes].halfscreen = 0;
1343 modelist[nummodes].dib = 1;
1344 modelist[nummodes].fullscreen = 1;
1345 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1346 sprintf (modelist[nummodes].modedesc, "%dx%dx%d",
1347 devmode.dmPelsWidth, devmode.dmPelsHeight,
1348 devmode.dmBitsPerPel);
1350 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1352 if ((modelist[nummodes].width == modelist[i].width) &&
1353 (modelist[nummodes].height == modelist[i].height) &&
1354 (modelist[nummodes].bpp == modelist[i].bpp))
1383 if (nummodes == originalnummodes)
1384 Con_SafePrintf ("No fullscreen DIB modes found\n");
1387 qboolean VID_Is8bit()
1392 #define GL_SHARED_TEXTURE_PALETTE_EXT 0x81FB
1394 void VID_Init8bitPalette()
1396 // Check for 8bit Extensions and initialize them.
1398 char thePalette[256*3];
1399 char *oldPalette, *newPalette;
1400 // LordHavoc: 8bit texture support broke many things... it now must be specifically stated on the commandline (-no8bit became -8bit)
1401 if (!COM_CheckParm("-8bit"))
1403 if (strstr(gl_extensions, "GL_EXT_shared_texture_palette"))
1405 if (!(glColorTableEXT = (void *)wglGetProcAddress("glColorTableEXT")))
1408 Con_SafePrintf("8-bit GL extensions enabled.\n");
1409 glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
1410 oldPalette = (char *) d_8to24table;
1411 newPalette = thePalette;
1414 *newPalette++ = *oldPalette++;
1415 *newPalette++ = *oldPalette++;
1416 *newPalette++ = *oldPalette++;
1419 glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, GL_RGB, 256, GL_RGB, GL_UNSIGNED_BYTE, (void *) thePalette);
1423 extern void Check_Gamma (unsigned char *pal);
1424 void VID_Setup15to8Palette ();
1431 void VID_Init (unsigned char *palette)
1433 int i, existingmode;
1434 int basenummodes, width, height, bpp, findbpp, done;
1435 // char gldir[MAX_OSPATH];
1439 memset(&devmode, 0, sizeof(devmode));
1441 Cvar_RegisterVariable (&vid_mode);
1442 Cvar_RegisterVariable (&vid_wait);
1443 Cvar_RegisterVariable (&vid_nopageflip);
1444 Cvar_RegisterVariable (&_vid_wait_override);
1445 Cvar_RegisterVariable (&_vid_default_mode);
1446 Cvar_RegisterVariable (&_vid_default_mode_win);
1447 Cvar_RegisterVariable (&vid_config_x);
1448 Cvar_RegisterVariable (&vid_config_y);
1449 Cvar_RegisterVariable (&vid_stretch_by_2);
1450 Cvar_RegisterVariable (&_windowed_mouse);
1452 Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);
1453 Cmd_AddCommand ("vid_describecurrentmode", VID_DescribeCurrentMode_f);
1454 Cmd_AddCommand ("vid_describemode", VID_DescribeMode_f);
1455 Cmd_AddCommand ("vid_describemodes", VID_DescribeModes_f);
1457 hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON2));
1459 InitCommonControls();
1461 VID_InitDIB (global_hInstance);
1462 basenummodes = nummodes = 1;
1464 VID_InitFullDIB (global_hInstance);
1466 if (COM_CheckParm("-window"))
1470 if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
1472 Sys_Error ("Can't run in non-RGB mode");
1475 ReleaseDC (NULL, hdc);
1479 vid_default = MODE_WINDOWED;
1484 Sys_Error ("No RGB fullscreen modes available");
1488 if (COM_CheckParm("-mode"))
1490 vid_default = atoi(com_argv[COM_CheckParm("-mode")+1]);
1494 if (COM_CheckParm("-current"))
1496 modelist[MODE_FULLSCREEN_DEFAULT].width =
1497 GetSystemMetrics (SM_CXSCREEN);
1498 modelist[MODE_FULLSCREEN_DEFAULT].height =
1499 GetSystemMetrics (SM_CYSCREEN);
1500 vid_default = MODE_FULLSCREEN_DEFAULT;
1501 leavecurrentmode = 1;
1505 if (COM_CheckParm("-width"))
1507 width = atoi(com_argv[COM_CheckParm("-width")+1]);
1514 if (COM_CheckParm("-bpp"))
1516 bpp = atoi(com_argv[COM_CheckParm("-bpp")+1]);
1525 if (COM_CheckParm("-height"))
1526 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1528 // if they want to force it, add the specified mode to the list
1529 if (COM_CheckParm("-force") && (nummodes < MAX_MODE_LIST))
1531 modelist[nummodes].type = MS_FULLDIB;
1532 modelist[nummodes].width = width;
1533 modelist[nummodes].height = height;
1534 modelist[nummodes].modenum = 0;
1535 modelist[nummodes].halfscreen = 0;
1536 modelist[nummodes].dib = 1;
1537 modelist[nummodes].fullscreen = 1;
1538 modelist[nummodes].bpp = bpp;
1539 sprintf (modelist[nummodes].modedesc, "%dx%dx%d",
1540 devmode.dmPelsWidth, devmode.dmPelsHeight,
1541 devmode.dmBitsPerPel);
1543 for (i=nummodes, existingmode = 0 ; i<nummodes ; i++)
1545 if ((modelist[nummodes].width == modelist[i].width) &&
1546 (modelist[nummodes].height == modelist[i].height) &&
1547 (modelist[nummodes].bpp == modelist[i].bpp))
1564 if (COM_CheckParm("-height"))
1566 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1568 for (i=1, vid_default=0 ; i<nummodes ; i++)
1570 if ((modelist[i].width == width) &&
1571 (modelist[i].height == height) &&
1572 (modelist[i].bpp == bpp))
1582 for (i=1, vid_default=0 ; i<nummodes ; i++)
1584 if ((modelist[i].width == width) && (modelist[i].bpp == bpp))
1623 Sys_Error ("Specified video mode not available");
1629 vid_initialized = true;
1631 if ((i = COM_CheckParm("-conwidth")) != 0)
1632 vid.conwidth = atoi(com_argv[i+1]);
1636 vid.conwidth &= 0xfff8; // make it a multiple of eight
1638 if (vid.conwidth < 320)
1641 // pick a conheight that matches with correct aspect
1642 vid.conheight = vid.conwidth*3 / 4;
1644 if ((i = COM_CheckParm("-conheight")) != 0)
1645 vid.conheight = atoi(com_argv[i+1]);
1646 if (vid.conheight < 200)
1647 vid.conheight = 200;
1649 DestroyWindow (hwnd_dialog);
1651 VID_SetPalette (palette);
1653 Check_Gamma(palette);
1655 VID_SetMode (vid_default, palette);
1657 maindc = GetDC(mainwindow);
1658 bSetupPixelFormat(maindc);
1660 baseRC = wglCreateContext( maindc );
1662 Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.");
1663 if (!wglMakeCurrent( maindc, baseRC ))
1664 Sys_Error ("wglMakeCurrent failed");
1668 // sprintf (gldir, "%s/glquake", com_gamedir);
1669 // Sys_mkdir (gldir);
1671 vid_realmode = vid_modenum;
1673 VID_Init8bitPalette();
1675 if (is8bit) // LordHavoc: avoid calculating 15to8 table if it won't be used
1676 VID_Setup15to8Palette ();
1678 vid_menudrawfn = VID_MenuDraw;
1679 vid_menukeyfn = VID_MenuKey;
1681 strcpy (badmode.modedesc, "Bad mode");
1682 vid_canalttab = true;
1686 //========================================================
1688 //========================================================
1690 extern void M_Menu_Options_f (void);
1691 extern void M_Print (int cx, int cy, char *str);
1692 extern void M_PrintWhite (int cx, int cy, char *str);
1693 extern void M_DrawCharacter (int cx, int line, int num);
1694 extern void M_DrawTransPic (int x, int y, qpic_t *pic);
1695 extern void M_DrawPic (int x, int y, qpic_t *pic);
1697 static int vid_line, vid_wmodes;
1706 #define MAX_COLUMN_SIZE 9
1707 #define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 2)
1708 #define MAX_MODEDESCS (MAX_COLUMN_SIZE*3)
1710 static modedesc_t modedescs[MAX_MODEDESCS];
1717 void VID_MenuDraw (void)
1721 int lnummodes, i, k, column, row;
1724 p = Draw_CachePic ("gfx/vidmodes.lmp");
1725 M_DrawPic ( (320-p->width)/2, 4, p);
1728 lnummodes = VID_NumModes ();
1730 for (i=1 ; (i<lnummodes) && (vid_wmodes < MAX_MODEDESCS) ; i++)
1732 ptr = VID_GetModeDescription (i);
1733 pv = VID_GetModePtr (i);
1737 modedescs[k].modenum = i;
1738 modedescs[k].desc = ptr;
1739 modedescs[k].iscur = 0;
1741 if (i == vid_modenum)
1742 modedescs[k].iscur = 1;
1750 M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)");
1755 for (i=0 ; i<vid_wmodes ; i++)
1757 if (modedescs[i].iscur)
1758 M_PrintWhite (column, row, modedescs[i].desc);
1760 M_Print (column, row, modedescs[i].desc);
1764 if ((i % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
1772 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*2,
1773 "Video modes must be set from the");
1774 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3,
1775 "command line with -width <width>");
1776 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4,
1777 "and -bpp <bits-per-pixel>");
1778 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6,
1779 "Select windowed mode with -window");
1788 void VID_MenuKey (int key)
1793 S_LocalSound ("misc/menu1.wav");
1794 M_Menu_Options_f ();