home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #6 / amigaacscoverdisc1998-061998.iso / games / descent / source / 2d / palette.c < prev    next >
Text File  |  1998-06-08  |  12KB  |  523 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/palette.c $
  15.  * $Revision: 1.41 $
  16.  * $Author: john $
  17.  * $Date: 1995/02/02 14:26:31 $
  18.  *
  19.  * Graphical routines for setting the palette
  20.  *
  21.  * $Log: palette.c $
  22.  * Revision 1.41  1995/02/02  14:26:31  john
  23.  * Made palette fades work better with gamma thingy..
  24.  * 
  25.  * Revision 1.40  1994/12/08  19:03:46  john
  26.  * Made functions use cfile.
  27.  * 
  28.  * Revision 1.39  1994/12/01  11:23:27  john
  29.  * Limited Gamma from 0-8.
  30.  * 
  31.  * Revision 1.38  1994/11/28  01:31:08  mike
  32.  * optimize color lookup function, caching recently used colors.
  33.  * 
  34.  * Revision 1.37  1994/11/18  22:50:18  john
  35.  * Changed shorts to ints in parameters.
  36.  * 
  37.  * Revision 1.36  1994/11/15  17:54:59  john
  38.  * Made text palette fade in when game over.
  39.  * 
  40.  * Revision 1.35  1994/11/10  19:53:14  matt
  41.  * Fixed error handling is gr_use_palette_table()
  42.  * 
  43.  * Revision 1.34  1994/11/07  13:53:48  john
  44.  * Added better gamma stufff.
  45.  * 
  46.  * Revision 1.33  1994/11/07  13:37:56  john
  47.  * Added gamma correction stuff.
  48.  * 
  49.  * Revision 1.32  1994/11/05  13:20:14  john
  50.  * Fixed bug with find_closest_color_current not working.
  51.  * 
  52.  * Revision 1.31  1994/11/05  13:08:09  john
  53.  * MAde it return 0 when palette already faded out.
  54.  * 
  55.  * Revision 1.30  1994/11/05  13:05:34  john
  56.  * Added back in code to allow keys during fade.
  57.  * 
  58.  * Revision 1.29  1994/11/05  12:49:50  john
  59.  * Fixed bug with previous comment..
  60.  * 
  61.  * Revision 1.28  1994/11/05  12:48:46  john
  62.  * Made palette only fade in / out when its supposed to.
  63.  * 
  64.  * Revision 1.27  1994/11/05  12:46:43  john
  65.  * Changed palette stuff a bit.
  66.  * 
  67.  * Revision 1.26  1994/11/01  12:59:35  john
  68.  * Reduced palette.256 size.
  69.  * 
  70.  * Revision 1.25  1994/10/26  23:55:35  john
  71.  * Took out roller; Took out inverse table.
  72.  * 
  73.  * Revision 1.24  1994/10/04  22:03:05  matt
  74.  * Fixed bug: palette wasn't fading all the way out or in
  75.  * 
  76.  * Revision 1.23  1994/09/22  16:08:40  john
  77.  * Fixed some palette stuff.
  78.  * 
  79.  * Revision 1.22  1994/09/19  11:44:31  john
  80.  * Changed call to allocate selector to the dpmi module.
  81.  * 
  82.  * Revision 1.21  1994/09/12  19:28:09  john
  83.  * Fixed bug with unclipped fonts clipping.
  84.  * 
  85.  * Revision 1.20  1994/09/12  18:18:39  john
  86.  * Set 254 and 255 to fade to themselves in fadetable
  87.  * 
  88.  * Revision 1.19  1994/09/12  14:40:10  john
  89.  * Neatend.
  90.  * 
  91.  * Revision 1.18  1994/09/09  09:31:55  john
  92.  * Made find_closest_color not look at superx spot of 254
  93.  * 
  94.  * Revision 1.17  1994/08/09  11:27:08  john
  95.  * Add cthru stuff.
  96.  * 
  97.  * Revision 1.16  1994/08/01  11:03:51  john
  98.  * MAde it read in old/new palette.256
  99.  * 
  100.  * Revision 1.15  1994/07/27  18:30:27  john
  101.  * Took away the blending table.
  102.  * 
  103.  * Revision 1.14  1994/06/09  10:39:52  john
  104.  * In fade out.in functions, returned 1 if key was pressed...
  105.  * 
  106.  * Revision 1.13  1994/05/31  19:04:16  john
  107.  * Added key to stop fade if desired.
  108.  * 
  109.  * Revision 1.12  1994/05/06  12:50:20  john
  110.  * Added supertransparency; neatend things up; took out warnings.
  111.  * 
  112.  * Revision 1.11  1994/05/03  19:39:02  john
  113.  * *** empty log message ***
  114.  * 
  115.  * Revision 1.10  1994/04/22  11:16:07  john
  116.  * *** empty log message ***
  117.  * 
  118.  * Revision 1.9  1994/04/08  16:59:40  john
  119.  * Add fading poly's; Made palette fade 32 instead of 16.
  120.  * 
  121.  * Revision 1.8  1994/03/16  17:21:17  john
  122.  * Added slow palette searching options.
  123.  * 
  124.  * Revision 1.7  1994/01/07  11:47:33  john
  125.  * made use cflib
  126.  * 
  127.  * Revision 1.6  1993/12/21  11:41:04  john
  128.  * *** empty log message ***
  129.  * 
  130.  * Revision 1.5  1993/12/09  15:02:47  john
  131.  * Changed palette stuff majorly
  132.  * 
  133.  * Revision 1.4  1993/12/07  12:31:41  john
  134.  * moved bmd_palette to gr_palette
  135.  * 
  136.  * Revision 1.3  1993/10/15  16:22:23  john
  137.  * *** empty log message ***
  138.  * 
  139.  * Revision 1.2  1993/09/26  18:59:46  john
  140.  * fade stuff
  141.  * 
  142.  * Revision 1.1  1993/09/08  11:44:03  john
  143.  * Initial revision
  144.  * 
  145.  *
  146.  */
  147.  
  148. #include <conio.h>
  149. #include <stdlib.h>
  150. #include <stdio.h>
  151. #include <io.h>
  152.  
  153.  
  154. #include "types.h"
  155. #include "mem.h"
  156. #include "gr.h"
  157. #include "grdef.h"
  158. #include "cfile.h"
  159. #include "error.h"
  160. #include "mono.h"
  161. #include "fix.h"
  162. #include "key.h"
  163.  
  164. extern int gr_installed;
  165.  
  166. ubyte gr_palette[256*3];
  167. ubyte gr_current_pal[256*3];
  168. ubyte gr_fade_table[256*34];
  169.  
  170. ushort gr_palette_selector;
  171. ushort gr_fade_table_selector;
  172.  
  173. ubyte gr_palette_gamma = 0;
  174. int gr_palette_gamma_param = 0;
  175. ubyte gr_palette_faded_out = 1;
  176.  
  177. void gr_palette_set_gamma( int gamma )
  178. {
  179.     if ( gamma < 0 ) gamma = 0;
  180.     if ( gamma > 8 ) gamma = 8;
  181.  
  182.     if (gr_palette_gamma_param != gamma )    {
  183.         gr_palette_gamma_param = gamma;
  184.         gr_palette_gamma = gamma;
  185.         if (!gr_palette_faded_out)
  186.             gr_palette_load( gr_palette );
  187.     }    
  188. }
  189.  
  190. int gr_palette_get_gamma()
  191. {
  192.     return gr_palette_gamma_param;
  193. }
  194.  
  195.  
  196. void gr_use_palette_table( char * filename )
  197. {
  198.     CFILE *fp;
  199.     int i,fsize;
  200.  
  201.     fp = cfopen( filename, "rb" );
  202.     if ( fp==NULL)
  203.         Error("Can't open palette file <%s>",filename);
  204.  
  205.     fsize    = cfilelength( fp );
  206.     Assert( fsize == 9472 );
  207.     cfread( gr_palette, 256*3, 1, fp );
  208.     cfread( gr_fade_table, 256*34, 1, fp );
  209.     cfclose(fp);
  210.  
  211.     // This is the TRANSPARENCY COLOR
  212.     for (i=0; i<GR_FADE_LEVELS; i++ )    {
  213.         gr_fade_table[i*256+255] = 255;
  214.     }
  215.  
  216. }
  217.  
  218. #define SQUARE(x) ((x)*(x))
  219.  
  220. #define    MAX_COMPUTED_COLORS    32
  221.  
  222. int    Num_computed_colors=0;
  223.  
  224. typedef struct {
  225.     ubyte    r,g,b,color_num;
  226. } color_record;
  227.  
  228. color_record Computed_colors[MAX_COMPUTED_COLORS];
  229.  
  230. //    Add a computed color (by gr_find_closest_color) to list of computed colors in Computed_colors.
  231. //    If list wasn't full already, increment Num_computed_colors.
  232. //    If was full, replace a random one.
  233. void add_computed_color(int r, int g, int b, int color_num)
  234. {
  235.     int    add_index;
  236.  
  237.     if (Num_computed_colors < MAX_COMPUTED_COLORS) {
  238.         add_index = Num_computed_colors;
  239.         Num_computed_colors++;
  240.     } else
  241.         add_index = (rand() * MAX_COMPUTED_COLORS) >> 15;
  242.  
  243.     Computed_colors[add_index].r = r;
  244.     Computed_colors[add_index].g = g;
  245.     Computed_colors[add_index].b = b;
  246.     Computed_colors[add_index].color_num = color_num;
  247. }
  248.  
  249. void init_computed_colors(void)
  250. {
  251.     int    i;
  252.  
  253.     for (i=0; i<MAX_COMPUTED_COLORS; i++)
  254.         Computed_colors[i].r = 255;        //    Make impossible to match.
  255. }
  256.  
  257. int gr_find_closest_color( int r, int g, int b )
  258. {
  259.     int i, j;
  260.     int best_value, best_index, value;
  261.  
  262.     if (Num_computed_colors == 0)
  263.         init_computed_colors();
  264.  
  265.     //    If we've already computed this color, return it!
  266.     for (i=0; i<Num_computed_colors; i++)
  267.         if (r == Computed_colors[i].r)
  268.             if (g == Computed_colors[i].g)
  269.                 if (b == Computed_colors[i].b) {
  270.                     if (i > 4) {
  271.                         color_record    trec;
  272.                         trec = Computed_colors[i-1];
  273.                         Computed_colors[i-1] = Computed_colors[i];
  274.                         Computed_colors[i] = trec;
  275.                         return Computed_colors[i-1].color_num;
  276.                     }
  277.                     return Computed_colors[i].color_num;
  278.                 }
  279.  
  280. //    r &= 63;
  281. //    g &= 63;
  282. //    b &= 63;
  283.  
  284.     best_value = SQUARE(r-gr_palette[0])+SQUARE(g-gr_palette[1])+SQUARE(b-gr_palette[2]);
  285.     best_index = 0;
  286.     if (best_value==0) {
  287.         add_computed_color(r, g, b, best_index);
  288.          return best_index;
  289.     }
  290.     j=0;
  291.     // only go to 255, 'cause we dont want to check the transparent color.
  292.     for (i=1; i<254; i++ )    {
  293.         j += 3;
  294.         value = SQUARE(r-gr_palette[j])+SQUARE(g-gr_palette[j+1])+SQUARE(b-gr_palette[j+2]);
  295.         if ( value < best_value )    {
  296.             if (value==0) {
  297.                 add_computed_color(r, g, b, i);
  298.                 return i;
  299.             }
  300.             best_value = value;
  301.             best_index = i;
  302.         }
  303.     }
  304.     add_computed_color(r, g, b, best_index);
  305.     return best_index;
  306. }
  307.  
  308. int gr_find_closest_color_15bpp( int rgb )
  309. {
  310.     return gr_find_closest_color( ((rgb>>10)&31)*2, ((rgb>>5)&31)*2, (rgb&31)*2 );
  311. }
  312.  
  313.  
  314. int gr_find_closest_color_current( int r, int g, int b )
  315. {
  316.     int i, j;
  317.     int best_value, best_index, value;
  318.  
  319. //    r &= 63;
  320. //    g &= 63;
  321. //    b &= 63;
  322.  
  323.     best_value = SQUARE(r-gr_current_pal[0])+SQUARE(g-gr_current_pal[1])+SQUARE(b-gr_current_pal[2]);
  324.     best_index = 0;
  325.     if (best_value==0)
  326.          return best_index;
  327.  
  328.     j=0;
  329.     // only go to 255, 'cause we dont want to check the transparent color.
  330.     for (i=1; i<254; i++ )    {
  331.         j += 3;
  332.         value = SQUARE(r-gr_current_pal[j])+SQUARE(g-gr_current_pal[j+1])+SQUARE(b-gr_current_pal[j+2]);
  333.         if ( value < best_value )    {
  334.             if (value==0)
  335.                 return i;
  336.             best_value = value;
  337.             best_index = i;
  338.         }
  339.     }
  340.     return best_index;
  341. }
  342.  
  343.  
  344. static int last_r=0, last_g=0, last_b=0;
  345.  
  346. void gr_palette_step_up( int r, int g, int b )
  347. {
  348.     int i;
  349.     ubyte *p;
  350.     int temp;
  351.  
  352.     if (gr_palette_faded_out) return;
  353.  
  354.     if ( (r==last_r) && (g==last_g) && (b==last_b) ) return;
  355.  
  356.     last_r = r;
  357.     last_g = g;
  358.     last_b = b;
  359.  
  360.     outp( 0x3c6, 0xff );
  361.     outp( 0x3c8, 0 );
  362.     p=gr_palette;
  363.     for (i=0; i<256; i++ )    {
  364.         temp = (int)(*p++) + r + gr_palette_gamma;
  365.         if (temp<0) temp=0;
  366.         else if (temp>63) temp=63;
  367.         outp( 0x3c9, temp );
  368.         temp = (int)(*p++) + g + gr_palette_gamma;
  369.         if (temp<0) temp=0;
  370.         else if (temp>63) temp=63;
  371.         outp( 0x3c9, temp );
  372.         temp = (int)(*p++) + b + gr_palette_gamma;
  373.         if (temp<0) temp=0;
  374.         else if (temp>63) temp=63;
  375.         outp( 0x3c9, temp );
  376.     }
  377. }
  378.  
  379. void gr_palette_clear()
  380. {
  381.     int i;
  382.     outp( 0x3c6, 0xff );
  383.     outp( 0x3c8, 0 );
  384.     for (i=0; i<768; i++ )    {
  385.         outp( 0x3c9, 0 );
  386.     }
  387.     gr_palette_faded_out = 1;
  388. }
  389.  
  390. void gr_palette_load( ubyte * pal )    
  391. {
  392.     int i;
  393.     ubyte c;
  394.     outp( 0x3c6, 0xff );
  395.     outp( 0x3c8, 0 );
  396.     for (i=0; i<768; i++ )    {
  397.         c = pal[i] + gr_palette_gamma;
  398.         if ( c > 63 ) c = 63;
  399.         outp( 0x3c9,c);
  400.          gr_current_pal[i] = pal[i];
  401.     }
  402.     gr_palette_faded_out = 0;
  403.  
  404.     init_computed_colors();
  405. }
  406.  
  407.  
  408.  
  409. int gr_palette_fade_out(ubyte *pal, int nsteps, int allow_keys )    
  410. {
  411.     ubyte c;
  412.     int i,j;
  413.     fix fade_palette[768];
  414.     fix fade_palette_delta[768];
  415.  
  416.     allow_keys  = allow_keys;
  417.  
  418.     if (gr_palette_faded_out) return 0;
  419.  
  420.     for (i=0; i<768; i++ )    {
  421.         fade_palette[i] = i2f(pal[i]+gr_palette_gamma);
  422.         fade_palette_delta[i] = fade_palette[i] / nsteps;
  423.     }
  424.  
  425.     for (j=0; j<nsteps; j++ )    {
  426.         gr_sync_display();
  427.         outp( 0x3c6, 0xff );
  428.         outp( 0x3c8, 0 );
  429.         for (i=0; i<768; i++ )    {        
  430.             fade_palette[i] -= fade_palette_delta[i];
  431.             if (fade_palette[i] < 0 )
  432.                 fade_palette[i] = 0;
  433.             c = f2i(fade_palette[i]);
  434.             if ( c > 63 ) c = 63;
  435.             outp( 0x3c9, c );                                
  436.         }
  437.     }
  438.     gr_palette_faded_out = 1;
  439.     return 0;
  440. }
  441.  
  442. int gr_palette_fade_in(ubyte *pal, int nsteps, int allow_keys)    
  443. {
  444.     int i,j;
  445.     ubyte c;
  446.     fix fade_palette[768];
  447.     fix fade_palette_delta[768];
  448.  
  449.     allow_keys  = allow_keys;
  450.  
  451.     if (!gr_palette_faded_out) return 0;
  452.  
  453.     for (i=0; i<768; i++ )    {
  454.         gr_current_pal[i] = pal[i];
  455.         fade_palette[i] = 0;
  456.         fade_palette_delta[i] = i2f(pal[i]+gr_palette_gamma) / nsteps;
  457.     }
  458.  
  459.     for (j=0; j<nsteps; j++ )    {
  460.         gr_sync_display();
  461.         outp( 0x3c6, 0xff );
  462.         outp( 0x3c8, 0 );
  463.         for (i=0; i<768; i++ )    {        
  464.             fade_palette[i] += fade_palette_delta[i];
  465.             if (fade_palette[i] > i2f(pal[i]+gr_palette_gamma) )
  466.                 fade_palette[i] = i2f(pal[i]+gr_palette_gamma);
  467.             c = f2i(fade_palette[i]);
  468.             if ( c > 63 ) c = 63;
  469.             outp( 0x3c9, c );                                
  470.         }
  471.     }
  472.     gr_palette_faded_out = 0;
  473.     return 0;
  474. }
  475.  
  476. void gr_make_cthru_table(ubyte * table, ubyte r, ubyte g, ubyte b )
  477. {
  478.     int i;
  479.     ubyte r1, g1, b1;
  480.  
  481.     for (i=0; i<256; i++ )    {
  482.         r1 = gr_palette[i*3+0] + r;
  483.         if ( r1 > 63 ) r1 = 63;
  484.         g1 = gr_palette[i*3+1] + g;
  485.         if ( g1 > 63 ) g1 = 63;
  486.         b1 = gr_palette[i*3+2] + b;
  487.         if ( b1 > 63 ) b1 = 63;
  488.         table[i] = gr_find_closest_color( r1, g1, b1 );
  489.     }
  490. }
  491.  
  492. void gr_palette_read(ubyte * palette)
  493. {
  494.     int i;
  495.     outp( 0x3c6, 0xff );
  496.     outp( 0x3c7, 0 );
  497.     for (i=0; i<768; i++ )    {
  498.         *palette++ = inp( 0x3c9 );
  499.     }
  500. }
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522. 
  523.