-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathbmpman.cpp
More file actions
3420 lines (2712 loc) · 88.2 KB
/
bmpman.cpp
File metadata and controls
3420 lines (2712 loc) · 88.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) Volition, Inc. 1999. All rights reserved.
*
* All source code herein is the property of Volition, Inc. You may not sell
* or otherwise commercially exploit the source or things you created based on the
* source.
*
*/
#ifndef NDEBUG
#define BMPMAN_NDEBUG
#endif
#define WIN32_LEAN_AND_MEAN
#define BMPMAN_INTERNAL
#include "globalincs/pstypes.h"
#include "anim/animplay.h"
#include "anim/packunpack.h"
#include "bmpman/bm_internal.h"
#include "ddsutils/ddsutils.h"
#include "debugconsole/console.h"
#include "globalincs/systemvars.h"
#include "graphics/2d.h"
#include "graphics/matrix.h"
#include "io/key.h"
#include "io/timer.h"
#include "jpgutils/jpgutils.h"
#include "network/multiutil.h"
#include "parse/parselo.h"
#include "pcxutils/pcxutils.h"
#include "pngutils/pngutils.h"
#include "ship/ship.h"
#include "tgautils/tgautils.h"
#include "tracing/Monitor.h"
#include "tracing/tracing.h"
#include <cctype>
#include <climits>
#include <iomanip>
#include <memory>
// --------------------------------------------------------------------------------------------------------------------
// Private macros.
/**
* @todo upgrade this to an inline funciton, taking bitmap_entry and const char* as arguments
*/
#define EFF_FILENAME_CHECK { if ( be->type == BM_TYPE_EFF ) strcpy_s( filename, be->info.ani.eff.filename ); else strcpy_s( filename, be->filename ); }
// --------------------------------------------------------------------------------------------------------------------
// Monitor variables
MONITOR(NumBitmapPage)
MONITOR(SizeBitmapPage)
// --------------------------------------------------------------------------------------------------------------------
// Definition of public variables (declared as extern in bmpman.h).
int ENVMAP = -1;
int IRRMAP = -1;
size_t bm_texture_ram = 0;
int Bm_paging = 0;
// Extension type lists
const BM_TYPE bm_type_list[] = { BM_TYPE_DDS, BM_TYPE_TGA, BM_TYPE_PNG, BM_TYPE_JPG, BM_TYPE_PCX };
const char *bm_ext_list[] = { ".dds", ".tga", ".png", ".jpg", ".pcx" };
const int BM_NUM_TYPES = sizeof(bm_type_list) / sizeof(bm_type_list[0]);
const BM_TYPE bm_ani_type_list[] = { BM_TYPE_EFF, BM_TYPE_ANI, BM_TYPE_PNG };
// NOTE: it would be better to have apng files use the .apng extension
// However there's lots of assumptions throughout the codebase that file extensions are only
// three chars long. If this is ever changed the the .apng extension could be used
const char *bm_ani_ext_list[] = { ".eff", ".ani", ".png" };
const int BM_ANI_NUM_TYPES = sizeof(bm_ani_type_list) / sizeof(bm_ani_type_list[0]);
void(*bm_set_components)(ubyte *pixel, ubyte *r, ubyte *g, ubyte *b, ubyte *a) = NULL;
// --------------------------------------------------------------------------------------------------------------------
// Definition of public variables (declared as extern in bm_internal.h).
SCP_vector<std::array<bitmap_slot, BM_BLOCK_SIZE>> bm_blocks;
// --------------------------------------------------------------------------------------------------------------------
// Definition of private variables at file scope (static).
static bool bm_inited = false;
static uint Bm_next_signature = 0x1234;
static int Bm_low_mem = 0;
SCP_map<int,ubyte*> bm_lookup_cache;
/**
* How much RAM bmpman can use for textures.
*
* @details Set to <1 to make it use all it wants.
*
* @note was initialized to 16*1024*1024 at some point to "use only 16MB for textures"
*/
static int Bm_max_ram = 0;
static int Bm_ignore_duplicates = 0;
static int Bm_ignore_load_count = 0;
// This needs to be declared somewhere and bm_internal.h has no own source file
gr_bitmap_info::~gr_bitmap_info() = default;
/**
* Finds if a bitmap entry contains an animation
*/
static bool bm_is_anim(bitmap_entry* entry)
{
return ((entry->type == BM_TYPE_ANI) ||
(entry->type == BM_TYPE_EFF) ||
(entry->type == BM_TYPE_PNG && entry->info.ani.apng.is_apng));
}
bitmap_slot* bm_get_slot(int handle, bool separate_ani_frames) {
Assertion(handle >= 0, "Invalid handle %d passed to bm_get_slot!", handle);
// The lower 16-bit contain the index in the bitmap block
auto index = handle & 0xFFFF;
// The upper 16-bit contain the number of the used block
auto block_index = handle >> 16;
Assertion(block_index >= 0 && block_index < (int) bm_blocks.size(),
"Bitmap handle %d has an invalid block number %d!",
handle,
block_index);
Assertion(index >= 0 && index < (int) bm_blocks[block_index].size(),
"Bitmap handle %d has an invalid block index %d!",
handle,
index);
auto slot = &bm_blocks[block_index][index];
auto entry = &slot->entry;
if (separate_ani_frames || !bm_is_anim(entry)) {
// Not an animation or we don't want the first frame so we are done now
return slot;
}
// Get the entry of the first frame by calling ourself again but this time we enforce separate ani frames (since we
// already know that this handle is the correct one)
return bm_get_slot(entry->info.ani.first_frame, true);
}
// --------------------------------------------------------------------------------------------------------------------
// Declaration of private functions and templates(declared as static type func(type param);)
bitmap_lookup::bitmap_lookup(int bitmap_num):
Bitmap_data(NULL)
{
if ( !bm_is_valid(bitmap_num) ) return;
Num_channels = 3;
if ( bm_has_alpha_channel(bitmap_num) ) {
Num_channels = 4;
}
bitmap_entry *be = bm_get_entry(bitmap_num);
Width = be->bm.w;
Height = be->bm.h;
auto cache_search = bm_lookup_cache.find(bitmap_num);
if (cache_search == bm_lookup_cache.end()) {
Bitmap_data = (ubyte*)vm_malloc(Width * Height * Num_channels * sizeof(ubyte));
gr_get_bitmap_from_texture((void*)Bitmap_data, bitmap_num);
bm_lookup_cache.insert({bitmap_num, Bitmap_data});
} else {
Bitmap_data = cache_search->second;
}
}
bitmap_lookup::~bitmap_lookup()
{
}
bool bitmap_lookup::valid()
{
return Bitmap_data != NULL;
}
float bitmap_lookup::map_texture_address(float address)
{
// assume we're just wrapping
return address - floorf(address);
}
float bitmap_lookup::get_channel_alpha(float u, float v)
{
Assert( Bitmap_data != NULL );
int x = fl2i(map_texture_address(u) * (Width-1));
int y = fl2i(map_texture_address(v) * (Height-1));
return i2fl(Bitmap_data[(y*Width + x)*Num_channels + 3]) / 255.0f;
}
void clear_bm_lookup_cache() {
for(auto &iter: bm_lookup_cache) {
free(iter.second);
}
bm_lookup_cache.clear();
}
/**
* Converts the bitmap referenced by bmp to the type specified by flags
*/
static void bm_convert_format(bitmap *bmp, ushort flags);
/**
* Frees a bitmap's data if it can
*/
static void bm_free_data(bitmap_slot* n, bool release = false);
/**
* A special version of bm_free_data() that can be safely used in gr_*_texture
* to save system memory once textures have been transfered to API memory
* it doesn't restore the slot to a pristine state, it only releases the data
*
* @attention: THIS SHOULD ONLY BE USED FROM bm_unload_fast()!!!
*/
static void bm_free_data_fast(int handle);
/**
* Given a raw filename and an extension set, try and find the bitmap
* that isn't already loaded and may exist somewhere on the disk
*
* @returns -1 if it could not be found,
* @returns index into ext_list[] if it was found as a file, fills img_cfg if available
*/
static int bm_load_sub_slow(const char *real_filename, const int num_ext, const char **ext_list, CFILE **img_cfp = NULL, int dir_type = CF_TYPE_ANY);
/**
* Given a raw filename, try and find a bitmap that's already loaded
*
* @returns 0 if it could not be found,
* @returns 1 if it already exists, fills in handle
*/
static int bm_load_sub_fast(const char *real_filename, int *handle, int dir_type = CF_TYPE_ANY, bool animated_type = false);
/**
* @brief Finds a start handle to a block of contiguous bitmap slots
*
* This will return a bitmap handle which has space for n slots.
*
* @param n How many slots the bitmap needs
* @param start_block At which block the search should start. Do not use. This is only for internal purposes.
*
* @returns -1 if the block could not be found
* @returns the handle of the block ?
*/
static int find_block_of(int n, int start_block = 0);
static int get_handle(int block, int index) {
Assertion(block >= 0, "Negative block values are not allowed!");
Assertion(index >= 0, "Negative index values are not allowed!");
return (uint32_t) block << 16 | (uint16_t) index;
}
static void allocate_new_block() {
bm_blocks.emplace_back();
auto& new_block = bm_blocks.back();
for (auto& slot : new_block) {
auto& entry = slot.entry;
entry.filename[0] = '\0';
entry.type = BM_TYPE_NONE;
entry.comp_type = BM_TYPE_NONE;
entry.dir_type = CF_TYPE_ANY;
entry.info.user.data = nullptr;
entry.mem_taken = 0;
entry.bm.data = 0;
entry.bm.palette = nullptr;
entry.info.ani.eff.type = BM_TYPE_NONE;
entry.info.ani.eff.filename[0] = '\0';
#ifdef BMPMAN_NDEBUG
entry.data_size = 0;
entry.used_count = 0;
entry.used_last_frame = 0;
entry.used_this_frame = 0;
#endif
entry.load_count = 0;
gr_bm_init(&slot);
// clears flags, bbp, data, etc
bm_free_data(&slot);
}
}
// --------------------------------------------------------------------------------------------------------------------
// Macro-defined functions
DCF(bm_frag, "Shows BmpMan fragmentation") {
if (dc_optional_string_either("help", "--help")) {
dc_printf("Displays a graphic showing the BmpMan fragmentation. Color key:\n");
dc_printf("\tGray : NONE\n");
dc_printf("\tRed : PCXn");
dc_printf("\tGreen : USER, TGA, PNG, DDS, other\n");
dc_printf("\tBlue : ANI, EFF\n\n");
dc_printf("Once done reviewing the graphic, press any key to return to the console\n");
return;
}
gr_clear();
int x = 0, y = 0;
int xs = 2, ys = 2;
int w = 4, h = 4;
for (auto& block : bm_blocks) {
for (size_t i = 0; i < BM_BLOCK_SIZE; ++i) {
switch (block[i].entry.type) {
case BM_TYPE_NONE:
gr_set_color(128, 128, 128);
break;
case BM_TYPE_PCX:
gr_set_color(255, 0, 0);
break;
case BM_TYPE_USER:
case BM_TYPE_TGA:
case BM_TYPE_PNG:
case BM_TYPE_DDS:
gr_set_color(0, 255, 0);
break;
case BM_TYPE_ANI:
case BM_TYPE_EFF:
gr_set_color(0, 0, 255);
break;
default:
gr_set_color(0, 255, 0);
break;
}
gr_rect(x + xs, y + ys, w, h);
x += w + xs + xs;
if (x > 639) {
x = 0;
y += h + ys + ys;
}
}
}
gr_flip();
key_getch();
}
DCF(bm_used, "Shows BmpMan Slot Usage") {
if (dc_optional_string_either("help", "--help")) {
dc_printf("Displays used bmpman slots usage with a breakdown per filetype\n\n");
return;
}
int none = 0, pcx = 0, user = 0, tga = 0, png = 0; int jpg = 0, dds = 0, ani = 0;
int eff = 0, eff_dds = 0, eff_tga = 0, eff_png = 0, eff_jpg = 0, eff_pcx = 0;
int render_target_dynamic = 0, render_target_static = 0;
for (auto& block : bm_blocks) {
for (size_t i = 0; i < BM_BLOCK_SIZE; ++i) {
switch (block[i].entry.type) {
case BM_TYPE_NONE:
none++;
break;
case BM_TYPE_PCX:
pcx++;
break;
case BM_TYPE_USER:
user++;
break;
case BM_TYPE_TGA:
tga++;
break;
case BM_TYPE_PNG:
// TODO distinguish png(static) from apng
png++;
break;
case BM_TYPE_JPG:
jpg++;
break;
case BM_TYPE_DDS:
dds++;
break;
case BM_TYPE_ANI:
ani++;
break;
case BM_TYPE_EFF:
eff++;
switch (block[i].entry.info.ani.eff.type) {
case BM_TYPE_DDS:
eff_dds++;
break;
case BM_TYPE_TGA:
eff_tga++;
break;
case BM_TYPE_PNG:
eff_png++;
break;
case BM_TYPE_JPG:
eff_jpg++;
break;
case BM_TYPE_PCX:
eff_pcx++;
break;
default:
Warning(LOCATION, "Unhandled EFF image type (%i), get a coder!", block[i].entry.info.ani.eff.type);
break;
}
break;
case BM_TYPE_RENDER_TARGET_STATIC:
render_target_static++;
break;
case BM_TYPE_RENDER_TARGET_DYNAMIC:
render_target_dynamic++;
break;
default:
Warning(LOCATION, "Unhandled image type (%i), get a coder!", block[i].entry.type);
break;
}
}
}
SCP_stringstream text;
text << "BmpMan Used Slots\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << none << ", NONE\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << pcx << ", PCX\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << user << ", User\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << tga << ", TGA\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << png << ", PNG\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << jpg << ", JPG\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << dds << ", DDS\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << ani << ", ANI\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << eff << ", EFF\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << eff_dds << ", EFF/DDS\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << eff_tga << ", EFF/TGA\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << eff_png << ", EFF/PNG\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << eff_jpg << ", EFF/JPG\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << eff_pcx << ", EFF/PCX\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << render_target_static << ", Render/Static\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << render_target_dynamic << ", Render/Dynamic\n";
text << " " << std::dec << std::setw(4) << std::setfill('0') << bmpman_count_bitmaps() << "/" << bmpman_count_available_slots() << ", Total\n";
text << "\n";
// TODO consider converting 1's to monospace to make debug console output prettier
mprintf(("%s", text.str().c_str())); // log for ease for copying data
dc_printf("%s", text.str().c_str()); // instant gratification
}
DCF(bmpman, "Shows/changes bitmap caching parameters and usage") {
if (dc_optional_string_either("help", "--help")) {
dc_printf("Usage: BmpMan [arg]\nWhere arg can be any of the following:\n");
dc_printf("\tflush Unloads all bitmaps.\n");
dc_printf("\tram [x] Sets max mem usage to x MB. (Set to 0 to have no limit.)\n");
dc_printf("\t? Displays status of Bitmap manager.\n");
return;
}
if (dc_optional_string_either("status", "--status") || dc_optional_string_either("?", "--?")) {
dc_printf("Total RAM usage: " SIZE_T_ARG " bytes\n", bm_texture_ram);
if (Bm_max_ram > 1024 * 1024) {
dc_printf("\tMax RAM allowed: %.1f MB\n", i2fl(Bm_max_ram) / (1024.0f*1024.0f));
} else if (Bm_max_ram > 1024) {
dc_printf("\tMax RAM allowed: %.1f KB\n", i2fl(Bm_max_ram) / (1024.0f));
} else if (Bm_max_ram > 0) {
dc_printf("\tMax RAM allowed: %d bytes\n", Bm_max_ram);
} else {
dc_printf("\tNo RAM limit\n");
}
return;
}
if (dc_optional_string("flush")) {
dc_printf("Total RAM usage before flush: " SIZE_T_ARG " bytes\n", bm_texture_ram);
for (auto& block : bm_blocks) {
for (size_t i = 0; i < BM_BLOCK_SIZE; ++i) {
if (block[i].entry.type != BM_TYPE_NONE) {
bm_free_data(&block[i]);
}
}
}
dc_printf("Total RAM after flush: " SIZE_T_ARG " bytes\n", bm_texture_ram);
} else if (dc_optional_string("ram")) {
dc_stuff_int(&Bm_max_ram);
if (Bm_max_ram > 0) {
dc_printf("BmpMan limited to %i, MB's\n", Bm_max_ram);
Bm_max_ram *= 1024 * 1024;
} else if (Bm_max_ram == 0) {
dc_printf("!!BmpMan memory is unlimited!!\n");
} else {
dc_printf("Illegal value. Must be non-negative.");
}
} else {
dc_printf("<BmpMan> No argument given\n");
}
}
DCF(bmpslots, "Writes bitmap slot info to fs2_open.log") {
if (dc_optional_string_either("help", "--help")) {
dc_printf("Usage: bmpslots\n");
dc_printf("\tWrites bitmap slot info to fs2_open.log\n");
return;
}
bm_print_bitmaps();
}
// --------------------------------------------------------------------------------------------------------------------
// Definition of all functions, in alphabetical order
void bm_close() {
if (bm_inited) {
for (auto& block : bm_blocks) {
for (auto& slot : block) {
bm_free_data(&slot); // clears flags, bbp, data, etc
if (slot.gr_info != nullptr) {
// free graphics data
delete slot.gr_info;
slot.gr_info = nullptr;
}
}
}
bm_blocks.clear();
bm_inited = false;
}
}
int bm_create(int bpp, int w, int h, void *data, int flags) {
if (bpp == 8) {
Assert(flags & BMP_AABITMAP);
} else {
Assert((bpp == 16) || (bpp == 24) || (bpp == 32));
}
Assertion(bm_inited, "bmpman must be initialized before this function can be called!");
int n = find_block_of(1);
// Out of bitmap slots
if (n == -1)
return -1;
// make sure that we have valid data
if (data == NULL) {
Int3();
return -1;
}
auto entry = bm_get_entry(n);
memset(entry, 0, sizeof(bitmap_entry));
sprintf_safe(entry->filename, "TMP%dx%d+%d", w, h, bpp);
entry->type = BM_TYPE_USER;
entry->comp_type = BM_TYPE_NONE;
entry->bm.w = (short)w;
entry->bm.h = (short)h;
entry->bm.rowsize = (short)w;
entry->bm.bpp = (ubyte)bpp;
entry->bm.true_bpp = (ubyte)bpp;
entry->bm.flags = (ubyte)flags;
entry->bm.data = 0;
entry->bm.palette = nullptr;
entry->info.user.bpp = (ubyte)bpp;
entry->info.user.data = data;
entry->info.user.flags = (ubyte)flags;
entry->signature = Bm_next_signature++;
entry->handle = n;
entry->mem_taken = (w * h * (bpp >> 3));
entry->load_count++;
bm_update_memory_used(n, (int)entry->mem_taken);
gr_bm_create(bm_get_slot(n));
return n;
}
int bm_create_3d(int bpp, int w, int h, int d, void* data) {
Assert((bpp == 16) || (bpp == 24) || (bpp == 32));
Assertion(bm_inited, "bmpman must be initialized before this function can be called!");
int n = find_block_of(1);
// Out of bitmap slots
if (n == -1)
return -1;
// make sure that we have valid data
if (data == nullptr) {
UNREACHABLE("No valid data received for 3D Bitmap creation!");
return -1;
}
auto entry = bm_get_entry(n);
memset(entry, 0, sizeof(bitmap_entry));
sprintf_safe(entry->filename, "TMP%dx%dx%d+%d", w, h, d, bpp);
entry->type = BM_TYPE_3D;
entry->comp_type = BM_TYPE_NONE;
entry->bm.w = (short)w;
entry->bm.h = (short)h;
entry->bm.d = (short)d;
entry->bm.rowsize = (short)w;
entry->bm.bpp = (ubyte)bpp;
entry->bm.true_bpp = (ubyte)bpp;
entry->bm.flags = (ubyte)BMP_TEX_XPARENT;
entry->bm.data = 0;
entry->bm.palette = nullptr;
entry->info.user.bpp = (ubyte)bpp;
entry->info.user.data = data;
entry->info.user.flags = (ubyte)BMP_TEX_XPARENT;
entry->signature = Bm_next_signature++;
entry->handle = n;
entry->mem_taken = (w * h * d * (bpp >> 3));
entry->load_count++;
bm_update_memory_used(n, (int)entry->mem_taken);
gr_bm_create(bm_get_slot(n));
return n;
}
void bm_convert_format(bitmap *bmp, ushort flags) {
int idx;
// no transparency for 24 bpp images
if (!(flags & BMP_AABITMAP) && (bmp->bpp == 24))
return;
if (Is_standalone) {
return;
} else {
if(!(flags & BMP_AABITMAP))
Assert((bmp->bpp == 16) || (bmp->bpp == 32));
}
// maybe swizzle to be an xparent texture
if (!(bmp->flags & BMP_TEX_XPARENT) && (flags & BMP_TEX_XPARENT)) {
for (idx = 0; idx<bmp->w*bmp->h; idx++) {
// if the pixel is transparent
if (((ushort*)bmp->data)[idx] == Gr_t_green.mask) {
((ushort*)bmp->data)[idx] = 0;
}
}
bmp->flags |= BMP_TEX_XPARENT;
}
}
void bm_free_data(bitmap_slot* bs, bool release)
{
bitmap *bmp;
auto be = &bs->entry;
bmp = &be->bm;
gr_bm_free_data(bs, release);
// If there isn't a bitmap in this structure, don't
// do anything but clear out the bitmap info
if (be->type==BM_TYPE_NONE)
goto SkipFree;
// Don't free up memory for user defined bitmaps, since
// BmpMan isn't the one in charge of allocating/deallocing them.
if (be->type == BM_TYPE_USER || be->type == BM_TYPE_3D) {
#ifdef BMPMAN_NDEBUG
if ( be->data_size != 0 )
bm_texture_ram -= be->data_size;
#endif
goto SkipFree;
}
// If this bitmap doesn't have any data to free, skip
// the freeing it part of this.
if (bmp->data == 0) {
#ifdef BMPMAN_NDEBUG
if ( be->data_size != 0 )
bm_texture_ram -= be->data_size;
#endif
goto SkipFree;
}
// Free up the data now!
#ifdef BMPMAN_NDEBUG
bm_texture_ram -= be->data_size;
#endif
vm_free((void *)bmp->data);
// reset the load_count to at least 1, don't do this in SkipFree though
// since the real count ends up wrong
be->load_count = 1;
SkipFree:
// Clear out & reset the bitmap data structure
bmp->flags = 0;
bmp->bpp = 0;
bmp->data = 0;
bmp->palette = NULL;
#ifdef BMPMAN_NDEBUG
be->data_size = 0;
#endif
be->signature = Bm_next_signature++;
}
void bm_free_data_fast(int handle)
{
bitmap_entry *be;
bitmap *bmp;
be = bm_get_entry(handle);
bmp = &be->bm;
// If there isn't a bitmap in this structure, don't
// do anything but clear out the bitmap info
if (be->type == BM_TYPE_NONE)
return;
// Don't free up memory for user defined bitmaps, since
// BmpMan isn't the one in charge of allocating/deallocing them.
if (be->type == BM_TYPE_USER || be->type == BM_TYPE_3D) {
#ifdef BMPMAN_NDEBUG
if ( be->data_size != 0 )
bm_texture_ram -= be->data_size;
#endif
return;
}
// If this bitmap doesn't have any data to free, skip
// the freeing it part of this.
if (bmp->data == 0) {
#ifdef BMPMAN_NDEBUG
if ( be->data_size != 0 ) {
bm_texture_ram -= be->data_size;
be->data_size = 0;
}
#endif
return;
}
// Free up the data now!
#ifdef BMPMAN_NDEBUG
bm_texture_ram -= be->data_size;
be->data_size = 0;
#endif
vm_free((void *)bmp->data);
bmp->data = 0;
}
int bm_get_anim_frame(const int frame1_handle, float elapsed_time, const float divisor, const bool loop)
{
bitmap_entry *be = bm_get_entry(frame1_handle);
if (be->info.ani.num_frames <= 1) {
// this is a still image
return 0;
}
int last_frame = be->info.ani.num_frames - 1;
bitmap_entry *last_framep = bm_get_entry(frame1_handle + last_frame);
if (elapsed_time < 0.0f) {
elapsed_time = 0.0f;
}
int frame = 0;
// variable frame delay animations
if (be->info.ani.apng.is_apng == true) {
if (divisor > 0.0f) {
// scale to get the real elapsed time
elapsed_time = elapsed_time / divisor * last_framep->info.ani.apng.frame_delay;
}
if (loop == true) {
elapsed_time = fmod(elapsed_time, last_framep->info.ani.apng.frame_delay);
}
int i = frame1_handle;
for ( ; i < (frame1_handle + be->info.ani.num_frames); ++i) {
// see bm_lock_apng for precalculated incremental delay for each frame
if (elapsed_time <= bm_get_entry(i)->info.ani.apng.frame_delay) {
break;
}
}
frame = i - frame1_handle;
}
// fixed frame delay animations; simpler
else {
if (divisor > 0.0f) {
// scale to get the real elapsed time
frame = fl2i(elapsed_time / divisor * be->info.ani.num_frames);
}
else {
frame = fl2i(elapsed_time * i2fl(be->info.ani.fps));
}
if (loop == true) {
frame %= be->info.ani.num_frames;
}
}
// note; this also makes non-looping anims hold on their last frame
CLAMP(frame, 0, last_frame);
return frame;
}
void bm_get_components(ubyte *pixel, ubyte *r, ubyte *g, ubyte *b, ubyte *a) {
int bit_32 = 0;
if ((gr_screen.bits_per_pixel == 32) && (Gr_current_red == &Gr_red)) {
bit_32 = 1;
}
if (r != NULL) {
if (bit_32) {
*r = ubyte(((*((unsigned int*)pixel) & Gr_current_red->mask) >> Gr_current_red->shift)*Gr_current_red->scale);
} else {
*r = ubyte(((((unsigned short*)pixel)[0] & Gr_current_red->mask) >> Gr_current_red->shift)*Gr_current_red->scale);
}
}
if (g != NULL) {
if (bit_32) {
*g = ubyte(((*((unsigned int*)pixel) & Gr_current_green->mask) >> Gr_current_green->shift)*Gr_current_green->scale);
} else {
*g = ubyte(((((unsigned short*)pixel)[0] & Gr_current_green->mask) >> Gr_current_green->shift)*Gr_current_green->scale);
}
}
if (b != NULL) {
if (bit_32) {
*b = ubyte(((*((unsigned int*)pixel) & Gr_current_blue->mask) >> Gr_current_blue->shift)*Gr_current_blue->scale);
} else {
*b = ubyte(((((unsigned short*)pixel)[0] & Gr_current_blue->mask) >> Gr_current_blue->shift)*Gr_current_blue->scale);
}
}
// get the alpha value
if (a != NULL) {
*a = 1;
Assert(!bit_32);
if (!(((unsigned short*)pixel)[0] & 0x8000)) {
*a = 0;
}
}
}
const char *bm_get_filename(int handle) {
auto entry = bm_get_entry(handle);
return entry->filename;
}
void bm_get_filename(int bitmapnum, char *filename) {
if (!bm_is_valid(bitmapnum)) {
strcpy(filename, "");
return;
}
// return filename
strcpy(filename, bm_get_entry(bitmapnum)->filename);
}
void bm_get_frame_usage(int *ntotal, int *nnew) {
#ifdef BMPMAN_NDEBUG
*ntotal = 0;
*nnew = 0;
for (auto& block : bm_blocks) {
for (auto& slot : block) {
auto& entry = slot.entry;
if ((entry.type != BM_TYPE_NONE) && (entry.used_this_frame)) {
if (!entry.used_last_frame) {
*nnew += (int)entry.mem_taken;
}
*ntotal += (int)entry.mem_taken;
}
entry.used_last_frame = entry.used_this_frame;
entry.used_this_frame = 0;
}
}
#endif
}
int bm_get_info(int handle, int *w, int * h, ushort* flags, int *nframes, int *fps) {
bitmap * bmp;
if (!bm_inited) return -1;
auto entry = bm_get_entry(handle);
Assertion(entry->handle == handle, "Invalid bitmap handle %d passed to bm_get_info().\nThis might be due to an invalid animation somewhere else.\n", handle); // INVALID BITMAP HANDLE!
if ((entry->type == BM_TYPE_NONE) || (entry->handle != handle)) {
if (w) *w = 0;
if (h) *h = 0;
if (flags) *flags = 0;
if (nframes) *nframes = 0;
if (fps) *fps = 0;
return -1;
}
bmp = &(entry->bm);
if (w) *w = bmp->w;
if (h) *h = bmp->h;
if (flags) *flags = bmp->flags;
if (bm_is_anim(entry)) {
if (nframes) {
*nframes = entry->info.ani.num_frames;
}
if (fps) {
*fps = entry->info.ani.fps;
}
return bm_get_entry(entry->info.ani.first_frame)->handle;
} else {
if (nframes) {
*nframes = 1;
}
if (fps) {
*fps = 0;
}
return handle;
}
}
int bm_get_depth(int handle, int* depth) {
bitmap* bmp;
if (!bm_inited) return -1;
auto entry = bm_get_entry(handle);
Assertion(entry->handle == handle, "Invalid bitmap handle %d passed to bm_get_info().\nThis might be due to an invalid animation somewhere else.\n", handle); // INVALID BITMAP HANDLE!
if (entry->type != BM_TYPE_3D) {
*depth = 0;
return -1;
}
bmp = &(entry->bm);
*depth = bmp->d;
return handle;
}
int bm_get_num_mipmaps(int num) {
auto entry = bm_get_entry(num);
if (entry->num_mipmaps == 0)
return 1;
return entry->num_mipmaps;
}
void bm_get_palette(int handle, ubyte *pal, char *name) {
int w, h;
const char* filename = bm_get_entry(handle)->filename;
if (name) {
strcpy(name, filename);
}
int pcx_error = pcx_read_header(filename, NULL, &w, &h, NULL, pal);
if (pcx_error != PCX_ERROR_NONE) {
// Error(LOCATION, "Couldn't open '%s'\n", filename );
}
}
int bm_get_tcache_type(int num) {
if (bm_is_compressed(num))
return TCACHE_TYPE_COMPRESSED;
return TCACHE_TYPE_NORMAL;
}