1 /* $Id: texmerge.c,v 1.5 2005-07-30 01:50:17 chris Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
17 * Routines to cache merged textures.
37 #define MAX_NUM_CACHE_BITMAPS 200
39 #define MAX_NUM_CACHE_BITMAPS 50
42 //static grs_bitmap * cache_bitmaps[MAX_NUM_CACHE_BITMAPS];
46 grs_bitmap * bottom_bmp;
52 static TEXTURE_CACHE Cache[MAX_NUM_CACHE_BITMAPS];
54 static int num_cache_entries = 0;
56 static int cache_hits = 0;
57 static int cache_misses = 0;
59 void texmerge_close();
60 void merge_textures_super_xparent(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
62 void merge_textures_new(int type, grs_bitmap *bottom_bmp, grs_bitmap *top_bmp,
65 //----------------------------------------------------------------------
67 int texmerge_init(int num_cached_textures)
71 if ( num_cached_textures <= MAX_NUM_CACHE_BITMAPS )
72 num_cache_entries = num_cached_textures;
74 num_cache_entries = MAX_NUM_CACHE_BITMAPS;
76 for (i=0; i<num_cache_entries; i++ ) {
77 // Make temp tmap for use when combining
78 Cache[i].bitmap = gr_create_bitmap( 64, 64 );
80 //if (get_selector( Cache[i].bitmap->bm_data, 64*64, &Cache[i].bitmap->bm_selector))
81 // Error( "ERROR ALLOCATING CACHE BITMAP'S SELECTORS!!!!" );
83 Cache[i].last_frame_used = -1;
84 Cache[i].top_bmp = NULL;
85 Cache[i].bottom_bmp = NULL;
88 atexit( texmerge_close );
97 for (i=0; i<num_cache_entries; i++ ) {
98 Cache[i].last_frame_used = -1;
99 Cache[i].top_bmp = NULL;
100 Cache[i].bottom_bmp = NULL;
101 Cache[i].orient = -1;
106 //-------------------------------------------------------------------------
107 void texmerge_close()
111 for (i=0; i<num_cache_entries; i++ ) {
112 gr_free_bitmap( Cache[i].bitmap );
113 Cache[i].bitmap = NULL;
117 //--unused-- int info_printed = 0;
119 grs_bitmap * texmerge_get_cached_bitmap( int tmap_bottom, int tmap_top )
121 grs_bitmap *bitmap_top, *bitmap_bottom;
123 int lowest_frame_count;
124 int least_recently_used;
126 // if ( ((FrameCount % 1000)==0) && ((cache_hits+cache_misses)>0) && (!info_printed) ) {
127 // mprintf( 0, "Texmap caching: %d hits, %d misses. (Missed=%d%%)\n", cache_hits, cache_misses, (cache_misses*100)/(cache_hits+cache_misses) );
133 bitmap_top = &GameBitmaps[Textures[tmap_top&0x3FFF].index];
134 bitmap_bottom = &GameBitmaps[Textures[tmap_bottom].index];
136 orient = ((tmap_top&0xC000)>>14) & 3;
138 least_recently_used = 0;
139 lowest_frame_count = Cache[0].last_frame_used;
141 for (i=0; i<num_cache_entries; i++ ) {
142 if ( (Cache[i].last_frame_used > -1) && (Cache[i].top_bmp==bitmap_top) && (Cache[i].bottom_bmp==bitmap_bottom) && (Cache[i].orient==orient )) {
144 Cache[i].last_frame_used = FrameCount;
145 return Cache[i].bitmap;
147 if ( Cache[i].last_frame_used < lowest_frame_count ) {
148 lowest_frame_count = Cache[i].last_frame_used;
149 least_recently_used = i;
153 //---- Page out the LRU bitmap;
156 // Make sure the bitmaps are paged in...
157 #ifdef PIGGY_USE_PAGING
158 piggy_page_flushed = 0;
160 PIGGY_PAGE_IN(Textures[tmap_top&0x3FFF]);
161 PIGGY_PAGE_IN(Textures[tmap_bottom]);
162 if (piggy_page_flushed) {
163 // If cache got flushed, re-read 'em.
164 piggy_page_flushed = 0;
165 PIGGY_PAGE_IN(Textures[tmap_top&0x3FFF]);
166 PIGGY_PAGE_IN(Textures[tmap_bottom]);
168 Assert( piggy_page_flushed == 0 );
172 ogl_freebmtexture(Cache[least_recently_used].bitmap);
176 if (bitmap_top->bm_flags & BM_FLAG_SUPER_TRANSPARENT) {
177 merge_textures_super_xparent( orient, bitmap_bottom, bitmap_top, Cache[least_recently_used].bitmap->bm_data );
178 Cache[least_recently_used].bitmap->bm_flags = BM_FLAG_TRANSPARENT;
179 Cache[least_recently_used].bitmap->avg_color = bitmap_top->avg_color;
181 merge_textures_new( orient, bitmap_bottom, bitmap_top, Cache[least_recently_used].bitmap->bm_data );
182 Cache[least_recently_used].bitmap->bm_flags = bitmap_bottom->bm_flags & (~BM_FLAG_RLE);
183 Cache[least_recently_used].bitmap->avg_color = bitmap_bottom->avg_color;
186 Cache[least_recently_used].top_bmp = bitmap_top;
187 Cache[least_recently_used].bottom_bmp = bitmap_bottom;
188 Cache[least_recently_used].last_frame_used = FrameCount;
189 Cache[least_recently_used].orient = orient;
191 return Cache[least_recently_used].bitmap;
194 void merge_textures_new( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
196 ubyte * top_data, *bottom_data;
198 if ( top_bmp->bm_flags & BM_FLAG_RLE )
199 top_bmp = rle_expand_texture(top_bmp);
201 if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
202 bottom_bmp = rle_expand_texture(bottom_bmp);
204 // Assert( bottom_bmp != top_bmp );
206 top_data = top_bmp->bm_data;
207 bottom_data = bottom_bmp->bm_data;
209 // Assert( bottom_data != top_data );
211 // mprintf( 0, "Type=%d\n", type );
217 gr_merge_textures( bottom_data, top_data, dest_data );
220 gr_merge_textures_1( bottom_data, top_data, dest_data );
223 gr_merge_textures_2( bottom_data, top_data, dest_data );
226 gr_merge_textures_3( bottom_data, top_data, dest_data );
231 void merge_textures_super_xparent( int type, grs_bitmap * bottom_bmp, grs_bitmap * top_bmp, ubyte * dest_data )
236 ubyte * top_data, *bottom_data;
238 if ( top_bmp->bm_flags & BM_FLAG_RLE )
239 top_bmp = rle_expand_texture(top_bmp);
241 if ( bottom_bmp->bm_flags & BM_FLAG_RLE )
242 bottom_bmp = rle_expand_texture(bottom_bmp);
244 // Assert( bottom_bmp != top_bmp );
246 top_data = top_bmp->bm_data;
247 bottom_data = bottom_bmp->bm_data;
249 // Assert( bottom_data != top_data );
251 //mprintf( 0, "SuperX remapping type=%d\n", type );
257 for (y=0; y<64; y++ )
258 for (x=0; x<64; x++ ) {
259 c = top_data[ 64*y+x ];
260 if (c==TRANSPARENCY_COLOR)
261 c = bottom_data[ 64*y+x ];
263 c = TRANSPARENCY_COLOR;
269 for (y=0; y<64; y++ )
270 for (x=0; x<64; x++ ) {
271 c = top_data[ 64*x+(63-y) ];
272 if (c==TRANSPARENCY_COLOR)
273 c = bottom_data[ 64*y+x ];
275 c = TRANSPARENCY_COLOR;
281 for (y=0; y<64; y++ )
282 for (x=0; x<64; x++ ) {
283 c = top_data[ 64*(63-y)+(63-x) ];
284 if (c==TRANSPARENCY_COLOR)
285 c = bottom_data[ 64*y+x ];
287 c = TRANSPARENCY_COLOR;
293 for (y=0; y<64; y++ )
294 for (x=0; x<64; x++ ) {
295 c = top_data[ 64*(63-x)+y ];
296 if (c==TRANSPARENCY_COLOR)
297 c = bottom_data[ 64*y+x ];
299 c = TRANSPARENCY_COLOR;