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.
16 * Routines to cache merged textures.
36 #define MAX_NUM_CACHE_BITMAPS 200
38 #define MAX_NUM_CACHE_BITMAPS 50
41 //static grs_bitmap * cache_bitmaps[MAX_NUM_CACHE_BITMAPS];
45 grs_bitmap * bottom_bmp;
51 static TEXTURE_CACHE Cache[MAX_NUM_CACHE_BITMAPS];
53 static int num_cache_entries = 0;
55 static int cache_hits = 0;
56 static int cache_misses = 0;
58 void texmerge_close();
59 void merge_textures_super_xparent(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
61 void merge_textures_new(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
64 //----------------------------------------------------------------------
66 int texmerge_init(int num_cached_textures)
70 if ( num_cached_textures <= MAX_NUM_CACHE_BITMAPS )
71 num_cache_entries = num_cached_textures;
73 num_cache_entries = MAX_NUM_CACHE_BITMAPS;
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 );
79 //if (get_selector( Cache[i].bitmap->bm_data, 64*64, &Cache[i].bitmap->bm_selector))
80 // Error( "ERROR ALLOCATING CACHE BITMAP'S SELECTORS!!!!" );
82 Cache[i].last_frame_used = -1;
83 Cache[i].top_bmp = NULL;
84 Cache[i].bottom_bmp = NULL;
87 atexit( texmerge_close );
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;
105 //-------------------------------------------------------------------------
106 void texmerge_close()
110 for (i=0; i<num_cache_entries; i++ ) {
111 gr_free_bitmap( Cache[i].bitmap );
112 Cache[i].bitmap = NULL;
116 //--unused-- int info_printed = 0;
118 grs_bitmap * texmerge_get_cached_bitmap( int tmap_bottom, int tmap_top )
120 grs_bitmap *bitmap_top, *bitmap_bottom;
122 int lowest_frame_count;
123 int least_recently_used;
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) );
132 bitmap_top = &GameBitmaps[Textures[tmap_top&0x3FFF].index];
133 bitmap_bottom = &GameBitmaps[Textures[tmap_bottom].index];
135 orient = ((tmap_top&0xC000)>>14) & 3;
137 least_recently_used = 0;
138 lowest_frame_count = Cache[0].last_frame_used;
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 )) {
143 Cache[i].last_frame_used = FrameCount;
144 return Cache[i].bitmap;
146 if ( Cache[i].last_frame_used < lowest_frame_count ) {
147 lowest_frame_count = Cache[i].last_frame_used;
148 least_recently_used = i;
152 //---- Page out the LRU bitmap;
155 // Make sure the bitmaps are paged in...
156 #ifdef PIGGY_USE_PAGING
157 piggy_page_flushed = 0;
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]);
167 Assert( piggy_page_flushed == 0 );
171 ogl_freebmtexture(Cache[least_recently_used].bitmap);
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;
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;
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;
190 return Cache[least_recently_used].bitmap;
193 void merge_textures_new( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
195 ubyte * top_data, *bottom_data;
197 if ( top_bmp->bm_flags & BM_FLAG_RLE )
198 top_bmp = rle_expand_texture(top_bmp);
200 if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
201 bottom_bmp = rle_expand_texture(bottom_bmp);
203 // Assert( bottom_bmp != top_bmp );
205 top_data = top_bmp->bm_data;
206 bottom_data = bottom_bmp->bm_data;
208 // Assert( bottom_data != top_data );
210 // mprintf( 0, "Type=%d\n", type );
216 gr_merge_textures( bottom_data, top_data, dest_data );
219 gr_merge_textures_1( bottom_data, top_data, dest_data );
222 gr_merge_textures_2( bottom_data, top_data, dest_data );
225 gr_merge_textures_3( bottom_data, top_data, dest_data );
230 void merge_textures_super_xparent( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
235 ubyte * top_data, *bottom_data;
237 if ( top_bmp->bm_flags & BM_FLAG_RLE )
238 top_bmp = rle_expand_texture(top_bmp);
240 if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
241 bottom_bmp = rle_expand_texture(bottom_bmp);
243 // Assert( bottom_bmp != top_bmp );
245 top_data = top_bmp->bm_data;
246 bottom_data = bottom_bmp->bm_data;
248 // Assert( bottom_data != top_data );
250 //mprintf( 0, "SuperX remapping type=%d\n", type );
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 ];
262 c = TRANSPARENCY_COLOR;
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 ];
274 c = TRANSPARENCY_COLOR;
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 ];
286 c = TRANSPARENCY_COLOR;
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 ];
298 c = TRANSPARENCY_COLOR;