]> icculus.org git repositories - divverent/darkplaces.git/blob - vid_wgl.c
removed (experimental, and broken) particles with polygonal explosion (after some...
[divverent/darkplaces.git] / vid_wgl.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.  
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // gl_vidnt.c -- NT GL vid component
21
22 #include "quakedef.h"
23 #include "winquake.h"
24 #include "resource.h"
25 #include <commctrl.h>
26
27 #define MAX_MODE_LIST   30
28 #define VID_ROW_SIZE    3
29 #define MAXWIDTH                10000
30 #define MAXHEIGHT               10000
31
32 #define MODE_WINDOWED                   0
33 #define NO_MODE                                 (MODE_WINDOWED - 1)
34 #define MODE_FULLSCREEN_DEFAULT (MODE_WINDOWED + 1)
35
36 typedef struct {
37         modestate_t     type;
38         int                     width;
39         int                     height;
40         int                     modenum;
41         int                     dib;
42         int                     fullscreen;
43         int                     bpp;
44         int                     halfscreen;
45         char            modedesc[17];
46 } vmode_t;
47
48 typedef struct {
49         int                     width;
50         int                     height;
51 } lmode_t;
52
53 lmode_t lowresmodes[] = {
54         {320, 200},
55         {320, 240},
56         {400, 300},
57         {512, 384},
58 };
59
60 const char *gl_vendor;
61 const char *gl_renderer;
62 const char *gl_version;
63 const char *gl_extensions;
64
65 qboolean                scr_skipupdate;
66
67 static vmode_t  modelist[MAX_MODE_LIST];
68 static int              nummodes;
69 static vmode_t  *pcurrentmode;
70 static vmode_t  badmode;
71
72 static DEVMODE  gdevmode;
73 static qboolean vid_initialized = false;
74 static qboolean windowed, leavecurrentmode;
75 static qboolean vid_canalttab = false;
76 static qboolean vid_wassuspended = false;
77 static int              usingmouse;
78 extern qboolean mouseactive;  // from in_win.c
79 static HICON    hIcon;
80
81 int                     DIBWidth, DIBHeight;
82 RECT            WindowRect;
83 DWORD           WindowStyle, ExWindowStyle;
84
85 HWND    mainwindow;
86
87 int                     vid_modenum = NO_MODE;
88 int                     vid_realmode;
89 int                     vid_default = MODE_WINDOWED;
90 static int      windowed_default;
91 unsigned char   vid_curpal[256*3];
92
93 HGLRC   baseRC;
94 HDC             maindc;
95
96 HWND WINAPI InitializeWindow (HINSTANCE hInstance, int nCmdShow);
97
98 viddef_t        vid;                            // global video state
99
100 float           gldepthmin, gldepthmax;
101
102 modestate_t     modestate = MS_UNINIT;
103
104 void VID_MenuDraw (void);
105 void VID_MenuKey (int key);
106
107 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
108 void AppActivate(BOOL fActive, BOOL minimize);
109 char *VID_GetModeDescription (int mode);
110 void ClearAllStates (void);
111 void VID_UpdateWindowStatus (void);
112
113 //====================================
114
115 // Note that 0 is MODE_WINDOWED
116 //cvar_t                _vid_default_mode = {"_vid_default_mode","0", true};
117 // Note that 3 is MODE_FULLSCREEN_DEFAULT
118 //cvar_t                _vid_default_mode_win = {"_vid_default_mode_win","3", true};
119
120 int                     window_center_x, window_center_y, window_x, window_y, window_width, window_height;
121 RECT            window_rect;
122
123 // direct draw software compatability stuff
124
125 void CenterWindow(HWND hWndCenter, int width, int height, BOOL lefttopjustify)
126 {
127     int     CenterX, CenterY;
128
129         CenterX = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
130         CenterY = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
131         if (CenterX > CenterY*2)
132                 CenterX >>= 1;  // dual screens
133         CenterX = (CenterX < 0) ? 0: CenterX;
134         CenterY = (CenterY < 0) ? 0: CenterY;
135         SetWindowPos (hWndCenter, NULL, CenterX, CenterY, 0, 0,
136                         SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW | SWP_DRAWFRAME);
137 }
138
139 qboolean VID_SetWindowedMode (int modenum)
140 {
141         int                             lastmodestate, width, height;
142         RECT                    rect;
143
144         lastmodestate = modestate;
145
146         WindowRect.top = WindowRect.left = 0;
147
148         WindowRect.right = modelist[modenum].width;
149         WindowRect.bottom = modelist[modenum].height;
150
151         DIBWidth = modelist[modenum].width;
152         DIBHeight = modelist[modenum].height;
153
154         WindowStyle = WS_OVERLAPPED | WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
155         ExWindowStyle = 0;
156
157         rect = WindowRect;
158         AdjustWindowRectEx(&rect, WindowStyle, false, 0);
159
160         width = rect.right - rect.left;
161         height = rect.bottom - rect.top;
162
163         // Create the DIB window
164         mainwindow = CreateWindowEx (ExWindowStyle, gamename, gamename, WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
165
166         if (!mainwindow)
167                 Sys_Error ("Couldn't create DIB window");
168
169         // Center and show the DIB window
170         CenterWindow(mainwindow, WindowRect.right - WindowRect.left, WindowRect.bottom - WindowRect.top, false);
171
172         ShowWindow (mainwindow, SW_SHOWDEFAULT);
173         UpdateWindow (mainwindow);
174
175         modestate = MS_WINDOWED;
176
177         if (vid.conheight > modelist[modenum].height)
178                 vid.conheight = modelist[modenum].height;
179         if (vid.conwidth > modelist[modenum].width)
180                 vid.conwidth = modelist[modenum].width;
181
182         SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
183         SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
184
185         return true;
186 }
187
188
189 qboolean VID_SetFullDIBMode (int modenum)
190 {
191         int                             lastmodestate, width, height;
192         RECT                    rect;
193
194         if (!leavecurrentmode)
195         {
196                 gdevmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
197                 gdevmode.dmBitsPerPel = modelist[modenum].bpp;
198                 gdevmode.dmPelsWidth = modelist[modenum].width <<
199                                                            modelist[modenum].halfscreen;
200                 gdevmode.dmPelsHeight = modelist[modenum].height;
201                 gdevmode.dmSize = sizeof (gdevmode);
202
203                 if (ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
204                         Sys_Error ("Couldn't set fullscreen DIB mode");
205         }
206
207         lastmodestate = modestate;
208         modestate = MS_FULLDIB;
209
210         WindowRect.top = WindowRect.left = 0;
211
212         WindowRect.right = modelist[modenum].width;
213         WindowRect.bottom = modelist[modenum].height;
214
215         DIBWidth = modelist[modenum].width;
216         DIBHeight = modelist[modenum].height;
217
218         WindowStyle = WS_POPUP;
219         ExWindowStyle = 0;
220
221         rect = WindowRect;
222         AdjustWindowRectEx(&rect, WindowStyle, false, 0);
223
224         width = rect.right - rect.left;
225         height = rect.bottom - rect.top;
226
227         // Create the DIB window
228         mainwindow = CreateWindowEx (ExWindowStyle, gamename, gamename, WindowStyle, rect.left, rect.top, width, height, NULL, NULL, global_hInstance, NULL);
229
230         if (!mainwindow)
231                 Sys_Error ("Couldn't create DIB window");
232
233         ShowWindow (mainwindow, SW_SHOWDEFAULT);
234         UpdateWindow (mainwindow);
235
236         if (vid.conheight > modelist[modenum].height)
237                 vid.conheight = modelist[modenum].height;
238         if (vid.conwidth > modelist[modenum].width)
239                 vid.conwidth = modelist[modenum].width;
240
241 // needed because we're not getting WM_MOVE messages fullscreen on NT
242         window_x = 0;
243         window_y = 0;
244
245         SendMessage (mainwindow, WM_SETICON, (WPARAM)true, (LPARAM)hIcon);
246         SendMessage (mainwindow, WM_SETICON, (WPARAM)false, (LPARAM)hIcon);
247
248         return true;
249 }
250
251
252 int VID_SetMode (int modenum)
253 {
254         int                             original_mode, temp;
255         qboolean                stat;
256     MSG                         msg;
257
258         if ((windowed && (modenum != 0)) || (!windowed && (modenum < 1)) || (!windowed && (modenum >= nummodes)))
259                 Sys_Error ("Bad video mode\n");
260
261 // so Con_Printfs don't mess us up by forcing vid and snd updates
262         temp = scr_disabled_for_loading;
263         scr_disabled_for_loading = true;
264
265         CDAudio_Pause ();
266
267         if (vid_modenum == NO_MODE)
268                 original_mode = windowed_default;
269         else
270                 original_mode = vid_modenum;
271
272         // Set either the fullscreen or windowed mode
273         if (modelist[modenum].type == MS_WINDOWED)
274         {
275 //              if (vid_mouse.integer && key_dest == key_game)
276 //              {
277 //                      stat = VID_SetWindowedMode(modenum);
278 //                      usingmouse = true;
279 //                      IN_ActivateMouse ();
280 //                      IN_HideMouse ();
281 //              }
282 //              else
283 //              {
284 //                      usingmouse = false;
285 //                      IN_DeactivateMouse ();
286 //                      IN_ShowMouse ();
287 //                      stat = VID_SetWindowedMode(modenum);
288 //              }
289                 stat = VID_SetWindowedMode(modenum);
290         }
291         else if (modelist[modenum].type == MS_FULLDIB)
292         {
293                 stat = VID_SetFullDIBMode(modenum);
294 //              usingmouse = true;
295 //              IN_ActivateMouse ();
296 //              IN_HideMouse ();
297         }
298         else
299                 Sys_Error ("VID_SetMode: Bad mode type in modelist");
300
301         window_width = DIBWidth;
302         window_height = DIBHeight;
303         VID_UpdateWindowStatus ();
304
305         CDAudio_Resume ();
306         scr_disabled_for_loading = temp;
307
308         if (!stat)
309                 Sys_Error ("Couldn't set video mode");
310
311 // now we try to make sure we get the focus on the mode switch, because
312 // sometimes in some systems we don't.  We grab the foreground, then
313 // finish setting up, pump all our messages, and sleep for a little while
314 // to let messages finish bouncing around the system, then we put
315 // ourselves at the top of the z order, then grab the foreground again,
316 // Who knows if it helps, but it probably doesn't hurt
317         SetForegroundWindow (mainwindow);
318         vid_modenum = modenum;
319         Cvar_SetValue ("vid_mode", (float)vid_modenum);
320
321         while (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
322         {
323         TranslateMessage (&msg);
324         DispatchMessage (&msg);
325         }
326
327         Sleep (100);
328
329         SetWindowPos (mainwindow, HWND_TOP, 0, 0, 0, 0, SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOCOPYBITS);
330
331         SetForegroundWindow (mainwindow);
332
333 // fix the leftover Alt from any Alt-Tab or the like that switched us away
334         ClearAllStates ();
335
336         if (!msg_suppress_1)
337                 Con_SafePrintf ("Video mode %s initialized.\n", VID_GetModeDescription (vid_modenum));
338
339 //      vid.recalc_refdef = 1;
340
341         return true;
342 }
343
344
345 /*
346 ================
347 VID_UpdateWindowStatus
348 ================
349 */
350 void VID_UpdateWindowStatus (void)
351 {
352
353         window_rect.left = window_x;
354         window_rect.top = window_y;
355         window_rect.right = window_x + window_width;
356         window_rect.bottom = window_y + window_height;
357         window_center_x = (window_rect.left + window_rect.right) / 2;
358         window_center_y = (window_rect.top + window_rect.bottom) / 2;
359
360         IN_UpdateClipCursor ();
361 }
362
363
364 //====================================
365
366 /*
367 =================
368 GL_BeginRendering
369
370 =================
371 */
372 void GL_BeginRendering (int *x, int *y, int *width, int *height)
373 {
374         *x = *y = 0;
375         *width = WindowRect.right - WindowRect.left;
376         *height = WindowRect.bottom - WindowRect.top;
377
378 //      if (!wglMakeCurrent( maindc, baseRC ))
379 //              Sys_Error ("wglMakeCurrent failed");
380
381 //      glViewport (*x, *y, *width, *height);
382 }
383
384
385 void GL_EndRendering (void)
386 {
387         int usemouse;
388         if (r_render.integer && !scr_skipupdate)
389                 SwapBuffers(maindc);
390
391 // handle the mouse state when windowed if that's changed
392         usemouse = false;
393         if (vid_mouse.integer && key_dest == key_game)
394                 usemouse = true;
395         if (modestate == MS_FULLDIB)
396                 usemouse = true;
397         if (!ActiveApp)
398                 usemouse = false;
399         if (usemouse)
400         {
401                 if (!usingmouse)
402                 {
403                         usingmouse = true;
404                         IN_ActivateMouse ();
405                         IN_HideMouse();
406                 }
407         }
408         else
409         {
410                 if (usingmouse)
411                 {
412                         usingmouse = false;
413                         IN_DeactivateMouse ();
414                         IN_ShowMouse();
415                 }
416         }
417 }
418
419 void VID_SetDefaultMode (void)
420 {
421         IN_DeactivateMouse ();
422 }
423
424 void VID_RestoreSystemGamma(void);
425
426 void    VID_Shutdown (void)
427 {
428         HGLRC hRC;
429         HDC       hDC;
430         int i;
431         GLuint temp[8192];
432
433         if (vid_initialized)
434         {
435                 vid_canalttab = false;
436                 hRC = wglGetCurrentContext();
437         hDC = wglGetCurrentDC();
438
439         wglMakeCurrent(NULL, NULL);
440
441                 // LordHavoc: free textures before closing (may help NVIDIA)
442                 for (i = 0;i < 8192;i++)
443                         temp[i] = i+1;
444                 glDeleteTextures(8192, temp);
445
446         if (hRC)
447             wglDeleteContext(hRC);
448
449                 if (hDC && mainwindow)
450                         ReleaseDC(mainwindow, hDC);
451
452                 if (modestate == MS_FULLDIB)
453                         ChangeDisplaySettings (NULL, 0);
454
455                 if (maindc && mainwindow)
456                         ReleaseDC (mainwindow, maindc);
457
458                 AppActivate(false, false);
459
460                 VID_RestoreSystemGamma();
461         }
462 }
463
464
465 //==========================================================================
466
467
468 BOOL bSetupPixelFormat(HDC hDC)
469 {
470     static PIXELFORMATDESCRIPTOR pfd = {
471         sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
472         1,                              // version number
473         PFD_DRAW_TO_WINDOW              // support window
474         |  PFD_SUPPORT_OPENGL   // support OpenGL
475         |  PFD_DOUBLEBUFFER ,   // double buffered
476         PFD_TYPE_RGBA,                  // RGBA type
477         24,                             // 24-bit color depth
478         0, 0, 0, 0, 0, 0,               // color bits ignored
479         0,                              // no alpha buffer
480         0,                              // shift bit ignored
481         0,                              // no accumulation buffer
482         0, 0, 0, 0,                     // accum bits ignored
483         32,                             // 32-bit z-buffer      
484         0,                              // no stencil buffer
485         0,                              // no auxiliary buffer
486         PFD_MAIN_PLANE,                 // main layer
487         0,                              // reserved
488         0, 0, 0                         // layer masks ignored
489     };
490     int pixelformat;
491
492     if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
493     {
494         MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
495         return false;
496     }
497
498     if (SetPixelFormat(hDC, pixelformat, &pfd) == false)
499     {
500         MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
501         return false;
502     }
503
504     return true;
505 }
506
507
508
509 byte scantokey[128] =
510 {
511 //      0           1      2     3     4     5       6       7      8         9      A       B           C     D            E           F
512         0          ,27    ,'1'  ,'2'  ,'3'  ,'4'    ,'5'    ,'6'   ,'7'      ,'8'   ,'9'    ,'0'        ,'-'  ,'='         ,K_BACKSPACE,9     , // 0
513         'q'        ,'w'   ,'e'  ,'r'  ,'t'  ,'y'    ,'u'    ,'i'   ,'o'      ,'p'   ,'['    ,']'        ,13   ,K_CTRL      ,'a'        ,'s'   , // 1
514         'd'        ,'f'   ,'g'  ,'h'  ,'j'  ,'k'    ,'l'    ,';'   ,'\''     ,'`'   ,K_SHIFT,'\\'       ,'z'  ,'x'         ,'c'        ,'v'   , // 2
515         'b'        ,'n'   ,'m'  ,','  ,'.'  ,'/'    ,K_SHIFT,'*'   ,K_ALT    ,' '   ,0      ,K_F1       ,K_F2 ,K_F3        ,K_F4       ,K_F5  , // 3
516         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
517         K_DOWNARROW,K_PGDN,K_INS,K_DEL,0    ,0      ,0      ,K_F11 ,K_F12    ,0     ,0      ,0          ,0    ,0           ,0          ,0     , // 5
518         0          ,0     ,0    ,0    ,0    ,0      ,0      ,0     ,0        ,0     ,0      ,0          ,0    ,0           ,0          ,0     , // 6
519         0          ,0     ,0    ,0    ,0    ,0      ,0      ,0     ,0        ,0     ,0      ,0          ,0    ,0           ,0          ,0       // 7
520 };
521
522 /*
523 byte shiftscantokey[128] =
524
525 //      0           1      2     3     4     5       6       7      8         9      A       B           C    D            E           F 
526         0          ,27    ,'!'  ,'@'  ,'#'  ,'$'    ,'%'    ,'^'   ,'&'      ,'*'   ,'('    ,')'        ,'_' ,'+'         ,K_BACKSPACE,9    , // 0
527         'Q'        ,'W'   ,'E'  ,'R'  ,'T'  ,'Y'    ,'U'    ,'I'   ,'O'      ,'P'   ,'{'    ,'}'        ,13  ,K_CTRL      ,'A'        ,'S'  , // 1
528         'D'        ,'F'   ,'G'  ,'H'  ,'J'  ,'K'    ,'L'    ,':'   ,'"'      ,'~'   ,K_SHIFT,'|'        ,'Z' ,'X'         ,'C'        ,'V'  , // 2
529         'B'        ,'N'   ,'M'  ,'<'  ,'>'  ,'?'    ,K_SHIFT,'*'   ,K_ALT    ,' '   ,0      ,K_F1       ,K_F2,K_F3        ,K_F4       ,K_F5 , // 3
530         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
531         K_DOWNARROW,K_PGDN,K_INS,K_DEL,0    ,0      ,0      ,K_F11 ,K_F12    ,0     ,0      ,0          ,0   ,0           ,0          ,0    , // 5
532         0          ,0     ,0    ,0    ,0    ,0      ,0      ,0     ,0        ,0     ,0      ,0          ,0   ,0           ,0          ,0    , // 6
533         0          ,0     ,0    ,0    ,0    ,0      ,0      ,0     ,0        ,0     ,0      ,0          ,0   ,0           ,0          ,0      // 7 
534 }; 
535 */
536
537 /*
538 =======
539 MapKey
540
541 Map from windows to quake keynums
542 =======
543 */
544 int MapKey (int key, int virtualkey)
545 {
546         key = (key>>16)&255;
547         if (key > 127)
548                 return 0;
549         if (scantokey[key] == 0)
550                 Con_DPrintf("key 0x%02x has no translation\n", key);
551 //      if (scantokey[key] >= 0x20 && scantokey[key] < 0x7F)
552 //              return realchar;
553         return scantokey[key];
554 }
555
556 /*
557 ===================================================================
558
559 MAIN WINDOW
560
561 ===================================================================
562 */
563
564 /*
565 ================
566 ClearAllStates
567 ================
568 */
569 void ClearAllStates (void)
570 {
571         int             i;
572         
573 // send an up event for each key, to make sure the server clears them all
574         for (i=0 ; i<256 ; i++)
575         {
576                 Key_Event (i, false);
577         }
578
579         Key_ClearStates ();
580         IN_ClearStates ();
581 }
582
583 void VID_RestoreGameGamma(void);
584 extern qboolean host_loopactive;
585
586 void AppActivate(BOOL fActive, BOOL minimize)
587 /****************************************************************************
588 *
589 * Function:     AppActivate
590 * Parameters:   fActive - True if app is activating
591 *
592 * Description:  If the application is activating, then swap the system
593 *               into SYSPAL_NOSTATIC mode so that our palettes will display
594 *               correctly.
595 *
596 ****************************************************************************/
597 {
598         static BOOL     sound_active;
599
600         ActiveApp = fActive;
601         Minimized = minimize;
602
603 // enable/disable sound on focus gain/loss
604         if (!ActiveApp && sound_active)
605         {
606                 S_BlockSound ();
607                 sound_active = false;
608         }
609         else if (ActiveApp && !sound_active)
610         {
611                 S_UnblockSound ();
612                 sound_active = true;
613         }
614
615         if (fActive)
616         {
617                 if (modestate == MS_FULLDIB)
618                 {
619 //                      usingmouse = true;
620 //                      IN_ActivateMouse ();
621 //                      IN_HideMouse ();
622                         if (vid_canalttab && vid_wassuspended)
623                         {
624                                 vid_wassuspended = false;
625                                 ChangeDisplaySettings (&gdevmode, CDS_FULLSCREEN);
626                                 ShowWindow(mainwindow, SW_SHOWNORMAL);
627                         }
628
629                         // LordHavoc: from dabb, fix for alt-tab bug in NVidia drivers
630                         MoveWindow(mainwindow,0,0,gdevmode.dmPelsWidth,gdevmode.dmPelsHeight,false);
631                 }
632 //              else if ((modestate == MS_WINDOWED) && vid_mouse.integer && key_dest == key_game)
633 //              {
634 //                      usingmouse = true;
635 //                      IN_ActivateMouse ();
636 //                      IN_HideMouse ();
637 //              }
638                 if (host_loopactive)
639                         VID_RestoreGameGamma();
640         }
641
642         if (!fActive)
643         {
644                 usingmouse = false;
645                 IN_DeactivateMouse ();
646                 IN_ShowMouse ();
647                 if (modestate == MS_FULLDIB)
648                 {
649 //                      usingmouse = false;
650 //                      IN_DeactivateMouse ();
651 //                      IN_ShowMouse ();
652                         if (vid_canalttab)
653                         { 
654                                 ChangeDisplaySettings (NULL, 0);
655                                 vid_wassuspended = true;
656                         }
657                 }
658 //              else if ((modestate == MS_WINDOWED) && vid_mouse.integer)
659 //              {
660 //                      usingmouse = false;
661 //                      IN_DeactivateMouse ();
662 //                      IN_ShowMouse ();
663 //              }
664                 VID_RestoreSystemGamma();
665         }
666 }
667
668 LONG CDAudio_MessageHandler(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
669
670 /* main window procedure */
671 LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM  wParam, LPARAM lParam)
672 {
673     LONG    lRet = 1;
674         int             fActive, fMinimized, temp;
675         extern unsigned int uiWheelMessage;
676
677         if ( uMsg == uiWheelMessage )
678                 uMsg = WM_MOUSEWHEEL;
679
680     switch (uMsg)
681     {
682                 case WM_KILLFOCUS:
683                         if (modestate == MS_FULLDIB)
684                                 ShowWindow(mainwindow, SW_SHOWMINNOACTIVE);
685                         break;
686
687                 case WM_CREATE:
688                         break;
689
690                 case WM_MOVE:
691                         window_x = (int) LOWORD(lParam);
692                         window_y = (int) HIWORD(lParam);
693                         VID_UpdateWindowStatus ();
694                         break;
695
696                 case WM_KEYDOWN:
697                 case WM_SYSKEYDOWN:
698                         Key_Event (MapKey(lParam, wParam), true);
699                         break;
700                         
701                 case WM_KEYUP:
702                 case WM_SYSKEYUP:
703                         Key_Event (MapKey(lParam, wParam), false);
704                         break;
705
706                 case WM_SYSCHAR:
707                 // keep Alt-Space from happening
708                         break;
709
710         // this is complicated because Win32 seems to pack multiple mouse events into
711         // one update sometimes, so we always check all states and look for events
712                 case WM_LBUTTONDOWN:
713                 case WM_LBUTTONUP:
714                 case WM_RBUTTONDOWN:
715                 case WM_RBUTTONUP:
716                 case WM_MBUTTONDOWN:
717                 case WM_MBUTTONUP:
718                 case WM_MOUSEMOVE:
719                         temp = 0;
720
721                         if (wParam & MK_LBUTTON)
722                                 temp |= 1;
723
724                         if (wParam & MK_RBUTTON)
725                                 temp |= 2;
726
727                         if (wParam & MK_MBUTTON)
728                                 temp |= 4;
729
730                         IN_MouseEvent (temp);
731
732                         break;
733
734                 // JACK: This is the mouse wheel with the Intellimouse
735                 // Its delta is either positive or neg, and we generate the proper
736                 // Event.
737                 case WM_MOUSEWHEEL: 
738                         if ((short) HIWORD(wParam) > 0) {
739                                 Key_Event(K_MWHEELUP, true);
740                                 Key_Event(K_MWHEELUP, false);
741                         } else {
742                                 Key_Event(K_MWHEELDOWN, true);
743                                 Key_Event(K_MWHEELDOWN, false);
744                         }
745                         break;
746
747         case WM_SIZE:
748             break;
749
750             case WM_CLOSE:
751                         if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit", MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES)
752                                 Sys_Quit ();
753
754                 break;
755
756                 case WM_ACTIVATE:
757                         fActive = LOWORD(wParam);
758                         fMinimized = (BOOL) HIWORD(wParam);
759                         AppActivate(!(fActive == WA_INACTIVE), fMinimized);
760
761                 // fix the leftover Alt from any Alt-Tab or the like that switched us away
762                         ClearAllStates ();
763
764                         break;
765
766             case WM_DESTROY:
767         {
768                         if (mainwindow)
769                                 DestroyWindow (mainwindow);
770
771             PostQuitMessage (0);
772         }
773         break;
774
775                 case MM_MCINOTIFY:
776             lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam);
777                         break;
778
779         default:
780             /* pass all unhandled messages to DefWindowProc */
781             lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
782         break;
783     }
784
785     /* return 1 if handled message, 0 if not */
786     return lRet;
787 }
788
789
790 /*
791 =================
792 VID_NumModes
793 =================
794 */
795 int VID_NumModes (void)
796 {
797         return nummodes;
798 }
799
800         
801 /*
802 =================
803 VID_GetModePtr
804 =================
805 */
806 vmode_t *VID_GetModePtr (int modenum)
807 {
808
809         if ((modenum >= 0) && (modenum < nummodes))
810                 return &modelist[modenum];
811         else
812                 return &badmode;
813 }
814
815
816 /*
817 =================
818 VID_GetModeDescription
819 =================
820 */
821 char *VID_GetModeDescription (int mode)
822 {
823         char            *pinfo;
824         vmode_t         *pv;
825         static char     temp[100];
826
827         if ((mode < 0) || (mode >= nummodes))
828                 return NULL;
829
830         if (!leavecurrentmode)
831         {
832                 pv = VID_GetModePtr (mode);
833                 pinfo = pv->modedesc;
834         }
835         else
836         {
837                 sprintf (temp, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
838                 pinfo = temp;
839         }
840
841         return pinfo;
842 }
843
844
845 // KJB: Added this to return the mode driver name in description for console
846
847 char *VID_GetExtModeDescription (int mode)
848 {
849         static char     pinfo[40];
850         vmode_t         *pv;
851
852         if ((mode < 0) || (mode >= nummodes))
853                 return NULL;
854
855         pv = VID_GetModePtr (mode);
856         if (modelist[mode].type == MS_FULLDIB)
857         {
858                 if (!leavecurrentmode)
859                         sprintf(pinfo,"%s fullscreen", pv->modedesc);
860                 else
861                         sprintf (pinfo, "Desktop resolution (%dx%d)", modelist[MODE_FULLSCREEN_DEFAULT].width, modelist[MODE_FULLSCREEN_DEFAULT].height);
862         }
863         else
864         {
865                 if (modestate == MS_WINDOWED)
866                         sprintf(pinfo, "%s windowed", pv->modedesc);
867                 else
868                         sprintf(pinfo, "windowed");
869         }
870
871         return pinfo;
872 }
873
874
875 /*
876 =================
877 VID_DescribeCurrentMode_f
878 =================
879 */
880 void VID_DescribeCurrentMode_f (void)
881 {
882         Con_Printf ("%s\n", VID_GetExtModeDescription (vid_modenum));
883 }
884
885
886 /*
887 =================
888 VID_NumModes_f
889 =================
890 */
891 void VID_NumModes_f (void)
892 {
893
894         if (nummodes == 1)
895                 Con_Printf ("%d video mode is available\n", nummodes);
896         else
897                 Con_Printf ("%d video modes are available\n", nummodes);
898 }
899
900
901 /*
902 =================
903 VID_DescribeMode_f
904 =================
905 */
906 void VID_DescribeMode_f (void)
907 {
908         int             t, modenum;
909         
910         modenum = atoi (Cmd_Argv(1));
911
912         t = leavecurrentmode;
913         leavecurrentmode = 0;
914
915         Con_Printf ("%s\n", VID_GetExtModeDescription (modenum));
916
917         leavecurrentmode = t;
918 }
919
920
921 /*
922 =================
923 VID_DescribeModes_f
924 =================
925 */
926 void VID_DescribeModes_f (void)
927 {
928         int                     i, lnummodes, t;
929         char            *pinfo;
930         vmode_t         *pv;
931
932         lnummodes = VID_NumModes ();
933
934         t = leavecurrentmode;
935         leavecurrentmode = 0;
936
937         for (i=1 ; i<lnummodes ; i++)
938         {
939                 pv = VID_GetModePtr (i);
940                 pinfo = VID_GetExtModeDescription (i);
941                 Con_Printf ("%2d: %s\n", i, pinfo);
942         }
943
944         leavecurrentmode = t;
945 }
946
947 void VID_AddMode(int type, int width, int height, int modenum, int halfscreen, int dib, int fullscreen, int bpp)
948 {
949         int i;
950         if (nummodes >= MAX_MODE_LIST)
951                 return;
952         modelist[nummodes].type = type;
953         modelist[nummodes].width = width;
954         modelist[nummodes].height = height;
955         modelist[nummodes].modenum = modenum;
956         modelist[nummodes].halfscreen = halfscreen;
957         modelist[nummodes].dib = dib;
958         modelist[nummodes].fullscreen = fullscreen;
959         modelist[nummodes].bpp = bpp;
960         if (bpp == 0)
961                 sprintf (modelist[nummodes].modedesc, "%dx%d", width, height);
962         else
963                 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", width, height, bpp);
964         for (i = 0;i < nummodes;i++)
965         {
966                 if (!memcmp(&modelist[i], &modelist[nummodes], sizeof(vmode_t)))
967                         return;
968         }
969         nummodes++;
970 }
971
972 void VID_InitDIB (HINSTANCE hInstance)
973 {
974         int w, h;
975         WNDCLASS                wc;
976
977         // Register the frame class
978     wc.style         = 0;
979     wc.lpfnWndProc   = (WNDPROC)MainWndProc;
980     wc.cbClsExtra    = 0;
981     wc.cbWndExtra    = 0;
982     wc.hInstance     = hInstance;
983     wc.hIcon         = 0;
984     wc.hCursor       = LoadCursor (NULL,IDC_ARROW);
985         wc.hbrBackground = NULL;
986     wc.lpszMenuName  = 0;
987     wc.lpszClassName = gamename;
988
989     if (!RegisterClass (&wc) )
990                 Sys_Error ("Couldn't register window class");
991
992         /*
993         modelist[0].type = MS_WINDOWED;
994
995         if (COM_CheckParm("-width"))
996                 modelist[0].width = atoi(com_argv[COM_CheckParm("-width")+1]);
997         else
998                 modelist[0].width = 640;
999
1000         if (modelist[0].width < 320)
1001                 modelist[0].width = 320;
1002
1003         if (COM_CheckParm("-height"))
1004                 modelist[0].height= atoi(com_argv[COM_CheckParm("-height")+1]);
1005         else
1006                 modelist[0].height = modelist[0].width * 240/320;
1007
1008         if (modelist[0].height < 240)
1009                 modelist[0].height = 240;
1010
1011         sprintf (modelist[0].modedesc, "%dx%d", modelist[0].width, modelist[0].height);
1012
1013         modelist[0].modenum = MODE_WINDOWED;
1014         modelist[0].dib = 1;
1015         modelist[0].fullscreen = 0;
1016         modelist[0].halfscreen = 0;
1017         modelist[0].bpp = 0;
1018
1019         nummodes = 1;
1020         */
1021         if (COM_CheckParm("-width"))
1022                 w = atoi(com_argv[COM_CheckParm("-width")+1]);
1023         else
1024                 w = 640;
1025
1026         if (w < 320)
1027                 w = 320;
1028
1029         if (COM_CheckParm("-height"))
1030                 h = atoi(com_argv[COM_CheckParm("-height")+1]);
1031         else
1032                 h = w * 240/320;
1033
1034         if (h < 240)
1035                 h = 240;
1036
1037         VID_AddMode(MS_WINDOWED, w, h, 0, 0, 1, 0, 0);
1038 }
1039
1040
1041 /*
1042 =================
1043 VID_InitFullDIB
1044 =================
1045 */
1046 void VID_InitFullDIB (HINSTANCE hInstance)
1047 {
1048         DEVMODE devmode;
1049 //      int             i;
1050         int             modenum;
1051         int             originalnummodes;
1052 //      int             existingmode;
1053         int             numlowresmodes;
1054         int             j;
1055         int             bpp;
1056         int             done;
1057         BOOL    stat;
1058
1059 // enumerate >8 bpp modes
1060         originalnummodes = nummodes;
1061         modenum = 0;
1062
1063         do
1064         {
1065                 stat = EnumDisplaySettings (NULL, modenum, &devmode);
1066
1067                 if ((devmode.dmBitsPerPel >= 15) && (devmode.dmPelsWidth <= MAXWIDTH) && (devmode.dmPelsHeight <= MAXHEIGHT) && (nummodes < MAX_MODE_LIST))
1068                 {
1069                         devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1070
1071                         if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1072                         {
1073                         // if the width is more than twice the height, reduce it by half because this
1074                         // is probably a dual-screen monitor
1075                                 if ((!COM_CheckParm("-noadjustaspect")) && (devmode.dmPelsWidth > (devmode.dmPelsHeight << 1)))
1076                                         VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth >> 1, devmode.dmPelsHeight, 0, 1, 1, 1, devmode.dmBitsPerPel);
1077                                 else
1078                                         VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1079                                 /*
1080                                 modelist[nummodes].type = MS_FULLDIB;
1081                                 modelist[nummodes].width = devmode.dmPelsWidth;
1082                                 modelist[nummodes].height = devmode.dmPelsHeight;
1083                                 modelist[nummodes].modenum = 0;
1084                                 modelist[nummodes].halfscreen = 0;
1085                                 modelist[nummodes].dib = 1;
1086                                 modelist[nummodes].fullscreen = 1;
1087                                 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1088                                 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1089
1090                         // if the width is more than twice the height, reduce it by half because this
1091                         // is probably a dual-screen monitor
1092                                 if (!COM_CheckParm("-noadjustaspect"))
1093                                 {
1094                                         if (modelist[nummodes].width > (modelist[nummodes].height << 1))
1095                                         {
1096                                                 modelist[nummodes].width >>= 1;
1097                                                 modelist[nummodes].halfscreen = 1;
1098                                                 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", modelist[nummodes].width, modelist[nummodes].height, modelist[nummodes].bpp);
1099                                         }
1100                                 }
1101
1102                                 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1103                                 {
1104                                         if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1105                                         {
1106                                                 existingmode = 1;
1107                                                 break;
1108                                         }
1109                                 }
1110
1111                                 if (!existingmode)
1112                                         nummodes++;
1113                                 */
1114                         }
1115                 }
1116
1117                 modenum++;
1118         }
1119         while (stat);
1120
1121 // see if there are any low-res modes that aren't being reported
1122         numlowresmodes = sizeof(lowresmodes) / sizeof(lowresmodes[0]);
1123         bpp = 16;
1124         done = 0;
1125
1126         do
1127         {
1128                 for (j=0 ; (j<numlowresmodes) && (nummodes < MAX_MODE_LIST) ; j++)
1129                 {
1130                         devmode.dmBitsPerPel = bpp;
1131                         devmode.dmPelsWidth = lowresmodes[j].width;
1132                         devmode.dmPelsHeight = lowresmodes[j].height;
1133                         devmode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
1134
1135                         if (ChangeDisplaySettings (&devmode, CDS_TEST | CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL)
1136                                 VID_AddMode(MS_FULLDIB, devmode.dmPelsWidth, devmode.dmPelsHeight, 0, 0, 1, 1, devmode.dmBitsPerPel);
1137                         /*
1138                         {
1139                                 modelist[nummodes].type = MS_FULLDIB;
1140                                 modelist[nummodes].width = devmode.dmPelsWidth;
1141                                 modelist[nummodes].height = devmode.dmPelsHeight;
1142                                 modelist[nummodes].modenum = 0;
1143                                 modelist[nummodes].halfscreen = 0;
1144                                 modelist[nummodes].dib = 1;
1145                                 modelist[nummodes].fullscreen = 1;
1146                                 modelist[nummodes].bpp = devmode.dmBitsPerPel;
1147                                 sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1148
1149                                 for (i=originalnummodes, existingmode = 0 ; i<nummodes ; i++)
1150                                 {
1151                                         if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1152                                         {
1153                                                 existingmode = 1;
1154                                                 break;
1155                                         }
1156                                 }
1157
1158                                 if (!existingmode)
1159                                         nummodes++;
1160                         }
1161                         */
1162                 }
1163                 switch (bpp)
1164                 {
1165                         case 16:
1166                                 bpp = 32;
1167                                 break;
1168
1169                         case 32:
1170                                 bpp = 24;
1171                                 break;
1172
1173                         case 24:
1174                                 done = 1;
1175                                 break;
1176                 }
1177         }
1178         while (!done);
1179
1180         if (nummodes == originalnummodes)
1181                 Con_SafePrintf ("No fullscreen DIB modes found\n");
1182 }
1183
1184 static int grabsysgamma = true;
1185 WORD systemgammaramps[3][256], currentgammaramps[3][256];
1186
1187 int VID_SetGamma(float prescale, float gamma, float scale, float base)
1188 {
1189         int i;
1190         HDC hdc;
1191         hdc = GetDC (NULL);
1192
1193         BuildGammaTable16(prescale, gamma, scale, base, &currentgammaramps[0][0]);
1194         for (i = 0;i < 256;i++)
1195                 currentgammaramps[1][i] = currentgammaramps[2][i] = currentgammaramps[0][i];
1196
1197         i = SetDeviceGammaRamp(hdc, &currentgammaramps[0][0]);
1198
1199         ReleaseDC (NULL, hdc);
1200         return i; // return success or failure
1201 }
1202
1203 void VID_RestoreGameGamma(void)
1204 {
1205         VID_UpdateGamma(true);
1206 }
1207
1208 void VID_GetSystemGamma(void)
1209 {
1210         HDC hdc;
1211         hdc = GetDC (NULL);
1212
1213         GetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1214
1215         ReleaseDC (NULL, hdc);
1216 }
1217
1218 void VID_RestoreSystemGamma(void)
1219 {
1220         HDC hdc;
1221         hdc = GetDC (NULL);
1222
1223         SetDeviceGammaRamp(hdc, &systemgammaramps[0][0]);
1224
1225         ReleaseDC (NULL, hdc);
1226 }
1227
1228 /*
1229 ===================
1230 VID_Init
1231 ===================
1232 */
1233 void    VID_Init (void)
1234 {
1235         int             i;
1236 //      int             existingmode;
1237         int             basenummodes, width, height, bpp, findbpp, done;
1238         HDC             hdc;
1239         DEVMODE devmode;
1240
1241         memset(&devmode, 0, sizeof(devmode));
1242
1243 //      Cvar_RegisterVariable (&_vid_default_mode);
1244 //      Cvar_RegisterVariable (&_vid_default_mode_win);
1245
1246         Cmd_AddCommand ("vid_nummodes", VID_NumModes_f);
1247         Cmd_AddCommand ("vid_describecurrentmode", VID_DescribeCurrentMode_f);
1248         Cmd_AddCommand ("vid_describemode", VID_DescribeMode_f);
1249         Cmd_AddCommand ("vid_describemodes", VID_DescribeModes_f);
1250
1251         VID_GetSystemGamma();
1252
1253         hIcon = LoadIcon (global_hInstance, MAKEINTRESOURCE (IDI_ICON2));
1254
1255         InitCommonControls();
1256
1257         VID_InitDIB (global_hInstance);
1258         basenummodes = nummodes = 1;
1259
1260         VID_InitFullDIB (global_hInstance);
1261
1262         if (COM_CheckParm("-window"))
1263         {
1264                 hdc = GetDC (NULL);
1265
1266                 if (GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE)
1267                         Sys_Error ("Can't run in non-RGB mode");
1268
1269                 ReleaseDC (NULL, hdc);
1270
1271                 windowed = true;
1272
1273                 vid_default = MODE_WINDOWED;
1274         }
1275         else
1276         {
1277                 if (nummodes == 1)
1278                         Sys_Error ("No RGB fullscreen modes available");
1279
1280                 windowed = false;
1281
1282                 if (COM_CheckParm("-mode"))
1283                         vid_default = atoi(com_argv[COM_CheckParm("-mode")+1]);
1284                 else
1285                 {
1286                         if (COM_CheckParm("-current"))
1287                         {
1288                                 modelist[MODE_FULLSCREEN_DEFAULT].width = GetSystemMetrics (SM_CXSCREEN);
1289                                 modelist[MODE_FULLSCREEN_DEFAULT].height = GetSystemMetrics (SM_CYSCREEN);
1290                                 vid_default = MODE_FULLSCREEN_DEFAULT;
1291                                 leavecurrentmode = 1;
1292                         }
1293                         else
1294                         {
1295                                 if (COM_CheckParm("-width"))
1296                                         width = atoi(com_argv[COM_CheckParm("-width")+1]);
1297                                 else
1298                                         width = 640;
1299
1300                                 if (COM_CheckParm("-bpp"))
1301                                 {
1302                                         bpp = atoi(com_argv[COM_CheckParm("-bpp")+1]);
1303                                         findbpp = 0;
1304                                 }
1305                                 else
1306                                 {
1307                                         bpp = 15;
1308                                         findbpp = 1;
1309                                 }
1310
1311                                 if (COM_CheckParm("-height"))
1312                                         height = atoi(com_argv[COM_CheckParm("-height")+1]);
1313
1314                         // if they want to force it, add the specified mode to the list
1315                                 if (COM_CheckParm("-force") && (nummodes < MAX_MODE_LIST))
1316                                         VID_AddMode(MS_FULLDIB, width, height, 0, 0, 1, 1, bpp);
1317                                 /*
1318                                 {
1319                                         modelist[nummodes].type = MS_FULLDIB;
1320                                         modelist[nummodes].width = width;
1321                                         modelist[nummodes].height = height;
1322                                         modelist[nummodes].modenum = 0;
1323                                         modelist[nummodes].halfscreen = 0;
1324                                         modelist[nummodes].dib = 1;
1325                                         modelist[nummodes].fullscreen = 1;
1326                                         modelist[nummodes].bpp = bpp;
1327                                         sprintf (modelist[nummodes].modedesc, "%dx%dx%d", devmode.dmPelsWidth, devmode.dmPelsHeight, devmode.dmBitsPerPel);
1328
1329                                         for (i=nummodes, existingmode = 0 ; i<nummodes ; i++)
1330                                         {
1331                                                 if ((modelist[nummodes].width == modelist[i].width) && (modelist[nummodes].height == modelist[i].height) && (modelist[nummodes].bpp == modelist[i].bpp))
1332                                                 {
1333                                                         existingmode = 1;
1334                                                         break;
1335                                                 }
1336                                         }
1337
1338                                         if (!existingmode)
1339                                                 nummodes++;
1340                                 }
1341                                 */
1342
1343                                 done = 0;
1344
1345                                 do
1346                                 {
1347                                         if (COM_CheckParm("-height"))
1348                                         {
1349                                                 height = atoi(com_argv[COM_CheckParm("-height")+1]);
1350
1351                                                 for (i=1, vid_default=0 ; i<nummodes ; i++)
1352                                                 {
1353                                                         if ((modelist[i].width == width) && (modelist[i].height == height) && (modelist[i].bpp == bpp))
1354                                                         {
1355                                                                 vid_default = i;
1356                                                                 done = 1;
1357                                                                 break;
1358                                                         }
1359                                                 }
1360                                         }
1361                                         else
1362                                         {
1363                                                 for (i=1, vid_default=0 ; i<nummodes ; i++)
1364                                                 {
1365                                                         if ((modelist[i].width == width) && (modelist[i].bpp == bpp))
1366                                                         {
1367                                                                 vid_default = i;
1368                                                                 done = 1;
1369                                                                 break;
1370                                                         }
1371                                                 }
1372                                         }
1373
1374                                         if (!done)
1375                                         {
1376                                                 if (findbpp)
1377                                                 {
1378                                                         switch (bpp)
1379                                                         {
1380                                                         case 15: bpp = 16;break;
1381                                                         case 16: bpp = 32;break;
1382                                                         case 32: bpp = 24;break;
1383                                                         case 24: done = 1;break;
1384                                                         }
1385                                                 }
1386                                                 else
1387                                                         done = 1;
1388                                         }
1389                                 }
1390                                 while (!done);
1391
1392                                 if (!vid_default)
1393                                         Sys_Error ("Specified video mode not available");
1394                         }
1395                 }
1396         }
1397
1398         vid_initialized = true;
1399
1400         if ((i = COM_CheckParm("-conwidth")) != 0)
1401                 vid.conwidth = atoi(com_argv[i+1]);
1402         else
1403                 vid.conwidth = 640;
1404
1405         vid.conwidth &= 0xfff8; // make it a multiple of eight
1406
1407         if (vid.conwidth < 320)
1408                 vid.conwidth = 320;
1409
1410         // pick a conheight that matches with correct aspect
1411         vid.conheight = vid.conwidth*3 / 4;
1412
1413         if ((i = COM_CheckParm("-conheight")) != 0)
1414                 vid.conheight = atoi(com_argv[i+1]);
1415         if (vid.conheight < 200)
1416                 vid.conheight = 200;
1417
1418         VID_SetMode (vid_default);
1419
1420         maindc = GetDC(mainwindow);
1421         bSetupPixelFormat(maindc);
1422
1423         baseRC = wglCreateContext( maindc );
1424         if (!baseRC)
1425                 Sys_Error ("Could not initialize GL (wglCreateContext failed).\n\nMake sure you are in 65536 color mode, and try running -window.");
1426         if (!wglMakeCurrent( maindc, baseRC ))
1427                 Sys_Error ("wglMakeCurrent failed");
1428
1429         GL_Init ();
1430
1431         // LordHavoc: special differences for ATI (broken 8bit color when also using 32bit? weird!)
1432         if (strncasecmp(gl_vendor,"ATI",3)==0)
1433         {
1434                 if (strncasecmp(gl_renderer,"Rage Pro",8)==0)
1435                         isRagePro = true;
1436         }
1437         if (strncasecmp(gl_renderer,"Matrox G200 Direct3D",20)==0) // a D3D driver for GL? sigh...
1438                 isG200 = true;
1439
1440 //      sprintf (gldir, "%s/glquake", com_gamedir);
1441 //      Sys_mkdir (gldir);
1442
1443         vid_realmode = vid_modenum;
1444
1445         vid_menudrawfn = VID_MenuDraw;
1446         vid_menukeyfn = VID_MenuKey;
1447
1448         strcpy (badmode.modedesc, "Bad mode");
1449         vid_canalttab = true;
1450 }
1451
1452
1453 //========================================================
1454 // Video menu stuff
1455 //========================================================
1456
1457 extern void M_Menu_Options_f (void);
1458 extern void M_Print (int cx, int cy, char *str);
1459 extern void M_PrintWhite (int cx, int cy, char *str);
1460 extern void M_DrawCharacter (int cx, int line, int num);
1461 extern void M_DrawPic (int x, int y, qpic_t *pic);
1462
1463 static int      vid_line, vid_wmodes;
1464
1465 typedef struct
1466 {
1467         int             modenum;
1468         char    *desc;
1469         int             iscur;
1470 } modedesc_t;
1471
1472 #define MAX_COLUMN_SIZE         9
1473 #define MODE_AREA_HEIGHT        (MAX_COLUMN_SIZE + 2)
1474 #define MAX_MODEDESCS           (MAX_COLUMN_SIZE*3)
1475
1476 static modedesc_t       modedescs[MAX_MODEDESCS];
1477
1478 /*
1479 ================
1480 VID_MenuDraw
1481 ================
1482 */
1483 void VID_MenuDraw (void)
1484 {
1485         qpic_t          *p;
1486         char            *ptr;
1487         int                     lnummodes, i, k, column, row;
1488         vmode_t         *pv;
1489
1490         p = Draw_CachePic ("gfx/vidmodes.lmp");
1491         M_DrawPic ( (320-p->width)/2, 4, p);
1492
1493         vid_wmodes = 0;
1494         lnummodes = VID_NumModes ();
1495         
1496         for (i=1 ; (i<lnummodes) && (vid_wmodes < MAX_MODEDESCS) ; i++)
1497         {
1498                 ptr = VID_GetModeDescription (i);
1499                 pv = VID_GetModePtr (i);
1500
1501                 k = vid_wmodes;
1502
1503                 modedescs[k].modenum = i;
1504                 modedescs[k].desc = ptr;
1505                 modedescs[k].iscur = 0;
1506
1507                 if (i == vid_modenum)
1508                         modedescs[k].iscur = 1;
1509
1510                 vid_wmodes++;
1511
1512         }
1513
1514         if (vid_wmodes > 0)
1515         {
1516                 M_Print (2*8, 36+0*8, "Fullscreen Modes (WIDTHxHEIGHTxBPP)");
1517
1518                 column = 8;
1519                 row = 36+2*8;
1520
1521                 for (i=0 ; i<vid_wmodes ; i++)
1522                 {
1523                         if (modedescs[i].iscur)
1524                                 M_PrintWhite (column, row, modedescs[i].desc);
1525                         else
1526                                 M_Print (column, row, modedescs[i].desc);
1527
1528                         column += 13*8;
1529
1530                         if ((i % VID_ROW_SIZE) == (VID_ROW_SIZE - 1))
1531                         {
1532                                 column = 8;
1533                                 row += 8;
1534                         }
1535                 }
1536         }
1537
1538         M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*2, "Video modes must be set from the");
1539         M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*3, "command line with -width <width>");
1540         M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*4, "and -bpp <bits-per-pixel>");
1541         M_Print (3*8, 36 + MODE_AREA_HEIGHT * 8 + 8*6, "Select windowed mode with -window");
1542 }
1543
1544
1545 /*
1546 ================
1547 VID_MenuKey
1548 ================
1549 */
1550 void VID_MenuKey (int key)
1551 {
1552         switch (key)
1553         {
1554         case K_ESCAPE:
1555                 S_LocalSound ("misc/menu1.wav");
1556                 M_Menu_Options_f ();
1557                 break;
1558
1559         default:
1560                 break;
1561         }
1562 }