This commit was manufactured by cvs2svn to create tag 'd2x-0_1_2'.
[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 #ifdef HAVE_CONFIG_H
15 #include <conf.h>
16 #endif
17
18 #include <stdio.h>
19
20 #include "pa_enabl.h"                   //$$POLY_ACC
21 #include "gr.h"
22 #include "error.h"
23 #include "game.h"
24 #include "textures.h"
25 #include "mono.h"
26 #include "rle.h"
27 #include "piggy.h"
28
29 #if defined(POLY_ACC)
30 #include "poly_acc.h"
31 #endif
32
33 #ifdef OGL
34 #include "ogl_init.h"
35 #define MAX_NUM_CACHE_BITMAPS 200
36 #else
37 #define MAX_NUM_CACHE_BITMAPS 50
38 #endif
39
40 //static grs_bitmap * cache_bitmaps[MAX_NUM_CACHE_BITMAPS];                     
41
42 typedef struct  {
43         grs_bitmap * bitmap;
44         grs_bitmap * bottom_bmp;
45         grs_bitmap * top_bmp;
46         int             orient;
47         int             last_frame_used;
48 } TEXTURE_CACHE;
49
50 static TEXTURE_CACHE Cache[MAX_NUM_CACHE_BITMAPS];
51
52 static int num_cache_entries = 0;
53
54 static int cache_hits = 0;
55 static int cache_misses = 0;
56
57 void texmerge_close();
58 void merge_textures_super_xparent(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
59                                                                                          ubyte *dest_data);
60 void merge_textures_new(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
61                                                                 ubyte *dest_data);
62
63 #if defined(POLY_ACC)       // useful to all of D2 I think.
64 extern grs_bitmap * rle_get_id_sub( grs_bitmap * bmp );
65
66 //----------------------------------------------------------------------
67 // Given pointer to a bitmap returns a unique value that describes the bitmap.
68 // Returns 0xFFFFFFFF if this bitmap isn't a texmerge'd bitmap.
69 uint texmerge_get_unique_id( grs_bitmap * bmp )
70 {
71     int i,n;
72     uint tmp;
73     grs_bitmap * tmpbmp;
74     // Check in texmerge cache
75     for (i=0; i<num_cache_entries; i++ )    {
76         if ( (Cache[i].last_frame_used > -1) && (Cache[i].bitmap == bmp) )  {
77             tmp = (uint)Cache[i].orient<<30;
78             tmp |= ((uint)(Cache[i].top_bmp - GameBitmaps))<<16;
79             tmp |= (uint)(Cache[i].bottom_bmp - GameBitmaps);
80             return tmp;
81         }
82     }
83     // Check in rle cache
84     tmpbmp = rle_get_id_sub( bmp );
85     if ( tmpbmp )   {
86         return (uint)(tmpbmp-GameBitmaps);
87     }
88     // Must be a normal bitmap
89      return (uint)(bmp-GameBitmaps);
90 }
91 #endif
92
93 //----------------------------------------------------------------------
94
95 int texmerge_init(int num_cached_textures)
96 {
97         int i;
98
99         if ( num_cached_textures <= MAX_NUM_CACHE_BITMAPS )
100                 num_cache_entries = num_cached_textures;
101         else
102                 num_cache_entries = MAX_NUM_CACHE_BITMAPS;
103         
104         for (i=0; i<num_cache_entries; i++ )    {
105                         // Make temp tmap for use when combining
106                 Cache[i].bitmap = gr_create_bitmap( 64, 64 );
107
108                 //if (get_selector( Cache[i].bitmap->bm_data, 64*64,  &Cache[i].bitmap->bm_selector))
109                 //      Error( "ERROR ALLOCATING CACHE BITMAP'S SELECTORS!!!!" );
110
111                 Cache[i].last_frame_used = -1;
112                 Cache[i].top_bmp = NULL;
113                 Cache[i].bottom_bmp = NULL;
114                 Cache[i].orient = -1;
115         }
116         atexit( texmerge_close );
117
118         return 1;
119 }
120
121 void texmerge_flush()
122 {
123         int i;
124
125         for (i=0; i<num_cache_entries; i++ )    {
126                 Cache[i].last_frame_used = -1;
127                 Cache[i].top_bmp = NULL;
128                 Cache[i].bottom_bmp = NULL;
129                 Cache[i].orient = -1;
130         }
131 }
132
133
134 //-------------------------------------------------------------------------
135 void texmerge_close()
136 {
137         int i;
138
139         for (i=0; i<num_cache_entries; i++ )    {
140                 gr_free_bitmap( Cache[i].bitmap );
141                 Cache[i].bitmap = NULL;
142         }
143 }
144
145 //--unused-- int info_printed = 0;
146
147 grs_bitmap * texmerge_get_cached_bitmap( int tmap_bottom, int tmap_top )
148 {
149         grs_bitmap *bitmap_top, *bitmap_bottom;
150         int i, orient;
151         int lowest_frame_count;
152         int least_recently_used;
153
154 //      if ( ((FrameCount % 1000)==0) && ((cache_hits+cache_misses)>0) && (!info_printed) )     {
155 //              mprintf( 0, "Texmap caching:  %d hits, %d misses. (Missed=%d%%)\n", cache_hits, cache_misses, (cache_misses*100)/(cache_hits+cache_misses)  );
156 //              info_printed = 1;
157 //      } else {
158 //              info_printed = 0;
159 //      }
160
161         bitmap_top = &GameBitmaps[Textures[tmap_top&0x3FFF].index];
162         bitmap_bottom = &GameBitmaps[Textures[tmap_bottom].index];
163         
164         orient = ((tmap_top&0xC000)>>14) & 3;
165
166         least_recently_used = 0;
167         lowest_frame_count = Cache[0].last_frame_used;
168         
169         for (i=0; i<num_cache_entries; i++ )    {
170                 if ( (Cache[i].last_frame_used > -1) && (Cache[i].top_bmp==bitmap_top) && (Cache[i].bottom_bmp==bitmap_bottom) && (Cache[i].orient==orient ))   {
171                         cache_hits++;
172                         Cache[i].last_frame_used = FrameCount;
173                         return Cache[i].bitmap;
174                 }       
175                 if ( Cache[i].last_frame_used < lowest_frame_count )    {
176                         lowest_frame_count = Cache[i].last_frame_used;
177                         least_recently_used = i;
178                 }
179         }
180
181         //---- Page out the LRU bitmap;
182         cache_misses++;
183
184         // Make sure the bitmaps are paged in...
185 #ifdef PIGGY_USE_PAGING
186         piggy_page_flushed = 0;
187
188         PIGGY_PAGE_IN(Textures[tmap_top&0x3FFF]);
189         PIGGY_PAGE_IN(Textures[tmap_bottom]);
190         if (piggy_page_flushed) {
191                 // If cache got flushed, re-read 'em.
192                 piggy_page_flushed = 0;
193                 PIGGY_PAGE_IN(Textures[tmap_top&0x3FFF]);
194                 PIGGY_PAGE_IN(Textures[tmap_bottom]);
195         }
196         Assert( piggy_page_flushed == 0 );
197 #endif
198
199 #ifdef OGL
200         ogl_freebmtexture(Cache[least_recently_used].bitmap);
201 #endif
202
203
204         if (bitmap_top->bm_flags & BM_FLAG_SUPER_TRANSPARENT)   {
205                 merge_textures_super_xparent( orient, bitmap_bottom, bitmap_top, Cache[least_recently_used].bitmap->bm_data );
206                 Cache[least_recently_used].bitmap->bm_flags = BM_FLAG_TRANSPARENT;
207                 Cache[least_recently_used].bitmap->avg_color = bitmap_top->avg_color;
208         } else  {
209                 merge_textures_new( orient, bitmap_bottom, bitmap_top, Cache[least_recently_used].bitmap->bm_data );
210                 Cache[least_recently_used].bitmap->bm_flags = bitmap_bottom->bm_flags & (~BM_FLAG_RLE);
211                 Cache[least_recently_used].bitmap->avg_color = bitmap_bottom->avg_color;
212         }
213                 
214         Cache[least_recently_used].top_bmp = bitmap_top;
215         Cache[least_recently_used].bottom_bmp = bitmap_bottom;
216         Cache[least_recently_used].last_frame_used = FrameCount;
217         Cache[least_recently_used].orient = orient;
218
219         return Cache[least_recently_used].bitmap;
220 }
221
222 void merge_textures_new( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
223 {
224 #ifdef MACINTOSH
225         ubyte c;
226         int x,y;
227 #endif
228         ubyte * top_data, *bottom_data;
229
230         if ( top_bmp->bm_flags & BM_FLAG_RLE )
231                 top_bmp = rle_expand_texture(top_bmp);
232
233         if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
234                 bottom_bmp = rle_expand_texture(bottom_bmp);
235
236 //      Assert( bottom_bmp != top_bmp );
237
238         top_data = top_bmp->bm_data;
239         bottom_data = bottom_bmp->bm_data;
240
241 //      Assert( bottom_data != top_data );
242
243         // mprintf( 0, "Type=%d\n", type );
244
245         switch( type )  {
246                 case 0:
247                         // Normal
248                         
249 #ifndef MACINTOSH
250                         gr_merge_textures( bottom_data, top_data, dest_data );
251 #else
252                         for (y=0; y<64; y++ )
253                                 for (x=0; x<64; x++ )   {
254                                         c = top_data[ 64*y+x ];         
255                                         if (c==TRANSPARENCY_COLOR)
256                                                 c = bottom_data[ 64*y+x ];
257                                         *dest_data++ = c;
258                                 }
259 #endif
260                         break;
261                 case 1:
262 #ifndef MACINTOSH
263                         gr_merge_textures_1( bottom_data, top_data, dest_data );
264 #else
265                         for (y=0; y<64; y++ )
266                                 for (x=0; x<64; x++ )   {
267                                         c = top_data[ 64*x+(63-y) ];            
268                                         if (c==TRANSPARENCY_COLOR)
269                                                 c = bottom_data[ 64*y+x ];
270                                         *dest_data++ = c;
271                                 }
272 #endif
273                         break;
274                 case 2:
275 #ifndef MACINTOSH
276                         gr_merge_textures_2( bottom_data, top_data, dest_data );
277 #else
278                         for (y=0; y<64; y++ )
279                                 for (x=0; x<64; x++ )   {
280                                         c = top_data[ 64*(63-y)+(63-x) ];
281                                         if (c==TRANSPARENCY_COLOR)
282                                                 c = bottom_data[ 64*y+x ];
283                                         *dest_data++ = c;
284                                 }
285 #endif
286                         break;
287                 case 3:
288 #ifndef MACINTOSH
289                         gr_merge_textures_3( bottom_data, top_data, dest_data );
290 #else
291                         for (y=0; y<64; y++ )
292                                 for (x=0; x<64; x++ )   {
293                                         c = top_data[ 64*(63-x)+y  ];
294                                         if (c==TRANSPARENCY_COLOR)
295                                                 c = bottom_data[ 64*y+x ];
296                                         *dest_data++ = c;
297                                 }
298 #endif
299                         break;
300         }
301 }
302
303 void merge_textures_super_xparent( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
304 {
305         ubyte c;
306         int x,y;
307
308         ubyte * top_data, *bottom_data;
309
310         if ( top_bmp->bm_flags & BM_FLAG_RLE )
311                 top_bmp = rle_expand_texture(top_bmp);
312
313         if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
314                 bottom_bmp = rle_expand_texture(bottom_bmp);
315
316 //      Assert( bottom_bmp != top_bmp );
317
318         top_data = top_bmp->bm_data;
319         bottom_data = bottom_bmp->bm_data;
320
321 //      Assert( bottom_data != top_data );
322
323         //mprintf( 0, "SuperX remapping type=%d\n", type );
324         //Int3();
325          
326         switch( type )  {
327                 case 0:
328                         // Normal
329                         for (y=0; y<64; y++ )
330                                 for (x=0; x<64; x++ )   {
331                                         c = top_data[ 64*y+x ];         
332                                         if (c==TRANSPARENCY_COLOR)
333                                                 c = bottom_data[ 64*y+x ];
334                                         else if (c==254)
335                                                 c = TRANSPARENCY_COLOR;
336                                         *dest_data++ = c;
337                                 }
338                         break;
339                 case 1:
340                         // 
341                         for (y=0; y<64; y++ )
342                                 for (x=0; x<64; x++ )   {
343                                         c = top_data[ 64*x+(63-y) ];            
344                                         if (c==TRANSPARENCY_COLOR)
345                                                 c = bottom_data[ 64*y+x ];
346                                         else if (c==254)
347                                                 c = TRANSPARENCY_COLOR;
348                                         *dest_data++ = c;
349                                 }
350                         break;
351                 case 2:
352                         // Normal
353                         for (y=0; y<64; y++ )
354                                 for (x=0; x<64; x++ )   {
355                                         c = top_data[ 64*(63-y)+(63-x) ];
356                                         if (c==TRANSPARENCY_COLOR)
357                                                 c = bottom_data[ 64*y+x ];
358                                         else if (c==254)
359                                                 c = TRANSPARENCY_COLOR;
360                                         *dest_data++ = c;
361                                 }
362                         break;
363                 case 3:
364                         // Normal
365                         for (y=0; y<64; y++ )
366                                 for (x=0; x<64; x++ )   {
367                                         c = top_data[ 64*(63-x)+y  ];
368                                         if (c==TRANSPARENCY_COLOR)
369                                                 c = bottom_data[ 64*y+x ];
370                                         else if (c==254)
371                                                 c = TRANSPARENCY_COLOR;
372                                         *dest_data++ = c;
373                                 }
374                         break;
375         }
376 }