home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / 2d / gr.c < prev    next >
Text File  |  1998-06-08  |  19KB  |  727 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13. /*
  14.  * $Source: f:/miner/source/2d/rcs/gr.c $
  15.  * $Revision: 1.56 $
  16.  * $Author: john $
  17.  * $Date: 1995/05/08 11:22:47 $
  18.  *
  19.  * Graphical routines for setting video modes, etc.
  20.  *
  21.  * $Log: gr.c $
  22.  * Revision 1.56  1995/05/08  11:22:47  john
  23.  * Added 320x400 3dbios mode.
  24.  * 
  25.  * Revision 1.55  1995/02/02  16:44:05  john
  26.  * Again with prev.
  27.  * 
  28.  * Revision 1.54  1995/02/02  16:42:18  john
  29.  * Fixed palette with text fading out.
  30.  * 
  31.  * Revision 1.53  1995/02/02  14:26:20  john
  32.  * Made palette fades work better with gamma thingy..
  33.  * 
  34.  * Revision 1.52  1995/02/02  14:08:59  john
  35.  * Made palette gamma reset to 0 before exiting to Dos.
  36.  * 
  37.  * Revision 1.51  1995/01/30  18:06:35  john
  38.  * Added text screen fade in/out, and restored video mode properly.
  39.  * 
  40.  * Revision 1.50  1995/01/24  17:58:16  john
  41.  * Added code to return to 80x25 when graphics close.
  42.  * 
  43.  * Revision 1.49  1994/11/30  20:16:05  john
  44.  * Fixed bug that the grd_curscreen flags were never initialized.
  45.  * 
  46.  * Revision 1.48  1994/11/24  13:24:43  john
  47.  * Made sure that some rep movs had the cld set first.
  48.  * Took some unused functions out.
  49.  * 
  50.  * Revision 1.47  1994/11/18  22:50:20  john
  51.  * Changed shorts to ints in parameters.
  52.  * 
  53.  * Revision 1.46  1994/11/15  18:28:36  john
  54.  * Made text screen fade in.
  55.  * 
  56.  * Revision 1.45  1994/11/15  17:55:11  john
  57.  * Made text palette fade in when game over.
  58.  * 
  59.  * Revision 1.44  1994/11/07  12:07:30  john
  60.  * Made save/restore cursor work a bit better.
  61.  * 
  62.  * Revision 1.43  1994/11/05  12:46:44  john
  63.  * Changed palette stuff a bit.
  64.  * 
  65.  * Revision 1.42  1994/10/26  23:55:50  john
  66.  * Took out roller; Took out inverse table.
  67.  * 
  68.  * Revision 1.41  1994/10/10  13:59:50  john
  69.  * *** empty log message ***
  70.  * 
  71.  * Revision 1.40  1994/10/10  13:58:50  john
  72.  * Added better VGA detection scheme.
  73.  * 
  74.  * Revision 1.39  1994/09/29  10:09:15  john
  75.  * Hacked out VGA card detection for now.
  76.  * 
  77.  * Revision 1.38  1994/09/22  17:35:35  john
  78.  * Fixed bug with palette not reloading in
  79.  * gr_set_mode
  80.  * 
  81.  * Revision 1.37  1994/09/22  16:08:42  john
  82.  * Fixed some palette stuff.
  83.  * 
  84.  * Revision 1.36  1994/09/19  11:44:23  john
  85.  * Changed call to allocate selector to the dpmi module.
  86.  * 
  87.  * Revision 1.35  1994/09/12  19:28:11  john
  88.  * Fixed bug with unclipped fonts clipping.
  89.  * 
  90.  * Revision 1.34  1994/09/12  18:20:18  john
  91.  * Made text fade out
  92.  * 
  93.  * Revision 1.33  1994/09/12  14:40:15  john
  94.  * Neatend.
  95.  * 
  96.  * Revision 1.32  1994/08/15  15:01:01  matt
  97.  * Set ptr to NULL after freeing
  98.  * 
  99.  * Revision 1.31  1994/07/27  18:30:28  john
  100.  * Took away the blending table.
  101.  * 
  102.  * Revision 1.30  1994/06/24  17:26:59  john
  103.  * Made rowsizes bigger than actual screen work with SVGA.
  104.  * 
  105.  * Revision 1.29  1994/05/12  17:33:16  john
  106.  * Added circle code.
  107.  * 
  108.  * Revision 1.28  1994/05/10  19:51:49  john
  109.  * Added 320x100 mode.
  110.  * 
  111.  * Revision 1.27  1994/05/06  12:50:23  john
  112.  * Added supertransparency; neatend things up; took out warnings.
  113.  * 
  114.  * Revision 1.26  1994/05/03  19:39:00  john
  115.  * *** empty log message ***
  116.  * 
  117.  * Revision 1.25  1994/04/08  16:59:34  john
  118.  * Add fading poly's; Made palette fade 32 instead of 16.
  119.  * 
  120.  * Revision 1.24  1994/03/14  16:56:16  john
  121.  * Changed grs_bitmap structure to include bm_flags.
  122.  * 
  123.  * Revision 1.23  1994/02/18  15:32:27  john
  124.  * *** empty log message ***
  125.  * 
  126.  * Revision 1.22  1994/01/25  11:40:44  john
  127.  * Added gr_check_mode function.
  128.  * 
  129.  * Revision 1.21  1993/12/21  19:57:48  john
  130.  * added selector stuff.
  131.  * 
  132.  * Revision 1.20  1993/12/21  11:40:32  john
  133.  * *** empty log message ***
  134.  * 
  135.  * Revision 1.19  1993/12/09  15:02:13  john
  136.  * Changed palette stuff majorly
  137.  * 
  138.  * Revision 1.18  1993/11/16  11:28:36  john
  139.  * *** empty log message ***
  140.  * 
  141.  * Revision 1.17  1993/10/26  13:17:53  john
  142.  * *** empty log message ***
  143.  * 
  144.  * Revision 1.16  1993/10/15  16:23:42  john
  145.  * y
  146.  * 
  147.  * Revision 1.15  1993/09/29  16:15:21  john
  148.  * optimized
  149.  * 
  150.  * Revision 1.14  1993/09/28  19:06:51  john
  151.  * made gr_set_mode change the grd_cursreen->sc_mode variable.
  152.  * 
  153.  * Revision 1.13  1993/09/28  12:52:12  matt
  154.  * Set aspect ratio of current screen in gr_init() and gr_set_mode().
  155.  * 
  156.  * Revision 1.12  1993/09/27  13:00:24  john
  157.  * made gr_set_mode not call mode_reset on fail
  158.  * 
  159.  * Revision 1.11  1993/09/27  12:51:27  john
  160.  * fixed gr_set_mode to return values
  161.  * 
  162.  * Revision 1.10  1993/09/26  18:59:12  john
  163.  * fade in/out stuff
  164.  * 
  165.  * Revision 1.9  1993/09/21  14:00:41  john
  166.  * added code to save 43/50 line modes.
  167.  * 
  168.  * Revision 1.8  1993/09/20  14:48:48  john
  169.  * *** empty log message ***
  170.  * 
  171.  * Revision 1.7  1993/09/16  17:27:48  john
  172.  * Added code to save/restore video mode.
  173.  * 
  174.  * Revision 1.6  1993/09/16  16:30:15  john
  175.  * Made gr_close retore Mode 3 always.
  176.  * 
  177.  * Revision 1.5  1993/09/14  18:40:07  john
  178.  * Made it so that gr_setmode doesn't change curcanv font and colors.
  179.  * 
  180.  * Revision 1.4  1993/09/14  16:27:01  matt
  181.  * Changes gr_change_mode() to be gr_set_mode()
  182.  * After gr_set_mode(), grd_curcanv is the canvas of the new screen
  183.  * Made gr_set_mode() work with the new grd_curcanv, not the old one
  184.  * 
  185.  * Revision 1.3  1993/09/14  13:08:37  john
  186.  * Added gr_changemode
  187.  * 
  188.  * Revision 1.2  1993/09/08  17:36:37  john
  189.  * Looking for error for Yuan... Neatened the nested ifs in setmode.
  190.  * 
  191.  * Revision 1.1  1993/09/08  11:43:51  john
  192.  * Initial revision
  193.  * 
  194.  *
  195.  */
  196.  
  197. #include <dos.h>
  198. #include <stdlib.h>
  199. #include <malloc.h>
  200. #include <stdio.h>
  201. #include <conio.h>
  202. #include <string.h>
  203.  
  204. #include "types.h"
  205. #include "mem.h"
  206. #include "gr.h"
  207. #include "grdef.h"
  208. #include "error.h"
  209. #include "mono.h"
  210. #include "dpmi.h"
  211. #include "palette.h"
  212. #include "dpmi.h"
  213.  
  214. unsigned char * gr_video_memory = (unsigned char *)0xA0000;
  215.  
  216. char gr_pal_default[768];
  217.  
  218. int gr_installed = 0;
  219.  
  220. ubyte * pVideoMode =  (volatile ubyte *)0x449;
  221. ushort * pNumColumns = (volatile ushort *)0x44a;
  222. ubyte * pNumRows = (volatile ubyte *)0x484;
  223. ushort * pCharHeight = (volatile ushort *)0x485;
  224. ushort * pCursorPos = (volatile ushort *)0x450;
  225. ushort * pCursorType = (volatile ushort *)0x460;
  226. ushort * pTextMemory = (volatile ushort *)0xb8000;
  227.  
  228. typedef struct screen_save {
  229.     ubyte     video_mode;
  230.     ubyte     is_graphics;
  231.     ushort    char_height;
  232.     ubyte        width;
  233.     ubyte        height;
  234.     ubyte        cursor_x, cursor_y;
  235.     ubyte        cursor_sline, cursor_eline;
  236.     ushort * video_memory;
  237. } screen_save;
  238.  
  239. screen_save gr_saved_screen;
  240.  
  241. int gr_show_screen_info = 0;
  242.  
  243. void gr_set_cellheight( ubyte height )
  244. {
  245.     ubyte temp;
  246.  
  247.    outp( 0x3d4, 9 );
  248.     temp = inp( 0x3d5 );
  249.    temp &= 0xE0;
  250.     temp |= height;
  251.     outp( 0x3d5, temp );
  252. }
  253.  
  254. void gr_set_linear()
  255. {
  256.     outpw( 0x3c4, 0xE04 );          // enable chain4 mode
  257.     outpw( 0x3d4, 0x4014 );          // turn on dword mode
  258.     outpw( 0x3d4, 0xa317 );          // turn off byte mode
  259. }
  260.  
  261. void gr_16_to_256()
  262. {
  263.     outpw( 0x3ce, 0x4005 );         // set Shift reg to 1
  264.  
  265.     inp( 0x3da );                    // dummy input
  266.  
  267.     outp( 0x3c0, 0x30 );
  268.     outp( 0x3c0, 0x61 );           // turns on PCS & PCC
  269.  
  270.     inp( 0x3da );                    // dummy input
  271.  
  272.     outp( 0x3c0, 0x33 );
  273.     outp( 0x3c0, 0 );
  274. }
  275.  
  276. void gr_turn_screen_off()
  277. {
  278.     ubyte temp;
  279.     temp = inp( 0x3da );
  280.     outp( 0x3c0, 0 );
  281. }
  282.  
  283. void gr_turn_screen_on()
  284. {
  285.     ubyte temp;
  286.     temp = inp( 0x3da );
  287.     outp( 0x3c0, 0x20 );
  288. }
  289.  
  290. void gr_set_misc_mode( uint mode )
  291. {
  292.     union REGS regs;
  293.  
  294.     memset( ®s, 0, sizeof(regs) );
  295.     regs.w.ax = mode;
  296.     int386( 0x10, ®s, ®s );
  297.  
  298. }
  299.  
  300. void gr_set_3dbios_mode( uint mode )
  301. {
  302.     union REGS regs;
  303.     memset( ®s, 0, sizeof(regs) );
  304.     regs.w.ax = 0x4fd0;
  305.     regs.w.bx = 0x3d00 | (mode & 0xff);
  306.     int386( 0x10, ®s, ®s );
  307. }
  308.  
  309.  
  310.  
  311. void gr_set_text_25()
  312. {
  313.     union REGS regs;
  314.  
  315.     regs.w.ax = 3;
  316.     int386( 0x10, ®s, ®s );
  317.  
  318. }
  319.  
  320. void gr_set_text_43()
  321. {
  322.     union REGS regs;
  323.  
  324.     regs.w.ax = 0x1201;
  325.     regs.w.bx = 0x30;
  326.     int386( 0x10, ®s, ®s );
  327.  
  328.     regs.w.ax = 3;
  329.     int386( 0x10, ®s, ®s );
  330.  
  331.     regs.w.ax = 0x1112;
  332.     regs.w.bx = 0x0;
  333.     int386( 0x10, ®s, ®s );
  334. }
  335.  
  336. void gr_set_text_50()
  337. {
  338.     union REGS regs;
  339.  
  340.     regs.w.ax = 0x1202;
  341.     regs.w.bx = 0x30;
  342.     int386( 0x10, ®s, ®s );
  343.  
  344.     regs.w.ax = 3;
  345.     int386( 0x10, ®s, ®s );
  346.  
  347.     regs.w.ax = 0x1112;
  348.     regs.w.bx = 0x0;
  349.     int386( 0x10, ®s, ®s );
  350. }
  351.  
  352. ubyte is_graphics_mode()
  353. {
  354.     byte tmp;
  355.     tmp = inp( 0x3DA );        // Reset flip-flip
  356.     outp( 0x3C0, 0x30 );        // Select attr register 10
  357.     tmp = inp( 0x3C1 );    // Get graphics/text bit
  358.     return tmp & 1;
  359. }
  360.  
  361. void gr_setcursor(ubyte x, ubyte y, ubyte sline, ubyte eline)
  362. {
  363.     union REGS regs;
  364.  
  365.     memset( ®s, 0, sizeof(regs) );
  366.     regs.w.ax = 0x0200;
  367.     regs.w.bx = 0;
  368.     regs.h.dh = y;
  369.     regs.h.dl = x;
  370.     int386( 0x10, ®s, ®s );
  371.  
  372.     memset( ®s, 0, sizeof(regs) );
  373.     regs.w.ax = 0x0100;
  374.     regs.h.ch = sline & 0xf;
  375.     regs.h.cl = eline & 0xf;
  376.     int386( 0x10, ®s, ®s );
  377. }
  378.  
  379. void gr_getcursor(ubyte *x, ubyte *y, ubyte * sline, ubyte * eline)
  380. {
  381.     union REGS regs;
  382.  
  383.     memset( ®s, 0, sizeof(regs) );
  384.     regs.w.ax = 0x0300;
  385.     regs.w.bx = 0;
  386.     int386( 0x10, ®s, ®s );
  387.     *y = regs.h.dh;
  388.     *x = regs.h.dl;
  389.     *sline = regs.h.ch;
  390.     *eline = regs.h.cl;
  391. }
  392.  
  393.  
  394. int gr_save_mode()
  395. {
  396.     int i;
  397.  
  398.     gr_saved_screen.is_graphics = is_graphics_mode();
  399.     gr_saved_screen.video_mode = *pVideoMode;
  400.     
  401.     if (!gr_saved_screen.is_graphics)    {
  402.         gr_saved_screen.width = *pNumColumns;
  403.         gr_saved_screen.height = *pNumRows+1;
  404.         gr_saved_screen.char_height = *pCharHeight;
  405.         gr_getcursor(&gr_saved_screen.cursor_x, &gr_saved_screen.cursor_y, &gr_saved_screen.cursor_sline, &gr_saved_screen.cursor_eline );
  406.         //MALLOC(gr_saved_screen.video_memory,ushort, gr_saved_screen.width*gr_saved_screen.height );//Hack by Krb
  407.         gr_saved_screen.video_memory=(ushort *)malloc((gr_saved_screen.width*gr_saved_screen.height)*sizeof(ushort));
  408.         for (i=0; i < gr_saved_screen.width*gr_saved_screen.height; i++ )
  409.             gr_saved_screen.video_memory[i] = pTextMemory[i];
  410.     }
  411.  
  412.     if (gr_show_screen_info )    {
  413.         printf( "Current video mode 0x%x:\n",  gr_saved_screen.video_mode );
  414.         if (gr_saved_screen.is_graphics)
  415.             printf( "Graphics mode\n" );
  416.         else    {
  417.             printf( "Text mode\n" );
  418.             printf( "( %d columns by %d rows)\n", gr_saved_screen.width, gr_saved_screen.height );
  419.             printf( "Char height is %d pixel rows\n", gr_saved_screen.char_height );
  420.             printf( "Cursor of type 0x%x,0x%x is at (%d, %d)\n", gr_saved_screen.cursor_sline, gr_saved_screen.cursor_eline,gr_saved_screen.cursor_x, gr_saved_screen.cursor_y );
  421.         }
  422.     }
  423.  
  424.     return 0;
  425. }
  426.  
  427. int isvga()
  428. {
  429.     union REGS regs;
  430.  
  431.     memset( ®s, 0, sizeof(regs) );
  432.     regs.w.ax = 0x1a00;
  433.     int386( 0x10, ®s, ®s );
  434.  
  435.     if ( regs.h.al == 0x1a )
  436.          return 1;
  437.  
  438.     return 0;
  439. }
  440.  
  441. void gr_restore_mode()
  442. {
  443.     int i;
  444.  
  445.     //gr_set_text_25(); 
  446.  
  447.     gr_palette_fade_out( gr_palette, 32, 0 );
  448.     gr_palette_set_gamma(0);
  449.  
  450.     if ( gr_saved_screen.video_mode == 3 )    {
  451.         switch( gr_saved_screen.height )      {
  452.         case 43:    gr_set_text_43(); break;
  453.         case 50:    gr_set_text_50(); break;
  454.         default:    gr_set_text_25(); break;
  455.         }
  456.     } else {
  457.         gr_set_misc_mode(gr_saved_screen.video_mode);    
  458.     }
  459.  
  460.     if (gr_saved_screen.is_graphics==0)    {
  461.         gr_sync_display();
  462.         gr_sync_display();
  463.         gr_palette_read( gr_pal_default );
  464.         gr_palette_clear();
  465.  
  466.         for (i=0; i < gr_saved_screen.width*gr_saved_screen.height; i++ )    
  467.             pTextMemory[i]=gr_saved_screen.video_memory[i];
  468.         gr_setcursor( gr_saved_screen.cursor_x, gr_saved_screen.cursor_y, gr_saved_screen.cursor_sline, gr_saved_screen.cursor_eline );
  469.         gr_palette_faded_out = 1;
  470.         gr_palette_fade_in( gr_pal_default, 32, 0 );
  471.     }
  472.  
  473. }
  474.  
  475. int gr_close()
  476. {
  477.     if (gr_installed==1)
  478.     {
  479.         gr_installed = 0;
  480.         gr_restore_mode();
  481.         free(grd_curscreen);
  482.           if( gr_saved_screen.video_memory ) {
  483.             free(gr_saved_screen.video_memory);
  484.             gr_saved_screen.video_memory = NULL;
  485.         }
  486.     }
  487.  
  488.     return 0;
  489. }
  490.  
  491. int gr_vesa_setmode( int mode )
  492. {
  493.     int retcode;
  494.  
  495.     retcode=gr_vesa_checkmode( mode );
  496.     if ( retcode ) return retcode;
  497.  
  498.     return gr_vesa_setmodea( mode );
  499. }
  500.  
  501.  
  502. int gr_set_mode(int mode)
  503. {
  504.     int retcode;
  505.     unsigned int w,h,t,data, r;
  506.  
  507.     //JOHNgr_disable_default_palette_loading();
  508.  
  509.     switch(mode)
  510.     {
  511.     case SM_ORIGINAL:
  512.         return 0;
  513.     case 0:
  514.         if (!isvga()) return 1;
  515.         gr_set_misc_mode(0x13);    
  516.         w = 320; r = 320; h = 200; t=BM_LINEAR; data = 0xA0000;
  517.         break;
  518.     case SM_640x400V:
  519.         retcode = gr_vesa_setmode( 0x100 ); 
  520.         if (retcode !=0 ) return retcode;
  521.         w = 640; r = 640; h = 400; t=BM_SVGA; data = 0;
  522.         break;
  523.     case SM_640x480V:
  524.         retcode = gr_vesa_setmode( 0x101 ); 
  525.         if (retcode !=0 ) return retcode;
  526.         w = 640; r = 640; h = 480; t=BM_SVGA; data = 0;
  527.         break;
  528.     case SM_800x600V:
  529.         retcode = gr_vesa_setmode( 0x103 ); 
  530.         if (retcode !=0 ) return retcode;
  531.         w = 800; r = 800; h = 600; t=BM_SVGA; data = 0;
  532.         break;
  533.     case SM_1024x768V:
  534.         retcode = gr_vesa_setmode( 0x105 ); 
  535.         if (retcode !=0 ) return retcode;
  536.         w = 1024; r = 1024; h = 768; t=BM_SVGA; data = 0;
  537.         break;
  538.     case SM_640x480V15:
  539.         retcode = gr_vesa_setmode( 0x110 ); 
  540.         if (retcode !=0 ) return retcode;
  541.         w = 640; r = 640*2; h=480; t=BM_SVGA15; data = 0;
  542.         break;
  543.     case SM_800x600V15:
  544.         retcode = gr_vesa_setmode( 0x113 ); 
  545.         if (retcode !=0 ) return retcode;
  546.         w = 800; r = 800*2; h=600; t=BM_SVGA15; data = 0;
  547.         break;
  548.     case 19:
  549.         if (!isvga()) return 1;
  550.         gr_set_misc_mode(0x13);    
  551. //        {
  552. //            ubyte x;
  553. //            x = inp( 0x3c5 );
  554. //            x |= 8;
  555. //            outp( 0x3c5, x );
  556. //        }
  557.         gr_set_cellheight( 3 );
  558.  
  559.         w = 320; r = 320; h = 100; t=BM_LINEAR; data = 0xA0000;
  560.         break;
  561.     case 20:
  562.         retcode = gr_vesa_setmode( 0x102 ); 
  563.         //gr_enable_default_palette_loading();
  564.         if (retcode !=0 ) return retcode;
  565.         gr_16_to_256();
  566.         gr_set_linear();
  567.         //gr_set_cellheight( 1 );
  568.         gr_vesa_setlogical( 400 );
  569.         w = 400; r = 400; h = 600; t=BM_SVGA; data = 0;
  570.         break;
  571.     case 21:
  572.         if (!isvga()) return 1;
  573.         gr_set_misc_mode(0xd);    
  574.         gr_16_to_256();
  575.         gr_set_linear();
  576.         gr_set_cellheight( 3 );
  577.         w = 160; r = 160; h = 100; t=BM_LINEAR; data = 0xA0000;
  578.         break;
  579.     case 22:            // 3dmax 320x400
  580.         if (!isvga()) return 1;
  581.         gr_set_3dbios_mode(0x31);
  582.         //w = 320; r = 320/4; h = 400; t=BM_MODEX; data = 0;
  583.         w = 320; r = 320; h = 400; t=BM_SVGA; data = 0;
  584.         break;
  585.     default:
  586.         if (!isvga()) return 1;
  587.         w = gr_modex_setmode( mode );
  588.         //gr_enable_default_palette_loading();
  589.         h = w & 0xffff; w = w >> 16; r = w / 4;t = BM_MODEX; data = 0;
  590.         break;
  591.     }
  592.     gr_palette_clear();
  593.  
  594.     memset( grd_curscreen, 0, sizeof(grs_screen));
  595.     grd_curscreen->sc_mode = mode;
  596.     grd_curscreen->sc_w = w;
  597.     grd_curscreen->sc_h = h;
  598.     grd_curscreen->sc_aspect = fixdiv(grd_curscreen->sc_w*3,grd_curscreen->sc_h*4);
  599.     grd_curscreen->sc_canvas.cv_bitmap.bm_x = 0;
  600.     grd_curscreen->sc_canvas.cv_bitmap.bm_y = 0;
  601.     grd_curscreen->sc_canvas.cv_bitmap.bm_w = w;
  602.     grd_curscreen->sc_canvas.cv_bitmap.bm_h = h;
  603.     grd_curscreen->sc_canvas.cv_bitmap.bm_rowsize = r;
  604.     grd_curscreen->sc_canvas.cv_bitmap.bm_type = t;
  605.     grd_curscreen->sc_canvas.cv_bitmap.bm_data = (unsigned char *)data;
  606.     gr_set_current_canvas(NULL);
  607.  
  608.     //gr_enable_default_palette_loading();
  609.  
  610.     return 0;
  611. }
  612.  
  613. int gr_init(int mode)
  614. {
  615.     int org_gamma;
  616.     int retcode;
  617.  
  618.     // Only do this function once!
  619.     if (gr_installed==1)
  620.         return 1;
  621.  
  622.     if (gr_init_A0000())
  623.         return 10;
  624.  
  625.     // Save the current text screen mode
  626.     if (gr_save_mode()==1)
  627.         return 1;
  628.  
  629.     // Save the current palette, and fade it out to black.
  630.     gr_palette_read( gr_pal_default );
  631.     gr_palette_faded_out = 0;
  632.     org_gamma = gr_palette_get_gamma();
  633.     gr_palette_set_gamma( 0 );
  634.     gr_palette_fade_out( gr_pal_default, 32, 0 );
  635.     gr_palette_clear();
  636.     gr_palette_set_gamma( org_gamma );
  637.     gr_sync_display();
  638.     gr_sync_display();
  639.  
  640.     //MALLOC( grd_curscreen,grs_screen,1 );//Hack by KRB
  641.     grd_curscreen=(grs_screen*)malloc(1*sizeof(grs_screen));
  642.     memset( grd_curscreen, 0, sizeof(grs_screen));
  643.  
  644.     // Set the mode.
  645.     if (retcode=gr_set_mode(mode))
  646.     {
  647.         gr_restore_mode();
  648.         return retcode;
  649.     }
  650.     //JOHNgr_disable_default_palette_loading();
  651.  
  652.     // Set all the screen, canvas, and bitmap variables that
  653.     // aren't set by the gr_set_mode call:
  654.     grd_curscreen->sc_canvas.cv_color = 0;
  655.     grd_curscreen->sc_canvas.cv_drawmode = 0;
  656.     grd_curscreen->sc_canvas.cv_font = NULL;
  657.     grd_curscreen->sc_canvas.cv_font_fg_color = 0;
  658.     grd_curscreen->sc_canvas.cv_font_bg_color = 0;
  659.     gr_set_current_canvas( &grd_curscreen->sc_canvas );
  660.  
  661.     if (!dpmi_allocate_selector( &gr_fade_table, 256*GR_FADE_LEVELS, &gr_fade_table_selector ))
  662.         Error( "Error allocating fade table selector!" );
  663.  
  664.     if (!dpmi_allocate_selector( &gr_palette, 256*3, &gr_palette_selector ))
  665.         Error( "Error allocating palette selector!" );
  666.  
  667. //    if (!dpmi_allocate_selector( &gr_inverse_table, 32*32*32, &gr_inverse_table_selector ))
  668. //        Error( "Error allocating inverse table selector!" );
  669.  
  670.  
  671.     // Set flags indicating that this is installed.
  672.     gr_installed = 1;
  673.     atexit(gr_close);
  674.  
  675.     return 0;
  676. }
  677.  
  678. int gr_mode13_checkmode()
  679. {
  680.     if (isvga()) 
  681.         return 0;
  682.     else
  683.         return 1;
  684. }
  685.  
  686. //  0=Mode set OK
  687. //  1=No VGA adapter installed
  688. //  2=Program doesn't support this VESA granularity
  689. //  3=Monitor doesn't support that VESA mode.:
  690. //  4=Video card doesn't support that VESA mode.
  691. //  5=No VESA driver found.
  692. //  6=Bad Status after VESA call/
  693. //  7=Not enough DOS memory to call VESA functions.
  694. //  8=Error using DPMI.
  695. //  9=Error setting logical line width.
  696. // 10=Error allocating selector for A0000h
  697. // 11=Not a valid mode support by gr.lib
  698.  
  699. int gr_check_mode(int mode)
  700. {
  701.     switch(mode)
  702.     {
  703.     case 19:
  704.     case SM_320x200C:
  705.     case SM_320x200U:
  706.     case SM_320x240U:
  707.     case SM_360x200U:
  708.     case SM_360x240U:
  709.     case SM_376x282U:
  710.     case SM_320x400U:
  711.     case SM_320x480U:
  712.     case SM_360x400U:
  713.     case SM_360x480U:
  714.     case SM_360x360U:
  715.     case SM_376x308U:
  716.     case SM_376x564U:        return gr_mode13_checkmode();
  717.     case SM_640x400V:        return gr_vesa_checkmode( 0x100 ); 
  718.     case SM_640x480V:     return gr_vesa_checkmode( 0x101 ); 
  719.     case SM_800x600V:     return gr_vesa_checkmode( 0x103 ); 
  720.     case SM_1024x768V:    return gr_vesa_setmode( 0x105 ); 
  721.     case SM_640x480V15:    return gr_vesa_setmode( 0x110 ); 
  722.     case SM_800x600V15:    return gr_vesa_setmode( 0x113 ); 
  723.     }
  724.     return 11;
  725. }
  726. 
  727.