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