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