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