2 * $Logfile: /Freespace2/code/Bmpman/BmpMan.cpp $
7 * Code to load and manage all bitmaps for the game
10 * Revision 1.1 2002/05/03 03:28:08 root
14 * 37 9/13/99 11:26p Andsager
15 * Add debug code to check for poorly sized anis
17 * 36 9/05/99 11:19p Dave
18 * Made d3d texture cache much more safe. Fixed training scoring bug where
19 * it would backout scores without ever having applied them in the first
22 * 35 8/20/99 2:09p Dave
25 * 34 8/10/99 6:54p Dave
26 * Mad optimizations. Added paging to the nebula effect.
28 * 33 8/06/99 1:52p Dave
29 * Bumped up MAX_BITMAPS for the demo.
31 * 32 8/02/99 1:49p Dave
32 * Fixed low-mem animation problem. Whee!
34 * 31 7/16/99 1:49p Dave
35 * 8 bit aabitmaps. yay.
37 * 30 7/13/99 1:15p Dave
38 * 32 bit support. Whee!
40 * 29 6/29/99 10:35a Dave
41 * Interface polygon bitmaps! Whee!
43 * 28 6/16/99 4:06p Dave
44 * New pilot info popup. Added new draw-bitmap-as-poly function.
46 * 27 5/05/99 9:02p Dave
47 * Fixed D3D aabitmap rendering. Spiffed up nebula effect a bit (added
48 * rotations, tweaked values, made bitmap selection more random). Fixed
49 * D3D beam weapon clipping problem. Added D3d frame dumping.
51 * 26 4/27/99 12:16a Dave
52 * Fixed beam weapon muzzle glow problem. Fixed premature timeout on the
53 * pxo server list screen. Fixed secondary firing for hosts on a
54 * standalone. Fixed wacky multiplayer weapon "shuddering" problem.
56 * 25 4/09/99 2:21p Dave
57 * Multiplayer beta stuff. CD checking.
59 * 24 4/08/99 10:42a Johnson
60 * Don't try to swizzle a texture to transparent in Fred.
62 * 23 3/31/99 8:24p Dave
63 * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
64 * and background nebulae. Added per-ship non-dimming pixel colors.
66 * 22 3/20/99 3:46p Dave
67 * Added support for model-based background nebulae. Added 3 new
70 * 21 2/11/99 3:08p Dave
71 * PXO refresh button. Very preliminary squad war support.
73 * 20 2/08/99 5:07p Dave
74 * FS2 chat server support. FS2 specific validated missions.
76 * 19 2/05/99 12:52p Dave
77 * Fixed Glide nondarkening textures.
79 * 18 2/04/99 6:29p Dave
80 * First full working rev of FS2 PXO support. Fixed Glide lighting
83 * 17 2/03/99 11:44a Dave
84 * Fixed d3d transparent textures.
86 * 16 1/15/99 11:29a Neilk
87 * Fixed D3D screen/texture pixel formatting problem.
89 * 15 1/14/99 12:48a Dave
90 * Todo list bug fixes. Made a pass at putting briefing icons back into
91 * FRED. Sort of works :(
93 * 14 1/12/99 12:53a Dave
94 * More work on beam weapons - made collision detection very efficient -
95 * collide against all object types properly - made 3 movement types
96 * smooth. Put in test code to check for possible non-darkening pixels on
99 * 13 1/08/99 2:08p Dave
100 * Fixed software rendering for pofview. Super early support for AWACS and
103 * 12 1/06/99 2:24p Dave
104 * Stubs and release build fixes.
106 * 11 12/14/98 4:01p Dave
107 * Got multi_data stuff working well with new xfer stuff.
109 * 10 12/06/98 2:36p Dave
110 * Drastically improved nebula fogging.
112 * 9 12/01/98 5:53p Dave
113 * Simplified the way pixel data is swizzled. Fixed tga bitmaps to work
114 * properly in D3D and Glide.
116 * 8 12/01/98 4:46p Dave
117 * Put in targa bitmap support (16 bit).
119 * 7 12/01/98 10:32a Johnson
120 * Fixed direct3d font problems. Fixed sun bitmap problem. Fixed direct3d
123 * 6 12/01/98 8:06a Dave
124 * Temporary checkin to fix some texture transparency problems in d3d.
126 * 5 11/30/98 5:31p Dave
127 * Fixed up Fred support for software mode.
129 * 4 11/30/98 1:07p Dave
130 * 16 bit conversion, first run.
132 * 3 10/22/98 6:14p Dave
133 * Optimized some #includes in Anim folder. Put in the beginnings of
134 * parse/localization support for externalized strings and tstrings.tbl
136 * 2 10/07/98 10:52a Dave
139 * 1 10/07/98 10:48a Dave
141 * 106 5/23/98 4:43p John
142 * Took out debugging sleep
144 * 105 5/23/98 4:14p John
145 * Added code to preload textures to video card for AGP. Added in code
146 * to page in some bitmaps that weren't getting paged in at level start.
148 * 104 5/20/98 12:59p John
149 * Turned optimizations on for debug builds. Also turning on automatic
150 * function inlining. Turned off the unreachable code warning.
152 * 103 5/20/98 10:20a Hoffoss
153 * Fixed warning in the code.
155 * 102 5/19/98 3:45p John
156 * fixed bug causing lowmem to drop half of the frames. Also halved fps
159 * 101 5/14/98 3:38p John
160 * Added in more non-darkening colors for Adam. Had to fix some bugs in
161 * BmpMan and Ani stuff to get this to work.
163 * 100 4/22/98 9:13p John
164 * Added code to replace frames of animations in vram if so desired.
166 * 99 4/17/98 6:56a John
167 * Fixed bug where RLE'd user bitmaps caused data to not get freed.
168 * (Turned off RLE for use bitmaps). Made lossy animations reduce
169 * resolution by a factor of 2 in low memory conditions.
171 * 98 4/16/98 6:31p Hoffoss
172 * Added function to get filename of a bitmap handle, which we don't have
175 * 97 4/01/98 9:27p John
176 * Fixed debug info in bmpman.
178 * 96 4/01/98 5:34p John
179 * Made only the used POFs page in for a level. Reduced some interp
180 * arrays. Made custom detail level work differently.
182 * 95 3/31/98 9:55a Lawrance
183 * JOHN: get xparency working for user bitmaps
185 * 94 3/30/98 4:02p John
186 * Made machines with < 32 MB of RAM use every other frame of certain
187 * bitmaps. Put in code to keep track of how much RAM we've malloc'd.
189 * 93 3/29/98 4:05p John
190 * New paging code that loads everything necessary at level startup.
192 * 92 3/27/98 11:20a John
193 * commented back in some debug code.
195 * 91 3/26/98 5:21p John
196 * Added new code to preload all bitmaps at the start of a level.
197 * Commented it out, though.
199 * 90 3/26/98 4:56p Jasen
200 * AL: Allow find_block_of() to allocate a series of bitmaps from index 0
202 * 89 3/26/98 10:21a John
203 * Made no palette-mapped bitmaps use 0,255,0 as transparent.
205 * 88 3/24/98 5:39p John
206 * Added debug code to show bitmap fragmentation. Made user bitmaps
207 * allocate from top of array.
209 * 87 3/22/98 3:28p John
210 * Added in stippled alpha for lower details. Made medium detail use
213 * 86 3/11/98 1:55p John
216 * 85 3/06/98 4:09p John
217 * Changed the way we do bitmap RLE'ing... this speds up HUD bitmaps by
220 * 84 3/02/98 6:00p John
221 * Moved MAX_BITMAPS into BmpMan.h so the stuff in the graphics code that
222 * is dependent on it won't break if it changes. Made ModelCache slots
223 * be equal to MAX_OBJECTS which is what it is.
225 * 83 3/02/98 9:51a John
226 * Added code to print the number of bitmap slots in use between levels.
228 * 82 2/16/98 3:54p John
229 * Changed a bunch of mprintfs to catagorize to BmpInfo
231 * 81 2/13/98 5:00p John
232 * Made user bitmaps not get wrote to level cache file.
234 * 80 2/06/98 8:25p John
235 * Added code for new bitmaps since last frame
237 * 79 2/06/98 8:10p John
238 * Added code to show amout of texture usage each frame.
240 * 78 2/05/98 9:21p John
241 * Some new Direct3D code. Added code to monitor a ton of stuff in the
244 * 77 1/29/98 11:48a John
245 * Added new counter measure rendering as model code. Made weapons be
246 * able to have impact explosion.
248 * 76 1/28/98 6:19p Dave
249 * Reduced standalone memory usage ~8 megs. Put in support for handling
250 * multiplayer submenu handling for endgame, etc.
252 * 75 1/17/98 12:55p John
253 * Fixed bug that I just created that loaded all ani frames.
255 * 74 1/17/98 12:33p John
256 * Made the game_busy function be called a constant amount of times per
257 * level load, making the bar prediction easier.
259 * 73 1/17/98 12:14p John
260 * Added loading... bar to freespace.
262 * 72 1/11/98 3:20p John
263 * Made so that if no .clt exists, it will load all the bitmaps
265 * 71 1/11/98 3:06p John
266 * Made bitmap loading stop when cache gets full.
268 * 70 1/11/98 2:45p John
269 * Changed .lst to .clt
271 * 69 1/11/98 2:14p John
272 * Changed a lot of stuff that had to do with bitmap loading. Made cfile
273 * not do callbacks, I put that in global code. Made only bitmaps that
274 * need to load for a level load.
276 * 67 1/09/98 4:07p John
277 * Made all bitmap functions return a bitmap "Handle" not number. This
278 * helps to find bm_release errors.
280 * 66 1/09/98 1:38p John
281 * Fixed some bugs from previous comment
283 * 65 1/09/98 1:32p John
284 * Added some debugging code to track down a weird error. Namely I fill
285 * in the be structure with bogus values when someone frees it.
287 * 64 12/28/97 2:00p John
288 * put in another assert checking for invalid lock/unlock sequencing
290 * 63 12/24/97 2:02p John
291 * Changed palette translation to be a little faster for unoptimized
294 * 62 12/18/97 8:59p Dave
295 * Finished putting in basic support for weapon select and ship select in
298 * 61 12/15/97 10:27p John
299 * fixed bug where 2 bm_loads of same file both open the header.
301 * 60 12/08/97 2:17p John
302 * fixed bug with bmpman and cf_callback.
303 * made cf_callback in Freespace set small font back when done.
305 * 59 12/03/97 5:01p Lawrance
306 * bump up MAX_BITMAPS to 1500. People have reached 1000 bitmaps while
307 * playing multiple missions.
309 * 58 12/02/97 3:59p John
310 * Added first rev of thruster glow, along with variable levels of
311 * translucency, which retquired some restructing of palman.
313 * 57 11/30/97 3:57p John
314 * Made fixed 32-bpp translucency. Made BmpMan always map translucent
315 * color into 255 even if you aren't supposed to remap and make it's
318 * 56 10/05/97 10:39a John
319 * fixed bug with palette on unmapped bitmaps. Made unmapped bitmaps get
320 * marked with xparent.
322 * 55 9/23/97 11:46a Lawrance
323 * fixed bug with rle'ing with spans get big
325 * 54 9/23/97 10:45a John
326 * made so you can tell bitblt code to rle a bitmap by passing flag to
329 * 53 9/19/97 10:18a John
330 * fixed bug with aa animations being re-rle'd every
334 * 52 9/09/97 10:08a Sandeep
335 * Fixed Compiler Level 4 warnings
337 * 51 9/08/97 2:02p John
338 * fixed typo in nprintf
340 * 50 9/08/97 1:56p John
341 * fixed some memory housekeeping bugs
343 * 49 9/03/97 4:19p John
344 * changed bmpman to only accept ani and pcx's. made passing .pcx or .ani
345 * to bm_load functions not needed. Made bmpman keep track of palettes
346 * for bitmaps not mapped into game palettes.
348 * 48 8/29/97 7:35p Lawrance
349 * check if .ani animation is already loaded in bm_load_animation()
351 * 47 8/25/97 11:14p Lawrance
352 * added support for .ani files in bm_load_animation()
354 * 46 8/17/97 2:42p Lawrance
355 * only flag PCX files as xparent if they have xparent pixels in them
357 * 45 8/15/97 9:57a Lawrance
358 * support multiple xparent entries for PCX files
360 * 44 8/05/97 10:18a Lawrance
361 * my_rand() being used temporarily instead of rand()
363 * 43 8/01/97 4:30p John
365 * 42 7/29/97 8:34a John
368 * 41 7/18/97 3:27p Lawrance
369 * have pcx files use (0,255,0) for transparency
371 * 40 7/16/97 3:07p John
373 * 39 7/10/97 8:34a John
374 * Added code to read TGA files.
376 * 38 6/20/97 1:50p John
377 * added rle code to bmpman. made gr8_aabitmap use it.
379 * 37 6/18/97 12:07p John
380 * fixed some color bugs
382 * 36 6/17/97 8:58p Lawrance
383 * fixed bug with not nulling bm.data with USER bitmaps
385 * 35 6/12/97 2:44a Lawrance
386 * changed bm_unlock() to take an index into bm_bitmaps(). Added
387 * ref_count to bitmap_entry struct
389 * 34 5/20/97 10:36a John
390 * Fixed problem with user bitmaps and direct3d caching.
392 * 33 5/14/97 1:59p John
393 * fixed a palette bug with vclips.
395 * 32 3/24/97 4:43p John
396 * speed up chunked collision detection by only checking cubes the vector
399 * 31 3/24/97 3:25p John
400 * Cleaned up and restructured model_collide code and fvi code. In fvi
401 * made code that finds uvs work.. Added bm_get_pixel to BmpMan.
403 * 30 3/11/97 2:49p Allender
405 * 29 2/18/97 9:43a Lawrance
406 * added Assert() in bm_release
408 * 28 1/22/97 4:29p John
409 * maybe fixed bug with code that counts total bytes of texture ram used.
411 * 27 1/22/97 4:19p Lawrance
412 * added flags to bm_create to allow transparency
414 * 26 1/21/97 5:24p John
415 * fixed another bug with bm_release.
417 * 25 1/21/97 5:12p John
418 * fixed bug with case
420 * 24 1/21/97 5:02p John
421 * Added code for 8bpp user bitmaps.
423 * 23 1/09/97 11:35a John
424 * Added some 2d functions to get/put screen images.
426 * 22 11/26/96 6:50p John
427 * Added some more hicolor primitives. Made windowed mode run as current
428 * bpp, if bpp is 8,16,or 32.
430 * 21 11/26/96 9:44a Allender
431 * Allow for use of different bitmap palettes
433 * 20 11/25/96 10:36a Allender
434 * started working on 32 bpp support. Added 15 bpp.
436 * 19 11/18/96 1:51p Allender
437 * fix up manager code to reread bitmaps if needed in newer bit depth
439 * 18 11/15/96 4:24p Allender
440 * more bmpman stuff -- only free bitmap slots when releasing copied
441 * texture -- otherwise, only release the data for the bitmap
443 * 17 11/15/96 3:33p Allender
444 * added support for converting to 16 bit textures when requested with
445 * bm_load. Added some other management functions
447 * 16 11/13/96 4:51p Allender
448 * started overhaul of bitmap manager. bm_load no longer actually load
449 * the data, only the info for the bitmap. Locking the bitmap now forces
450 * load when no data present (or will if bpp changes)
457 #include "pcxutils.h"
461 #include "animplay.h"
463 #include "systemvars.h"
465 #include "packunpack.h"
467 #include "grinternal.h"
468 #include "tgautils.h"
472 #define BMPMAN_NDEBUG
475 // keep this defined to use per-ship nondarkening pixels
476 #define BMPMAN_SPECIAL_NONDARK
480 #define BM_TYPE_NONE 0
481 #define BM_TYPE_PCX 1
482 #define BM_TYPE_USER 2
483 #define BM_TYPE_ANI 3 // in-house ANI format
484 #define BM_TYPE_TGA 4 // 16 bit targa
486 typedef union bm_extra_info {
488 // Stuff needed for animations
489 int first_frame; // used for animations -- points to index of first frame
490 ubyte num_frames; // used for animation -- number of frames in the animation
491 ubyte fps; // used for animation -- frames per second
494 // Stuff needed for user bitmaps
495 void *data; // For user bitmaps, this is where the data comes from
496 ubyte bpp; // For user bitmaps, this is what format the data is
497 ubyte flags; // Flags passed to bm_create
501 typedef struct bitmap_entry {
503 char filename[MAX_FILENAME_LEN]; // filename for this bitmap
505 uint signature; // a unique signature identifying the data
506 uint palette_checksum; // checksum used to be sure bitmap is in current palette
507 int handle; // Handle = id*MAX_BITMAPS + bitmapnum
508 int last_used; // When this bitmap was last used
510 ubyte type; // PCX, USER, ANI, etc
511 signed char ref_count; // Number of locks on bitmap. Can't unload unless ref_count is 0.
513 // Stuff to keep track of usage
514 ubyte preloaded; // If set, then this was loaded from the lst file
515 ubyte used_flags; // What flags it was accessed thru
520 // Data for animations and user bitmaps
525 ubyte used_last_frame; // If set, then it was used last frame
526 ubyte used_this_frame; // If set, then it was used this frame
527 int data_size; // How much data this bitmap uses
528 int used_count; // How many times it was accessed
532 uint Bm_next_signature = 0x1234;
534 bitmap_entry bm_bitmaps[MAX_BITMAPS];
536 int bm_texture_ram = 0;
538 // Bm_max_ram - How much RAM bmpman can use for textures.
539 // Set to <1 to make it use all it wants.
540 int Bm_max_ram = 0; //16*1024*1024; // Only use 16 MB for textures
542 int bm_next_handle = 1;
546 static int Bm_low_mem = 0;
548 // 16 bit pixel formats
549 int Bm_pixel_format = BM_PIXEL_FORMAT_ARGB;
551 // get and put functions for 16 bit pixels - neat bit slinging, huh?
552 #define BM_SET_R_ARGB(p, r) { p[1] &= ~(0x7c); p[1] |= ((r & 0x1f) << 2); }
553 #define BM_SET_G_ARGB(p, g) { p[0] &= ~(0xe0); p[1] &= ~(0x03); p[0] |= ((g & 0x07) << 5); p[1] |= ((g & 0x18) >> 3); }
554 #define BM_SET_B_ARGB(p, b) { p[0] &= ~(0x1f); p[0] |= b & 0x1f; }
555 #define BM_SET_A_ARGB(p, a) { p[1] &= ~(0x80); p[1] |= ((a & 0x01) << 7); }
557 #define BM_SET_R_D3D(p, r) { *p |= (ushort)(( (int)r / Gr_current_red->scale ) << Gr_current_red->shift); }
558 #define BM_SET_G_D3D(p, g) { *p |= (ushort)(( (int)g / Gr_current_green->scale ) << Gr_current_green->shift); }
559 #define BM_SET_B_D3D(p, b) { *p |= (ushort)(( (int)b / Gr_current_blue->scale ) << Gr_current_blue->shift); }
560 #define BM_SET_A_D3D(p, a) { if(a == 0){ *p = (ushort)Gr_current_green->mask; } }
562 #define BM_SET_R(p, r) { switch(Bm_pixel_format){ case BM_PIXEL_FORMAT_ARGB: BM_SET_R_ARGB(((char*)p), r); break; case BM_PIXEL_FORMAT_D3D: BM_SET_R_D3D(p, r); break; default: Int3(); } }
563 #define BM_SET_G(p, g) { switch(Bm_pixel_format){ case BM_PIXEL_FORMAT_ARGB: BM_SET_G_ARGB(((char*)p), g); break; case BM_PIXEL_FORMAT_D3D: BM_SET_G_D3D(p, g); break; default: Int3(); } }
564 #define BM_SET_B(p, b) { switch(Bm_pixel_format){ case BM_PIXEL_FORMAT_ARGB: BM_SET_B_ARGB(((char*)p), b); break; case BM_PIXEL_FORMAT_D3D: BM_SET_B_D3D(p, b); break; default: Int3(); } }
565 #define BM_SET_A(p, a) { switch(Bm_pixel_format){ case BM_PIXEL_FORMAT_ARGB: BM_SET_A_ARGB(((char*)p), a); break; case BM_PIXEL_FORMAT_D3D: BM_SET_A_D3D(p, a); break; default: Int3(); } }
567 // ===========================================
568 // Mode: 0 = High memory
569 // 1 = Low memory ( every other frame of ani's)
570 // 2 = Debug low memory ( only use first frame of each ani )
571 void bm_set_low_mem( int mode )
573 Assert( (mode >= 0) && (mode<=2 ));
578 static int bm_get_next_handle()
580 int n = bm_next_handle;
582 if ( bm_next_handle > 30000 ) {
588 // Frees a bitmaps data if it should, and
589 // Returns true if bitmap n can free it's data.
590 static void bm_free_data(int n)
595 Assert( n >= 0 && n < MAX_BITMAPS );
600 // If there isn't a bitmap in this structure, don't
601 // do anything but clear out the bitmap info
602 if ( be->type==BM_TYPE_NONE)
605 // If this bitmap doesn't have any data to free, skip
606 // the freeing it part of this.
607 if ( bmp->data == 0 )
610 // Don't free up memory for user defined bitmaps, since
611 // BmpMan isn't the one in charge of allocating/deallocing them.
612 if ( ( be->type==BM_TYPE_USER ) )
615 // Free up the data now!
617 // mprintf(( "Bitmap %d freed %d bytes\n", n, bm_bitmaps[n].data_size ));
619 bm_texture_ram -= be->data_size;
621 free((void *)bmp->data);
625 // Clear out & reset the bitmap data structure
633 be->signature = Bm_next_signature++;
639 int Bm_ram_freed = 0;
641 static void bm_free_some_ram( int n, int size )
644 if ( Bm_max_ram < 1 ) return;
645 if ( bm_texture_ram + size < Bm_max_ram ) return;
647 int current_time = timer_get_milliseconds();
649 while( bm_texture_ram + size > Bm_max_ram ) {
652 // Need to free some RAM up!
653 int i, oldest=-1, best_val=0;
654 for (i = 0; i < MAX_BITMAPS; i++) {
655 if ( (bm_bitmaps[i].type != BM_TYPE_NONE) && (bm_bitmaps[i].first_frame!=bm_bitmaps[n].first_frame) && (bm_bitmaps[i].ref_count==0) && (bm_bitmaps[i].data_size>0) ) {
656 int page_func = ( current_time-bm_bitmaps[i].last_used)*bm_bitmaps[i].data_size;
657 if ( (oldest==-1) || (page_func>best_val) ) {
659 best_val = page_func;
665 //mprintf(( "Freeing bitmap '%s'\n", bm_bitmaps[oldest].filename ));
666 for (i=0; i<bm_bitmaps[oldest].num_frames; i++ ) {
667 bm_free_data(bm_bitmaps[oldest].first_frame+i);
670 //mprintf(( "Couldn't free enough! %d\n", bm_texture_ram ));
679 static void *bm_malloc( int n, int size )
681 Assert( n >= 0 && n < MAX_BITMAPS );
682 // mprintf(( "Bitmap %d allocated %d bytes\n", n, size ));
684 bm_free_some_ram( n, size );
685 Assert( bm_bitmaps[n].data_size == 0 );
686 bm_bitmaps[n].data_size += size;
687 bm_texture_ram += size;
696 for (i=0; i<MAX_BITMAPS; i++ ) {
697 bm_free_data(i); // clears flags, bbp, data, etc
708 mprintf(( "Size of bitmap info = %d KB\n", sizeof( bm_bitmaps )/1024 ));
709 mprintf(( "Size of bitmap extra info = %d bytes\n", sizeof( bm_extra_info ) ));
716 for (i=0; i<MAX_BITMAPS; i++ ) {
717 bm_bitmaps[i].filename[0] = '\0';
718 bm_bitmaps[i].type = BM_TYPE_NONE;
719 bm_bitmaps[i].info.user.data = NULL;
720 bm_bitmaps[i].bm.data = 0;
721 bm_bitmaps[i].bm.palette = NULL;
723 bm_bitmaps[i].data_size = 0;
724 bm_bitmaps[i].used_count = 0;
725 bm_bitmaps[i].used_last_frame = 0;
726 bm_bitmaps[i].used_this_frame = 0;
728 bm_free_data(i); // clears flags, bbp, data, etc
736 // Returns number of bytes of bitmaps locked this frame
737 // ntotal = number of bytes of bitmaps locked this frame
738 // nnew = number of bytes of bitmaps locked this frame that weren't locked last frame
739 void bm_get_frame_usage(int *ntotal, int *nnew)
746 for (i=0; i<MAX_BITMAPS; i++ ) {
747 if ( (bm_bitmaps[i].type != BM_TYPE_NONE) && (bm_bitmaps[i].used_this_frame)) {
748 if ( !bm_bitmaps[i].used_last_frame ) {
749 *nnew += bm_bitmaps[i].bm.w*bm_bitmaps[i].bm.h;
751 *ntotal += bm_bitmaps[i].bm.w*bm_bitmaps[i].bm.h;
753 bm_bitmaps[i].used_last_frame = bm_bitmaps[i].used_this_frame;
754 bm_bitmaps[i].used_this_frame = 0;
759 void bm_get_frame_usage(int *ntotal, int *nnew)
764 // given a loaded bitmap with valid info, calculate sections
765 void bm_calc_sections(bitmap *be)
769 // number of x and y sections
770 be->sections.num_x = (ubyte)(be->w / MAX_BMAP_SECTION_SIZE);
771 if((be->sections.num_x * MAX_BMAP_SECTION_SIZE) < be->w){
772 be->sections.num_x++;
774 be->sections.num_y = (ubyte)(be->h / MAX_BMAP_SECTION_SIZE);
775 if((be->sections.num_y * MAX_BMAP_SECTION_SIZE) < be->h){
776 be->sections.num_y++;
779 // calculate the offsets for each section
780 for(idx=0; idx<be->sections.num_x; idx++){
781 be->sections.sx[idx] = (ushort)(MAX_BMAP_SECTION_SIZE * idx);
783 for(idx=0; idx<be->sections.num_y; idx++){
784 be->sections.sy[idx] = (ushort)(MAX_BMAP_SECTION_SIZE * idx);
788 // Creates a bitmap that exists in RAM somewhere, instead
789 // of coming from a disk file. You pass in a pointer to a
790 // block of 32 (or 8)-bit-per-pixel data. Right now, the only
791 // bpp you can pass in is 32 or 8. On success, it returns the
792 // bitmap number. You cannot free that RAM until bm_release
793 // is called on that bitmap.
794 int bm_create( int bpp, int w, int h, void * data, int flags )
796 int i, n, first_slot = MAX_BITMAPS;
798 // Assert((bpp==32)||(bpp==8));
800 Assert(flags & BMP_AABITMAP);
805 if ( !bm_inited ) bm_init();
807 for (i = MAX_BITMAPS-1; i >= 0; i-- ) {
808 if ( bm_bitmaps[i].type == BM_TYPE_NONE ) {
817 // Out of bitmap slots
818 if ( n == -1 ) return -1;
820 memset( &bm_bitmaps[n], 0, sizeof(bitmap_entry) );
822 sprintf( bm_bitmaps[n].filename, "TMP%dx%d", w, h );
823 bm_bitmaps[n].type = BM_TYPE_USER;
824 bm_bitmaps[n].palette_checksum = 0;
826 bm_bitmaps[n].bm.w = short(w);
827 bm_bitmaps[n].bm.h = short(h);
828 bm_bitmaps[n].bm.rowsize = short(w);
829 bm_bitmaps[n].bm.bpp = (unsigned char)(bpp);
830 bm_bitmaps[n].bm.flags = 0;
831 bm_bitmaps[n].bm.flags |= flags;
832 bm_bitmaps[n].bm.data = 0;
833 bm_bitmaps[n].bm.palette = NULL;
835 bm_bitmaps[n].info.user.bpp = ubyte(bpp);
836 bm_bitmaps[n].info.user.data = data;
837 bm_bitmaps[n].info.user.flags = ubyte(flags);
839 bm_bitmaps[n].signature = Bm_next_signature++;
841 bm_bitmaps[n].handle = bm_get_next_handle()*MAX_BITMAPS + n;
842 bm_bitmaps[n].last_used = -1;
844 // fill in section info
845 bm_calc_sections(&bm_bitmaps[n].bm);
847 return bm_bitmaps[n].handle;
850 // sub helper function. Given a raw filename and an extension, try and find the bitmap
851 // returns -1 if it could not be found
852 // 0 if it was found as a file
853 // 1 if it already exists, fills in handle
854 int Bm_ignore_duplicates = 0;
855 int bm_load_sub(char *real_filename, char *ext, int *handle)
858 char filename[MAX_FILENAME_LEN] = "";
860 strcpy( filename, real_filename );
861 strcat( filename, ext );
862 for (i=0; i<(int)strlen(filename); i++ ){
863 filename[i] = char(tolower(filename[i]));
866 // try to find given filename to see if it has been loaded before
867 if(!Bm_ignore_duplicates){
868 for (i = 0; i < MAX_BITMAPS; i++) {
869 if ( (bm_bitmaps[i].type != BM_TYPE_NONE) && !stricmp(filename, bm_bitmaps[i].filename) ) {
870 nprintf (("BmpMan", "Found bitmap %s -- number %d\n", filename, i));
871 *handle = bm_bitmaps[i].handle;
877 // try and find the file
879 CFILE *test = cfopen(filename, "rb");
886 // could not be found
890 // This loads a bitmap so we can draw with it later.
891 // It returns a negative number if it couldn't load
892 // the bitmap. On success, it returns the bitmap
893 // number. Function doesn't acutally load the data, only
894 // width, height, and possibly flags.
895 int bm_load( char * real_filename )
897 int i, n, first_slot = MAX_BITMAPS;
899 char filename[MAX_FILENAME_LEN];
904 if ( !bm_inited ) bm_init();
906 // nice little trick for keeping standalone memory usage way low - always return a bogus bitmap
907 if(Game_mode & GM_STANDALONE_SERVER){
908 strcpy(filename,"test128");
911 // make sure no one passed an extension
912 strcpy( filename, real_filename );
913 char *p = strchr( filename, '.' );
915 mprintf(( "Someone passed an extension to bm_load for file '%s'\n", real_filename ));
920 // try and find the pcx file
921 switch(bm_load_sub(filename, ".pcx", &handle)){
929 strcat(filename, ".pcx");
932 // found as pre-existing
939 // try and find the tga file
940 switch(bm_load_sub(filename, ".tga", &handle)){
948 strcat(filename, ".tga");
952 // found as pre-existing
960 int tga_error=targa_read_header( filename, &w, &h, &bpp, NULL );
961 if ( tga_error != TARGA_ERROR_NONE ) {
962 mprintf(( "Couldn't open '%s'\n", filename ));
968 int pcx_error=pcx_read_header( filename, &w, &h, NULL );
969 if ( pcx_error != PCX_ERROR_NONE ) {
970 mprintf(( "Couldn't open '%s'\n", filename ));
975 // Error( LOCATION, "Unknown bitmap type %s\n", filename );
978 for (i = 0; i < MAX_BITMAPS; i++) {
979 if ( (bm_bitmaps[i].type == BM_TYPE_NONE) && (first_slot == MAX_BITMAPS) ){
985 Assert( n < MAX_BITMAPS );
987 if ( n == MAX_BITMAPS ) return -1;
989 // ensure fields are cleared out from previous bitmap
990 // Assert(bm_bitmaps[n].bm.data == 0);
991 // Assert(bm_bitmaps[n].bm.palette == NULL);
992 // Assert(bm_bitmaps[n].ref_count == 0 );
993 // Assert(bm_bitmaps[n].user_data == NULL);
994 memset( &bm_bitmaps[n], 0, sizeof(bitmap_entry) );
996 // Mark the slot as filled, because cf_read might load a new bitmap
998 bm_bitmaps[n].type = tga ? (ubyte)BM_TYPE_TGA : (ubyte)BM_TYPE_PCX;
999 bm_bitmaps[n].signature = Bm_next_signature++;
1000 Assert ( strlen(filename) < MAX_FILENAME_LEN );
1001 strncpy(bm_bitmaps[n].filename, filename, MAX_FILENAME_LEN-1 );
1002 bm_bitmaps[n].bm.w = short(w);
1003 bm_bitmaps[n].bm.rowsize = short(w);
1004 bm_bitmaps[n].bm.h = short(h);
1005 bm_bitmaps[n].bm.bpp = 0;
1006 bm_bitmaps[n].bm.flags = 0;
1007 bm_bitmaps[n].bm.data = 0;
1008 bm_bitmaps[n].bm.palette = NULL;
1010 bm_bitmaps[n].palette_checksum = 0;
1011 bm_bitmaps[n].handle = bm_get_next_handle()*MAX_BITMAPS + n;
1012 bm_bitmaps[n].last_used = -1;
1014 // fill in section info
1015 bm_calc_sections(&bm_bitmaps[n].bm);
1017 return bm_bitmaps[n].handle;
1020 // special load function. basically allows you to load a bitmap which already exists (by filename).
1021 // this is useful because in some cases we need to have a bitmap which is locked in screen format
1022 // _and_ texture format, such as pilot pics and squad logos
1023 int bm_load_duplicate(char *filename)
1027 // ignore duplicates
1028 Bm_ignore_duplicates = 1;
1031 ret = bm_load(filename);
1034 Bm_ignore_duplicates = 0;
1039 DCF(bm_frag,"Shows BmpMan fragmentation")
1049 for (int i=0; i<MAX_BITMAPS; i++ ) {
1050 switch( bm_bitmaps[i].type ) {
1052 gr_set_color(128,128,128);
1055 gr_set_color(255,0,0);
1058 gr_set_color(0,255,0);
1061 gr_set_color(0,0,255);
1065 gr_rect( x+xs, y+ys, w, h );
1079 static int find_block_of(int n)
1085 for (i=0; i<MAX_BITMAPS; i++ ) {
1086 if ( bm_bitmaps[i].type == BM_TYPE_NONE ) {
1087 if (cnt==0) nstart = i;
1091 if ( cnt == n ) return nstart;
1094 // Error( LOCATION, "Couldn't find block of %d frames\n", n );
1098 // ------------------------------------------------------------------
1099 // bm_load_animation()
1101 // input: filename => filename of animation
1102 // nframes => OUTPUT parameter: number of frames in the animation
1103 // fps => OUTPUT/OPTIONAL parameter: intended fps for the animation
1105 // returns: bitmap number of first frame in the animation
1107 int bm_load_animation( char *real_filename, int *nframes, int *fps, int can_drop_frames)
1112 char filename[MAX_FILENAME_LEN];
1114 if ( !bm_inited ) bm_init();
1116 strcpy( filename, real_filename );
1117 char *p = strchr( filename, '.' );
1119 mprintf(( "Someone passed an extension to bm_load_animation for file '%s'\n", real_filename ));
1123 strcat( filename, ".ani" );
1125 if ( (fp = cfopen(filename, "rb")) == NULL ) {
1126 // Error(LOCATION,"Could not open filename %s in bm_load_ani()\n", filename);
1132 // for debug of ANI sizes
1133 strcpy(the_anim.name, real_filename);
1135 anim_read_header(&the_anim, fp);
1136 if ( can_drop_frames ) {
1137 if ( Bm_low_mem == 1 ) {
1139 the_anim.total_frames = ( the_anim.total_frames+1)/2;
1140 } else if ( Bm_low_mem == 2 ) {
1141 the_anim.total_frames = 1;
1146 *nframes = the_anim.total_frames;
1147 if ( fps != NULL ) {
1149 *fps = the_anim.fps / 2;
1151 *fps = the_anim.fps;
1155 // first check to see if this ani already has it's frames loaded
1156 for (i = 0; i < MAX_BITMAPS; i++) {
1157 if ( (bm_bitmaps[i].type == BM_TYPE_ANI) && !stricmp(filename, bm_bitmaps[i].filename) ) {
1162 if ( i < MAX_BITMAPS ) {
1163 // in low memory modes this can happen
1165 Assert(bm_bitmaps[i].info.ani.num_frames == *nframes);
1167 return bm_bitmaps[i].handle;
1170 n = find_block_of(*nframes);
1174 // Assert( n >= 0 );
1176 int first_handle = bm_get_next_handle();
1178 Assert ( strlen(filename) < MAX_FILENAME_LEN );
1179 for ( i = 0; i < *nframes; i++ ) {
1180 memset( &bm_bitmaps[n+i], 0, sizeof(bitmap_entry) );
1181 bm_bitmaps[n+i].info.ani.first_frame = n;
1182 bm_bitmaps[n+i].info.ani.num_frames = ubyte(the_anim.total_frames);
1183 bm_bitmaps[n+i].info.ani.fps = ubyte(the_anim.fps);
1184 bm_bitmaps[n+i].bm.w = short(the_anim.width);
1185 bm_bitmaps[n+i].bm.rowsize = short(the_anim.width);
1186 bm_bitmaps[n+i].bm.h = short(the_anim.height);
1188 bm_bitmaps[n+i].bm.w /= 2;
1189 bm_bitmaps[n+i].bm.rowsize /= 2;
1190 bm_bitmaps[n+i].bm.h /= 2;
1192 bm_bitmaps[n+i].bm.flags = 0;
1193 bm_bitmaps[n+i].bm.bpp = 0;
1194 bm_bitmaps[n+i].bm.data = 0;
1195 bm_bitmaps[n+i].bm.palette = NULL;
1196 bm_bitmaps[n+i].type = BM_TYPE_ANI;
1197 bm_bitmaps[n+i].palette_checksum = 0;
1198 bm_bitmaps[n+i].signature = Bm_next_signature++;
1199 bm_bitmaps[n+i].handle = first_handle*MAX_BITMAPS + n+i;
1200 bm_bitmaps[n+i].last_used = -1;
1202 // fill in section info
1203 bm_calc_sections(&bm_bitmaps[n+i].bm);
1206 sprintf( bm_bitmaps[n+i].filename, "%s", filename );
1208 sprintf( bm_bitmaps[n+i].filename, "%s[%d]", filename, i );
1212 return bm_bitmaps[n].handle;
1215 // Gets info. w,h,or flags,nframes or fps can be NULL if you don't care.
1216 void bm_get_info( int handle, int *w, int * h, ubyte * flags, int *nframes, int *fps, bitmap_section_info **sections )
1220 if ( !bm_inited ) return;
1222 int bitmapnum = handle % MAX_BITMAPS;
1223 Assert( bm_bitmaps[bitmapnum].handle == handle ); // INVALID BITMAP HANDLE!
1225 if ( (bm_bitmaps[bitmapnum].type == BM_TYPE_NONE) || (bm_bitmaps[bitmapnum].handle != handle) ) {
1228 if (flags) *flags = 0;
1229 if (nframes) *nframes=0;
1231 if (sections != NULL) *sections = NULL;
1235 bmp = &(bm_bitmaps[bitmapnum].bm);
1239 if (flags) *flags = bmp->flags;
1240 if ( bm_bitmaps[bitmapnum].type == BM_TYPE_ANI ) {
1242 *nframes = bm_bitmaps[bitmapnum].info.ani.num_frames;
1245 *fps= bm_bitmaps[bitmapnum].info.ani.fps;
1255 if(sections != NULL){
1256 *sections = &bm_bitmaps[bitmapnum].bm.sections;
1260 uint bm_get_signature( int handle )
1262 if ( !bm_inited ) bm_init();
1264 int bitmapnum = handle % MAX_BITMAPS;
1265 Assert( bm_bitmaps[bitmapnum].handle == handle ); // INVALID BITMAP HANDLE
1267 return bm_bitmaps[bitmapnum].signature;
1270 extern int palman_is_nondarkening(int r,int g, int b);
1271 static void bm_convert_format( int bitmapnum, bitmap *bmp, ubyte bpp, ubyte flags )
1276 if(Fred_running || Pofview_running || Is_standalone){
1277 Assert(bmp->bpp == 8);
1281 if(flags & BMP_AABITMAP){
1282 Assert(bmp->bpp == 8);
1284 Assert(bmp->bpp == 16);
1288 // maybe swizzle to be an xparent texture
1289 if(!(bmp->flags & BMP_TEX_XPARENT) && (flags & BMP_TEX_XPARENT)){
1290 for(idx=0; idx<bmp->w*bmp->h; idx++){
1292 // if the pixel is transparent
1293 if ( ((ushort*)bmp->data)[idx] == Gr_t_green.mask) {
1294 switch(Bm_pixel_format){
1295 // 1555, all we need to do is zero the whole thing
1296 case BM_PIXEL_FORMAT_ARGB:
1297 case BM_PIXEL_FORMAT_ARGB_D3D:
1298 ((ushort*)bmp->data)[idx] = 0;
1301 case BM_PIXEL_FORMAT_D3D:
1303 r /= Gr_t_red.scale;
1304 g /= Gr_t_green.scale;
1305 b /= Gr_t_blue.scale;
1306 a /= Gr_t_alpha.scale;
1307 ((ushort*)bmp->data)[idx] = (unsigned short)((a<<Gr_t_alpha.shift) | (r << Gr_t_red.shift) | (g << Gr_t_green.shift) | (b << Gr_t_blue.shift));
1315 bmp->flags |= BMP_TEX_XPARENT;
1319 // basically, map the bitmap into the current palette. used to be done for all pcx's, now just for
1320 // Fred, since its the only thing that uses the software tmapper
1321 void bm_swizzle_8bit_for_fred(bitmap_entry *be, bitmap *bmp, ubyte *data, ubyte *palette)
1323 int pcx_xparent_index = -1;
1328 for (i=0; i<256; i++ ) {
1332 if ( g == 255 && r == 0 && b == 0 ) {
1334 pcx_xparent_index = i;
1336 palxlat[i] = (ubyte)(palette_find( r, g, b ));
1339 for (i=0; i<bmp->w * bmp->h; i++ ) {
1340 ubyte c = palxlat[data[i]];
1343 be->palette_checksum = gr_palette_checksum;
1346 void bm_lock_pcx( int handle, int bitmapnum, bitmap_entry *be, bitmap *bmp, ubyte bpp, ubyte flags )
1348 ubyte *data, *palette;
1352 // Unload any existing data
1353 bm_free_data( bitmapnum );
1355 // allocate bitmap data
1357 // Assert(Fred_running || Pofview_running || Is_standalone);
1358 data = (ubyte *)bm_malloc(bitmapnum, bmp->w * bmp->h );
1359 #ifdef BMPMAN_NDEBUG
1360 Assert( be->data_size == bmp->w * bmp->h );
1363 bmp->data = (uint)data;
1365 bmp->palette = gr_palette;
1366 memset( data, 0, bmp->w * bmp->h);
1368 data = (ubyte*)bm_malloc(bitmapnum, bmp->w * bmp->h * 2);
1370 bmp->data = (uint)data;
1371 bmp->palette = NULL;
1372 memset( data, 0, bmp->w * bmp->h * 2);
1375 Assert( &be->bm == bmp );
1376 #ifdef BMPMAN_NDEBUG
1377 Assert( be->data_size > 0 );
1380 // some sanity checks on flags
1381 Assert(!((flags & BMP_AABITMAP) && (flags & BMP_TEX_ANY))); // no aabitmap textures
1382 Assert(!((flags & BMP_TEX_XPARENT) && (flags & BMP_TEX_NONDARK))); // can't be a transparent texture and a nondarkening texture
1383 Assert(!((flags & BMP_TEX_NONDARK) && (gr_screen.mode == GR_DIRECT3D))); // D3D should never be trying to get nondarkening textures
1386 int pcx_error=pcx_read_bitmap_8bpp( be->filename, data, palette );
1387 if ( pcx_error != PCX_ERROR_NONE ) {
1388 // Error( LOCATION, "Couldn't open '%s'\n", be->filename );
1389 //Error( LOCATION, "Couldn't open '%s'\n", filename );
1393 // now swizzle the thing into the proper format
1394 if(Fred_running || Pofview_running){
1395 bm_swizzle_8bit_for_fred(be, bmp, data, palette);
1401 if(flags & BMP_AABITMAP){
1402 pcx_error = pcx_read_bitmap_16bpp_aabitmap( be->filename, data );
1403 } else if(flags & BMP_TEX_NONDARK){
1404 pcx_error = pcx_read_bitmap_16bpp_nondark( be->filename, data );
1406 pcx_error = pcx_read_bitmap_16bpp( be->filename, data );
1408 if ( pcx_error != PCX_ERROR_NONE ) {
1409 // Error( LOCATION, "Couldn't open '%s'\n", be->filename );
1410 //Error( LOCATION, "Couldn't open '%s'\n", filename );
1415 #ifdef BMPMAN_NDEBUG
1416 Assert( be->data_size > 0 );
1420 bm_convert_format( bitmapnum, bmp, bpp, flags );
1423 void bm_lock_ani( int handle, int bitmapnum, bitmap_entry *be, bitmap *bmp, ubyte bpp, ubyte flags )
1426 anim_instance *the_anim_instance;
1430 int first_frame, nframes;
1432 first_frame = be->info.ani.first_frame;
1433 nframes = bm_bitmaps[first_frame].info.ani.num_frames;
1435 if ( (the_anim = anim_load(bm_bitmaps[first_frame].filename)) == NULL ) {
1436 // Error(LOCATION, "Error opening %s in bm_lock\n", be->filename);
1439 if ( (the_anim_instance = init_anim_instance(the_anim, bpp)) == NULL ) {
1440 // Error(LOCATION, "Error opening %s in bm_lock\n", be->filename);
1441 anim_free(the_anim);
1444 int can_drop_frames = 0;
1446 if ( the_anim->total_frames != bm_bitmaps[first_frame].info.ani.num_frames ) {
1447 can_drop_frames = 1;
1449 bm = &bm_bitmaps[first_frame].bm;
1451 size = bm->w * bm->h * 2;
1453 size = bm->w * bm->h;
1456 for ( i=0; i<nframes; i++ ) {
1457 be = &bm_bitmaps[first_frame+i];
1458 bm = &bm_bitmaps[first_frame+i].bm;
1460 // Unload any existing data
1461 bm_free_data( first_frame+i );
1464 // briefing editor in Fred2 uses aabitmaps (ani's) - force to 8 bit
1465 if(Fred_running || Is_standalone){
1470 bm->data = (uint)bm_malloc(first_frame + i, size);
1472 frame_data = anim_get_next_raw_buffer(the_anim_instance, 0 ,flags & BMP_AABITMAP ? 1 : 0, bm->bpp);
1474 if ( frame_data == NULL ) {
1475 // Error(LOCATION,"Fatal error locking .ani file: %s\n", be->filename);
1481 dptr = (ubyte *)bm->data;
1483 if ( (bm->w!=the_anim->width) || (bm->h!=the_anim->height) ) {
1485 // Int3(); // not ready yet - should only be ingame
1490 fix u, utmp, v, du, dv;
1494 du = ( the_anim->width*F1_0 ) / bm->w;
1495 dv = ( the_anim->height*F1_0 ) / bm->h;
1497 for (h = 0; h < bm->h; h++) {
1498 ubyte *drow = &dptr[bm->w * h];
1499 ubyte *srow = &sptr[f2i(v)*the_anim->width];
1503 for (w = 0; w < bm->w; w++) {
1504 *drow++ = srow[f2i(utmp)];
1513 fix u, utmp, v, du, dv;
1517 du = ( the_anim->width*F1_0 ) / bm->w;
1518 dv = ( the_anim->height*F1_0 ) / bm->h;
1520 for (h = 0; h < bm->h; h++) {
1521 ushort *drow = &((ushort*)dptr)[bm->w * h];
1522 ushort *srow = &((ushort*)sptr)[f2i(v)*the_anim->width];
1526 for (w = 0; w < bm->w; w++) {
1527 *drow++ = srow[f2i(utmp)];
1535 memcpy(dptr, sptr, size);
1538 bm_convert_format( first_frame+i, bm, bpp, flags );
1541 if ( (i < nframes-1) && can_drop_frames ) {
1542 frame_data = anim_get_next_raw_buffer(the_anim_instance, 0, flags & BMP_AABITMAP ? 1 : 0, bm->bpp);
1545 //mprintf(( "Checksum = %d\n", be->palette_checksum ));
1548 free_anim_instance(the_anim_instance);
1549 anim_free(the_anim);
1553 void bm_lock_user( int handle, int bitmapnum, bitmap_entry *be, bitmap *bmp, ubyte bpp, ubyte flags )
1558 // Unload any existing data
1559 bm_free_data( bitmapnum );
1561 switch( be->info.user.bpp ) {
1562 case 16: // user 16 bit bitmap
1564 bmp->flags = be->info.user.flags;
1565 bmp->data = (uint)be->info.user.data;
1568 case 8: // Going from 8 bpp to something (probably only for aabitmaps)
1570 Assert(flags & BMP_AABITMAP);
1572 bmp->data = (uint)malloc(bmp->w * bmp->h * 2);
1573 bmp->flags = be->info.user.flags;
1574 bmp->palette = NULL;
1576 // go through and map the pixels
1577 for(idx=0; idx<bmp->w * bmp->h; idx++){
1578 bit_16 = (ushort)((ubyte*)be->info.user.data)[idx];
1579 Assert(bit_16 <= 255);
1581 // stuff the final result
1582 memcpy((char*)bmp->data + (idx * 2), &bit_16, sizeof(ushort));
1585 Assert(flags & BMP_AABITMAP);
1587 bmp->flags = be->info.user.flags;
1588 bmp->data = (uint)be->info.user.data;
1592 // Error( LOCATION, "Unhandled user bitmap conversion from %d to %d bpp", be->info.user.bpp, bmp->bpp );
1595 bm_convert_format( bitmapnum, bmp, bpp, flags );
1598 void bm_lock_tga( int handle, int bitmapnum, bitmap_entry *be, bitmap *bmp, ubyte bpp, ubyte flags )
1602 // Unload any existing data
1603 bm_free_data( bitmapnum );
1605 if(Fred_running || Is_standalone){
1611 // should never try to make an aabitmap out of a targa
1612 Assert(!(flags & BMP_AABITMAP));
1614 // allocate bitmap data
1616 data = (ubyte*)bm_malloc(bitmapnum, bmp->w * bmp->h * 2);
1618 data = (ubyte*)bm_malloc(bitmapnum, bmp->w * bmp->h);
1621 bmp->data = (uint)data;
1622 bmp->palette = NULL;
1624 memset( data, 0, bmp->w * bmp->h * 2);
1626 memset( data, 0, bmp->w * bmp->h );
1629 Assert( &be->bm == bmp );
1630 #ifdef BMPMAN_NDEBUG
1631 Assert( be->data_size > 0 );
1634 int tga_error=targa_read_bitmap( be->filename, data, NULL, (bpp == 16) ? 2 : 1);
1635 if ( tga_error != TARGA_ERROR_NONE ) {
1636 // Error( LOCATION, "Couldn't open '%s'\n", be->filename );
1637 //Error( LOCATION, "Couldn't open '%s'\n", filename );
1641 #ifdef BMPMAN_NDEBUG
1642 Assert( be->data_size > 0 );
1646 bm_convert_format( bitmapnum, bmp, bpp, flags );
1649 MONITOR( NumBitmapPage );
1650 MONITOR( SizeBitmapPage );
1652 // This locks down a bitmap and returns a pointer to a bitmap
1653 // that can be accessed until you call bm_unlock. Only lock
1654 // a bitmap when you need it! This will convert it into the
1655 // appropriate format also.
1656 bitmap * bm_lock( int handle, ubyte bpp, ubyte flags )
1662 if ( !bm_inited ) bm_init();
1664 int bitmapnum = handle % MAX_BITMAPS;
1665 Assert( bm_bitmaps[bitmapnum].handle == handle ); // INVALID BITMAP HANDLE
1667 // flags &= (~BMP_RLE);
1669 // if we're on a standalone server, aways for it to lock to 8 bits
1674 // otherwise do it as normal
1676 if(Fred_running || Pofview_running){
1678 Assert( (bm_bitmaps[bitmapnum].type == BM_TYPE_PCX) || (bm_bitmaps[bitmapnum].type == BM_TYPE_ANI) || (bm_bitmaps[bitmapnum].type == BM_TYPE_TGA));
1680 if(flags & BMP_AABITMAP){
1683 Assert( bpp == 16 );
1688 be = &bm_bitmaps[bitmapnum];
1691 // If you hit this assert, chances are that someone freed the
1692 // wrong bitmap and now someone is trying to use that bitmap.
1694 Assert( be->type != BM_TYPE_NONE );
1696 // Increment ref count for bitmap since lock was made on it.
1697 Assert(be->ref_count >= 0);
1698 be->ref_count++; // Lock it before we page in data; this prevents a callback from freeing this
1699 // as it gets read in
1701 // Mark this bitmap as used this frame
1702 #ifdef BMPMAN_NDEBUG
1703 if ( be->used_this_frame < 255 ) {
1704 be->used_this_frame++;
1708 // if bitmap hasn't been loaded yet, then load it from disk
1709 // reread the bitmap from disk under certain conditions
1710 int pal_changed = 0;
1711 int rle_changed = 0;
1712 int fake_xparent_changed = 0;
1713 if ( (bmp->data == NULL) || (bpp != bmp->bpp) || pal_changed || rle_changed || fake_xparent_changed ) {
1714 Assert(be->ref_count == 1);
1716 if ( be->type != BM_TYPE_USER ) {
1717 if ( bmp->data == NULL ) {
1718 nprintf (("BmpMan","Loading %s for the first time.\n", be->filename));
1719 } else if ( bpp != bmp->bpp ) {
1720 nprintf (("BmpMan","Reloading %s from bitdepth %d to bitdepth %d\n", be->filename, bmp->bpp, bpp));
1721 } else if ( pal_changed ) {
1722 nprintf (("BmpMan","Reloading %s to remap palette\n", be->filename));
1723 } else if ( rle_changed ) {
1724 nprintf (("BmpMan","Reloading %s to change RLE.\n", be->filename));
1725 } else if ( fake_xparent_changed ) {
1726 nprintf (("BmpMan","Reloading %s to change fake xparency.\n", be->filename));
1730 MONITOR_INC( NumBitmapPage, 1 );
1731 MONITOR_INC( SizeBitmapPage, bmp->w*bmp->h );
1734 if ( be->type != BM_TYPE_USER ) {
1736 strcpy( flag_text, "--" );
1737 nprintf(( "Paging", "Loading %s (%dx%dx%dx%s)\n", be->filename, bmp->w, bmp->h, bpp, flag_text ));
1741 // select proper format
1742 if(flags & BMP_AABITMAP){
1743 BM_SELECT_ALPHA_TEX_FORMAT();
1744 } else if(flags & BMP_TEX_ANY){
1745 BM_SELECT_TEX_FORMAT();
1747 BM_SELECT_SCREEN_FORMAT();
1750 switch ( be->type ) {
1752 bm_lock_pcx( handle, bitmapnum, be, bmp, bpp, flags );
1756 bm_lock_ani( handle, bitmapnum, be, bmp, bpp, flags );
1760 bm_lock_user( handle, bitmapnum, be, bmp, bpp, flags );
1764 bm_lock_tga( handle, bitmapnum, be, bmp, bpp, flags );
1768 Warning(LOCATION, "Unsupported type in bm_lock -- %d\n", be->type );
1772 // always go back to screen format
1773 BM_SELECT_SCREEN_FORMAT();
1776 if ( be->type == BM_TYPE_ANI ) {
1777 int i,first = bm_bitmaps[bitmapnum].info.ani.first_frame;
1779 for ( i=0; i< bm_bitmaps[first].info.ani.num_frames; i++ ) {
1780 // Mark all the bitmaps in this bitmap or animation as recently used
1781 bm_bitmaps[first+i].last_used = timer_get_milliseconds();
1783 // Mark all the bitmaps in this bitmap or animation as used for the usage tracker.
1784 #ifdef BMPMAN_NDEBUG
1785 bm_bitmaps[first+i].used_count++;
1787 bm_bitmaps[first+i].used_flags = flags;
1790 // Mark all the bitmaps in this bitmap or animation as recently used
1791 be->last_used = timer_get_milliseconds();
1793 // Mark all the bitmaps in this bitmap or animation as used for the usage tracker.
1794 #ifdef BMPMAN_NDEBUG
1797 be->used_flags = flags;
1805 // Decrements the ref_count member of the bitmap_entry struct. A bitmap can only be unloaded
1806 // when the ref_count is 0.
1808 void bm_unlock( int handle )
1813 int bitmapnum = handle % MAX_BITMAPS;
1814 Assert( bm_bitmaps[bitmapnum].handle == handle ); // INVALID BITMAP HANDLE
1816 Assert(bitmapnum >= 0 && bitmapnum < MAX_BITMAPS);
1817 if ( !bm_inited ) bm_init();
1819 be = &bm_bitmaps[bitmapnum];
1823 Assert(be->ref_count >= 0); // Trying to unlock data more times than lock was called!!!
1832 char *bm_get_filename(int handle)
1836 n = handle % MAX_BITMAPS;
1837 Assert(bm_bitmaps[n].handle == handle); // INVALID BITMAP HANDLE
1838 return bm_bitmaps[n].filename;
1841 void bm_get_palette(int handle, ubyte *pal, char *name)
1846 int n= handle % MAX_BITMAPS;
1847 Assert( bm_bitmaps[n].handle == handle ); // INVALID BITMAP HANDLE
1849 filename = bm_bitmaps[n].filename;
1852 strcpy( name, filename );
1855 int pcx_error=pcx_read_header( filename, &w, &h, pal );
1856 if ( pcx_error != PCX_ERROR_NONE ){
1857 // Error(LOCATION, "Couldn't open '%s'\n", filename );
1861 // --------------------------------------------------------------------------------------
1862 // bm_release() - unloads the bitmap's data and entire slot, so bitmap 'n' won't be valid anymore
1864 // parameters: n => index into bm_bitmaps ( index returned from bm_load() or bm_create() )
1868 void bm_release(int handle)
1872 int n = handle % MAX_BITMAPS;
1874 Assert(n >= 0 && n < MAX_BITMAPS);
1875 be = &bm_bitmaps[n];
1877 if ( bm_bitmaps[n].type == BM_TYPE_NONE ) {
1878 return; // Already been released?
1881 if ( bm_bitmaps[n].type != BM_TYPE_USER ) {
1885 Assert( be->handle == handle ); // INVALID BITMAP HANDLE
1887 // If it is locked, cannot free it.
1888 if (be->ref_count != 0) {
1889 nprintf(("BmpMan", "tried to unload %s that has a lock count of %d.. not unloading\n", be->filename, be->ref_count));
1895 if ( bm_bitmaps[n].type == BM_TYPE_USER ) {
1896 bm_bitmaps[n].info.user.data = NULL;
1897 bm_bitmaps[n].info.user.bpp = 0;
1901 bm_bitmaps[n].type = BM_TYPE_NONE;
1903 // Fill in bogus structures!
1906 strcpy( bm_bitmaps[n].filename, "IVE_BEEN_RELEASED!" );
1907 bm_bitmaps[n].signature = 0xDEADBEEF; // a unique signature identifying the data
1908 bm_bitmaps[n].palette_checksum = 0xDEADBEEF; // checksum used to be sure bitmap is in current palette
1911 #ifdef BMPMAN_NDEBUG
1912 bm_bitmaps[n].data_size = -1; // How much data this bitmap uses
1914 bm_bitmaps[n].ref_count = -1; // Number of locks on bitmap. Can't unload unless ref_count is 0.
1917 bm_bitmaps[n].bm.w = bm_bitmaps[n].bm.h = -1;
1919 // Stuff needed for animations
1920 // Stuff needed for user bitmaps
1921 memset( &bm_bitmaps[n].info, 0, sizeof(bm_extra_info) );
1923 bm_bitmaps[n].handle = -1;
1931 // --------------------------------------------------------------------------------------
1932 // bm_unload() - unloads the data, but not the bitmap info.
1934 // parameters: n => index into bm_bitmaps ( index returned from bm_load() or bm_create() )
1936 // returns: 0 => unload failed
1937 // 1 => unload successful
1939 int bm_unload( int handle )
1944 int n = handle % MAX_BITMAPS;
1946 Assert(n >= 0 && n < MAX_BITMAPS);
1947 be = &bm_bitmaps[n];
1950 if ( be->type == BM_TYPE_NONE ) {
1951 return 0; // Already been released
1954 Assert( be->handle == handle ); // INVALID BITMAP HANDLE!
1956 // If it is locked, cannot free it.
1957 if (be->ref_count != 0) {
1958 nprintf(("BmpMan", "tried to unload %s that has a lock count of %d.. not unloading\n", be->filename, be->ref_count));
1962 nprintf(("BmpMan", "unloading %s. %dx%dx%d\n", be->filename, bmp->w, bmp->h, bmp->bpp));
1963 bm_free_data(n); // clears flags, bbp, data, etc
1969 // unload all used bitmaps
1970 void bm_unload_all()
1974 for (i = 0; i < MAX_BITMAPS; i++) {
1975 if ( bm_bitmaps[i].type != BM_TYPE_NONE ) {
1976 bm_unload(bm_bitmaps[i].handle);
1982 DCF(bmpman,"Shows/changes bitmap caching parameters and usage")
1985 dc_get_arg(ARG_STRING);
1986 if ( !strcmp( Dc_arg, "flush" )) {
1987 dc_printf( "Total RAM usage before flush: %d bytes\n", bm_texture_ram );
1989 for (i = 0; i < MAX_BITMAPS; i++) {
1990 if ( bm_bitmaps[i].type != BM_TYPE_NONE ) {
1994 dc_printf( "Total RAM after flush: %d bytes\n", bm_texture_ram );
1995 } else if ( !strcmp( Dc_arg, "ram" )) {
1996 dc_get_arg(ARG_INT);
1997 Bm_max_ram = Dc_arg_int*1024*1024;
1999 // print usage, not stats
2005 dc_printf( "Usage: BmpMan keyword\nWhere keyword can be in the following forms:\n" );
2006 dc_printf( "BmpMan flush Unloads all bitmaps.\n" );
2007 dc_printf( "BmpMan ram x Sets max mem usage to x MB. (Set to 0 to have no limit.)\n" );
2008 dc_printf( "\nUse '? BmpMan' to see status of Bitmap manager.\n" );
2009 Dc_status = 0; // don't print status if help is printed. Too messy.
2013 dc_printf( "Total RAM usage: %d bytes\n", bm_texture_ram );
2016 if ( Bm_max_ram > 1024*1024 )
2017 dc_printf( "Max RAM allowed: %.1f MB\n", i2fl(Bm_max_ram)/(1024.0f*1024.0f) );
2018 else if ( Bm_max_ram > 1024 )
2019 dc_printf( "Max RAM allowed: %.1f KB\n", i2fl(Bm_max_ram)/(1024.0f) );
2020 else if ( Bm_max_ram > 0 )
2021 dc_printf( "Max RAM allowed: %d bytes\n", Bm_max_ram );
2023 dc_printf( "No RAM limit\n" );
2029 // Marks a texture as being used for this level
2030 void bm_page_in_texture( int bitmapnum, int nframes )
2033 for (i=0; i<nframes;i++ ) {
2034 int n = bitmapnum % MAX_BITMAPS;
2036 bm_bitmaps[n+i].preloaded = 1;
2038 if ( D3D_enabled ) {
2039 bm_bitmaps[n+i].used_flags = BMP_TEX_OTHER;
2041 bm_bitmaps[n+i].used_flags = 0;
2046 // Marks a texture as being used for this level
2047 // If num_frames is passed, assume this is an animation
2048 void bm_page_in_nondarkening_texture( int bitmapnum, int nframes )
2051 for (i=0; i<nframes;i++ ) {
2052 int n = bitmapnum % MAX_BITMAPS;
2054 bm_bitmaps[n+i].preloaded = 4;
2056 if ( D3D_enabled ) {
2057 bm_bitmaps[n+i].used_flags = BMP_TEX_NONDARK;
2059 bm_bitmaps[n+i].used_flags = 0;
2064 // marks a texture as being a transparent textyre used for this level
2065 // Marks a texture as being used for this level
2066 // If num_frames is passed, assume this is an animation
2067 void bm_page_in_xparent_texture( int bitmapnum, int nframes)
2070 for (i=0; i<nframes;i++ ) {
2071 int n = bitmapnum % MAX_BITMAPS;
2073 bm_bitmaps[n+i].preloaded = 3;
2075 if ( D3D_enabled ) {
2076 // bm_bitmaps[n+i].used_flags = BMP_NO_PALETTE_MAP;
2077 bm_bitmaps[n+i].used_flags = BMP_TEX_XPARENT;
2079 bm_bitmaps[n+i].used_flags = 0;
2084 // Marks an aabitmap as being used for this level
2085 void bm_page_in_aabitmap( int bitmapnum, int nframes )
2089 for (i=0; i<nframes;i++ ) {
2090 int n = bitmapnum % MAX_BITMAPS;
2092 bm_bitmaps[n+i].preloaded = 2;
2094 if ( D3D_enabled ) {
2095 bm_bitmaps[n+i].used_flags = BMP_AABITMAP;
2097 bm_bitmaps[n+i].used_flags = 0;
2104 // Tell the bitmap manager to start keeping track of what bitmaps are used where.
2105 void bm_page_in_start()
2111 // Mark all as inited
2112 for (i = 0; i < MAX_BITMAPS; i++) {
2113 if ( bm_bitmaps[i].type != BM_TYPE_NONE ) {
2114 bm_unload(bm_bitmaps[i].handle);
2116 bm_bitmaps[i].preloaded = 0;
2117 #ifdef BMPMAN_NDEBUG
2118 bm_bitmaps[i].used_count = 0;
2120 bm_bitmaps[i].used_flags = 0;
2125 extern void gr_d3d_preload_init();
2126 extern int gr_d3d_preload(int bitmap_num, int is_aabitmap );
2128 void bm_page_in_stop()
2131 int ship_info_index;
2133 nprintf(( "BmpInfo","BMPMAN: Loading all used bitmaps.\n" ));
2135 // Load all the ones that are supposed to be loaded for this level.
2138 #ifdef BMPMAN_NDEBUG
2142 int d3d_preloading = 1;
2144 gr_d3d_preload_init();
2146 for (i = 0; i < MAX_BITMAPS; i++) {
2147 if ( bm_bitmaps[i].type != BM_TYPE_NONE ) {
2148 if ( bm_bitmaps[i].preloaded ) {
2149 #ifdef BMPMAN_SPECIAL_NONDARK
2150 // if this is a texture, check to see if a ship uses it
2151 ship_info_index = ship_get_texture(bm_bitmaps[i].handle);
2152 // use the colors from this ship
2153 if((ship_info_index >= 0) && (Ship_info[ship_info_index].num_nondark_colors > 0)){
2154 // mprintf(("Using custom pixels for %s\n", Ship_info[ship_info_index].name));
2155 palman_set_nondarkening(Ship_info[ship_info_index].nondark_colors, Ship_info[ship_info_index].num_nondark_colors);
2157 // use the colors from the default table
2159 // mprintf(("Using default pixels\n"));
2160 palman_set_nondarkening(Palman_non_darkening_default, Palman_num_nondarkening_default);
2164 // if preloaded == 3, load it as an xparent texture
2165 if(bm_bitmaps[i].used_flags == BMP_AABITMAP){
2166 bm_lock( bm_bitmaps[i].handle, 8, bm_bitmaps[i].used_flags );
2168 bm_lock( bm_bitmaps[i].handle, 16, bm_bitmaps[i].used_flags );
2170 bm_unlock( bm_bitmaps[i].handle );
2172 if ( d3d_preloading ) {
2173 if ( !gr_d3d_preload(bm_bitmaps[i].handle, (bm_bitmaps[i].preloaded==2) ) ) {
2174 mprintf(( "Out of VRAM. Done preloading.\n" ));
2180 #ifdef BMPMAN_NDEBUG
2181 if ( Bm_ram_freed ) {
2182 nprintf(( "BmpInfo","BMPMAN: Not enough cache memory to load all level bitmaps\n" ));
2190 nprintf(( "BmpInfo","BMPMAN: Loaded %d bitmaps that are marked as used for this level.\n", n ));
2192 int total_bitmaps = 0;
2193 for (i = 0; i < MAX_BITMAPS; i++) {
2194 if ( bm_bitmaps[i].type != BM_TYPE_NONE ) {
2197 if ( bm_bitmaps[i].type == BM_TYPE_USER ) {
2198 mprintf(( "User bitmap '%s'\n", bm_bitmaps[i].filename ));
2202 mprintf(( "Bmpman: %d/%d bitmap slots in use.\n", total_bitmaps, MAX_BITMAPS ));
2203 //mprintf(( "Bmpman: Usage went from %d KB to %d KB.\n", usage_before/1024, usage_after/1024 ));
2208 int bm_get_cache_slot( int bitmap_id, int separate_ani_frames )
2210 int n = bitmap_id % MAX_BITMAPS;
2212 Assert( bm_bitmaps[n].handle == bitmap_id ); // INVALID BITMAP HANDLE
2214 bitmap_entry *be = &bm_bitmaps[n];
2216 if ( (!separate_ani_frames) && (be->type == BM_TYPE_ANI) ) {
2217 return be->info.ani.first_frame;
2224 // convert a 24 bit value to a 16 bit value
2225 void bm_24_to_16(int bit_24, ushort *bit_16)
2227 ubyte *pixel = (ubyte*)&bit_24;
2230 bm_set_components((ubyte*)bit_16, (ubyte*)&pixel[0], (ubyte*)&pixel[1], (ubyte*)&pixel[2], &alpha);
2233 extern int D3D_32bit;
2235 void (*bm_set_components)(ubyte *pixel, ubyte *r, ubyte *g, ubyte *b, ubyte *a) = NULL;
2237 void bm_set_components_argb(ubyte *pixel, ubyte *rv, ubyte *gv, ubyte *bv, ubyte *av)
2240 *((ushort*)pixel) |= (ushort)(( (int)*rv / Gr_current_red->scale ) << Gr_current_red->shift);
2241 *((ushort*)pixel) |= (ushort)(( (int)*gv / Gr_current_green->scale ) << Gr_current_green->shift);
2242 *((ushort*)pixel) |= (ushort)(( (int)*bv / Gr_current_blue->scale ) << Gr_current_blue->shift);
2243 *((ushort*)pixel) &= ~(0x8000);
2245 *((ushort*)pixel) |= 0x8000;
2249 void bm_set_components_d3d(ubyte *pixel, ubyte *rv, ubyte *gv, ubyte *bv, ubyte *av)
2252 *((ushort*)pixel) |= (ushort)(( (int)*rv / Gr_current_red->scale ) << Gr_current_red->shift);
2253 *((ushort*)pixel) |= (ushort)(( (int)*gv / Gr_current_green->scale ) << Gr_current_green->shift);
2254 *((ushort*)pixel) |= (ushort)(( (int)*bv / Gr_current_blue->scale ) << Gr_current_blue->shift);
2256 *((ushort*)pixel) = (ushort)Gr_current_green->mask;
2260 void bm_set_components_argb_d3d_16_screen(ubyte *pixel, ubyte *rv, ubyte *gv, ubyte *bv, ubyte *av)
2262 *((ushort*)pixel) |= (ushort)(( (int)*rv / Gr_current_red->scale ) << Gr_current_red->shift);
2263 *((ushort*)pixel) |= (ushort)(( (int)*gv / Gr_current_green->scale ) << Gr_current_green->shift);
2264 *((ushort*)pixel) |= (ushort)(( (int)*bv / Gr_current_blue->scale ) << Gr_current_blue->shift);
2266 *((ushort*)pixel) = (ushort)Gr_current_green->mask;
2270 void bm_set_components_argb_d3d_32_screen(ubyte *pixel, ubyte *rv, ubyte *gv, ubyte *bv, ubyte *av)
2272 *((uint*)pixel) |= (uint)(( (int)*rv / Gr_current_red->scale ) << Gr_current_red->shift);
2273 *((uint*)pixel) |= (uint)(( (int)*gv / Gr_current_green->scale ) << Gr_current_green->shift);
2274 *((uint*)pixel) |= (uint)(( (int)*bv / Gr_current_blue->scale ) << Gr_current_blue->shift);
2276 *((uint*)pixel) = (uint)Gr_current_green->mask;
2280 void bm_set_components_argb_d3d_16_tex(ubyte *pixel, ubyte *rv, ubyte *gv, ubyte *bv, ubyte *av)
2282 *((ushort*)pixel) |= (ushort)(( (int)*rv / Gr_current_red->scale ) << Gr_current_red->shift);
2283 *((ushort*)pixel) |= (ushort)(( (int)*gv / Gr_current_green->scale ) << Gr_current_green->shift);
2284 *((ushort*)pixel) |= (ushort)(( (int)*bv / Gr_current_blue->scale ) << Gr_current_blue->shift);
2285 *((ushort*)pixel) &= ~(Gr_current_alpha->mask);
2287 *((ushort*)pixel) |= (ushort)(Gr_current_alpha->mask);
2289 *((ushort*)pixel) = 0;
2293 void bm_set_components_argb_d3d_32_tex(ubyte *pixel, ubyte *rv, ubyte *gv, ubyte *bv, ubyte *av)
2295 *((ushort*)pixel) |= (ushort)(( (int)*rv / Gr_current_red->scale ) << Gr_current_red->shift);
2296 *((ushort*)pixel) |= (ushort)(( (int)*gv / Gr_current_green->scale ) << Gr_current_green->shift);
2297 *((ushort*)pixel) |= (ushort)(( (int)*bv / Gr_current_blue->scale ) << Gr_current_blue->shift);
2298 *((ushort*)pixel) &= ~(Gr_current_alpha->mask);
2300 *((ushort*)pixel) |= (ushort)(Gr_current_alpha->mask);
2302 *((ushort*)pixel) = 0;
2306 // for selecting pixel formats
2307 void BM_SELECT_SCREEN_FORMAT()
2309 Gr_current_red = &Gr_red;
2310 Gr_current_green = &Gr_green;
2311 Gr_current_blue = &Gr_blue;
2312 Gr_current_alpha = &Gr_alpha;
2315 if(gr_screen.mode == GR_GLIDE){
2316 bm_set_components = bm_set_components_argb;
2317 } else if(gr_screen.mode == GR_DIRECT3D){
2318 if(Bm_pixel_format == BM_PIXEL_FORMAT_D3D){
2319 bm_set_components = bm_set_components_d3d;
2322 bm_set_components = bm_set_components_argb_d3d_32_screen;
2324 bm_set_components = bm_set_components_argb_d3d_16_screen;
2330 void BM_SELECT_TEX_FORMAT()
2332 Gr_current_red = &Gr_t_red;
2333 Gr_current_green = &Gr_t_green;
2334 Gr_current_blue = &Gr_t_blue;
2335 Gr_current_alpha = &Gr_t_alpha;
2338 if(gr_screen.mode == GR_GLIDE){
2339 bm_set_components = bm_set_components_argb;
2340 } else if(gr_screen.mode == GR_DIRECT3D){
2341 if(Bm_pixel_format == BM_PIXEL_FORMAT_D3D){
2342 bm_set_components = bm_set_components_d3d;
2345 bm_set_components = bm_set_components_argb_d3d_32_tex;
2347 bm_set_components = bm_set_components_argb_d3d_16_tex;
2353 void BM_SELECT_ALPHA_TEX_FORMAT()
2355 Gr_current_red = &Gr_ta_red;
2356 Gr_current_green = &Gr_ta_green;
2357 Gr_current_blue = &Gr_ta_blue;
2358 Gr_current_alpha = &Gr_ta_alpha;
2361 if(gr_screen.mode == GR_GLIDE){
2362 bm_set_components = bm_set_components_argb;
2363 } else if(gr_screen.mode == GR_DIRECT3D){
2364 if(Bm_pixel_format == BM_PIXEL_FORMAT_D3D){
2365 bm_set_components = bm_set_components_d3d;
2368 bm_set_components = bm_set_components_argb_d3d_32_tex;
2370 bm_set_components = bm_set_components_argb_d3d_16_tex;
2376 // set the rgba components of a pixel, any of the parameters can be -1
2378 void bm_set_components(ubyte *pixel, ubyte *rv, ubyte *gv, ubyte *bv, ubyte *av)
2382 // pick a byte size - 32 bits only if 32 bit mode d3d and screen format
2383 if(D3D_32bit && (Gr_current_red == &Gr_red)){
2388 *((uint*)pixel) |= (uint)(( (int)*rv / Gr_current_red->scale ) << Gr_current_red->shift);
2390 *((ushort*)pixel) |= (ushort)(( (int)*rv / Gr_current_red->scale ) << Gr_current_red->shift);
2393 *((uint*)pixel) |= (uint)(( (int)*gv / Gr_current_green->scale ) << Gr_current_green->shift);
2395 *((ushort*)pixel) |= (ushort)(( (int)*gv / Gr_current_green->scale ) << Gr_current_green->shift);
2398 *((uint*)pixel) |= (uint)(( (int)*bv / Gr_current_blue->scale ) << Gr_current_blue->shift);
2400 *((ushort*)pixel) |= (ushort)(( (int)*bv / Gr_current_blue->scale ) << Gr_current_blue->shift);
2403 // NOTE - this is a semi-hack. For direct3d we don't use an alpha bit, so if *av == 0, we just set the whole pixel to be Gr_green.mask
2404 // ergo, we need to do this _last_
2405 switch(Bm_pixel_format){
2406 // glide has an alpha channel so we have to unset ir or set it each time
2407 case BM_PIXEL_FORMAT_ARGB:
2409 *((ushort*)pixel) &= ~(0x8000);
2411 *((ushort*)pixel) |= 0x8000;
2415 // this d3d format has no alpha channel, so only make it "transparent", never make it "non-transparent"
2416 case BM_PIXEL_FORMAT_D3D:
2419 *((ushort*)pixel) = (ushort)Gr_current_green->mask;
2423 // nice 1555 texture format
2424 case BM_PIXEL_FORMAT_ARGB_D3D:
2425 // if we're writing to normal texture format
2426 if(Gr_current_red == &Gr_t_red){
2428 *((ushort*)pixel) &= ~(Gr_current_alpha->mask);
2430 *((ushort*)pixel) |= (ushort)(Gr_current_alpha->mask);
2432 *((ushort*)pixel) = 0;
2435 // otherwise if we're writing to screen format, still do it the green mask way
2439 *((uint*)pixel) = (uint)Gr_current_green->mask;
2441 *((ushort*)pixel) = (ushort)Gr_current_green->mask;
2450 // get the rgba components of a pixel, any of the parameters can be NULL
2451 void bm_get_components(ubyte *pixel, ubyte *r, ubyte *g, ubyte *b, ubyte *a)
2455 // pick a byte size - 32 bits only if 32 bit mode d3d and screen format
2456 if(D3D_32bit && (Gr_current_red == &Gr_red)){
2462 *r = ubyte(( (*((uint*)pixel) & Gr_current_red->mask)>>Gr_current_red->shift)*Gr_current_red->scale);
2464 *r = ubyte(( ( ((ushort*)pixel)[0] & Gr_current_red->mask)>>Gr_current_red->shift)*Gr_current_red->scale);
2469 *g = ubyte(( (*((uint*)pixel) & Gr_current_green->mask) >>Gr_current_green->shift)*Gr_current_green->scale);
2471 *g = ubyte(( ( ((ushort*)pixel)[0] & Gr_current_green->mask) >>Gr_current_green->shift)*Gr_current_green->scale);
2476 *b = ubyte(( (*((uint*)pixel) & Gr_current_blue->mask)>>Gr_current_blue->shift)*Gr_current_blue->scale);
2478 *b = ubyte(( ( ((ushort*)pixel)[0] & Gr_current_blue->mask)>>Gr_current_blue->shift)*Gr_current_blue->scale);
2482 // get the alpha value
2486 switch(Bm_pixel_format){
2487 // glide has an alpha channel so we have to unset ir or set it each time
2488 case BM_PIXEL_FORMAT_ARGB:
2490 if(!( ((ushort*)pixel)[0] & 0x8000)){
2495 // this d3d format has no alpha channel, so only make it "transparent", never make it "non-transparent"
2496 case BM_PIXEL_FORMAT_D3D:
2498 if( *((ushort*)pixel) == Gr_current_green->mask){
2503 // nice 1555 texture format mode
2504 case BM_PIXEL_FORMAT_ARGB_D3D:
2505 // if we're writing to a normal texture, use nice alpha bits
2506 if(Gr_current_red == &Gr_t_red){
2509 if(!(*((ushort*)pixel) & Gr_current_alpha->mask)){
2513 // otherwise do it as normal
2516 if(*((int*)pixel) == Gr_current_green->mask){
2520 if(*((ushort*)pixel) == Gr_current_green->mask){
2530 void bm_get_filename(int bitmapnum, char *filename)
2532 int n = bitmapnum % MAX_BITMAPS;
2535 strcpy(filename, bm_bitmaps[n].filename);
2538 // given a bitmap and a section, return the size (w, h)
2539 void bm_get_section_size(int bitmapnum, int sx, int sy, int *w, int *h)
2542 bitmap_section_info *sections;
2545 Assert((w != NULL) && (h != NULL));
2546 if((w == NULL) || (h == NULL)){
2551 bm_get_info(bitmapnum, &bw, &bh, NULL, NULL, NULL, §ions);
2553 // determine the width and height of this section
2554 *w = sx < (sections->num_x - 1) ? MAX_BMAP_SECTION_SIZE : bw - sections->sx[sx];
2555 *h = sy < (sections->num_y - 1) ? MAX_BMAP_SECTION_SIZE : bh - sections->sy[sy];