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.
20 #include "pa_enabl.h" //$$POLY_ACC
35 #define MAX_NUM_CACHE_BITMAPS 200
37 #define MAX_NUM_CACHE_BITMAPS 50
40 //static grs_bitmap * cache_bitmaps[MAX_NUM_CACHE_BITMAPS];
44 grs_bitmap * bottom_bmp;
50 static TEXTURE_CACHE Cache[MAX_NUM_CACHE_BITMAPS];
52 static int num_cache_entries = 0;
54 static int cache_hits = 0;
55 static int cache_misses = 0;
57 void texmerge_close();
58 void merge_textures_super_xparent(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
60 void merge_textures_new(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
63 #if defined(POLY_ACC) // useful to all of D2 I think.
64 extern grs_bitmap * rle_get_id_sub( grs_bitmap * bmp );
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 )
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);
84 tmpbmp = rle_get_id_sub( bmp );
86 return (uint)(tmpbmp-GameBitmaps);
88 // Must be a normal bitmap
89 return (uint)(bmp-GameBitmaps);
93 //----------------------------------------------------------------------
95 int texmerge_init(int num_cached_textures)
99 if ( num_cached_textures <= MAX_NUM_CACHE_BITMAPS )
100 num_cache_entries = num_cached_textures;
102 num_cache_entries = MAX_NUM_CACHE_BITMAPS;
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 );
108 //if (get_selector( Cache[i].bitmap->bm_data, 64*64, &Cache[i].bitmap->bm_selector))
109 // Error( "ERROR ALLOCATING CACHE BITMAP'S SELECTORS!!!!" );
111 Cache[i].last_frame_used = -1;
112 Cache[i].top_bmp = NULL;
113 Cache[i].bottom_bmp = NULL;
114 Cache[i].orient = -1;
116 atexit( texmerge_close );
121 void texmerge_flush()
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;
134 //-------------------------------------------------------------------------
135 void texmerge_close()
139 for (i=0; i<num_cache_entries; i++ ) {
140 gr_free_bitmap( Cache[i].bitmap );
141 Cache[i].bitmap = NULL;
145 //--unused-- int info_printed = 0;
147 grs_bitmap * texmerge_get_cached_bitmap( int tmap_bottom, int tmap_top )
149 grs_bitmap *bitmap_top, *bitmap_bottom;
151 int lowest_frame_count;
152 int least_recently_used;
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) );
161 bitmap_top = &GameBitmaps[Textures[tmap_top&0x3FFF].index];
162 bitmap_bottom = &GameBitmaps[Textures[tmap_bottom].index];
164 orient = ((tmap_top&0xC000)>>14) & 3;
166 least_recently_used = 0;
167 lowest_frame_count = Cache[0].last_frame_used;
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 )) {
172 Cache[i].last_frame_used = FrameCount;
173 return Cache[i].bitmap;
175 if ( Cache[i].last_frame_used < lowest_frame_count ) {
176 lowest_frame_count = Cache[i].last_frame_used;
177 least_recently_used = i;
181 //---- Page out the LRU bitmap;
184 // Make sure the bitmaps are paged in...
185 #ifdef PIGGY_USE_PAGING
186 piggy_page_flushed = 0;
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]);
196 Assert( piggy_page_flushed == 0 );
200 ogl_freebmtexture(Cache[least_recently_used].bitmap);
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;
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;
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;
219 return Cache[least_recently_used].bitmap;
222 void merge_textures_new( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
228 ubyte * top_data, *bottom_data;
230 if ( top_bmp->bm_flags & BM_FLAG_RLE )
231 top_bmp = rle_expand_texture(top_bmp);
233 if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
234 bottom_bmp = rle_expand_texture(bottom_bmp);
236 // Assert( bottom_bmp != top_bmp );
238 top_data = top_bmp->bm_data;
239 bottom_data = bottom_bmp->bm_data;
241 // Assert( bottom_data != top_data );
243 // mprintf( 0, "Type=%d\n", type );
250 gr_merge_textures( bottom_data, top_data, dest_data );
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 ];
263 gr_merge_textures_1( bottom_data, top_data, dest_data );
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 ];
276 gr_merge_textures_2( bottom_data, top_data, dest_data );
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 ];
289 gr_merge_textures_3( bottom_data, top_data, dest_data );
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 ];
303 void merge_textures_super_xparent( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
308 ubyte * top_data, *bottom_data;
310 if ( top_bmp->bm_flags & BM_FLAG_RLE )
311 top_bmp = rle_expand_texture(top_bmp);
313 if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
314 bottom_bmp = rle_expand_texture(bottom_bmp);
316 // Assert( bottom_bmp != top_bmp );
318 top_data = top_bmp->bm_data;
319 bottom_data = bottom_bmp->bm_data;
321 // Assert( bottom_data != top_data );
323 //mprintf( 0, "SuperX remapping type=%d\n", type );
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 ];
335 c = TRANSPARENCY_COLOR;
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 ];
347 c = TRANSPARENCY_COLOR;
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 ];
359 c = TRANSPARENCY_COLOR;
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 ];
371 c = TRANSPARENCY_COLOR;