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.
33 #define MAX_NUM_CACHE_BITMAPS 200
35 #define MAX_NUM_CACHE_BITMAPS 50
39 //static grs_bitmap * cache_bitmaps[MAX_NUM_CACHE_BITMAPS];
43 grs_bitmap * bottom_bmp;
49 static TEXTURE_CACHE Cache[MAX_NUM_CACHE_BITMAPS];
51 static int num_cache_entries = 0;
53 static int cache_hits = 0;
54 static int cache_misses = 0;
56 void texmerge_close(void);
57 void merge_textures_super_xparent(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
59 void merge_textures_new(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
62 //----------------------------------------------------------------------
64 int texmerge_init(int num_cached_textures)
68 if ( num_cached_textures <= MAX_NUM_CACHE_BITMAPS )
69 num_cache_entries = num_cached_textures;
71 num_cache_entries = MAX_NUM_CACHE_BITMAPS;
73 for (i=0; i<num_cache_entries; i++ ) {
74 // Make temp tmap for use when combining
75 Cache[i].bitmap = gr_create_bitmap( 64, 64 );
77 //if (get_selector( Cache[i].bitmap->bm_data, 64*64, &Cache[i].bitmap->bm_selector))
78 // Error( "ERROR ALLOCATING CACHE BITMAP'S SELECTORS!!!!" );
80 Cache[i].last_frame_used = -1;
81 Cache[i].top_bmp = NULL;
82 Cache[i].bottom_bmp = NULL;
85 atexit( texmerge_close );
94 for (i=0; i<num_cache_entries; i++ ) {
95 Cache[i].last_frame_used = -1;
96 Cache[i].top_bmp = NULL;
97 Cache[i].bottom_bmp = NULL;
103 //-------------------------------------------------------------------------
104 void texmerge_close()
108 for (i=0; i<num_cache_entries; i++ ) {
109 gr_free_bitmap( Cache[i].bitmap );
110 Cache[i].bitmap = NULL;
114 //--unused-- int info_printed = 0;
116 grs_bitmap * texmerge_get_cached_bitmap( int tmap_bottom, int tmap_top )
118 grs_bitmap *bitmap_top, *bitmap_bottom;
120 int lowest_frame_count;
121 int least_recently_used;
123 // if ( ((FrameCount % 1000)==0) && ((cache_hits+cache_misses)>0) && (!info_printed) ) {
124 // mprintf( 0, "Texmap caching: %d hits, %d misses. (Missed=%d%%)\n", cache_hits, cache_misses, (cache_misses*100)/(cache_hits+cache_misses) );
130 bitmap_top = &GameBitmaps[Textures[tmap_top&0x3FFF].index];
131 bitmap_bottom = &GameBitmaps[Textures[tmap_bottom].index];
133 orient = ((tmap_top&0xC000)>>14) & 3;
135 least_recently_used = 0;
136 lowest_frame_count = Cache[0].last_frame_used;
138 for (i=0; i<num_cache_entries; i++ ) {
139 if ( (Cache[i].last_frame_used > -1) && (Cache[i].top_bmp==bitmap_top) && (Cache[i].bottom_bmp==bitmap_bottom) && (Cache[i].orient==orient )) {
141 Cache[i].last_frame_used = FrameCount;
142 return Cache[i].bitmap;
144 if ( Cache[i].last_frame_used < lowest_frame_count ) {
145 lowest_frame_count = Cache[i].last_frame_used;
146 least_recently_used = i;
150 //---- Page out the LRU bitmap;
153 // Make sure the bitmaps are paged in...
154 #ifdef PIGGY_USE_PAGING
155 piggy_page_flushed = 0;
157 PIGGY_PAGE_IN(Textures[tmap_top&0x3FFF]);
158 PIGGY_PAGE_IN(Textures[tmap_bottom]);
159 if (piggy_page_flushed) {
160 // If cache got flushed, re-read 'em.
161 piggy_page_flushed = 0;
162 PIGGY_PAGE_IN(Textures[tmap_top&0x3FFF]);
163 PIGGY_PAGE_IN(Textures[tmap_bottom]);
165 Assert( piggy_page_flushed == 0 );
169 ogl_freebmtexture(Cache[least_recently_used].bitmap);
173 if (bitmap_top->bm_flags & BM_FLAG_SUPER_TRANSPARENT) {
174 merge_textures_super_xparent( orient, bitmap_bottom, bitmap_top, Cache[least_recently_used].bitmap->bm_data );
175 Cache[least_recently_used].bitmap->bm_flags = BM_FLAG_TRANSPARENT;
176 Cache[least_recently_used].bitmap->avg_color = bitmap_top->avg_color;
178 merge_textures_new( orient, bitmap_bottom, bitmap_top, Cache[least_recently_used].bitmap->bm_data );
179 Cache[least_recently_used].bitmap->bm_flags = bitmap_bottom->bm_flags & (~BM_FLAG_RLE);
180 Cache[least_recently_used].bitmap->avg_color = bitmap_bottom->avg_color;
183 Cache[least_recently_used].top_bmp = bitmap_top;
184 Cache[least_recently_used].bottom_bmp = bitmap_bottom;
185 Cache[least_recently_used].last_frame_used = FrameCount;
186 Cache[least_recently_used].orient = orient;
188 return Cache[least_recently_used].bitmap;
191 void merge_textures_new( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
193 ubyte * top_data, *bottom_data;
195 if ( top_bmp->bm_flags & BM_FLAG_RLE )
196 top_bmp = rle_expand_texture(top_bmp);
198 if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
199 bottom_bmp = rle_expand_texture(bottom_bmp);
201 // Assert( bottom_bmp != top_bmp );
203 top_data = top_bmp->bm_data;
204 bottom_data = bottom_bmp->bm_data;
206 // Assert( bottom_data != top_data );
208 // mprintf( 0, "Type=%d\n", type );
214 gr_merge_textures( bottom_data, top_data, dest_data );
217 gr_merge_textures_1( bottom_data, top_data, dest_data );
220 gr_merge_textures_2( bottom_data, top_data, dest_data );
223 gr_merge_textures_3( bottom_data, top_data, dest_data );
228 void merge_textures_super_xparent( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
233 ubyte * top_data, *bottom_data;
235 if ( top_bmp->bm_flags & BM_FLAG_RLE )
236 top_bmp = rle_expand_texture(top_bmp);
238 if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
239 bottom_bmp = rle_expand_texture(bottom_bmp);
241 // Assert( bottom_bmp != top_bmp );
243 top_data = top_bmp->bm_data;
244 bottom_data = bottom_bmp->bm_data;
246 // Assert( bottom_data != top_data );
248 //mprintf( 0, "SuperX remapping type=%d\n", type );
254 for (y=0; y<64; y++ )
255 for (x=0; x<64; x++ ) {
256 c = top_data[ 64*y+x ];
257 if (c==TRANSPARENCY_COLOR)
258 c = bottom_data[ 64*y+x ];
260 c = TRANSPARENCY_COLOR;
266 for (y=0; y<64; y++ )
267 for (x=0; x<64; x++ ) {
268 c = top_data[ 64*x+(63-y) ];
269 if (c==TRANSPARENCY_COLOR)
270 c = bottom_data[ 64*y+x ];
272 c = TRANSPARENCY_COLOR;
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 ];
284 c = TRANSPARENCY_COLOR;
290 for (y=0; y<64; y++ )
291 for (x=0; x<64; x++ ) {
292 c = top_data[ 64*(63-x)+y ];
293 if (c==TRANSPARENCY_COLOR)
294 c = bottom_data[ 64*y+x ];
296 c = TRANSPARENCY_COLOR;