home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / glquake_src / gl_vidamiga.c < prev    next >
C/C++ Source or Header  |  2000-03-06  |  19KB  |  781 lines

  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.  
  21. // gl_vidamiga.c - 19/01/2000 Massimiliano Tretene
  22.  
  23. #include <sys/ioctl.h>
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <signal.h>
  27.  
  28. #include "quakedef.h"
  29.  
  30. #include <mgl/gl.h>
  31.  
  32. #define WARP_WIDTH              320
  33. #define WARP_HEIGHT             240
  34.  
  35. unsigned char rawkeyconv[] = {
  36.   '`','1','2','3','4','5','6','7','8','9','0','-','=','\\',0,'0',
  37.   'q','w','e','r','t','y','u','i','o','p','[',']',0,K_END,K_DOWNARROW,K_PGDN,
  38.   'a','s','d','f','g','h','j','k','l',';','\'',K_F12,0,K_LEFTARROW,0,K_RIGHTARROW,
  39.   K_F11,'z','x','c','v','b','n','m',',','.','/',0,0,K_HOME,K_UPARROW,K_PGUP,
  40.   K_SPACE,K_BACKSPACE,K_TAB,K_ENTER,K_ENTER,K_ESCAPE,K_DEL,0,
  41.   0,0,'-',0,K_UPARROW,K_DOWNARROW,K_RIGHTARROW,K_LEFTARROW,
  42.   K_F1,K_F2,K_F3,K_F4,K_F5,K_F6,K_F7,K_F8,K_F9,K_F10,'(',')','/','*','+',K_PAUSE,
  43.   K_SHIFT,K_SHIFT,0,K_CTRL,K_ALT,K_ALT,0,0,
  44.   K_MOUSE1,K_MOUSE2,K_MOUSE3
  45. };
  46. int shutdown_keyboard;
  47.  
  48. struct Window *QuakeWindow = NULL;
  49.  
  50. GLboolean zbuffer = GL_TRUE;
  51.  
  52. GLfloat mouse_x = 0.0, mouse_y = 0.0, mouse_z = 0.0;
  53. GLint offset = 0;
  54. GLfloat fov = 70.0;
  55. GLfloat inf_w = 0.1;
  56. GLfloat zback = 1000.0;
  57. GLfloat alpha = 1.0;
  58.  
  59. GLcontext context;
  60. int numb=3;
  61. int bpp = 15;
  62. qboolean gl_warp = true;
  63.  
  64. unsigned    d_8to24table[256];
  65. unsigned char d_15to8table[65536];
  66.  
  67. static qboolean gl_videosync = true;
  68. static qboolean gl_dithering = true;
  69. static int gl_lockmode = MGL_LOCK_MANUAL;
  70. static int scr_width, scr_height;
  71. static float width;
  72. static qboolean UPDATE = FALSE;  
  73. static GLfloat gl_polyoffset = 0.0;
  74.  
  75. void VID_MenuDraw (void);
  76. void VID_MenuKey (int);
  77.  
  78. /*-----------------------------------------------------------------------*/
  79.  
  80. //int   texture_mode = GL_NEAREST;
  81. //int   texture_mode = GL_NEAREST_MIPMAP_NEAREST;
  82. //int   texture_mode = GL_NEAREST_MIPMAP_LINEAR;
  83. //int   texture_mode = GL_LINEAR_MIPMAP_NEAREST;
  84. //int   texture_mode = GL_LINEAR_MIPMAP_LINEAR;
  85.  
  86. #ifdef __060__
  87. int   texture_mode = GL_NEAREST;
  88. #else
  89. int   texture_mode = GL_LINEAR;
  90. #endif
  91.  
  92. extern int   gl_filter_min;
  93. extern int   gl_filter_max;
  94. extern cvar_t gl_fog;
  95. extern cvar_t gl_max_size;
  96.  
  97. int   texture_extension_number = 1;
  98.  
  99. float   gldepthmin, gldepthmax;
  100.  
  101. cvar_t  gl_ztrick = {"gl_ztrick","0"};
  102. cvar_t  gl_width = {"gl_width","320"};
  103.  
  104. const char *gl_vendor;
  105. const char *gl_renderer;
  106. const char *gl_version;
  107. const char *gl_extensions;
  108.  
  109. static float vid_gamma = 1.0;
  110.  
  111. qboolean is8bit = false;
  112. qboolean isPermedia = false;
  113. qboolean gl_mtexable = false;
  114.  
  115. /*-----------------------------------------------------------------------*/
  116. void D_BeginDirectRect (int x, int y, byte *pbitmap, int width, int height)
  117. {
  118. }
  119.  
  120. void D_EndDirectRect (int x, int y, int width, int height)
  121. {
  122. }
  123.  
  124.  
  125. void VID_Shutdown(void)
  126. {
  127.    if (context) {
  128.       mglDeleteContext();
  129.       MGLTerm();   
  130.    }   
  131. }
  132.  
  133. void signal_handler(int sig)
  134. {
  135.   printf("Received signal %d, exiting...\n", sig);
  136.   Sys_Quit();
  137.   exit(0);
  138. }
  139.  
  140. void InitSig(void)
  141. {
  142.   signal(SIGABRT, signal_handler);
  143.   signal(SIGFPE, signal_handler);
  144.   signal(SIGILL, signal_handler);
  145.   signal(SIGINT, signal_handler);
  146.   signal(SIGSEGV, signal_handler);
  147.   signal(SIGTERM, signal_handler);
  148. }
  149.  
  150. static void Check_Gamma (unsigned char *pal)
  151. {
  152.   float f, inf;
  153.   static unsigned char palette[768];
  154.   register int   i;
  155.   //static qboolean firsttime = false;
  156.   //if (firsttime) return;
  157.   
  158.   if ((i = COM_CheckParm("-gamma")) == 0) {
  159.     if ((gl_renderer && strstr(gl_renderer, "Voodoo")) ||
  160.       (gl_vendor && strstr(gl_vendor, "3Dfx")))
  161.       vid_gamma = 1;
  162.     else
  163.       vid_gamma = 0.7; // default to 0.7 on non-3dfx hardware
  164.   } else
  165.     vid_gamma = Q_atof(com_argv[i+1]);
  166.  
  167.   //firsttime=true;
  168.  
  169.   for (i=0 ; i<768 ; i++)
  170.   {
  171.     f = pow ( (pal[i]+1)/256.0 , vid_gamma );
  172.     inf = f*255 + 0.5;
  173.     if (inf < 0)
  174.       inf = 0;
  175.     if (inf > 255)
  176.       inf = 255;
  177.     palette[i] = inf;
  178.   }
  179.  
  180.   memcpy (pal, palette, sizeof(palette));
  181. }
  182. /*
  183. void VID_ShiftPalette(unsigned char *p)
  184. {
  185.   VID_SetPalette(p);
  186. }
  187. */
  188. void    VID_SetPalette (unsigned char *palette)
  189. {
  190.     register byte    *pal;
  191.     register unsigned short r,g,b;
  192.     register unsigned     v;
  193.     register unsigned short i;
  194.     register unsigned    *table;
  195.     int     r1,g1,b1;
  196.     int        k;
  197.     FILE *f;
  198.     char s[255];
  199.     float dist, bestdist;
  200.     static qboolean palflag = false;
  201.  
  202. //
  203. // 8 8 8 encoding
  204. //
  205.     pal = palette;
  206.     table = d_8to24table;
  207.     for (i=0 ; i<256 ; i++)
  208.     {
  209.         r = pal[0];
  210.         g = pal[1];
  211.         b = pal[2];
  212.         pal += 3;
  213.         
  214.         //v = (255<<24) + (r<<0) + (g<<8) + (b<<16);
  215.         //v = (255<<0) + (r<<24) + (g<<16) + (b<<8);
  216. //    v = LittleLong((170<<24) + (r<<0) + (g<<8) + (b<<16));
  217.  
  218.     v = (170<<0) + (r<<24) + (g<<16) + (b<<8);
  219.         *table++ = v;
  220.     }
  221.     d_8to24table[255] &= 0xffffff00;    // 255 is transparent
  222.  
  223.     // JACK: 3D distance calcs - k is last closest, l is the distance.
  224.     // FIXME: Precalculate this and cache to disk.
  225.     if (palflag)
  226.         return;
  227.     palflag = true;
  228.  
  229.     COM_FOpenFile("glquake/15to8.pal", &f);
  230.     if (f) {
  231.         fread(d_15to8table, 1<<15, 1, f);
  232.         fclose(f);
  233.     } else {
  234.         for (i=0; i < (1<<15); i++) {
  235.             /* Maps
  236.              000000000000000
  237.              000000000011111 = Red  = 0x1F
  238.              000001111100000 = Blue = 0x03E0
  239.              111110000000000 = Grn  = 0x7C00
  240.              */
  241.              r = ((i & 0x1F) << 3)+4;
  242.              g = ((i & 0x03E0) >> 2)+4;
  243.              b = ((i & 0x7C00) >> 7)+4;
  244.             pal = (unsigned char *)d_8to24table;
  245.             for (v=0,k=0,bestdist=10000.0; v<256; v++,pal+=4) {
  246.                  r1 = (int)r - (int)pal[0];
  247.                  g1 = (int)g - (int)pal[1];
  248.                  b1 = (int)b - (int)pal[2];
  249.                 dist = sqrt(((r1*r1)+(g1*g1)+(b1*b1)));
  250.                 if (dist < bestdist) {
  251.                     k=v;
  252.                     bestdist = dist;
  253.                 }
  254.             }
  255.             d_15to8table[i]=k;
  256.         }
  257.         sprintf(s, "%s/glquake", com_gamedir);
  258.          Sys_mkdir (s);
  259.         sprintf(s, "%s/glquake/15to8.pal", com_gamedir);
  260.         if ((f = fopen(s, "wb")) != NULL) {
  261.             fwrite(d_15to8table, 1<<15, 1, f);
  262.             fclose(f);
  263.         }
  264.     }
  265. }
  266.  
  267.  
  268. /*
  269. ===============
  270. GL_Init
  271. ===============
  272. */
  273. void GL_Init (void)
  274. {
  275.   gl_vendor = glGetString (GL_VENDOR);
  276.   Con_Printf ("GL_VENDOR: %s\n", gl_vendor);
  277.   gl_renderer = glGetString (GL_RENDERER);
  278.   Con_Printf ("GL_RENDERER: %s\n", gl_renderer);
  279.   if (strstr(gl_renderer, "(virge)")) gl_polyoffset = -0.0005;
  280.  
  281.   gl_version = glGetString (GL_VERSION);
  282.   Con_Printf ("GL_VERSION: %s\n", gl_version);
  283.   gl_extensions = glGetString (GL_EXTENSIONS);
  284.   Con_Printf ("GL_EXTENSIONS: %s\n", gl_extensions);
  285.  
  286. //  Con_Printf ("%s %s\n", gl_renderer, gl_version);
  287.  
  288.   glClearColor (1,0,0,0);
  289.   glCullFace(GL_FRONT);
  290.   glEnable(GL_TEXTURE_2D);
  291.  
  292.   glEnable(GL_ALPHA_TEST);
  293.   glAlphaFunc(GL_GREATER, 0.666);
  294.  
  295.   glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  296.   glShadeModel (GL_FLAT);
  297.  
  298.   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  299.   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  300.   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  301.   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  302.  
  303.   glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  304.  
  305. //  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  306.   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, (GLint)GL_REPLACE);
  307. }
  308.  
  309. /*
  310. =================
  311. GL_BeginRendering
  312.  
  313. =================
  314. */
  315. void GL_BeginRendering (int *x, int *y, int *width, int *height)
  316. {
  317.   extern cvar_t gl_clear;
  318.  
  319.   *x = *y = 0;
  320.   *width = scr_width;
  321.   *height = scr_height;
  322.  
  323.   if (gl_lockmode == MGL_LOCK_MANUAL) MGLLockDisplay(context);
  324.  
  325. //    if (!wglMakeCurrent( maindc, baseRC ))
  326. //    Sys_Error ("wglMakeCurrent failed");
  327.  
  328. //  glViewport (*x, *y, *width, *height);
  329. }
  330.  
  331.  
  332. void GL_EndRendering (void)
  333. {
  334.    MGLUnlockDisplay(context);
  335.    MGLSwitchDisplay(context);
  336.    
  337.    if (UPDATE) {
  338.       gl_width.value = width;
  339.       Con_Printf("CHANGED ------------------------------ %4.0f\n",gl_width.value);
  340.    
  341.       scr_width = (int)gl_width.value;
  342.       scr_height = scr_width * 3 / 4;
  343.       vid.conwidth = scr_width;
  344.       vid.conwidth &= 0xfff8; // make it a multiple of eight
  345.  
  346.       if (vid.conwidth < 320) vid.conwidth = 320;
  347.       vid.conheight = vid.conwidth * 3 / 4;
  348.  
  349.         vid.width = vid.conwidth;
  350.       vid.height = vid.conheight;
  351.  
  352.       MGLResizeContext(context, vid.width, vid.height);
  353.       QuakeWindow = mglGetWindowHandle();
  354.       vid.recalc_refdef = 1;
  355.       //if (gl_lockmode == MGL_LOCK_MANUAL) mglLockDisplay();
  356.       //SCR_UpdateScreen();
  357.       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  358.       //mglUnlockDisplay();
  359.       
  360.       UPDATE = FALSE;
  361.    }   
  362. /*   
  363.    {   
  364.      GLint current = 0;
  365.      GLint peek = 0;
  366.      MGLTexMemStat(context,¤t,&peek);
  367.      printf("Current: %d - Peek: %d\n",current,peek);
  368.    }  
  369. */   
  370. }
  371.  
  372. qboolean VID_Is8bit(void)
  373. {
  374.   return is8bit;
  375. }
  376.  
  377. void ChangeVideoSync_f (void)
  378. {
  379.   if (Cmd_Argc() == 1)
  380.   {
  381.      Con_Printf ("GL_VIDEOSYNC %d\n", gl_videosync);
  382.      return;
  383.   }
  384.  
  385.   gl_videosync = Q_atoi(Cmd_Argv(1)); 
  386.   mglEnableSync(gl_videosync);
  387. }
  388.  
  389. void ChangeDithering_f (void)
  390. {
  391.   if (Cmd_Argc() == 1)
  392.   {
  393.      Con_Printf ("GL_DITHERING IS %d\n", gl_dithering);
  394.      return;
  395.   }
  396.  
  397.   gl_dithering = Q_atoi(Cmd_Argv(1)); 
  398.   MGLSetState(context,GL_DITHER,gl_dithering);
  399. }
  400.  
  401. void ChangeLockMode_f (void)
  402. {
  403.   if (Cmd_Argc() == 1)
  404.   {
  405.      if (gl_lockmode == MGL_LOCK_SMART) Con_Printf ("SMART\n");
  406.      else if (gl_lockmode == MGL_LOCK_AUTOMATIC) Con_Printf ("AUTOMATIC\n");
  407.      else if (gl_lockmode == MGL_LOCK_MANUAL) Con_Printf ("MANUAL\n");
  408.      else Con_Printf ("UNKNOWN\n");
  409.      return;
  410.   }
  411.   
  412.   if (Q_strcasecmp(Cmd_Argv(1),"SMART") == 0) gl_lockmode = MGL_LOCK_SMART;
  413.   if (Q_strcasecmp(Cmd_Argv(1),"AUTOMATIC") == 0) gl_lockmode = MGL_LOCK_AUTOMATIC;
  414.   if (Q_strcasecmp(Cmd_Argv(1),"MANUAL") == 0) gl_lockmode = MGL_LOCK_MANUAL;
  415.  
  416.   mglLockMode(gl_lockmode);
  417. }
  418.  
  419. void ChangeVideoMode_f (void)
  420. {
  421.   if (Cmd_Argc() == 1)
  422.   {
  423.      Con_Printf ("GL_WIDTH is %4.0f\n", gl_width.value);
  424.      return;
  425.   }
  426.  
  427.    width = Q_atof(Cmd_Argv(1)); 
  428.    if (width != gl_width.value) UPDATE = TRUE;
  429. }
  430.  
  431. void ChangePolyOffset_f (void)
  432. {
  433.   if (Cmd_Argc() == 1)
  434.   {
  435.      Con_Printf ("GL_POLYOFFSET is %f\n", gl_polyoffset);
  436.      return;
  437.   }
  438.  
  439.    gl_polyoffset = Q_atof(Cmd_Argv(1)); 
  440.    mglSetZOffset(gl_polyoffset);
  441. }
  442.  
  443. void ChangeWarp_f (void)
  444. {
  445.   if (Cmd_Argc() == 1)
  446.   {
  447.      Con_Printf ("GL_WARP is %d\n", gl_warp);
  448.      return;
  449.   }
  450.  
  451.    gl_warp = Q_atof(Cmd_Argv(1)); 
  452. }
  453.  
  454. void VID_Init(unsigned char *palette)
  455. {
  456.   int i;
  457.   char gldir[MAX_OSPATH];
  458.   
  459.     Cvar_RegisterVariable (&gl_ztrick);
  460.     //Cvar_RegisterVariable (&gl_width);
  461.  
  462.   Cmd_AddCommand ("gl_videosync", &ChangeVideoSync_f);
  463.   Cmd_AddCommand ("gl_lockmode", &ChangeLockMode_f);
  464.   Cmd_AddCommand ("gl_dithering", &ChangeDithering_f);
  465.   Cmd_AddCommand ("gl_width", &ChangeVideoMode_f);
  466.   Cmd_AddCommand ("gl_polyoffset", &ChangePolyOffset_f);
  467.   Cmd_AddCommand ("gl_warp", &ChangeWarp_f);
  468.   
  469.   // fog 
  470.  
  471.   if ((i = COM_CheckParm("-fog")) != 0) gl_fog.value = 1;
  472.  
  473.   // texturemode
  474.  
  475.   if ((i = COM_CheckParm("-texturemode")) != 0) {
  476.     if (Q_strcasecmp(com_argv[i+1],"GL_NEAREST") == 0) texture_mode = GL_NEAREST;
  477.     if (Q_strcasecmp(com_argv[i+1],"GL_LINEAR") == 0) texture_mode = GL_LINEAR;
  478.     if (Q_strcasecmp(com_argv[i+1],"GL_NEAREST_MIPMAP_NEAREST") == 0) texture_mode = GL_NEAREST_MIPMAP_NEAREST;
  479.     if (Q_strcasecmp(com_argv[i+1],"GL_NEAREST_MIPMAP_LINEAR") == 0) texture_mode = GL_NEAREST_MIPMAP_LINEAR;
  480.     if (Q_strcasecmp(com_argv[i+1],"GL_LINEAR_MIPMAP_NEAREST") == 0) texture_mode = GL_LINEAR_MIPMAP_NEAREST;
  481.     if (Q_strcasecmp(com_argv[i+1],"GL_LINEAR_MIPMAP_LINEAR") == 0) texture_mode = GL_LINEAR_MIPMAP_LINEAR;
  482.  
  483.     gl_filter_min = texture_mode;
  484.     gl_filter_max = texture_mode;
  485.   }  
  486.   
  487.   // dither
  488.  
  489.   if ((i = COM_CheckParm("-dither")) != 0) {
  490.     if (Q_strcasecmp(com_argv[i+1],"ON") == 0) gl_dithering = GL_TRUE;
  491.     if (Q_strcasecmp(com_argv[i+1],"OFF") == 0) gl_dithering = GL_FALSE;
  492.   }  
  493.   
  494.   // lockmode
  495.  
  496.   if ((i = COM_CheckParm("-lockmode")) != 0) {
  497.     if (Q_strcasecmp(com_argv[i+1],"SMART") == 0) gl_lockmode = MGL_LOCK_SMART;
  498.     if (Q_strcasecmp(com_argv[i+1],"AUTOMATIC") == 0) gl_lockmode = MGL_LOCK_AUTOMATIC;
  499.     if (Q_strcasecmp(com_argv[i+1],"MANUAL") == 0) gl_lockmode = MGL_LOCK_MANUAL;
  500.   }  
  501.   
  502.   // number of buffers
  503.   
  504.   if ((i = COM_CheckParm("-buf")) != 0)
  505.     numb = Q_atoi(com_argv[i+1]);
  506.   
  507.   if (numb < 2) numb = 2;   
  508.   if (numb > 3) numb = 3;   
  509.   
  510.   if (numb == 2) gl_videosync = true;
  511.   else gl_videosync = false;
  512.   
  513.   // width
  514.  
  515.   scr_width = 320; //gl_width.value;
  516.   scr_height = scr_width * 3 / 4;
  517.   
  518.   if ((i = COM_CheckParm("-width")) != 0) {
  519.     scr_width = Q_atoi(com_argv[i+1]);
  520.     scr_height = scr_width * 3 / 4;
  521.   }  
  522.  
  523.   // height
  524.  
  525.   if ((i = COM_CheckParm("-height")) != 0) {
  526.     scr_height = Q_atoi(com_argv[i+1]);
  527.   }  
  528.  
  529.   // bpp
  530.  
  531.   if ((i = COM_CheckParm("-bpp")) != 0) {
  532.     bpp = Q_atoi(com_argv[i+1]);
  533.     if (bpp < 15) bpp = 15;
  534.     if (bpp > 16) bpp = 16;
  535.   }  
  536.   
  537.   // set vid parameters
  538.  
  539.   vid.colormap = host_colormap;
  540.   vid.fullbright = 256 - LittleLong (*((int *)vid.colormap + 2048));
  541.  
  542.   vid.conwidth = scr_width;
  543.   vid.conwidth &= 0xfff8; // make it a multiple of eight
  544.  
  545.   if (vid.conwidth < 320)
  546.     vid.conwidth = 320;
  547.  
  548.   // pick a conheight that matches with correct aspect
  549.   
  550.   vid.conheight = scr_height;
  551.  
  552.   //if ((i = COM_CheckParm("-conwidth")) != 0)
  553.   //  vid.conwidth = Q_atoi(com_argv[i+1]);
  554.   //if ((i = COM_CheckParm("-conheight")) != 0)
  555.   //  vid.conheight = Q_atoi(com_argv[i+1]);
  556.   //if (vid.conheight < 200)
  557.   //  vid.conheight = 200;
  558.  
  559.     vid.width = vid.conwidth;
  560.     vid.height = vid.conheight;
  561.   vid.maxwarpwidth = WARP_WIDTH;
  562.   vid.maxwarpheight = WARP_HEIGHT;
  563.   vid.aspect = ((float)vid.height / (float)vid.width) * (320.0 / 240.0);
  564.     //vid.aspect = 1;
  565.     vid.numpages = 2;
  566.     gl_width.value = vid.width;
  567.  
  568.     Con_Printf("Calling MGLInit...\n");
  569.     MGLInit();
  570.     
  571.     // vertexbuffer
  572.     
  573.     Con_Printf("Setting Vertex Buffer Size to 16000...\n");
  574.   mglChooseVertexBufferSize(16000);
  575.  
  576.     // windowmode
  577.  
  578.   if ((i = COM_CheckParm("-windowmode")) != 0) {
  579.     Con_Printf("Window mode ON\n");
  580.     mglChooseWindowMode(GL_TRUE);
  581.   }   
  582.   else {
  583.     
  584.      Con_Printf("Setting pixel depth to %d...\n",bpp);
  585.        mglChoosePixelDepth(bpp);
  586.  
  587.        Con_Printf("Setting number of buffers to %d...\n", numb);
  588.        mglChooseNumberOfBuffers(numb);
  589.  
  590.      if ((i = COM_CheckParm("-closewb")) != 0) {
  591.        Con_Printf("Closing the Workbench...\n");
  592.        mglProposeCloseDesktop(GL_TRUE);
  593.     }   
  594.   }   
  595.   
  596.     Con_Printf("Creating context...\n");
  597.     if (context = mglCreateContext(0,0, vid.width, vid.height)) {
  598.     
  599.        Con_Printf("Switching sync...\n");
  600.          mglEnableSync(gl_videosync);     
  601.          mglLockMode(gl_lockmode);
  602.      MGLSetState(context,GL_DITHER,gl_dithering);
  603.    
  604.      if (QuakeWindow = mglGetWindowHandle()) ModifyIDCMP(QuakeWindow, IDCMP_RAWKEY|IDCMP_MOUSEBUTTONS);
  605.  
  606.      GL_Init();
  607.      
  608.      mglSetZOffset(gl_polyoffset);
  609.  
  610.      sprintf (gldir, "%s/glquake", com_gamedir);
  611.      Sys_mkdir (gldir);
  612.  
  613.      Check_Gamma(palette);
  614.      VID_SetPalette(palette);
  615.  
  616.      Con_SafePrintf ("Video mode %dx%d initialized.\n", scr_width, scr_height);
  617.  
  618.      vid.recalc_refdef = 1;        // force a surface cache flush
  619.      //vid_menudrawfn = VID_MenuDraw;
  620.      //vid_menukeyfn = VID_MenuKey;
  621.   }   
  622.   else Sys_Error("no context, exiting...\n");
  623. }
  624.  
  625. void Force_CenterView_f (void)
  626. {
  627.   cl.viewangles[PITCH] = 0;
  628. }
  629.  
  630. void Sys_SendKeyEvents (void)
  631. {
  632.   struct IntuiMessage *imsg;
  633.   int kn;
  634.  
  635.   if (QuakeWindow) {
  636.  
  637.     while (imsg = (struct IntuiMessage *)GetMsg(QuakeWindow->UserPort)) {
  638.  
  639.       if (imsg->Class==IDCMP_RAWKEY || imsg->Class==IDCMP_MOUSEBUTTONS) {
  640.         kn = (int)rawkeyconv[imsg->Code & 0x7f];
  641.         if (imsg->Code & IECODE_UP_PREFIX)
  642.           Key_Event(kn,false);
  643.         else
  644.           Key_Event(kn,true);
  645.         if (shutdown_keyboard)
  646.         {
  647.           shutdown_keyboard=0;
  648.           return;
  649.         }
  650.       }
  651.       
  652.       ReplyMsg((struct Message *)imsg);
  653.     }      
  654.   }
  655. }
  656.  
  657. /*
  658. ================
  659. VID_MenuDraw
  660. ================
  661. */
  662.  
  663. #define MAX_COLUMN_SIZE 11
  664. #define MAX_MODEDESCS   33
  665. typedef struct
  666. {
  667.   int     modenum;
  668.   char    *desc;
  669.   int     iscur;
  670. } modedesc_t;
  671. static modedesc_t   modedescs[33];
  672. int numvidmodes=11;
  673. static int vid_wmodes,vid_line;
  674. int vid_column_size;
  675.  
  676. void VID_MenuDraw (void)
  677. {
  678.   qpic_t      *p;
  679.   char        *ptr;
  680.   int         nummodes, i, j, column, row, dup;
  681.   char        temp[100];
  682.  
  683.   p = Draw_CachePic ("gfx/vidmodes.lmp");
  684.   M_DrawPic ( (320-p->width)/2, 4, p);
  685.  
  686.   M_Print (9*8, 36 + MAX_COLUMN_SIZE * 8 + 8*3, "Press Enter to set mode");
  687.   //ptr = VID_GetModeDescription (vid_modenum);
  688.   sprintf (temp, "D to make %s the default", ptr);
  689.   M_Print (6*8, 36 + MAX_COLUMN_SIZE * 8 + 8*5, temp);
  690.   //ptr = VID_GetModeDescription ((int)_vid_default_mode.value);
  691. /*
  692.   if (ptr)
  693.   {
  694.     sprintf (temp, "Current default is %s", ptr);
  695.     M_Print (7*8, 36 + MAX_COLUMN_SIZE * 8 + 8*6, temp);
  696.   }
  697. */
  698.   M_Print (15*8, 36 + MAX_COLUMN_SIZE * 8 + 8*8, "Esc to exit");
  699.  
  700.   row = 36 + (vid_line % vid_column_size) * 8;
  701.   column = 8 + (vid_line / vid_column_size) * 13*8;
  702.  
  703.   M_DrawCharacter (column, row, 12+((int)(realtime*4)&1));
  704. }
  705.  
  706. /*
  707. ================
  708. VID_MenuKey
  709. ================
  710. */
  711. void VID_MenuKey (int key)
  712. {
  713.   switch (key)
  714.   {
  715.     case K_ESCAPE:
  716.       S_LocalSound ("misc/menu1.wav");
  717.       M_Menu_Options_f ();
  718.       break;
  719.   
  720.     case K_UPARROW:
  721.       S_LocalSound ("misc/menu1.wav");
  722.       vid_line--;
  723.   
  724.       if (vid_line < 0) vid_line = vid_wmodes - 1;
  725.       break;
  726.   
  727.     case K_DOWNARROW:
  728.       S_LocalSound ("misc/menu1.wav");
  729.       vid_line++;
  730.  
  731.       if (vid_line >= vid_wmodes)
  732.       vid_line = 0;
  733.       break;
  734.   
  735.     case K_LEFTARROW:
  736.       S_LocalSound ("misc/menu1.wav");
  737.       vid_line -= vid_column_size;
  738.   
  739.       if (vid_line < 0)
  740.       {
  741.         vid_line += ((vid_wmodes + (vid_column_size - 1)) /
  742.                      vid_column_size) * vid_column_size;
  743.   
  744.         while (vid_line >= vid_wmodes)
  745.                vid_line -= vid_column_size;
  746.       }
  747.       break;
  748.   
  749.     case K_RIGHTARROW:
  750.       S_LocalSound ("misc/menu1.wav");
  751.       vid_line += vid_column_size;
  752.   
  753.       if (vid_line >= vid_wmodes)
  754.       {
  755.         vid_line -= ((vid_wmodes + (vid_column_size - 1)) /
  756.                      vid_column_size) * vid_column_size;
  757.   
  758.         while (vid_line < 0)
  759.           vid_line += vid_column_size;
  760.       }
  761.       break;
  762.   
  763.     case K_ENTER:
  764.       S_LocalSound ("misc/menu1.wav");
  765.       Cvar_SetValue("gl_width",modedescs[vid_line].modenum);
  766.       //vid_modenum=modedescs[vid_line].modenum;
  767.       //config_notify=1;
  768.       break;
  769. /*
  770.     case 'D':
  771.     case 'd':
  772.       S_LocalSound ("misc/menu1.wav");
  773.       Cvar_SetValue ("_vid_default_mode", modedescs[vid_line].modenum);
  774.       vid_modenum=modedescs[vid_line].modenum;
  775.       break;
  776. */
  777.     default:
  778.       break;
  779.   }
  780. }
  781.