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 VID_HandlePause (qboolean pause)
156 void VID_ForceLockState (int lk)
160 void VID_LockBuffer (void)
164 void VID_UnlockBuffer (void)
168 int VID_ForceUnlockedAndReturnState (void)
173 void CenterWindow(HWND hWndCenter, int width, int height, BOOL lefttopjustify)
175 int CenterX, CenterY;
177 CenterX = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
178 CenterY = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
179 if (CenterX > CenterY*2)
180 CenterX >>= 1; // dual screens
181 CenterX = (CenterX < 0) ? 0: CenterX;
182 CenterY = (CenterY < 0) ? 0: CenterY;
183 SetWindowPos (hWndCenter, NULL, CenterX, CenterY, 0, 0,
184 SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
187 qboolean VID_SetWindowedMode (int modenum)
189 int lastmodestate, width, height;
192 lastmodestate = modestate;
194 WindowRect.top = WindowRect.left = 0;
196 WindowRect.right = modelist[modenum].width;
197 WindowRect.bottom = modelist[modenum].height;
199 DIBWidth = modelist[modenum].width;
200 DIBHeight = modelist[modenum].height;
202 WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU |
207 AdjustWindowRectEx(&rect, WindowStyle, FALSE, 0);
209 width = rect.right - rect.left;
210 height = rect.bottom - rect.top;
212 // Create the DIB window
213 dibwindow = CreateWindowEx (
232 Sys_Error ("Couldn't create DIB window");
234 // Center and show the DIB window
235 CenterWindow(dibwindow, WindowRect.right - WindowRect.left,
236 WindowRect.bottom - WindowRect.top, false);
238 ShowWindow (dibwindow, SW_SHOWDEFAULT);
239 UpdateWindow (dibwindow);
241 modestate = MS_WINDOWED;
243 // LordHavoc: using GDI functions on an OpenGL window? bad idea
245 // because we have set the background brush for the window to NULL
246 // (to avoid flickering when re-sizing the window on the desktop),
247 // we clear the window to black when created, otherwise it will be
248 // empty while Quake starts up.
249 hdc = GetDC(dibwindow);
250 PatBlt(hdc,0,0,WindowRect.right,WindowRect.bottom,BLACKNESS);
251 ReleaseDC(dibwindow, hdc);
254 if (vid.conheight > modelist[modenum].height)
255 vid.conheight = modelist[modenum].height;
256 if (vid.conwidth > modelist[modenum].width)
257 vid.conwidth = modelist[modenum].width;
258 vid.width = vid.conwidth;
259 vid.height = vid.conheight;
263 mainwindow = dibwindow;
265 SendMessage (mainwindow, WM_SETICON, (WPARAM)TRUE, (LPARAM)hIcon);
266 SendMessage (mainwindow, WM_SETICON, (WPARAM)FALSE, (LPARAM)hIcon);
272 qboolean VID_SetFullDIBMode (int modenum)
274 int lastmodestate, width, height;
277 if (!leavecurrentmode)
279 gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
280 gdevmode.dmBitsPerPel = modelist[modenum].bpp;
281 gdevmode.dmPelsWidth = modelist[modenum].width <<
282 modelist[modenum].halfscreen;
283 gdevmode.dmPelsHeight = modelist[modenum].height;
284 gdevmode.dmSize = sizeof (gdevmode);
286 if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
287 Sys_Error ("Couldn't set fullscreen DIB mode");
290 lastmodestate = modestate;
291 modestate = MS_FULLDIB;
293 WindowRect.top = WindowRect.left = 0;
295 WindowRect.right = modelist[modenum].width;
296 WindowRect.bottom = modelist[modenum].height;
298 DIBWidth = modelist[modenum].width;
299 DIBHeight = modelist[modenum].height;
301 WindowStyle = WS_POPUP;
305 AdjustWindowRectEx(&rect, WindowStyle, FALSE, 0);
307 width = rect.right - rect.left;
308 height = rect.bottom - rect.top;
310 // Create the DIB window
311 dibwindow = CreateWindowEx (
330 Sys_Error ("Couldn't create DIB window");
332 ShowWindow (dibwindow, SW_SHOWDEFAULT);
333 UpdateWindow (dibwindow);
335 // LordHavoc: using GDI functions on an OpenGL window? bad idea
337 // Because we have set the background brush for the window to NULL
338 // (to avoid flickering when re-sizing the window on the desktop), we
339 // clear the window to black when created, otherwise it will be
340 // empty while Quake starts up.
341 hdc = GetDC(dibwindow);
342 PatBlt(hdc,0,0,WindowRect.right,WindowRect.bottom,BLACKNESS);
343 ReleaseDC(dibwindow, hdc);
346 if (vid.conheight > modelist[modenum].height)
347 vid.conheight = modelist[modenum].height;
348 if (vid.conwidth > modelist[modenum].width)
349 vid.conwidth = modelist[modenum].width;
350 vid.width = vid.conwidth;
351 vid.height = vid.conheight;
355 // needed because we're not getting WM_MOVE messages fullscreen on NT
359 mainwindow = dibwindow;
361 SendMessage (mainwindow, WM_SETICON, (WPARAM)TRUE, (LPARAM)hIcon);
362 SendMessage (mainwindow, WM_SETICON, (WPARAM)FALSE, (LPARAM)hIcon);
368 int VID_SetMode (int modenum, unsigned char *palette)
370 int original_mode, temp;
374 if ((windowed && (modenum != 0)) ||
375 (!windowed && (modenum < 1)) ||
376 (!windowed && (modenum >= nummodes)))
378 Sys_Error ("Bad video mode\n");
381 // so Con_Printfs don't mess us up by forcing vid and snd updates
382 temp = scr_disabled_for_loading;
383 scr_disabled_for_loading = true;
387 if (vid_modenum == NO_MODE)
388 original_mode = windowed_default;
390 original_mode = vid_modenum;
392 // Set either the fullscreen or windowed mode
393 if (modelist[modenum].type == MS_WINDOWED)
395 if (_windowed_mouse.value && key_dest == key_game)
397 stat = VID_SetWindowedMode(modenum);
403 IN_DeactivateMouse ();
405 stat = VID_SetWindowedMode(modenum);
408 else if (modelist[modenum].type == MS_FULLDIB)
410 stat = VID_SetFullDIBMode(modenum);
416 Sys_Error ("VID_SetMode: Bad mode type in modelist");
419 window_width = DIBWidth;
420 window_height = DIBHeight;
421 VID_UpdateWindowStatus ();
424 scr_disabled_for_loading = temp;
428 Sys_Error ("Couldn't set video mode");
431 // now we try to make sure we get the focus on the mode switch, because
432 // sometimes in some systems we don't. We grab the foreground, then
433 // finish setting up, pump all our messages, and sleep for a little while
434 // to let messages finish bouncing around the system, then we put
435 // ourselves at the top of the z order, then grab the foreground again,
436 // Who knows if it helps, but it probably doesn't hurt
437 SetForegroundWindow (mainwindow);
438 VID_SetPalette (palette);
439 vid_modenum = modenum;
440 Cvar_SetValue ("vid_mode", (float)vid_modenum);
442 while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
444 TranslateMessage (&msg);
445 DispatchMessage (&msg);
450 SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0,
451 SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW |
454 SetForegroundWindow (mainwindow);
456 // fix the leftover Alt from any Alt-Tab or the like that switched us away
460 Con_SafePrintf ("Video mode %s initialized.\n", VID_GetModeDescription (vid_modenum));
462 VID_SetPalette (palette);
464 vid.recalc_refdef = 1;
472 VID_UpdateWindowStatus
475 void VID_UpdateWindowStatus (void)
478 window_rect.left = window_x;
479 window_rect.top = window_y;
480 window_rect.right = window_x + window_width;
481 window_rect.bottom = window_y + window_height;
482 window_center_x = (window_rect.left + window_rect.right) / 2;
483 window_center_y = (window_rect.top + window_rect.bottom) / 2;
485 IN_UpdateClipCursor ();
489 //====================================
491 void (APIENTRY *qglVertexPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
492 void (APIENTRY *qglColorPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
493 void (APIENTRY *qglTexCoordPointer)(GLint size, GLenum type, GLsizei stride, const GLvoid *ptr);
494 void (APIENTRY *qglArrayElement)(GLint i);
495 void (APIENTRY *qglDrawElements)(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
496 void (APIENTRY *qglMTexCoord2f)(GLenum, GLfloat, GLfloat);
497 void (APIENTRY *qglSelectTexture)(GLenum);
498 void (APIENTRY *glColorTableEXT)(int, int, int, int, int, const void*);
500 void CheckVertexArrays (void)
502 if (COM_CheckParm("-novertex"))
504 Con_Printf("...vertex array support disabled\n");
507 if ((qglArrayElement = (void *) wglGetProcAddress("glArrayElement"))
508 && (qglColorPointer = (void *) wglGetProcAddress("glColorPointer"))
509 // && (qglDrawArrays = (void *) wglGetProcAddress("glDrawArrays"))
510 && (qglDrawElements = (void *) wglGetProcAddress("glDrawElements"))
511 // && (qglInterleavedArrays = (void *) wglGetProcAddress("glInterleavedArrays"))
512 && (qglTexCoordPointer = (void *) wglGetProcAddress("glTexCoordPointer"))
513 && (qglVertexPointer = (void *) wglGetProcAddress("glVertexPointer"))
516 Con_Printf("...vertex array support detected\n");
521 Con_Printf("...vertex array support disabled (not detected - get a better driver)\n");
524 int texture_extension_number = 1;
526 void CheckMultiTexture(void)
528 qglMTexCoord2f = NULL;
529 qglSelectTexture = NULL;
530 // Check to see if multitexture is disabled
531 if (COM_CheckParm("-nomtex"))
533 Con_Printf("...multitexture disabled\n");
536 // Test for ARB_multitexture
537 if (!COM_CheckParm("-SGISmtex") && strstr(gl_extensions, "GL_ARB_multitexture "))
539 Con_Printf("...using GL_ARB_multitexture\n");
540 qglMTexCoord2f = (void *) wglGetProcAddress("glMultiTexCoord2fARB");
541 qglSelectTexture = (void *) wglGetProcAddress("glActiveTextureARB");
543 gl_mtex_enum = GL_TEXTURE0_ARB;
545 else if (strstr(gl_extensions, "GL_SGIS_multitexture ")) // Test for SGIS_multitexture (if ARB_multitexture not found)
547 Con_Printf("...using GL_SGIS_multitexture\n");
548 qglMTexCoord2f = (void *) wglGetProcAddress("glMTexCoord2fSGIS");
549 qglSelectTexture = (void *) wglGetProcAddress("glSelectTextureSGIS");
551 gl_mtex_enum = TEXTURE0_SGIS;
554 Con_Printf("...multitexture disabled (not detected)\n");
562 extern char *QSG_EXTENSIONS;
565 gl_vendor = glGetString (GL_VENDOR);
566 Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
567 gl_renderer = glGetString (GL_RENDERER);
568 Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
570 gl_version = glGetString (GL_VERSION);
571 Con_Printf ("GL_VERSION: %s\n", gl_version);
572 gl_extensions = glGetString (GL_EXTENSIONS);
573 Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
575 // Con_Printf ("%s %s\n", gl_renderer, gl_version);
577 if (strnicmp(gl_renderer,"Permedia",8)==0)
580 // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
581 if (strnicmp(gl_vendor,"ATI",3)==0)
584 if (strnicmp(gl_renderer,"Rage Pro",8)==0)
587 if (strnicmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
590 CheckMultiTexture ();
591 CheckVertexArrays ();
593 // LordHavoc: report supported extensions
594 Con_Printf ("\nQSG extensions: %s\n", QSG_EXTENSIONS);
596 // LordHavoc: set up state
597 // glEnable(GL_DEPTH_TEST);
598 // glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
599 // glShadeModel(GL_SMOOTH);
600 // glEnable(GL_TEXTURE_2D);
601 glAlphaFunc(GL_GREATER, 0.5);
602 // if (isRagePro || isG200)
604 // glEnable(GL_ALPHA_TEST);
605 // glAlphaFunc(GL_GREATER, 0.5);
608 // glDisable(GL_ALPHA_TEST);
610 // glDisable(GL_DITHER); // LordHavoc: disable dithering
611 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
612 // glEnable(GL_BLEND);
614 // glClearColor (0,0,0,0); // LordHavoc: changed from red to black
615 glCullFace(GL_FRONT);
617 // glAlphaFunc(GL_GREATER, 0.666);
626 void GL_BeginRendering (int *x, int *y, int *width, int *height)
628 // extern cvar_t gl_clear;
631 *width = WindowRect.right - WindowRect.left;
632 *height = WindowRect.bottom - WindowRect.top;
634 // if (!wglMakeCurrent( maindc, baseRC ))
635 // Sys_Error ("wglMakeCurrent failed");
637 // glViewport (*x, *y, *width, *height);
641 void GL_EndRendering (void)
646 // handle the mouse state when windowed if that's changed
647 if (modestate == MS_WINDOWED)
649 if (!_windowed_mouse.value)
653 IN_DeactivateMouse ();
655 windowed_mouse = false;
660 windowed_mouse = true;
661 if (key_dest == key_game && !mouseactive && ActiveApp)
666 else if (mouseactive && key_dest != key_game)
668 IN_DeactivateMouse ();
675 void VID_SetDefaultMode (void)
677 IN_DeactivateMouse ();
681 void VID_Shutdown (void)
690 vid_canalttab = false;
691 hRC = wglGetCurrentContext();
692 hDC = wglGetCurrentDC();
694 wglMakeCurrent(NULL, NULL);
696 // LordHavoc: free textures before closing (may help NVIDIA)
697 for (i = 0;i < 8192;i++) temp[i] = i;
698 glDeleteTextures(8192, temp);
701 wglDeleteContext(hRC);
703 if (hDC && dibwindow)
704 ReleaseDC(dibwindow, hDC);
706 if (modestate == MS_FULLDIB)
707 ChangeDisplaySettings (NULL, 0);
709 if (maindc && dibwindow)
710 ReleaseDC (dibwindow, maindc);
712 AppActivate(false, false);
717 //==========================================================================
720 BOOL bSetupPixelFormat(HDC hDC)
722 static PIXELFORMATDESCRIPTOR pfd = {
723 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
725 PFD_DRAW_TO_WINDOW // support window
726 | PFD_SUPPORT_OPENGL // support OpenGL
727 | PFD_DOUBLEBUFFER , // double buffered
728 PFD_TYPE_RGBA, // RGBA type
729 24, // 24-bit color depth
730 0, 0, 0, 0, 0, 0, // color bits ignored
731 0, // no alpha buffer
732 0, // shift bit ignored
733 0, // no accumulation buffer
734 0, 0, 0, 0, // accum bits ignored
735 32, // 32-bit z-buffer
736 0, // no stencil buffer
737 0, // no auxiliary buffer
738 PFD_MAIN_PLANE, // main layer
740 0, 0, 0 // layer masks ignored
744 if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
746 MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
750 if (SetPixelFormat(hDC, pixelformat, &pfd) == FALSE)
752 MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
761 byte scantokey[128] =
765 0 , 27, '1', '2', '3', '4', '5', '6',
766 '7', '8', '9', '0', '-', '=', K_BACKSPACE, 9, // 0
767 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
768 'o', 'p', '[', ']', 13 , K_CTRL,'a', 's', // 1
769 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
770 '\'' , '`', K_SHIFT,'\\', 'z', 'x', 'c', 'v', // 2
771 'b', 'n', 'm', ',', '.', '/', K_SHIFT,'*',
772 K_ALT,' ', 0 , K_F1, K_F2, K_F3, K_F4, K_F5, // 3
773 K_F6, K_F7, K_F8, K_F9, K_F10, K_PAUSE , 0 , K_HOME,
774 K_UPARROW,K_PGUP,'-',K_LEFTARROW,'5',K_RIGHTARROW,'+',K_END, //4
775 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0,0, 0, K_F11,
776 K_F12,0 , 0 , 0 , 0 , 0 , 0 , 0, // 5
777 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
778 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, // 6
779 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
780 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 7
783 byte shiftscantokey[128] =
787 0 , 27, '!', '@', '#', '$', '%', '^',
788 '&', '*', '(', ')', '_', '+', K_BACKSPACE, 9, // 0
789 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
790 'O', 'P', '{', '}', 13 , K_CTRL,'A', 'S', // 1
791 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
792 '"' , '~', K_SHIFT,'|', 'Z', 'X', 'C', 'V', // 2
793 'B', 'N', 'M', '<', '>', '?', K_SHIFT,'*',
794 K_ALT,' ', 0 , K_F1, K_F2, K_F3, K_F4, K_F5, // 3
795 K_F6, K_F7, K_F8, K_F9, K_F10, K_PAUSE , 0 , K_HOME,
796 K_UPARROW,K_PGUP,'_',K_LEFTARROW,'%',K_RIGHTARROW,'+',K_END, //4
797 K_DOWNARROW,K_PGDN,K_INS,K_DEL,0,0, 0, K_F11,
798 K_F12,0 , 0 , 0 , 0 , 0 , 0 , 0, // 5
799 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
800 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, // 6
801 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
802 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 7
810 Map from windows to quake keynums
818 if (scantokey[key] == 0)
819 Con_DPrintf("key 0x%02x has no translation\n", key);
820 return scantokey[key];
824 ===================================================================
828 ===================================================================
836 void ClearAllStates (void)
840 // send an up event for each key, to make sure the server clears them all
841 for (i=0 ; i<256 ; i++)
843 Key_Event (i, false);
850 void AppActivate(BOOL fActive, BOOL minimize)
851 /****************************************************************************
853 * Function: AppActivate
854 * Parameters: fActive - True if app is activating
856 * Description: If the application is activating, then swap the system
857 * into SYSPAL_NOSTATIC mode so that our palettes will display
860 ****************************************************************************/
862 static BOOL sound_active;
865 Minimized = minimize;
867 // enable/disable sound on focus gain/loss
868 if (!ActiveApp && sound_active)
871 sound_active = false;
873 else if (ActiveApp && !sound_active)
881 if (modestate == MS_FULLDIB)
885 if (vid_canalttab && vid_wassuspended) {
886 vid_wassuspended = false;
887 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
888 ShowWindow(mainwindow, SW_SHOWNORMAL);
891 else if ((modestate == MS_WINDOWED) && _windowed_mouse.value && key_dest == key_game)
900 if (modestate == MS_FULLDIB)
902 IN_DeactivateMouse ();
905 ChangeDisplaySettings (NULL, 0);
906 vid_wassuspended = true;
909 else if ((modestate == MS_WINDOWED) && _windowed_mouse.value)
911 IN_DeactivateMouse ();
917 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
919 /* main window procedure */
920 LONG WINAPI MainWndProc (
927 int fActive, fMinimized, temp;
928 extern unsigned int uiWheelMessage;
930 if ( uMsg == uiWheelMessage )
931 uMsg = WM_MOUSEWHEEL;
936 if (modestate == MS_FULLDIB)
937 ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
944 window_x = (int) LOWORD(lParam);
945 window_y = (int) HIWORD(lParam);
946 VID_UpdateWindowStatus ();
951 Key_Event (MapKey(lParam), true);
956 Key_Event (MapKey(lParam), false);
960 // keep Alt-Space from happening
963 // this is complicated because Win32 seems to pack multiple mouse events into
964 // one update sometimes, so we always check all states and look for events
974 if (wParam & MK_LBUTTON)
977 if (wParam & MK_RBUTTON)
980 if (wParam & MK_MBUTTON)
983 IN_MouseEvent (temp);
987 // JACK: This is the mouse wheel with the Intellimouse
988 // Its delta is either positive or neg, and we generate the proper
991 if ((short) HIWORD(wParam) > 0) {
992 Key_Event(K_MWHEELUP, true);
993 Key_Event(K_MWHEELUP, false);
995 Key_Event(K_MWHEELDOWN, true);
996 Key_Event(K_MWHEELDOWN, false);
1004 if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit",
1005 MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
1013 fActive = LOWORD(wParam);
1014 fMinimized = (BOOL) HIWORD(wParam);
1015 AppActivate(!(fActive == WA_INACTIVE), fMinimized);
1017 // fix the leftover Alt from any Alt-Tab or the like that switched us away
1025 DestroyWindow (dibwindow);
1027 PostQuitMessage (0);
1032 lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
1036 /* pass all unhandled messages to DefWindowProc */
1037 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
1041 /* return 1 if handled message, 0 if not */
1051 int VID_NumModes (void)
1062 vmode_t *VID_GetModePtr (int modenum)
1065 if ((modenum >= 0) && (modenum < nummodes))
1066 return &modelist[modenum];
1074 VID_GetModeDescription
1077 char *VID_GetModeDescription (int mode)
1081 static char temp[100];
1083 if ((mode < 0) || (mode >= nummodes))
1086 if (!leavecurrentmode)
1088 pv = VID_GetModePtr (mode);
1089 pinfo = pv->modedesc;
1093 sprintf (temp, "Desktop resolution (%dx%d)",
1094 modelist[MODE_FULLSCREEN_DEFAULT].width,
1095 modelist[MODE_FULLSCREEN_DEFAULT].height);
1103 // KJB: Added this to return the mode driver name in description for console
1105 char *VID_GetExtModeDescription (int mode)
1107 static char pinfo[40];
1110 if ((mode < 0) || (mode >= nummodes))
1113 pv = VID_GetModePtr (mode);
1114 if (modelist[mode].type == MS_FULLDIB)
1116 if (!leavecurrentmode)
1118 sprintf(pinfo,"%s fullscreen", pv->modedesc);
1122 sprintf (pinfo, "Desktop resolution (%dx%d)",
1123 modelist[MODE_FULLSCREEN_DEFAULT].width,
1124 modelist[MODE_FULLSCREEN_DEFAULT].height);
1129 if (modestate == MS_WINDOWED)
1130 sprintf(pinfo, "%s windowed", pv->modedesc);
1132 sprintf(pinfo, "windowed");
1141 VID_DescribeCurrentMode_f
1144 void VID_DescribeCurrentMode_f (void)
1146 Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum));
1155 void VID_NumModes_f (void)
1159 Con_Printf ("%d video mode is available\n", nummodes);
1161 Con_Printf ("%d video modes are available\n", nummodes);
1170 void VID_DescribeMode_f (void)
1174 modenum = atoi (Cmd_Argv(1));
1176 t = leavecurrentmode;
1177 leavecurrentmode = 0;
1179 Con_Printf ("%s\n", VID_GetExtModeDescription (modenum));
1181 leavecurrentmode = t;
1190 void VID_DescribeModes_f (void)
1192 int i, lnummodes, t;
1196 lnummodes = VID_NumModes ();
1198 t = leavecurrentmode;
1199 leavecurrentmode = 0;
1201 for (i=1 ; i<lnummodes ; i++)
1203 pv = VID_GetModePtr (i);
1204 pinfo = VID_GetExtModeDescription (i);
1205 Con_Printf ("%2d: %s\n", i, pinfo);
1208 leavecurrentmode = t;
1212 void VID_InitDIB (HINSTANCE hInstance)
1216 /* Register the frame class */
1218 wc.lpfnWndProc = (WNDPROC)MainWndProc;
1221 wc.hInstance = hInstance;
1223 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
1224 wc.hbrBackground = NULL;
1225 wc.lpszMenuName = 0;
1227 wc.lpszClassName = "DPNehahraQuake";
1229 wc.lpszClassName = "DarkPlacesQuake";
1232 if (!RegisterClass (&wc) )
1233 Sys_Error ("Couldn't register window class");
1235 modelist[0].type = MS_WINDOWED;
1237 if (COM_CheckParm("-width"))
1238 modelist[0].width = atoi(com_argv[COM_CheckParm("-width")+1]);
1240 modelist[0].width = 640;
1242 if (modelist[0].width < 320)
1243 modelist[0].width = 320;
1245 if (COM_CheckParm("-height"))
1246 modelist[0].height= atoi(com_argv[COM_CheckParm("-height")+1]);
1248 modelist[0].height = modelist[0].width * 240/320;
1250 if (modelist[0].height < 240)
1251 modelist[0].height = 240;
1253 sprintf (modelist[0].modedesc, "%dx%d",
1254 modelist[0].width, modelist[0].height);
1256 modelist[0].modenum = MODE_WINDOWED;
1257 modelist[0].dib = 1;
1258 modelist[0].fullscreen = 0;
1259 modelist[0].halfscreen = 0;
1260 modelist[0].bpp = 0;
1271 void VID_InitFullDIB (HINSTANCE hInstance)
1274 int i, modenum, originalnummodes, existingmode, numlowresmodes;
1278 // enumerate >8 bpp modes
1279 originalnummodes = nummodes;
1284 stat = EnumDisplaySettings (NULL, modenum, &devmode);
1286 if ((devmode.dmBitsPerPel >= 15) &&
1287 (devmode.dmPelsWidth <= MAXWIDTH) &&
1288 (devmode.dmPelsHeight <= MAXHEIGHT) &&
1289 (nummodes < MAX_MODE_LIST))
1291 devmode.dmFields = DM_BITSPERPEL |
1295 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) ==
1296 DISP_CHANGE_SUCCESSFUL)
1298 modelist[nummodes].type = MS_FULLDIB;
1299 modelist[nummodes].width = devmode.dmPelsWidth;
1300 modelist[nummodes].height = devmode.dmPelsHeight;
1301 modelist[nummodes].modenum = 0;
1302 modelist[nummodes].halfscreen = 0;
1303 modelist[nummodes].dib = 1;
1304 modelist[nummodes].fullscreen = 1;
1305 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1306 sprintf (modelist[nummodes].modedesc, "%dx%dx%d",
1307 devmode.dmPelsWidth, devmode.dmPelsHeight,
1308 devmode.dmBitsPerPel);
1310 // if the width is more than twice the height, reduce it by half because this
1311 // is probably a dual-screen monitor
1312 if (!COM_CheckParm("-noadjustaspect"))
1314 if (modelist[nummodes].width > (modelist[nummodes].height << 1))
1316 modelist[nummodes].width >>= 1;
1317 modelist[nummodes].halfscreen = 1;
1318 sprintf (modelist[nummodes].modedesc, "%dx%dx%d",
1319 modelist[nummodes].width,
1320 modelist[nummodes].height,
1321 modelist[nummodes].bpp);
1325 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1327 if ((modelist[nummodes].width == modelist[i].width) &&
1328 (modelist[nummodes].height == modelist[i].height) &&
1329 (modelist[nummodes].bpp == modelist[i].bpp))
1346 // see if there are any low-res modes that aren't being reported
1347 numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]);
1353 for (j=0 ; (j<numlowresmodes) && (nummodes < MAX_MODE_LIST) ; j++)
1355 devmode.dmBitsPerPel = bpp;
1356 devmode.dmPelsWidth = lowresmodes[j].width;
1357 devmode.dmPelsHeight = lowresmodes[j].height;
1358 devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1360 if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) ==
1361 DISP_CHANGE_SUCCESSFUL)
1363 modelist[nummodes].type = MS_FULLDIB;
1364 modelist[nummodes].width = devmode.dmPelsWidth;
1365 modelist[nummodes].height = devmode.dmPelsHeight;
1366 modelist[nummodes].modenum = 0;
1367 modelist[nummodes].halfscreen = 0;
1368 modelist[nummodes].dib = 1;
1369 modelist[nummodes].fullscreen = 1;
1370 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1371 sprintf (modelist[nummodes].modedesc, "%dx%dx%d",
1372 devmode.dmPelsWidth, devmode.dmPelsHeight,
1373 devmode.dmBitsPerPel);
1375 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1377 if ((modelist[nummodes].width == modelist[i].width) &&
1378 (modelist[nummodes].height == modelist[i].height) &&
1379 (modelist[nummodes].bpp == modelist[i].bpp))
1408 if (nummodes == originalnummodes)
1409 Con_SafePrintf ("No fullscreen DIB modes found\n");
1412 qboolean VID_Is8bit()
1417 #define GL_SHARED_TEXTURE_PALETTE_EXT 0x81FB
1419 void VID_Init8bitPalette()
1421 // Check for 8bit Extensions and initialize them.
1423 char thePalette[256*3];
1424 char *oldPalette, *newPalette;
1425 // LordHavoc: 8bit texture support broke many things... it now must be specifically stated on the commandline (-no8bit became -8bit)
1426 if (!COM_CheckParm("-8bit"))
1428 if (strstr(gl_extensions, "GL_EXT_shared_texture_palette"))
1430 if (!(glColorTableEXT = (void *)wglGetProcAddress("glColorTableEXT")))
1433 Con_SafePrintf("8-bit GL extensions enabled.\n");
1434 glEnable( GL_SHARED_TEXTURE_PALETTE_EXT );
1435 oldPalette = (char *) d_8to24table;
1436 newPalette = thePalette;
1439 *newPalette++ = *oldPalette++;
1440 *newPalette++ = *oldPalette++;
1441 *newPalette++ = *oldPalette++;
1444 glColorTableEXT(GL_SHARED_TEXTURE_PALETTE_EXT, GL_RGB, 256, GL_RGB, GL_UNSIGNED_BYTE, (void *) thePalette);
1448 extern void Check_Gamma (unsigned char *pal);
1449 void VID_Setup15to8Palette ();
1456 void VID_Init (unsigned char *palette)
1458 int i, existingmode;
1459 int basenummodes, width, height, bpp, findbpp, done;
1460 // char gldir[MAX_OSPATH];
1464 memset(&devmode, 0, sizeof(devmode));
1466 Cvar_RegisterVariable (&vid_mode);
1467 Cvar_RegisterVariable (&vid_wait);
1468 Cvar_RegisterVariable (&vid_nopageflip);
1469 Cvar_RegisterVariable (&_vid_wait_override);
1470 Cvar_RegisterVariable (&_vid_default_mode);
1471 Cvar_RegisterVariable (&_vid_default_mode_win);
1472 Cvar_RegisterVariable (&vid_config_x);
1473 Cvar_RegisterVariable (&vid_config_y);
1474 Cvar_RegisterVariable (&vid_stretch_by_2);
1475 Cvar_RegisterVariable (&_windowed_mouse);
1477 Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);
1478 Cmd_AddCommand ("vid_describecurrentmode", VID_DescribeCurrentMode_f);
1479 Cmd_AddCommand ("vid_describemode", VID_DescribeMode_f);
1480 Cmd_AddCommand ("vid_describemodes", VID_DescribeModes_f);
1482 hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON2));
1484 InitCommonControls();
1486 VID_InitDIB (global_hInstance);
1487 basenummodes = nummodes = 1;
1489 VID_InitFullDIB (global_hInstance);
1491 if (COM_CheckParm("-window"))
1495 if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
1497 Sys_Error ("Can't run in non-RGB mode");
1500 ReleaseDC (NULL, hdc);
1504 vid_default = MODE_WINDOWED;
1509 Sys_Error ("No RGB fullscreen modes available");
1513 if (COM_CheckParm("-mode"))
1515 vid_default = atoi(com_argv[COM_CheckParm("-mode")+1]);
1519 if (COM_CheckParm("-current"))
1521 modelist[MODE_FULLSCREEN_DEFAULT].width =
1522 GetSystemMetrics (SM_CXSCREEN);
1523 modelist[MODE_FULLSCREEN_DEFAULT].height =
1524 GetSystemMetrics (SM_CYSCREEN);
1525 vid_default = MODE_FULLSCREEN_DEFAULT;
1526 leavecurrentmode = 1;
1530 if (COM_CheckParm("-width"))
1532 width = atoi(com_argv[COM_CheckParm("-width")+1]);
1539 if (COM_CheckParm("-bpp"))
1541 bpp = atoi(com_argv[COM_CheckParm("-bpp")+1]);
1550 if (COM_CheckParm("-height"))
1551 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1553 // if they want to force it, add the specified mode to the list
1554 if (COM_CheckParm("-force") && (nummodes < MAX_MODE_LIST))
1556 modelist[nummodes].type = MS_FULLDIB;
1557 modelist[nummodes].width = width;
1558 modelist[nummodes].height = height;
1559 modelist[nummodes].modenum = 0;
1560 modelist[nummodes].halfscreen = 0;
1561 modelist[nummodes].dib = 1;
1562 modelist[nummodes].fullscreen = 1;
1563 modelist[nummodes].bpp = bpp;
1564 sprintf (modelist[nummodes].modedesc, "%dx%dx%d",
1565 devmode.dmPelsWidth, devmode.dmPelsHeight,
1566 devmode.dmBitsPerPel);
1568 for (i=nummodes, existingmode = 0 ; i<nummodes ; i++)
1570 if ((modelist[nummodes].width == modelist[i].width) &&
1571 (modelist[nummodes].height == modelist[i].height) &&
1572 (modelist[nummodes].bpp == modelist[i].bpp))
1589 if (COM_CheckParm("-height"))
1591 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1593 for (i=1, vid_default=0 ; i<nummodes ; i++)
1595 if ((modelist[i].width == width) &&
1596 (modelist[i].height == height) &&
1597 (modelist[i].bpp == bpp))
1607 for (i=1, vid_default=0 ; i<nummodes ; i++)
1609 if ((modelist[i].width == width) && (modelist[i].bpp == bpp))
1648 Sys_Error ("Specified video mode not available");
1654 vid_initialized = true;
1656 if ((i = COM_CheckParm("-conwidth")) != 0)
1657 vid.conwidth = atoi(com_argv[i+1]);
1661 vid.conwidth &= 0xfff8; // make it a multiple of eight
1663 if (vid.conwidth < 320)
1666 // pick a conheight that matches with correct aspect
1667 vid.conheight = vid.conwidth*3 / 4;
1669 if ((i = COM_CheckParm("-conheight")) != 0)
1670 vid.conheight = atoi(com_argv[i+1]);
1671 if (vid.conheight < 200)
1672 vid.conheight = 200;
1674 vid.maxwarpwidth = WARP_WIDTH;
1675 vid.maxwarpheight = WARP_HEIGHT;
1676 vid.colormap = host_colormap;
1677 vid.fullbright = 256 - LittleLong (*((int *)vid.colormap + 2048));
1679 DestroyWindow (hwnd_dialog);
1681 VID_SetPalette (palette);
1683 Check_Gamma(palette);
1685 VID_SetMode (vid_default, palette);
1687 maindc = GetDC(mainwindow);
1688 bSetupPixelFormat(maindc);
1690 baseRC = wglCreateContext( maindc );
1692 Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.");
1693 if (!wglMakeCurrent( maindc, baseRC ))
1694 Sys_Error ("wglMakeCurrent failed");
1698 // sprintf (gldir, "%s/glquake", com_gamedir);
1699 // Sys_mkdir (gldir);
1701 vid_realmode = vid_modenum;
1703 VID_Init8bitPalette();
1705 if (is8bit) // LordHavoc: avoid calculating 15to8 table if it won't be used
1706 VID_Setup15to8Palette ();
1708 vid_menudrawfn = VID_MenuDraw;
1709 vid_menukeyfn = VID_MenuKey;
1711 strcpy (badmode.modedesc, "Bad mode");
1712 vid_canalttab = true;
1716 //========================================================
1718 //========================================================
1720 extern void M_Menu_Options_f (void);
1721 extern void M_Print (int cx, int cy, char *str);
1722 extern void M_PrintWhite (int cx, int cy, char *str);
1723 extern void M_DrawCharacter (int cx, int line, int num);
1724 extern void M_DrawTransPic (int x, int y, qpic_t *pic);
1725 extern void M_DrawPic (int x, int y, qpic_t *pic);
1727 static int vid_line, vid_wmodes;
1736 #define MAX_COLUMN_SIZE 9
1737 #define MODE_AREA_HEIGHT (MAX_COLUMN_SIZE + 2)
1738 #define MAX_MODEDESCS (MAX_COLUMN_SIZE*3)
1740 static modedesc_t modedescs[MAX_MODEDESCS];
1747 void VID_MenuDraw (void)
1751 int lnummodes, i, k, column, row;
1754 p = Draw_CachePic ("gfx/vidmodes.lmp");
1755 M_DrawPic ( (320-p->width)/2, 4, p);
1758 lnummodes = VID_NumModes ();
1760 for (i=1 ; (i<lnummodes) && (vid_wmodes < MAX_MODEDESCS) ; i++)
1762 ptr = VID_GetModeDescription (i);
1763 pv = VID_GetModePtr (i);
1767 modedescs[k].modenum = i;
1768 modedescs[k].desc = ptr;
1769 modedescs[k].iscur = 0;
1771 if (i == vid_modenum)
1772 modedescs[k].iscur = 1;
1780 M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)");
1785 for (i=0 ; i<vid_wmodes ; i++)
1787 if (modedescs[i].iscur)
1788 M_PrintWhite (column, row, modedescs[i].desc);
1790 M_Print (column, row, modedescs[i].desc);
1794 if ((i % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
1802 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*2,
1803 "Video modes must be set from the");
1804 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3,
1805 "command line with -width <width>");
1806 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4,
1807 "and -bpp <bits-per-pixel>");
1808 M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6,
1809 "Select windowed mode with -window");
1818 void VID_MenuKey (int key)
1823 S_LocalSound ("misc/menu1.wav");
1824 M_Menu_Options_f ();