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.
18 #include "pa_enabl.h" //$$POLY_ACC
33 #define MAX_NUM_CACHE_BITMAPS 200
35 #define MAX_NUM_CACHE_BITMAPS 50
38 //static grs_bitmap * cache_bitmaps[MAX_NUM_CACHE_BITMAPS];
42 grs_bitmap * bottom_bmp;
48 static TEXTURE_CACHE Cache[MAX_NUM_CACHE_BITMAPS];
50 static int num_cache_entries = 0;
52 static int cache_hits = 0;
53 static int cache_misses = 0;
55 void texmerge_close();
56 void merge_textures_super_xparent(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
58 void merge_textures_new(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
61 #if defined(POLY_ACC) // useful to all of D2 I think.
62 extern grs_bitmap * rle_get_id_sub( grs_bitmap * bmp );
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 )
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);
82 tmpbmp = rle_get_id_sub( bmp );
84 return (uint)(tmpbmp-GameBitmaps);
86 // Must be a normal bitmap
87 return (uint)(bmp-GameBitmaps);
91 //----------------------------------------------------------------------
93 int texmerge_init(int num_cached_textures)
97 if ( num_cached_textures <= MAX_NUM_CACHE_BITMAPS )
98 num_cache_entries = num_cached_textures;
100 num_cache_entries = MAX_NUM_CACHE_BITMAPS;
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 );
106 //if (get_selector( Cache[i].bitmap->bm_data, 64*64, &Cache[i].bitmap->bm_selector))
107 // Error( "ERROR ALLOCATING CACHE BITMAP'S SELECTORS!!!!" );
109 Cache[i].last_frame_used = -1;
110 Cache[i].top_bmp = NULL;
111 Cache[i].bottom_bmp = NULL;
112 Cache[i].orient = -1;
114 atexit( texmerge_close );
119 void texmerge_flush()
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;
132 //-------------------------------------------------------------------------
133 void texmerge_close()
137 for (i=0; i<num_cache_entries; i++ ) {
138 gr_free_bitmap( Cache[i].bitmap );
139 Cache[i].bitmap = NULL;
143 //--unused-- int info_printed = 0;
145 grs_bitmap * texmerge_get_cached_bitmap( int tmap_bottom, int tmap_top )
147 grs_bitmap *bitmap_top, *bitmap_bottom;
149 int lowest_frame_count;
150 int least_recently_used;
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) );
159 bitmap_top = &GameBitmaps[Textures[tmap_top&0x3FFF].index];
160 bitmap_bottom = &GameBitmaps[Textures[tmap_bottom].index];
162 orient = ((tmap_top&0xC000)>>14) & 3;
164 least_recently_used = 0;
165 lowest_frame_count = Cache[0].last_frame_used;
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 )) {
170 Cache[i].last_frame_used = FrameCount;
171 return Cache[i].bitmap;
173 if ( Cache[i].last_frame_used < lowest_frame_count ) {
174 lowest_frame_count = Cache[i].last_frame_used;
175 least_recently_used = i;
179 //---- Page out the LRU bitmap;
182 // Make sure the bitmaps are paged in...
183 #ifdef PIGGY_USE_PAGING
184 piggy_page_flushed = 0;
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]);
194 Assert( piggy_page_flushed == 0 );
198 ogl_freebmtexture(Cache[least_recently_used].bitmap);
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;
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;
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;
217 return Cache[least_recently_used].bitmap;
220 void merge_textures_new( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
226 ubyte * top_data, *bottom_data;
228 if ( top_bmp->bm_flags & BM_FLAG_RLE )
229 top_bmp = rle_expand_texture(top_bmp);
231 if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
232 bottom_bmp = rle_expand_texture(bottom_bmp);
234 // Assert( bottom_bmp != top_bmp );
236 top_data = top_bmp->bm_data;
237 bottom_data = bottom_bmp->bm_data;
239 // Assert( bottom_data != top_data );
241 // mprintf( 0, "Type=%d\n", type );
248 gr_merge_textures( bottom_data, top_data, dest_data );
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 ];
261 gr_merge_textures_1( bottom_data, top_data, dest_data );
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 ];
274 gr_merge_textures_2( bottom_data, top_data, dest_data );
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 ];
287 gr_merge_textures_3( bottom_data, top_data, dest_data );
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 ];
301 void merge_textures_super_xparent( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
306 ubyte * top_data, *bottom_data;
308 if ( top_bmp->bm_flags & BM_FLAG_RLE )
309 top_bmp = rle_expand_texture(top_bmp);
311 if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
312 bottom_bmp = rle_expand_texture(bottom_bmp);
314 // Assert( bottom_bmp != top_bmp );
316 top_data = top_bmp->bm_data;
317 bottom_data = bottom_bmp->bm_data;
319 // Assert( bottom_data != top_data );
321 //mprintf( 0, "SuperX remapping type=%d\n", type );
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 ];
333 c = TRANSPARENCY_COLOR;
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 ];
345 c = TRANSPARENCY_COLOR;
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 ];
357 c = TRANSPARENCY_COLOR;
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 ];
369 c = TRANSPARENCY_COLOR;