]> icculus.org git repositories - btb/d2x.git/blob - main/texmerge.c
cvars for weapon ordering
[btb/d2x.git] / main / texmerge.c
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-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  *
16  * Routines to cache merged textures.
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #include <stdio.h>
25
26 #include "gr.h"
27 #include "error.h"
28 #include "game.h"
29 #include "textures.h"
30 #include "mono.h"
31 #include "rle.h"
32 #include "piggy.h"
33
34 #ifdef OGL
35 #include "ogl_init.h"
36 #define MAX_NUM_CACHE_BITMAPS 200
37 #else
38 #define MAX_NUM_CACHE_BITMAPS 50
39 #endif
40
41 //static grs_bitmap * cache_bitmaps[MAX_NUM_CACHE_BITMAPS];                     
42
43 typedef struct  {
44         grs_bitmap * bitmap;
45         grs_bitmap * bottom_bmp;
46         grs_bitmap * top_bmp;
47         int             orient;
48         int             last_frame_used;
49 } TEXTURE_CACHE;
50
51 static TEXTURE_CACHE Cache[MAX_NUM_CACHE_BITMAPS];
52
53 static int num_cache_entries = 0;
54
55 static int cache_hits = 0;
56 static int cache_misses = 0;
57
58 void texmerge_close();
59 void merge_textures_super_xparent(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
60                                                                                          ubyte *dest_data);
61 void merge_textures_new(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
62                                                                 ubyte *dest_data);
63
64 //----------------------------------------------------------------------
65
66 int texmerge_init(int num_cached_textures)
67 {
68         int i;
69
70         if ( num_cached_textures <= MAX_NUM_CACHE_BITMAPS )
71                 num_cache_entries = num_cached_textures;
72         else
73                 num_cache_entries = MAX_NUM_CACHE_BITMAPS;
74         
75         for (i=0; i<num_cache_entries; i++ )    {
76                         // Make temp tmap for use when combining
77                 Cache[i].bitmap = gr_create_bitmap( 64, 64 );
78
79                 //if (get_selector( Cache[i].bitmap->bm_data, 64*64,  &Cache[i].bitmap->bm_selector))
80                 //      Error( "ERROR ALLOCATING CACHE BITMAP'S SELECTORS!!!!" );
81
82                 Cache[i].last_frame_used = -1;
83                 Cache[i].top_bmp = NULL;
84                 Cache[i].bottom_bmp = NULL;
85                 Cache[i].orient = -1;
86         }
87         atexit( texmerge_close );
88
89         return 1;
90 }
91
92 void texmerge_flush()
93 {
94         int i;
95
96         for (i=0; i<num_cache_entries; i++ )    {
97                 Cache[i].last_frame_used = -1;
98                 Cache[i].top_bmp = NULL;
99                 Cache[i].bottom_bmp = NULL;
100                 Cache[i].orient = -1;
101         }
102 }
103
104
105 //-------------------------------------------------------------------------
106 void texmerge_close()
107 {
108         int i;
109
110         for (i=0; i<num_cache_entries; i++ )    {
111                 gr_free_bitmap( Cache[i].bitmap );
112                 Cache[i].bitmap = NULL;
113         }
114 }
115
116 //--unused-- int info_printed = 0;
117
118 grs_bitmap * texmerge_get_cached_bitmap( int tmap_bottom, int tmap_top )
119 {
120         grs_bitmap *bitmap_top, *bitmap_bottom;
121         int i, orient;
122         int lowest_frame_count;
123         int least_recently_used;
124
125 //      if ( ((FrameCount % 1000)==0) && ((cache_hits+cache_misses)>0) && (!info_printed) )     {
126 //              mprintf( 0, "Texmap caching:  %d hits, %d misses. (Missed=%d%%)\n", cache_hits, cache_misses, (cache_misses*100)/(cache_hits+cache_misses)  );
127 //              info_printed = 1;
128 //      } else {
129 //              info_printed = 0;
130 //      }
131
132         bitmap_top = &GameBitmaps[Textures[tmap_top&0x3FFF].index];
133         bitmap_bottom = &GameBitmaps[Textures[tmap_bottom].index];
134         
135         orient = ((tmap_top&0xC000)>>14) & 3;
136
137         least_recently_used = 0;
138         lowest_frame_count = Cache[0].last_frame_used;
139         
140         for (i=0; i<num_cache_entries; i++ )    {
141                 if ( (Cache[i].last_frame_used > -1) && (Cache[i].top_bmp==bitmap_top) && (Cache[i].bottom_bmp==bitmap_bottom) && (Cache[i].orient==orient ))   {
142                         cache_hits++;
143                         Cache[i].last_frame_used = FrameCount;
144                         return Cache[i].bitmap;
145                 }       
146                 if ( Cache[i].last_frame_used < lowest_frame_count )    {
147                         lowest_frame_count = Cache[i].last_frame_used;
148                         least_recently_used = i;
149                 }
150         }
151
152         //---- Page out the LRU bitmap;
153         cache_misses++;
154
155         // Make sure the bitmaps are paged in...
156 #ifdef PIGGY_USE_PAGING
157         piggy_page_flushed = 0;
158
159         PIGGY_PAGE_IN(Textures[tmap_top&0x3FFF]);
160         PIGGY_PAGE_IN(Textures[tmap_bottom]);
161         if (piggy_page_flushed) {
162                 // If cache got flushed, re-read 'em.
163                 piggy_page_flushed = 0;
164                 PIGGY_PAGE_IN(Textures[tmap_top&0x3FFF]);
165                 PIGGY_PAGE_IN(Textures[tmap_bottom]);
166         }
167         Assert( piggy_page_flushed == 0 );
168 #endif
169
170 #ifdef OGL
171         ogl_freebmtexture(Cache[least_recently_used].bitmap);
172 #endif
173
174
175         if (bitmap_top->bm_flags & BM_FLAG_SUPER_TRANSPARENT)   {
176                 merge_textures_super_xparent( orient, bitmap_bottom, bitmap_top, Cache[least_recently_used].bitmap->bm_data );
177                 Cache[least_recently_used].bitmap->bm_flags = BM_FLAG_TRANSPARENT;
178                 Cache[least_recently_used].bitmap->avg_color = bitmap_top->avg_color;
179         } else  {
180                 merge_textures_new( orient, bitmap_bottom, bitmap_top, Cache[least_recently_used].bitmap->bm_data );
181                 Cache[least_recently_used].bitmap->bm_flags = bitmap_bottom->bm_flags & (~BM_FLAG_RLE);
182                 Cache[least_recently_used].bitmap->avg_color = bitmap_bottom->avg_color;
183         }
184
185         Cache[least_recently_used].top_bmp = bitmap_top;
186         Cache[least_recently_used].bottom_bmp = bitmap_bottom;
187         Cache[least_recently_used].last_frame_used = FrameCount;
188         Cache[least_recently_used].orient = orient;
189
190         return Cache[least_recently_used].bitmap;
191 }
192
193 void merge_textures_new( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
194 {
195         ubyte * top_data, *bottom_data;
196
197         if ( top_bmp->bm_flags & BM_FLAG_RLE )
198                 top_bmp = rle_expand_texture(top_bmp);
199
200         if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
201                 bottom_bmp = rle_expand_texture(bottom_bmp);
202
203 //      Assert( bottom_bmp != top_bmp );
204
205         top_data = top_bmp->bm_data;
206         bottom_data = bottom_bmp->bm_data;
207
208 //      Assert( bottom_data != top_data );
209
210         // mprintf( 0, "Type=%d\n", type );
211
212         switch( type )  {
213         case 0:
214                 // Normal
215
216                 gr_merge_textures( bottom_data, top_data, dest_data );
217                 break;
218         case 1:
219                 gr_merge_textures_1( bottom_data, top_data, dest_data );
220                 break;
221         case 2:
222                 gr_merge_textures_2( bottom_data, top_data, dest_data );
223                 break;
224         case 3:
225                 gr_merge_textures_3( bottom_data, top_data, dest_data );
226                 break;
227         }
228 }
229
230 void merge_textures_super_xparent( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
231 {
232         ubyte c;
233         int x,y;
234
235         ubyte * top_data, *bottom_data;
236
237         if ( top_bmp->bm_flags & BM_FLAG_RLE )
238                 top_bmp = rle_expand_texture(top_bmp);
239
240         if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
241                 bottom_bmp = rle_expand_texture(bottom_bmp);
242
243 //      Assert( bottom_bmp != top_bmp );
244
245         top_data = top_bmp->bm_data;
246         bottom_data = bottom_bmp->bm_data;
247
248 //      Assert( bottom_data != top_data );
249
250         //mprintf( 0, "SuperX remapping type=%d\n", type );
251         //Int3();
252          
253         switch( type )  {
254                 case 0:
255                         // Normal
256                         for (y=0; y<64; y++ )
257                                 for (x=0; x<64; x++ )   {
258                                         c = top_data[ 64*y+x ];         
259                                         if (c==TRANSPARENCY_COLOR)
260                                                 c = bottom_data[ 64*y+x ];
261                                         else if (c==254)
262                                                 c = TRANSPARENCY_COLOR;
263                                         *dest_data++ = c;
264                                 }
265                         break;
266                 case 1:
267                         // 
268                         for (y=0; y<64; y++ )
269                                 for (x=0; x<64; x++ )   {
270                                         c = top_data[ 64*x+(63-y) ];            
271                                         if (c==TRANSPARENCY_COLOR)
272                                                 c = bottom_data[ 64*y+x ];
273                                         else if (c==254)
274                                                 c = TRANSPARENCY_COLOR;
275                                         *dest_data++ = c;
276                                 }
277                         break;
278                 case 2:
279                         // Normal
280                         for (y=0; y<64; y++ )
281                                 for (x=0; x<64; x++ )   {
282                                         c = top_data[ 64*(63-y)+(63-x) ];
283                                         if (c==TRANSPARENCY_COLOR)
284                                                 c = bottom_data[ 64*y+x ];
285                                         else if (c==254)
286                                                 c = TRANSPARENCY_COLOR;
287                                         *dest_data++ = c;
288                                 }
289                         break;
290                 case 3:
291                         // Normal
292                         for (y=0; y<64; y++ )
293                                 for (x=0; x<64; x++ )   {
294                                         c = top_data[ 64*(63-x)+y  ];
295                                         if (c==TRANSPARENCY_COLOR)
296                                                 c = bottom_data[ 64*y+x ];
297                                         else if (c==254)
298                                                 c = TRANSPARENCY_COLOR;
299                                         *dest_data++ = c;
300                                 }
301                         break;
302         }
303 }