-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgfxengine.cpp
More file actions
4691 lines (4021 loc) · 97.5 KB
/
gfxengine.cpp
File metadata and controls
4691 lines (4021 loc) · 97.5 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
/*
* PROPRIETARY INFORMATION. This software is proprietary to POWDER
* Development, and is not to be reproduced, transmitted, or disclosed
* in any way without written permission.
*
* Produced by: Jeff Lait
*
* POWDER Development
*
* NAME: gfxengine.cpp ( POWDER Library, C++ )
*
* COMMENTS:
* This simple set of routines allows a somewhat platform independent
* way of drawing to the GBA.
*/
#include "gfxengine.h"
#include "gfxengine.h"
#include "mygba.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "gfx/all_bitmaps.h"
#include "map.h"
#include "creature.h"
#include "glbdef.h"
#include "assert.h"
#include "item.h"
#include "control.h"
#include "msg.h"
#include "stylus.h"
#include "victory.h"
#include "bmp.h"
// The number of 8x8 tiles to have. The maximum for 256 colours
// on the GBA is 512. SDL is theoritically infinite, except the
// 1024 and 2048 bits are used for flip flags.
#if defined(USING_SDL)
#define TILEARRAY 1023
#else
#define TILEARRAY 512
#endif
// This is used for the maximum number the bitmap routines can grab.
// We can safely bump this up in the SDL case.
#if defined(USING_SDL)
// Since it is a 16x16 map, this is sufficient.
#define TILESTASH 256
#else
#define TILESTASH 50
#endif
// Gets the number of bytes of a tile in our current tileset
#ifdef USING_SDL
#define BYTEPERTILE (glb_tilesets[glb_tileset].tilewidth*glb_tilesets[glb_tileset].tilewidth)
#define WORDPERTILE (glb_tilesets[glb_tileset].tilewidth*glb_tilesets[glb_tileset].tilewidth / 2)
#define TILEWIDTH (glb_tilesets[glb_tileset].tilewidth)
#define TILEHEIGHT (glb_tilesets[glb_tileset].tilewidth)
#else
#define BYTEPERTILE 8*8
#define WORDPERTILE 4*8
#define TILEWIDTH 8
#define TILEHEIGHT 8
#endif
#ifdef USING_DS
#define RAW_SCREEN_WIDTH 256
#define RAW_SCREEN_HEIGHT 192
#define PLATE_WIDTH 256
#define PLATE_HEIGHT 192
#include <fat.h>
#else
#ifdef USING_SDL
#define RAW_SCREEN_WIDTH (TILEWIDTH * 32)
#define RAW_SCREEN_HEIGHT (TILEWIDTH * 24)
#define PLATE_WIDTH 256
#define PLATE_HEIGHT 192
#else
#ifdef _3DS
#define RAW_SCREEN_WIDTH 256
#define RAW_SCREEN_HEIGHT 192
#define PLATE_WIDTH 256
#define PLATE_HEIGHT 192
#else
#define RAW_SCREEN_WIDTH 240
#define RAW_SCREEN_HEIGHT 160
#define PLATE_WIDTH 240
#define PLATE_HEIGHT 160
#endif
#endif
#endif
// Global variables
int glb_newframe = 0;
volatile int glb_framecount = 0;
int glb_gfxmode = -1;
// Tileset defaults to Akoi Meexx.
#ifndef iPOWDER
#ifndef _3DS
int glb_tileset = 4;
#else
int glb_tileset = 0;
#endif
#else
int glb_tileset = 2;
int glb_modetilesets[2] = { 2, 2 };
int glb_tilesetmode = 0;
#endif
#ifdef _3DS
u16* bmp_powder;
#endif
int glb_currentfont = 1;
// This is our current tile reference array.
// glb_tileidx[TILE_NAME] gives the gba tile index.
// This has already been multiplied by 4!
u16 glb_tileidx[NUM_TILES];
// This is the gba tile that ties to the given character.
u16 glb_charidx[256];
#define MAX_COLOUR_CHAR 10
u8 *glb_colourchardata[MAX_COLOUR_CHAR];
// glb_idxcount[gbaidx] gives the number of times that tile
// is currently used.
u16 glb_idxcount[TILEARRAY];
// This is a map of our current 64x64 tile layout.
u16 *glb_tilemap, *glb_shadowmap, *glb_mobmap;
u16 *glb_abstilemap, *glb_absmobmap, *glb_absshadowmap;
#ifdef _3DS
u16 *glb_abstilemap2, *glb_absshadowmap2, *glb_absmobmap2;
#endif
// This is 32x24 and matches the currently displayed characters.
u8 *glb_charmap;
// Used in our compositing engine for building the paper doll
u8 *glb_comptiledata = 0;
int glb_lastdesttile;
// Used in our greyscale sprite engine, must be global so we
// can resize this
u8 *glb_greyscaletiledata = 0;
// Our one lone sprite.
u16 glb_sprite = SPRITE_CURSOR;
// This is the current scroll offset of where our tile layout matches
// with the tilemap.
int glb_scrollx, glb_scrolly;
// This is the tile offset of the center tile. We use this for inventory.
int glb_offx, glb_offy;
// This is the pixel level scroll to nudge all the scrolling planes.
s8 glb_nudgex = 0, glb_nudgey = 0;
// Current inventory position.. Defaults to where the first added
// item will go.
int glb_invx = 1, glb_invy = 0;
MOB *glb_invmob = 0;
// Are we in sprite or dungeon mode?
bool glb_usesprites = true;
// bg2 tiles. This maps to the screen with 240/8 = 30 to 160/8 = 20
// ratio. Thus, there are are 600 tiles.
tile_info_ptr glb_bg2tiles;
tile_info_ptr glb_bg1tiles;
// This has the same format as tiles! It is XxY for 8x8 blocks,
// of which there are 30x20.
char *glb_bg2tiledata;
// This the palette entry for each of the standard colours.
char glb_stdcolor[NUM_COLOURS];
// This is the mapping for each colour in the sprite palette into
// the greyscale equivalent.
u8 *glb_spritegreyscale = 0;
// Text look up tables...
const char *glb_texttable[] = {
"abcdefghij",
"klmnopqrst",
"uvwxyz" SYMBOLSTRING_HEART SYMBOLSTRING_MAGIC SYMBOLSTRING_NEXT SYMBOLSTRING_DLEVEL,
"ABCDEFGHIJ",
"KLMNOPQRST",
"UVWXYZ" SYMBOLSTRING_LEFT SYMBOLSTRING_RIGHT SYMBOLSTRING_UP SYMBOLSTRING_DOWN,
"0123456789",
"!@#$%^&*()",
"=-\\',./+_|",
"\"<>?[]{}`~",
",.<>;:" SYMBOLSTRING_AC SYMBOLSTRING_TRIDUDE SYMBOLSTRING_CURSOR " ",
SYMBOLSTRING_UNIQUE " ",
0
};
u8
rgbtogrey(u8 cr, u8 cg, u8 cb)
{
// Roughly a 5:6:1 weighting, but scaled up so we
// get to divide by 16 for some meaningless efficiency.
int grey = cg * 8 + cr * 6 + cb * 2;
grey += 7;
grey /= 16;
if (grey > 255)
grey = 255;
return grey;
}
#ifdef USING_SDL
static int
rawtoplate_x(int x)
{
int sx = (int) ((x * PLATE_WIDTH) / ((float)(RAW_SCREEN_WIDTH)) + 0.5);
if (sx >= PLATE_WIDTH)
sx = PLATE_WIDTH-1;
return sx;
}
static int
rawtoplate_y(int y)
{
int sy = (int) ((y * PLATE_HEIGHT) / ((float)(RAW_SCREEN_HEIGHT)) + 0.5);
if (sy >= PLATE_HEIGHT)
sy = PLATE_HEIGHT-1;
return sy;
}
#else
static int
rawtoplate_x(int x)
{
return x;
}
static int
rawtoplate_y(int y)
{
return y;
}
#endif
int
gfx_getchartile(char c)
{
int textline;
const char *text;
int pos;
// Special cases...
switch (c)
{
case '\b':
c = SYMBOL_LEFT;
break;
case '\n':
case '\r':
c = SYMBOL_NEXT;
break;
case '\t':
c = SYMBOL_UP;
break;
}
pos = 0;
textline = 0;
while (glb_texttable[textline])
{
text = glb_texttable[textline];
while (*text)
{
if (*text == c)
return pos;
pos++;
text++;
}
textline++;
}
// Failure, lower case a.
return 1;
}
u16
gfx_lockColourCharTile(u8 c, COLOUR_NAMES colour, u8 *result)
{
u8 virtualtile, charnum;
int idx, sourceidx;
// Find a free colour tile in our desired range.
for (virtualtile = COLOURED_SYMBOLS; virtualtile < COLOURED_SYMBOLS + MAX_COLOUR_CHAR; virtualtile++)
{
if (glb_charidx[virtualtile] == INVALID_TILEIDX)
{
// This is a free index!
break;
}
}
// Check to see if we failed, if so, return the no tile.
// Flag that we shouldn't unlock this tile.
if (virtualtile == COLOURED_SYMBOLS + MAX_COLOUR_CHAR)
{
*result = 0xff;
return gfx_lookupTile(TILE_NOTILE);
}
// Now try and allocate a tile.
idx = gfx_findFreeTile(1);
if (idx == INVALID_TILEIDX)
{
// Bah, ran out of tiles!
*result = 0xff;
return gfx_lookupTile(TILE_NOTILE);
}
// Now find what origin tile we want to map from.
sourceidx = gfx_getchartile(c);
charnum = virtualtile - COLOURED_SYMBOLS;
// Check to see if our tiledata is available.
if (!glb_colourchardata[charnum])
glb_colourchardata[charnum] = (u8 *) new u16[WORDPERTILE];
u8 *dst;
const u8 *src;
u16 *palette = (u16 *) glb_tilesets[glb_tileset].palette;
int i;
dst = glb_colourchardata[charnum];
src = (const u8*)&glb_tilesets[glb_tileset].alphabet[glb_currentfont][sourceidx*BYTEPERTILE];
for (i = 0; i < BYTEPERTILE; i++)
{
// Anything not black or transparent is turned into
// our desired colour.
if (*src == 0 || *src == glb_stdcolor[COLOUR_BLACK] || !palette[*src] || palette[*src] == 0x842)
*dst = *src;
else
*dst = glb_stdcolor[colour];
dst++;
src++;
}
// And send the resulting tile to VRAM.
ham_ReloadTileGfx(glb_bg1tiles,
(u16*) glb_colourchardata[charnum],
idx,
1);
glb_idxcount[idx]++;
glb_charidx[virtualtile] = idx;
// When we unlock, this is the tile we'll actually unlock!
*result = virtualtile;
return idx;
}
u16
gfx_lockCharTile(u8 c, u8 *result)
{
int idx;
int sourceidx;
// Check our character tile array to see if it is already done.
*result = c;
idx = glb_charidx[c];
if (idx != INVALID_TILEIDX)
{
// Great, already allocated, inc and return.
glb_idxcount[idx]++;
return idx;
}
// Must allocate it...
idx = gfx_findFreeTile(1);
if (idx != INVALID_TILEIDX)
{
sourceidx = gfx_getchartile(c);
ham_ReloadTileGfx(glb_bg1tiles,
(u16*)(&glb_tilesets[glb_tileset].alphabet[glb_currentfont][sourceidx * BYTEPERTILE]),
idx,
1);
glb_idxcount[idx]++;
glb_charidx[c] = idx;
return idx;
}
// Damn, failure to allocate!
UT_ASSERT(!"Failure to allocate char tile");
// Flag that we shouldn't unlock this tile.
*result = 0xff;
return gfx_lookupTile(TILE_NOTILE);
}
void
gfx_unlockCharTile(u8 c)
{
u16 idx;
if (c == 0xff)
return; // Blank dude.
idx = glb_charidx[c];
UT_ASSERT(idx != INVALID_TILEIDX);
if (idx == INVALID_TILEIDX)
return;
UT_ASSERT(glb_idxcount[idx]);
if (glb_idxcount[idx])
glb_idxcount[idx]--;
if (!glb_idxcount[idx])
{
// TIle is now free.
glb_charidx[c] = INVALID_TILEIDX;
}
}
u16
gfx_findFreeTile(int size)
{
int tile, j;
for (tile = 0; tile <= TILEARRAY - size; tile++)
{
if (glb_idxcount[tile])
continue;
// We have a zero, check if next size are zero..
for (j = 0; j < size; j++)
{
if (glb_idxcount[tile+j])
{
tile = tile+j;
break;
}
}
// Making it here implies that we found a free tile.
if (j == size)
return tile;
}
// Failure to find a free one.
return INVALID_TILEIDX;
}
u16
gfx_lookupTile(TILE_NAMES tile)
{
if (tile == INVALID_TILEIDX)
return INVALID_TILEIDX;
return glb_tileidx[tile];
}
u16
gfx_lockTile(TILE_NAMES tile, TILE_NAMES *result)
{
u16 idx;
if (result)
*result = tile;
if (tile == INVALID_TILEIDX)
return INVALID_TILEIDX;
idx = glb_tileidx[tile];
if (idx != INVALID_TILEIDX)
{
if (tile == TILE_VOID || tile == TILE_NOTILE)
{
// We only lock these tiles once ever.
return idx;
}
glb_idxcount[idx]++;
glb_idxcount[idx+1]++;
glb_idxcount[idx+2]++;
glb_idxcount[idx+3]++;
return idx;
}
idx = gfx_findFreeTile(4);
if (idx != INVALID_TILEIDX)
{
// A free index! Return it.
ham_ReloadTileGfx(glb_bg1tiles,
(u16*)(&glb_tilesets[glb_tileset].dungeon[tile * 4 * BYTEPERTILE]),
idx,
4);
glb_idxcount[idx]++;
glb_idxcount[idx+1]++;
glb_idxcount[idx+2]++;
glb_idxcount[idx+3]++;
glb_tileidx[tile] = idx;
return idx;
}
// Failed to find free...
UT_ASSERT(!"FAILED TO ALLOC TILE!");
if (result)
*result = (TILE_NAMES) INVALID_TILEIDX;
return gfx_lookupTile(TILE_NOTILE);
}
void
gfx_forceLockTile(TILE_NAMES tile, int mincount)
{
int idx, j;
// See if our tile is already locked.
// Problem: If we have a
idx = gfx_lookupTile(tile);
if (idx == INVALID_TILEIDX)
{
// Tile not yet locked, lock it!
idx = gfx_lockTile(tile, 0);
}
// Verify we have the minimum count.
for (j = 0; j < 4; j++)
{
if (glb_idxcount[idx+j] < mincount)
glb_idxcount[idx+j] = mincount;
}
}
void
gfx_unlockTile(TILE_NAMES tile)
{
u16 idx;
if (tile == INVALID_TILEIDX)
return;
if (tile == TILE_NOTILE || tile == TILE_VOID)
{
// Never unlock these special tiles.
// We need to be able to always procur them
return;
}
idx = glb_tileidx[tile];
UT_ASSERT(idx != INVALID_TILEIDX);
if (idx == INVALID_TILEIDX)
return;
// Must have already locked it...
UT_ASSERT(glb_idxcount[idx]);
UT_ASSERT(glb_idxcount[idx+1]);
UT_ASSERT(glb_idxcount[idx+2]);
UT_ASSERT(glb_idxcount[idx+3]);
glb_idxcount[idx]--;
glb_idxcount[idx+1]--;
glb_idxcount[idx+2]--;
glb_idxcount[idx+3]--;
if (!glb_idxcount[idx])
{
// Tile is now free...
glb_tileidx[tile] = INVALID_TILEIDX;
}
}
int
gfx_getNumFreeTiles()
{
int idx, num;
num = 0;
for (idx = 0; idx < TILEARRAY; idx++)
{
if (glb_idxcount[idx] == 0)
num++;
}
return num;
}
//
// VBL func
//
void vblFunc()
{
glb_newframe = 1;
glb_framecount++;
}
void
gfx_buildstdcolors()
{
COLOUR_NAMES colour;
FOREACH_COLOUR(colour)
{
glb_stdcolor[colour] = gfx_lookupcolor(glb_colourdefs[colour].red,
glb_colourdefs[colour].green,
glb_colourdefs[colour].blue);
}
// Build the standard colours...
glb_stdcolor[COLOUR_INVISIBLE] = 0; // Always true.
// This is also a good time to update our grey table if we have it.
gfx_updatespritegreytable();
}
void
gfx_init()
{
// Initialize everything...
ham_Init();
// Install our interrupt handler
#if defined(USING_SDL) || defined(USING_DS) || defined(NO_HAM) || defined(_3DS)
ham_StartIntHandler(INT_TYPE_VBL, &vblFunc);
#else
ham_StartIntHandler(INT_TYPE_VBL, (void *)&vblFunc);
#endif
// These maps store the TILE_NAMES currently at each map position.
// The absolute version do the same for absolute overlays.
// This allows us to avoid double writes, and load tiles into
// VRAM as necessary.
// 0xff is used as INVALID_TILEIDX is 65535.
glb_tilemap = new u16[32*32];
glb_shadowmap = new u16[32*32];
glb_mobmap = new u16[32*32];
glb_abstilemap = new u16[17*12];
glb_absmobmap = new u16[17*12];
glb_absshadowmap = new u16[17*12];
glb_charmap = new u8[32*24];
memset(glb_tilemap, 0xff, 32*32 * sizeof(u16));
memset(glb_shadowmap, 0xff, 32*32 * sizeof(u16));
memset(glb_mobmap, 0xff, 32*32 * sizeof(u16));
memset(glb_abstilemap, 0xff, 17*12*sizeof(u16));
memset(glb_absmobmap, 0xff, 17*12*sizeof(u16));
memset(glb_absshadowmap, 0xff, 17*12*sizeof(u16));
memset(glb_charmap, 0xff, 32*24);
#ifdef _3DS
glb_abstilemap2 = new u16[17*12];
glb_absmobmap2 = new u16[17*12];
glb_absshadowmap2 = new u16[32*32];
memset(glb_abstilemap2, 0xff, 17*12*sizeof(u16));
memset(glb_absmobmap2, 0xff, 17*12*sizeof(u16));
memset(glb_absshadowmap2, 0xff, 17*12*sizeof(u16));
int dw, dh; // unused
bmp_slug_and_blood = bmp_load("gfx/tridude_goodbye_hires.bmp", dw, dh, true);
bmp_powder = bmp_load("gfx/powder.bmp", dw, dh, true);
#endif
}
void
gfx_reblitslugandblood()
{
u16 *screen;
int x, y;
screen = hamfake_lockScreen();
#if defined(USING_DS) || defined(USING_SDL)
// Scroll back the screen since it was in GBA coords.
screen -= TILEWIDTH + 2*TILEHEIGHT * RAW_SCREEN_WIDTH;
if (RAW_SCREEN_WIDTH != PLATE_WIDTH ||
RAW_SCREEN_HEIGHT != PLATE_HEIGHT)
{
for (y = 0; y < RAW_SCREEN_HEIGHT; y++)
{
int sy = rawtoplate_y(y);
for (x = 0; x < RAW_SCREEN_WIDTH; x++)
{
int sx = rawtoplate_x(x);
screen[y*RAW_SCREEN_WIDTH+x] = bmp_slug_and_blood[sy*PLATE_WIDTH+sx];
}
}
}
else
{
for (y = 0; y < PLATE_HEIGHT; y++)
{
for (x = 0; x < PLATE_WIDTH; x++)
{
screen[y*RAW_SCREEN_WIDTH+x] = bmp_slug_and_blood[y*PLATE_WIDTH+x];
}
}
}
#else
for (y = 0; y < PLATE_WIDTH*PLATE_HEIGHT; y++)
{
screen[y] = bmp_slug_and_blood[y];
}
#endif
hamfake_unlockScreen(screen);
#ifdef _3DS
screen = hamfake_lockRawScreen();
for (y = 0; y < PLATE_WIDTH*PLATE_HEIGHT; y++)
{
screen[y] = bmp_powder[y];
}
hamfake_unlockScreen(screen);
#endif
}
void
gfx_setmode(int mode)
{
int x, y;
map_info_ptr bg2mapset;
// A no-op.
if (mode == glb_gfxmode)
return;
glb_gfxmode = mode;
if (mode == 3)
{
// Intro screen...
ham_SetBgMode(3);
gfx_reblitslugandblood();
// Ensure we have current sprite date.
hamfake_LoadSpritePal((void *)glb_tilesets[glb_tileset].spritepalette, 512);
hamfake_ReloadSpriteGfx((u16*)(&glb_tilesets[glb_tileset].sprite[glb_sprite*4*BYTEPERTILE]), 0, 4);
// NOTE: This actually copies incorrect data! Likely due to it
// using 32bit copies or ignoring waitstates.
// memcpy( (unsigned short *) 0x06000000,
// bmp_slug_and_blood,
// 38400*2 );
return;
}
// Must be tiled mode.
UT_ASSERT(mode == 0);
// This is all four bgs, no zooming.
ham_SetBgMode(0);
// Wipe any charmap left over from raw mode.
memset(glb_charmap, 0xff, 32*24);
ham_LoadBGPal((void *)glb_tilesets[glb_tileset].palette, 512);
hamfake_LoadSpritePal((void *)glb_tilesets[glb_tileset].spritepalette, 512);
// So, what are our layers?
// 0: 32x32: Text and minimap.
// 1: 64x64: Terrain tiles
// 2: 64x64: Monters/Item tiles
// 3: 64x64: Shadow tiles
#if 1
// We want a 32x32 as we only will use one screen worth.
ham_bg[0].mi = ham_InitMapEmptySet(0, 0);
// We want a 64x64 screen.
ham_bg[1].mi = ham_InitMapEmptySet(3, 0);
// 64x64 map
bg2mapset = ham_InitMapEmptySet(3, 0);
// We want a 64x64 screen.
ham_bg[3].mi = ham_InitMapEmptySet(3, 0);
#ifdef _3DS
ham_bg[4].mi = ham_InitMapEmptySet(0, 0);
ham_bg[5].mi = ham_InitMapEmptySet(0, 0);
ham_bg[6].mi = ham_InitMapEmptySet(0, 0);
#endif
#else
// We want a 32x32 as we only will use one screen worth.
ham_bg[0].mi = ham_InitMapEmptySet(0, 0);
// We want a 64x64 screen.
ham_bg[1].mi = ham_bg[0].mi;
// 32 x 32 map.
bg2mapset = ham_bg[0].mi;
// We want a 64x64 screen.
ham_bg[3].mi = ham_bg[0].mi;
#endif
ham_bg[1].ti = ham_InitTileEmptySet(TILEARRAY, 1, 1);
// Init the dungeon tile set...
// ** EXCESSIVE PROFANITY DETECTED **
// ** DELETING OFFENDING LINES **
// ** RESUMING COMMENT **
// to respond to so FOAD, HTH, HAND.
// Thus, we now create a single tile set of TILEARRAY size.
// NOTE: We now share this with the bitmap cache, so that
// code currently assumes it is contiguous and uses 8x8 rather
// than 16x16, so for now we'll keep them the same.
//ham_bg[1].ti = ham_InitTileSet((void *)&DUNGEONTILES,
// (NUM_TILES-3) * 4 * WORDPERTILE, 1, 1);
glb_bg1tiles = ham_bg[1].ti;
ham_bg[0].ti = ham_bg[1].ti;
// Initialize our arrays to be all empty...
memset(glb_tileidx, 0xff, sizeof(u16) * NUM_TILES);
memset(glb_charidx, 0xff, sizeof(u16) * 256);
memset(glb_idxcount, 0, sizeof(u16) * TILEARRAY);
// Create the level 2 bg, where we do all our crazy pixel level
// stuff.
// You would think this size would have to be 600. That crashes
// due to out of vram, yet I seem to be able to write to it just
// fine? WTF?
// Calculations show that we can't fit, even in an ideal case,
// this much data in the 64k available in the tile/map data.
// One option is to use the object data set (which is 32k), except
// the actual screen required is 37.5k. THus, we instead create
// a tilestash which we can read from & deal with that way.
// glb_bg2tiles = ham_InitTileEmptySet(TILESTASH, 1, 0);
glb_bg2tiles = glb_bg1tiles;
// Highest priority as it will sit on top.
ham_InitBg(0, 1, 0, 0);
ham_InitBg(1, 1, 3, 0);
// Create our level 3 background, where we have our lighting overlay
// and put blood sprites, etc.
ham_bg[3].ti = ham_bg[1].ti;
// Above the lower map, but below the text.
ham_InitBg(3, 1, 1, 0);
memset(glb_tilemap, 0xff, 32*32 * sizeof(u16));
memset(glb_shadowmap, 0xff, 32*32 * sizeof(u16));
memset(glb_mobmap, 0xff, 32*32 * sizeof(u16));
memset(glb_abstilemap, 0xff, 17*12*sizeof(u16));
memset(glb_absmobmap, 0xff, 17*12*sizeof(u16));
memset(glb_absshadowmap, 0xff, 17*12*sizeof(u16));
memset(glb_charmap, 0xff, 32*24);
ham_bg[2].ti = glb_bg2tiles;
ham_bg[2].mi = bg2mapset;
// We allocate a half sized block as u16 and cast back to char
// to ensure we are aligned to u16.
glb_bg2tiledata = (char *) new u16[WORDPERTILE*TILESTASH];
memset(glb_bg2tiledata, 0, BYTEPERTILE*TILESTASH);
// No rot. Above the ground but below the shadow.
ham_InitBg(2, 1, 2, 0);
#ifdef _3DS
ham_bg[4].ti = ham_bg[1].ti;
ham_bg[5].ti = ham_bg[2].ti;
ham_bg[6].ti = ham_bg[0].ti;
memset(glb_abstilemap2, 0xff, 17*12*sizeof(u16));
memset(glb_absmobmap2, 0xff, 17*12*sizeof(u16));
memset(glb_absshadowmap2, 0xff, 17*12*sizeof(u16));
ham_InitBg(4, 1, 3, 0);
ham_InitBg(5, 1, 2, 0);
ham_InitBg(6, 1, 2, 0);
#endif
gfx_buildstdcolors();
// We always want the no tile locked so we can show it when
// things go bad...
// We want to lock VOID first as it will be tile 0, and thus
// our default screen will be sweet, sweet, black.
// Actaully, there is apparently no guarantee that the initial
// tile values are 0. Thus, we must manually set all of our
// tile planes to void here.
int voidtileidx;
// We know these locks must always succeed so we don't care to stash
// the result. In any case, we have engineered it that they are
// never freed.
voidtileidx = gfx_lockTile(TILE_VOID, 0);
gfx_lockTile(TILE_NOTILE, 0);
for (y = 0; y < 64; y++)
{
for (x = 0; x < 64; x++)
{
if (x < 32 && y < 32)
{
ham_SetMapTile(0, x, y, voidtileidx);
}
ham_SetMapTile(1, x, y, voidtileidx);
ham_SetMapTile(2, x, y, voidtileidx);
ham_SetMapTile(3, x, y, voidtileidx);
#ifdef _3DS
ham_SetMapTile(4, x, y, voidtileidx);
ham_SetMapTile(5, x, y, voidtileidx);
ham_SetMapTile(6, x, y, voidtileidx);
#endif
}
}
// Load the sprite.
hamfake_ReloadSpriteGfx((u16*)(&glb_tilesets[glb_tileset].sprite[glb_sprite*4*BYTEPERTILE]), 0, 4);
}
//
// This forces a rebuilding of all the tiles from our cache to the
// ham space.
//
void
gfx_refreshtiles()
{
int x, y, tile, tileidx;
// Ignore this if we are not in a graphics mode.
if (glb_gfxmode)
return;
// A refresh will also clear anything in the abs maps, so we
// first unlock those tiles.
for (y = 0; y < 12; y++)
{
for (x = 0; x < 17; x++)
{
tile = glb_abstilemap[y * 17 + x];
if (tile != INVALID_TILEIDX)
{
gfx_unlockTile((TILE_NAMES)tile);
glb_abstilemap[y * 17 + x] = INVALID_TILEIDX;
}
#ifdef _3DS
tile = glb_abstilemap2[y * 17 + x];
if (tile != INVALID_TILEIDX)
{
gfx_unlockTile((TILE_NAMES)tile);
glb_abstilemap2[y * 17 + x] = INVALID_TILEIDX;
}
#endif
tile = glb_absshadowmap[y * 17 + x];
if (tile != INVALID_TILEIDX)
{
gfx_unlockTile((TILE_NAMES)tile);
glb_absshadowmap[y * 17 + x] = INVALID_TILEIDX;
}
#ifdef _3DS
tile = glb_absshadowmap2[y * 17 + x];
if (tile != INVALID_TILEIDX)
{
gfx_unlockTile((TILE_NAMES)tile);
glb_absshadowmap2[y * 17 + x] = INVALID_TILEIDX;
}
#endif
tile = glb_absmobmap[y * 17 + x];
if (tile != INVALID_TILEIDX)
{
gfx_unlockTile((TILE_NAMES)tile);
glb_absmobmap[y * 17 + x] = INVALID_TILEIDX;
}
#ifdef _3DS
tile = glb_absmobmap2[y * 17 + x];
if (tile != INVALID_TILEIDX)
{
gfx_unlockTile((TILE_NAMES)tile);
glb_absmobmap2[y * 17 + x] = INVALID_TILEIDX;
}
#endif
}
}
// TODO: Insert map might prove a tad bit more efficient, though
// would need a full size scratch space.
for (y = 0; y < 32; y++)
{
for (x = 0; x < 32; x++)
{
tile = glb_tilemap[(y + glb_scrolly) * 32 + x + glb_scrollx];
tileidx = gfx_lockTile((TILE_NAMES)tile, (TILE_NAMES *) &tile);
ham_SetMapTile(1, x*2, y*2, tileidx);
ham_SetMapTile(1, x*2+1, y*2, tileidx+1);
ham_SetMapTile(1, x*2, y*2+1, tileidx+2);
ham_SetMapTile(1, x*2+1, y*2+1, tileidx+3);
// The above lock should be a double count, so we unlock
// afterwards. Any write to glb_tilemap must have done
// a lock!
gfx_unlockTile((TILE_NAMES)tile);
}
}
// Update overlay buffer...
for (y = 0; y < 32; y++)
{
for (x = 0; x < 32; x++)
{