forked from ComputerNerd/Retro-Graphics-Toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasstilemap.cpp
1705 lines (1409 loc) · 43 KB
/
classtilemap.cpp
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
/*
This file is part of Retro Graphics Toolkit
Retro Graphics Toolkit is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or any later version.
Retro Graphics Toolkit is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Retro Graphics Toolkit. If not, see <http://www.gnu.org/licenses/>.
Copyright Sega16 (or whatever you wish to call me) (2012-2018)
*/
#include <FL/fl_ask.H>
#include <memory>
#include "class_global.h"
#include "macros.h"
#include "project.h"
#include "callback_tilemap.h"
#include "filemisc.h"
#include "classtilemap.h"
#include "compressionWrapper.h"
#include "quant.h"
#include "dither.h"
#include "palette.h"
#include "gui.h"
#include "filereader.h"
#include "errorMsg.h"
tileMap::tileMap(Project*prj)noexcept: tileMap(1, 1, prj) {}
tileMap::tileMap(uint32_t w, uint32_t h, Project*prj)noexcept {
this->prj = prj;
amt = 1;
mapSizeW = w;
mapSizeHA = mapSizeH = h;
isBlock = false;
tileMapDat = (uint8_t*)calloc(w * h, TileMapSizePerEntry);
offset = 0;
}
tileMap::tileMap(const tileMap&other, Project*prj)noexcept {
if (this != &other) {
planeName = other.planeName;
this->prj = prj;
mapSizeW = other.mapSizeW;
mapSizeH = other.mapSizeH;
mapSizeHA = other.mapSizeHA;
isBlock = other.isBlock;
offset = other.offset;
if (isBlock) {
amt = other.amt;
mapSizeHA = mapSizeH * amt;
} else {
amt = 1;
mapSizeHA = mapSizeH;
}
tileMapDat = (uint8_t*)malloc(mapSizeW * mapSizeHA * TileMapSizePerEntry);
memcpy(tileMapDat, other.tileMapDat, mapSizeW * mapSizeHA * TileMapSizePerEntry);
}
}
tileMap::tileMap(const tileMap&other)noexcept {
tileMap(other, other.prj);
}
tileMap::tileMap(tileMap&& other)noexcept {
if (this != &other) {
planeName = other.planeName;
prj = other.prj;
mapSizeW = other.mapSizeW;
mapSizeH = other.mapSizeH;
mapSizeHA = other.mapSizeHA;
isBlock = other.isBlock;
offset = other.offset;
amt = other.amt;
tileMapDat = other.tileMapDat;
other.tileMapDat = 0;
}
}
tileMap& tileMap::operator=(tileMap&& other)noexcept {
tileMap((tileMap&&)other);
return *this;
}
tileMap& tileMap::operator=(const tileMap& other)noexcept {
tileMap(other, other.prj);
return *this;
}
tileMap::~tileMap()noexcept {
free(tileMapDat);
tileMapDat = nullptr;
}
void tileMap::ditherAsImage(bool entire) {
uint8_t*image;
uint32_t w, h;
w = mapSizeW;
h = mapSizeHA;
w *= prj->tileC->width();
h *= prj->tileC->height();
image = (uint8_t *)malloc(w * h * 4);
if (!image)
show_malloc_error(w * h * 4)
if (entire) {
truecolor_to_image(image, -1);
ditherImage(image, w, h, true, true);
void*indexPtr = ditherImage(image, w, h, true, false, false, 0, false, 0, false, true);
truecolorimageToTiles((uint8_t*)indexPtr, -1, false, false, true, true);
free(indexPtr);
} else {
for (unsigned row = 0; row < prj->pal->rowCntPal; ++row) {
printf("Row: %u\n", row);
truecolor_to_image(image, row);
ditherImage(image, w, h, true, true);
void*indexPtr = ditherImage(image, w, h, true, false, false, 0, false, 0, false, true);
//convert back to tiles
truecolorimageToTiles((uint8_t*)indexPtr, row, false, false, true, true);
free(indexPtr);
}
}
free(image);
}
void tileMap::allRowSet(unsigned row) {
unsigned x, y;
for (y = 0; y < mapSizeHA; ++y) {
for (x = 0; x < mapSizeW; ++x)
set_pal_row(x, y, row);
}
}
static void sumTile(uint8_t*tilePtr, uint32_t*sums, Project*prj) {
uint32_t sum[3];//In hopes that the compiler is smart enough to keep these in registers
memset(sum, 0, sizeof(sum));
for (unsigned j = 0; j < prj->tileC->tcSize; ++j) {
if (tilePtr[3]) {
sum[0] += tilePtr[0];
sum[1] += tilePtr[1];
sum[2] += tilePtr[2];
}
tilePtr += 4;
}
sums[0] = sum[0];
sums[1] = sum[1];
sums[2] = sum[2];
}
bool tileMap::pickTileRowQuantChoice(unsigned rows) {
unsigned w = mapSizeW, h = mapSizeHA;
unsigned char userpal[3][256];
if ((prj->gameSystem == NES) && (prj->subSystem & NES2x2)) {
w /= 2;
h /= 2;
}
uint8_t*imgin = (uint8_t*)malloc(w * h * 3);
if (!imgin) {
show_malloc_error(w * h * 3)
return false;
}
uint8_t*imgout = (uint8_t*)malloc(w * h);
if (!imgout) {
show_malloc_error(w * h)
return false;
}
uint32_t sums[3];
uint8_t*imgptr = imgin;
if ((prj->gameSystem == NES) && (prj->subSystem & NES2x2)) {
for (unsigned y = 0; y < mapSizeHA; y += 2) {
for (unsigned x = 0; x < mapSizeW; x += 2) {
memset(sums, 0, sizeof(sums));
for (unsigned i = 0; i < prj->pal->rowCntPal; ++i) {
uint32_t sumtmp[3];
sumTile(prj->tileC->truetDat.data() + (get_tile(x + (i & 1), y + (i / 2))*prj->tileC->tcSize), sumtmp, prj);
sums[0] += sumtmp[0];
sums[1] += sumtmp[1];
sums[2] += sumtmp[2];
}
*imgptr++ = sums[0] / 256;
*imgptr++ = sums[1] / 256;
*imgptr++ = sums[2] / 256;
}
}
} else {
for (unsigned y = 0; y < mapSizeHA; ++y) {
for (unsigned x = 0; x < mapSizeW; ++x) {
sumTile(prj->tileC->truetDat.data() + (get_tile(x, y)*prj->tileC->tcSize), sums, prj);
*imgptr++ = sums[0] / 64;
*imgptr++ = sums[1] / 64;
*imgptr++ = sums[2] / 64;
}
}
}
dl3quant(imgin, w, h, rows, userpal, false, 0);
dl3floste(imgin, imgout, w, h, rows, 0, userpal);
imgptr = imgout;
if ((prj->gameSystem == NES) && (prj->subSystem & NES2x2)) {
for (unsigned y = 0; y < mapSizeHA; y += 2) {
for (unsigned x = 0; x < mapSizeW; x += 2) {
set_pal_row(x, y, (*imgptr) % rows);
set_pal_row(x + 1, y, (*imgptr) % rows);
set_pal_row(x, y + 1, (*imgptr) % rows);
set_pal_row(x + 1, y + 1, (*imgptr++) % rows);
}
}
} else {
for (unsigned y = 0; y < mapSizeHA; ++y) {
for (unsigned x = 0; x < mapSizeW; ++x)
set_pal_row(x, y, (*imgptr++) % rows);
}
}
free(imgin);
free(imgout);
return true;
}
bool tileMap::inRange(uint32_t x, uint32_t y)const {
if (mapSizeW < x || mapSizeHA < y) {
printf("Out of range %u %u\n", x, y);
return false;
} else
return true;
}
void tileMap::setRaw(uint32_t x, uint32_t y, uint32_t val) {
if (inRange(x, y)) {
uint32_t*tptr = (uint32_t*)tileMapDat;
tptr += (y * mapSizeW) + x;
*tptr = val;
}
}
uint32_t tileMap::getRaw(uint32_t x, uint32_t y)const {
if (inRange(x, y)) {
uint32_t*tptr = (uint32_t*)tileMapDat;
tptr += (y * mapSizeW) + x;
return *tptr;
} else
return 0;
}
void tileMap::resizeBlocks(uint32_t wn, uint32_t hn) {
uint32_t amtTemp = mapSizeW * mapSizeH * amt;
amtTemp /= (wn * hn);
if (amtTemp) {
amt = amtTemp;
mapSizeW = wn;
mapSizeH = hn;
mapSizeHA = mapSizeH * amt;
if (window) {
char tmp[16];
snprintf(tmp, 16, "%u", amt);
window->map_amt->value(tmp);
}
} else if (window)
window->updateMapWH();
ScrollUpdate();
}
void tileMap::blockAmt(uint32_t newAmt) {
if (newAmt == amt)
return;
tileMapDat = (uint8_t*)realloc(tileMapDat, TileMapSizePerEntry * mapSizeW * mapSizeH * newAmt);
if (newAmt > amt)
memset(tileMapDat + (amt * TileMapSizePerEntry * mapSizeW * mapSizeH), 0, (newAmt - amt)*mapSizeW * mapSizeH * TileMapSizePerEntry);
amt = newAmt;
mapSizeHA = mapSizeH * amt;
ScrollUpdate();
}
const char * MapWidthTxt = "Map width";
const char * MapHeightTxt = "Map height";
static void rect2rect(uint8_t*in, uint8_t*out, unsigned xin, unsigned yin, unsigned win, unsigned wout, unsigned hout) {
in += (yin * win) + xin;
while (hout--) {
memcpy(out, in, wout);
in += win;
out += wout;
}
}
void tileMap::toggleBlocks(bool set) {
if (set != isBlock) {
if (set) {
//First get user input on the dimensions of each block
char * str_ptr;
str_ptr = (char *)fl_input("Enter block width");
if (!str_ptr)
return;
if (!verify_str_number_only(str_ptr))
return;
uint32_t w, h;
w = atoi(str_ptr);
if (mapSizeW % w) {
fl_alert("You must enter a number that will result in an integer when dividing by %d", mapSizeW);
return;
}
str_ptr = (char *)fl_input("Enter block height");
if (!str_ptr)
return;
if (!verify_str_number_only(str_ptr))
return;
h = atoi(str_ptr);
if (mapSizeH % h) {
fl_alert("You must enter a number that will result in an integer when dividing by %d", mapSizeH);
return;
}
if ((mapSizeW != w) || (mapSizeH != h)) {
//The tiles will need to be rearranged
uint8_t*tmp = (uint8_t*)malloc(mapSizeW * mapSizeH * TileMapSizePerEntry);
uint8_t*out = tmp;
for (uint_fast32_t y = 0; y < mapSizeH; y += h) {
for (uint_fast32_t x = 0; x < mapSizeW; x += w) {
rect2rect(tileMapDat, out, x * TileMapSizePerEntry, y, mapSizeW * TileMapSizePerEntry, w * TileMapSizePerEntry, h);
out += w * h * TileMapSizePerEntry;
}
}
memcpy(tileMapDat, tmp, mapSizeH * mapSizeW * TileMapSizePerEntry);
free(tmp);
}
isBlock = true;
amt = (mapSizeW * mapSizeH) / (w * h);
mapSizeW = w;
mapSizeH = h;
mapSizeHA = mapSizeH * amt;
} else {
isBlock = false;
mapSizeH *= amt;
amt = 1; //amt must = 1 when blocks are not in use
mapSizeHA = mapSizeH;
}
}
if (window) {
window->updateMapWH();
if (set) {
window->map_w->label("Block width");
window->map_w->callback(resizeBlocksCB);
window->map_h->label("Block height");
window->map_h->callback(resizeBlocksCB);
window->map_amt->show();
char tmp[16];
snprintf(tmp, 16, "%u", amt);
window->map_amt->value(tmp);
} else {
window->map_w->callback(callback_resize_map);
window->map_w->label(MapWidthTxt);
window->map_h->callback(callback_resize_map);
window->map_h->label(MapHeightTxt);
window->map_amt->hide();
}
updateTileSelectAmt();
ScrollUpdate();
}
}
bool tileMap::get_hflip(uint32_t x, uint32_t y)const {
if (inRange(x, y))
return (tileMapDat[((y * mapSizeW) + x) * 4] >> 3) & 1;
else
return false;
}
bool tileMap::get_vflip(uint32_t x, uint32_t y)const {
if (inRange(x, y))
return (tileMapDat[((y * mapSizeW) + x) * 4] >> 4) & 1;
else
return false;
}
bool tileMap::get_prio(uint32_t x, uint32_t y)const {
if (inRange(x, y))
return (tileMapDat[((y * mapSizeW) + x) * 4] >> 7) & 1;
else
return false;
}
unsigned tileMap::getPalRow(uint32_t x, uint32_t y)const {
if (inRange(x, y)) {
if ((prj->gameSystem == NES) && (prj->subSystem & NES2x2)) {
x &= ~1;
y &= ~1;
}
return (tileMapDat[((y * mapSizeW) + x) * 4] >> 5) & 3;
} else
return 0;
}
void tileMap::set_pal_row(uint32_t x, uint32_t y, unsigned row) {
if ((prj->gameSystem == NES) && (prj->subSystem & NES2x2)) {
x &= ~1;
y &= ~1;
tileMapDat[((y * mapSizeW) + x) * 4] &= ~(3 << 5);
tileMapDat[((y * mapSizeW) + x) * 4] |= row << 5;
tileMapDat[((y * mapSizeW) + (x | 1)) * 4] &= ~(3 << 5);
tileMapDat[((y * mapSizeW) + (x | 1)) * 4] |= row << 5;
tileMapDat[(((y | 1)*mapSizeW) + x) * 4] &= ~(3 << 5);
tileMapDat[(((y | 1)*mapSizeW) + x) * 4] |= row << 5;
tileMapDat[(((y | 1)*mapSizeW) + (x | 1)) * 4] &= ~(3 << 5);
tileMapDat[(((y | 1)*mapSizeW) + (x | 1)) * 4] |= row << 5;
} else {
tileMapDat[((y * mapSizeW) + x) * 4] &= ~(3 << 5);
tileMapDat[((y * mapSizeW) + x) * 4] |= row << 5;
}
}
int32_t tileMap::get_tile(uint32_t x, uint32_t y)const {
//first calculate which tile we want
if ((mapSizeW <= x) || (mapSizeHA) <= y) {
printf("Error tile (%d,%d) does not exist on this map\n", x, y);
return -1;
}
uint32_t selected_tile = ((y * mapSizeW) + x) * 4;
//get both bytes
uint8_t temp_1, temp_2, temp_3;
temp_1 = tileMapDat[selected_tile + 1]; //least significant is stored in the lowest address
temp_2 = tileMapDat[selected_tile + 2];
temp_3 = tileMapDat[selected_tile + 3]; //most significant
return (temp_1 << 16) + (temp_2 << 8) + temp_3;
}
int32_t tileMap::get_tileRow(uint32_t x, uint32_t y, unsigned useRow)const {
//first calculate which tile we want
uint32_t selected_tile = ((y * mapSizeW) + x) * 4;
if ((prj->gameSystem == NES) && (prj->subSystem & NES2x2)) {
x &= ~1;
y &= ~1;
}
uint32_t selected_tile_pal = ((y * mapSizeW) + x) * 4;
//get both bytes
if (((tileMapDat[selected_tile_pal] >> 5) & 3) == useRow) {
uint8_t temp_1, temp_2, temp_3;
temp_1 = tileMapDat[selected_tile + 1]; //least significant is stored in the lowest address
temp_2 = tileMapDat[selected_tile + 2];
temp_3 = tileMapDat[selected_tile + 3]; //most significant
return (temp_1 << 16) + (temp_2 << 8) + temp_3;
} else
return -1;
}
void tileMap::set_vflip(uint32_t x, uint32_t y, bool vflip_set) {
if (vflip_set)
tileMapDat[((y * mapSizeW) + x) * 4] |= 1 << 4;
else
tileMapDat[((y * mapSizeW) + x) * 4] &= ~(1 << 4);
}
static int bondsCheckTile(int tile, int mx, unsigned x, unsigned y) {
if (tile < 0)
tile = 0;
if (tile > mx) {
printf("Warning tile value %d exceeded %d at x: %u y: %u\n", tile, mx, x, y);
tile = mx;
}
return tile;
}
bool tileMap::saveToFile(const char*fname, fileType_t type, int clipboard, CompressionType compression, const char*label, const char*nesFname, const char*labelNES) {
uint32_t x, y;
FILE * myfile;
size_t fileSize;
uint8_t* mapptr;
if (clipboard)
myfile = 0;
else if (type != fileType_t::tBinary)
myfile = fopen(fname, "w");
else
myfile = fopen(fname, "wb");
if (likely(myfile || clipboard)) {
switch (prj->gameSystem) {
case segaGenesis:
{ uint16_t * TheMap;
fileSize = (mapSizeW * mapSizeH * amt) * 2;
mapptr = (uint8_t*)malloc(fileSize);
TheMap = (uint16_t*)mapptr;
for (y = 0; y < mapSizeH * amt; ++y) {
for (x = 0; x < mapSizeW; ++x) {
int tile = get_tile(x, y);
tile += offset;
tile = bondsCheckTile(tile, 2047, x, y);
*TheMap = (uint16_t)tileMapDat[((y * mapSizeW) + x) * 4] << 8; //get attributes
*TheMap++ |= (uint16_t)tile; //add tile
}
}
}
break;
case masterSystem:
case gameGear:
{ uint8_t * TheMap;
fileSize = (mapSizeW * mapSizeH * amt) * 2;
mapptr = (uint8_t*)malloc(fileSize);
TheMap = mapptr;
for (y = 0; y < mapSizeH * amt; ++y) {
for (x = 0; x < mapSizeW; ++x) {
/*
MSB LSB
---pcvhn nnnnnnnn
- = Unused. Some games use these bits as flags for collision and damage
zones. (such as Wonderboy in Monster Land, Zillion 2)
p = Priority flag. When set, sprites will be displayed underneath the
background pattern in question.
c = Palette select.
v = Vertical flip flag.
h = Horizontal flip flag.
n = Pattern index, any one of 512 patterns in VRAM can be selected.
*/
int tile = get_tile(x, y);
tile += offset;
tile = bondsCheckTile(tile, 511, x, y);
*TheMap++ = tile & 255;
*TheMap++ = ((tile >> 8) & 1) | (get_hflip(x, y) << 1) | (get_vflip(x, y) << 2) | (getPalRow(x, y) << 3) | (get_prio(x, y) << 4);
}
}
}
break;
case NES:
case TMS9918: // Both systems have eight bit tile entries.
{ uint8_t * TheMap;
fileSize = mapSizeW * mapSizeHA;
mapptr = (uint8_t *)malloc(fileSize);
TheMap = mapptr;
for (y = 0; y < mapSizeHA; ++y) {
for (x = 0; x < mapSizeW; ++x) {
int tile = get_tile(x, y);
tile += offset;
tile = bondsCheckTile(tile, 255, x, y);
*TheMap++ = tile;
}
}
}
break;
default:
show_default_error
}
if (compression != CompressionType::Uncompressed) {
void*TheMap = mapptr;
if (prj->gameSystem == segaGenesis) {
// The endian must be corrected before compressing.
uint16_t * ptr = (uint16_t*)mapptr;
for (unsigned i = 0; i < mapSizeW * mapSizeHA; ++i) {
uint16_t tmp = *ptr;
boost::endian::conditional_reverse_inplace<boost::endian::order::native, boost::endian::order::big>(tmp);
*ptr++ = tmp;
}
}
mapptr = (uint8_t*)encodeType(TheMap, fileSize, fileSize, compression);
free(TheMap);
}
char temp[2048];
snprintf(temp, 2048, "Width %d Height %d %s", mapSizeW, mapSizeHA, typeToText(compression));
int bits;
if ((prj->gameSystem == segaGenesis) && (compression == CompressionType::Uncompressed))
bits = 16;
else
bits = 8;
if (!saveBinAsText(mapptr, fileSize, myfile, type, temp, label, bits, compression == CompressionType::Uncompressed ? boost::endian::order::native : getEndianBySystem())) {
free(mapptr);
return false;
}
free(mapptr);
if (myfile)
fclose(myfile);
puts("File Saved");
} else
return false;
if (prj->gameSystem == NES) {
if (clipboard)
fl_alert("The tilemap is currently on the clipboard. Paste this now and then press okay to place the attributes on the clipboard");
if (nesFname) {
if (clipboard)
myfile = nullptr;
else if (type != fileType_t::tBinary)
myfile = fopen(nesFname, "w");
else
myfile = fopen(nesFname, "wb");
if (likely(myfile || clipboard)) {
uint8_t * AttrMap = (uint8_t *)malloc(((mapSizeW + 2) / 4) * ((mapSizeHA + 2) / 4));
uint8_t * freeAttrMap = AttrMap;
for (y = 0; y < mapSizeHA; y += 4) {
for (x = 0; x < mapSizeW; x += 4)
* AttrMap++ = getPalRow(x, y) | (getPalRow(x + 2, y) << 2) | (getPalRow(x, y + 2) << 4) | (getPalRow(x + 2, y + 2) << 6);
}
if (saveBinAsText(freeAttrMap, ((mapSizeW + 2) / 4) * ((mapSizeHA + 2) / 4), myfile, type, 0, labelNES, 8, boost::endian::order::native) == false)
return false;
free(freeAttrMap);
if (myfile)
fclose(myfile);
} else
return false;
}
}
return true;
}
bool tileMap::saveToFile(void) {
/*!
Saves tilemap to file returns true on success or cancellation
returns false if there was an error but remember if the user cancels this it is not an error
*/
//first see how this file should be saved
uint32_t x, y;
fileType_t type = askSaveType();
int clipboard;
if (type != fileType_t::tBinary) {
clipboard = clipboardAsk();
if (clipboard == 2)
return true;
} else
clipboard = 0;
bool pickedFile;
std::string fname, nesFname;
if (clipboard)
pickedFile = true;
else
pickedFile = loadOrSaveFile(fname, "Save tilemap to...", true);
if (!pickedFile)
return true;
if (prj->gameSystem == NES) {
pickedFile = loadOrSaveFile(nesFname, "Save tilemap attributes to...", true);
if (!pickedFile)
return true;
}
CompressionType compression = compressionAsk();
if (compression == CompressionType::Cancel)
return true;
saveToFile(fname.c_str(), type, clipboard, compression, planeName.c_str(), nesFname.c_str());
return true;
}
static void zero_error_tile_map(int32_t x) {
//this is a long string I do not want it stored more than once
fl_alert("Please enter value greater than zero you on the other hand entered %d", x);
}
boost::endian::order tileMap::getEndianBySystem() {
if (prj->gameSystem == segaGenesis)
return boost::endian::order::big;
else
return boost::endian::order::little;
}
bool tileMap::loadFromFile() {
//start by loading the file
/*Only will return false when there is a malloc error or file error.
Return true upon user cancellation or the user not entering a number correctly.*/
boost::endian::order systemEndian = getEndianBySystem();
unsigned bytesPerElement;
switch (prj->gameSystem) {
case segaGenesis:
case masterSystem:
case gameGear:
bytesPerElement = 2;
break;
case NES:
case TMS9918:
bytesPerElement = 1;
break;
default:
show_default_error
return false;
}
filereader f = filereader(systemEndian, bytesPerElement, "Load a tilemap");
if (f.amt == 0)
return true;
size_t file_size;
//get width and height
int blocksLoad = fl_ask("Are you loading blocks?");
int32_t w, h;
char * str_ptr;
if (blocksLoad)
str_ptr = (char *)fl_input("Enter block width");
else
str_ptr = (char *)fl_input("Enter width");
if (!str_ptr)
return true;
if (!verify_str_number_only(str_ptr))
return true;
w = atoi(str_ptr);
if (w <= 0) {
zero_error_tile_map(w);
return true;
}
if (prj->gameSystem == NES && (w & 1) && (prj->subSystem & NES2x2)) {
fl_alert("Error unlike in Sega Genesis mode NES mode needs the width and height to be a multiple to 2");
return true;
}
if (blocksLoad)
str_ptr = (char *)fl_input("Enter block height");
else
str_ptr = (char *)fl_input("Enter height");
if (!str_ptr)
return true;
if (!verify_str_number_only(str_ptr))
return true;
h = atoi(str_ptr);
if (h <= 0) {
zero_error_tile_map(h);
return true;
}
if (prj->gameSystem == NES && (h & 1) && (prj->subSystem & NES2x2)) {
fl_alert("Error unlike in Sega Genesis mode NES mode needs the width and height the be a multiple to 2");
return true;
}
//we can now load the map
int32_t offset;
str_ptr = (char *)fl_input("Enter offset\nThis number will be subtracted from the tilemap's tile value can be positive or negative");
if (!str_ptr)
return true;
if (!verify_str_number_only(str_ptr))
return true;
offset = atoi(str_ptr);
if (window)
window->tmapOffset->value(str_ptr);
this->offset = offset;
if (window)
window->BlocksCBtn->value(blocksLoad ? 1 : 0);
unsigned index = f.selDat();
file_size = f.lens[index];
size_t size_temp;
uint32_t blocksLoaded = file_size / w / h;
switch (prj->gameSystem) {
case segaGenesis:
case masterSystem:
case gameGear:
if (blocksLoad)
size_temp = blocksLoaded * w * h;
else
size_temp = w * h * 2; //Size of data that is to be loaded
blocksLoaded /= 2;
break;
case NES:
if (blocksLoad)
size_temp = blocksLoaded * w * h;
else
size_temp = w * h;
break;
default:
show_default_error
}
printf("W %d H %d blocks loaded %d\n", w, h, blocksLoaded);
if (window)
window->updateMapWH();
mapSizeW = w;
mapSizeH = h;
if (blocksLoad)
amt = blocksLoaded;
else
amt = 1;
mapSizeHA = mapSizeH * amt;
if (size_temp > file_size)
fl_alert("Warning: The Tile map that you are attempting to load is smaller than a tile map that has the width and height that you specified\nThis missing data will be padded with tile zero");
//start converting to tile
//free(tile_map);
tileMapDat = (uint8_t *)realloc(tileMapDat, (w * h) * 4 * amt);
uint8_t * tempMap = (uint8_t *) malloc(size_temp);
if (unlikely(!tileMapDat)) {
show_malloc_error(size_temp)
}
memcpy(tempMap, f.dat[index], size_temp);
uint32_t x, y;
for (y = 0; y < mapSizeH * amt; ++y) {
for (x = 0; x < mapSizeW; ++x) {
switch (prj->gameSystem) {
case segaGenesis:
if (((x + (y * w) + 1) * 2) <= file_size) {
uint16_t* ptr16 = (uint16_t*)tempMap;
uint16_t temp = *ptr16++;
tempMap = (uint8_t*)ptr16;
//set attributes
tileMapDat[((y * mapSizeW) + x) * 4] = (uint8_t)(temp >> 8) & 0xF8;
temp &= 2047;
int32_t tempCalculated = int32_t(temp) - offset;
set_tile(x, y, tempCalculated > 0 ? tempCalculated : 0);
} else
set_tile(x, y, 0);
break;
case masterSystem:
case gameGear:
if (((x + (y * w) + 1) * 2) <= file_size) {
uint8_t attrs = *tempMap++;
uint16_t tile = *tempMap++;
tile |= (attrs & 1) << 8;
set_tile_full(x, y, tile, (attrs >> 3) & 1, (attrs >> 1) & 1, (attrs >> 2) & 1, (attrs >> 4) & 1);
} else
set_tile(x, y, 0);
break;
case NES:
if ((x + (y * w) + 1) <= file_size) {
int temp = *tempMap++;
if (temp - offset > 0)
set_tile(x, y, (int32_t)temp - offset);
else
set_tile(x, y, 0);
} else
set_tile(x, y, 0);
break;
default:
show_default_error
}
}
}
if (prj->gameSystem == NES) {
//now load attributes
filereader f2 = filereader(boost::endian::order::native, 1, "Load Attributes");
if (f2.amt) {
unsigned indx2 = f.selDat();
uint8_t* tempbuf = (uint8_t*)f.dat[indx2];
for (y = 0; y < mapSizeH * amt; y += 4) {
for (x = 0; x < mapSizeW; x += 4) {
set_pal_row(x, y, *tempbuf & 3);
set_pal_row(x, y + 1, *tempbuf & 3);
set_pal_row(x + 1, y, *tempbuf & 3);
set_pal_row(x + 1, y + 1, *tempbuf & 3);
set_pal_row(x + 2, y, ((*tempbuf) >> 2) & 3);
set_pal_row(x + 2, y + 1, ((*tempbuf) >> 2) & 3);
set_pal_row(x + 3, y, ((*tempbuf) >> 2) & 3);
set_pal_row(x + 3, y + 1, ((*tempbuf) >> 2) & 3);
++tempbuf;
}
}
}
}
tempMap -= file_size;
free(tempMap);
isBlock = blocksLoad;
toggleBlocks(blocksLoad);
if (window)
window->redraw();
return true;
}
void tileMap::swapTile(uint32_t oldTile, uint32_t newTile) {
uint_fast32_t x, y;
int32_t temp;
for (y = 0; y < mapSizeHA; y++) {
for (x = 0; x < mapSizeW; x++) {
temp = get_tile(x, y);
if (temp == oldTile)
set_tile(x, y, newTile);
}
}
}
void tileMap::sub_tile_map(uint32_t oldTile, uint32_t newTile, bool hflip, bool vflip) {
uint_fast32_t x, y;
int_fast32_t temp;
for (y = 0; y < mapSizeHA; y++) {
for (x = 0; x < mapSizeW; x++) {
temp = get_tile(x, y);
if (temp == oldTile) {
set_tile(x, y, newTile);
if (hflip == true)
set_hflip(x, y, true);
if (vflip)
set_vflip(x, y, true);
}
else if (temp > oldTile) {
temp--;