-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmugentexthandler.cpp
1126 lines (936 loc) · 29.8 KB
/
mugentexthandler.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
#include "prism/mugentexthandler.h"
#include <assert.h>
#include <algorithm>
#include <set>
#include <variant>
#include "prism/mugendefreader.h"
#include "prism/mugenanimationhandler.h"
#include "prism/log.h"
#include "prism/system.h"
#include "prism/drawing.h"
#include "prism/texture.h"
#include "prism/math.h"
#include "prism/stlutil.h"
using namespace std;
namespace prism {
typedef enum {
MUGEN_FONT_TYPE_BITMAP,
MUGEN_FONT_TYPE_TRUETYPE,
MUGEN_FONT_TYPE_ELECBYTE,
} MugenFontType;
typedef enum {
MUGEN_ELECBYTE_FONT_TYPE_VARIABLE,
MUGEN_ELECBYTE_FONT_TYPE_FIXED,
} MugenElecbyteFontType;
typedef struct {
int mExists;
int mStartX;
int mWidth;
} MugenElecbyteFontMapEntry;
typedef struct {
MugenElecbyteFontMapEntry mMap[0x100];
MugenSpriteFileSprite mSprite;
MugenElecbyteFontType mType;
} MugenElecbyteFont;
typedef struct {
TruetypeFont mFont;
} MugenTruetypeFont;
typedef struct {
MugenSpriteFile mSprites;
} MugenBitmapFont;
typedef struct {
MugenFontType mType;
std::variant<MugenElecbyteFont, MugenBitmapFont, MugenTruetypeFont> mData;
Vector3DI mSize;
Vector3DI mSpacing;
Vector3DI mOffset;
} MugenFont;
static struct {
map<int, MugenFont> mFonts;
} gMugenFontData;
static void loadBitmapFont(MugenDefScript* tScript, MugenFont* tFont) {
MugenBitmapFont e;
char* path = getAllocatedMugenDefStringVariable(tScript, "def", "file");
e.mSprites = loadMugenSpriteFileWithoutPalette(path);
freeMemory(path);
tFont->mData = e;
}
static void loadMugenTruetypeFont(MugenDefScript* tScript, MugenFont* tFont) {
MugenTruetypeFont e;
char* name = getAllocatedMugenDefStringVariable(tScript, "def", "file");
e.mFont = loadTruetypeFont(name, tFont->mSize.y);
freeMemory(name);
tFont->mData = e;
}
typedef struct {
MugenFont* mFont;
MugenElecbyteFont* mElecbyteFont;
int i;
} ElecbyteMapParseCaller;
static void parseSingleMapElement(void* tCaller, void* tData) {
ElecbyteMapParseCaller* caller = (ElecbyteMapParseCaller*)tCaller;
MugenDefScriptGroupElement* element = (MugenDefScriptGroupElement*)tData;
assert(element->mType == MUGEN_DEF_SCRIPT_GROUP_STRING_ELEMENT);
MugenDefScriptStringElement* e = (MugenDefScriptStringElement*)element->mData;
MugenElecbyteFontMapEntry entry;
char key[100];
if (caller->mElecbyteFont->mType == MUGEN_ELECBYTE_FONT_TYPE_VARIABLE) {
int items = sscanf(e->mString, "%s %d %d", key, &entry.mStartX, &entry.mWidth);
if (items != 3) {
logWarningFormat("Unable to parse entry from string: %s", e->mString);
caller->i++;
return;
}
}
else if (caller->mElecbyteFont->mType == MUGEN_ELECBYTE_FONT_TYPE_FIXED) {
int items = sscanf(e->mString, "%s", key);
(void)items;
assert(items == 1);
entry.mStartX = caller->i * caller->mFont->mSize.x;
entry.mWidth = caller->mFont->mSize.x;
}
uint8_t keyValue;
if (strlen(key) == 1) {
keyValue = (uint8_t)key[0];
}
else if (key[0] == '0' && key[1] == 'x') {
int tempKeyValue; // dreamcast crashes when reading into keyValue directly, so read into temp
int items = sscanf(e->mString, "%i", &tempKeyValue);
if (items != 1) {
logWarningFormat("Unable to parse keyValue from string: %s", e->mString);
keyValue = 0;
}
else
{
keyValue = (uint8_t)tempKeyValue;
}
}
else {
logError("Unrecognized map key.");
logErrorString(key);
recoverFromError();
keyValue = 0xFF;
}
entry.mExists = 1;
caller->mElecbyteFont->mMap[keyValue % 0x100] = entry;
caller->i++;
}
static MugenElecbyteFontType getMugenElecbyteFontType(MugenDefScript* tScript) {
MugenElecbyteFontType ret;
char* text = getAllocatedMugenDefStringVariable(tScript, "def", "type");
turnStringLowercase(text);
if (!strcmp("fixed", text)) {
ret = MUGEN_ELECBYTE_FONT_TYPE_FIXED;
}
else if (!strcmp("variable", text)) {
ret = MUGEN_ELECBYTE_FONT_TYPE_VARIABLE;
}
else {
logError("Unable to determine font type.");
logErrorString(text);
recoverFromError();
ret = MUGEN_ELECBYTE_FONT_TYPE_VARIABLE;
}
freeMemory(text);
return ret;
}
static void loadMugenElecbyteFont(MugenDefScript* tScript, const Buffer& tTextureBuffer, MugenFont* tFont) {
MugenElecbyteFont e;
e.mType = getMugenElecbyteFontType(tScript);
for (int i = 0; i < 0x100; i++) e.mMap[i].mExists = 0;
MugenDefScriptGroup* group = &tScript->mGroups["map"];
ElecbyteMapParseCaller caller;
caller.mFont = tFont;
caller.mElecbyteFont = &e;
caller.i = 0;
list_map(&group->mOrderedElementList, parseSingleMapElement, &caller);
e.mSprite = loadSingleTextureFromPCXBuffer(tTextureBuffer);
tFont->mData = e;
}
static MugenFontType getMugenFontTypeFromScript(MugenDefScript* tScript) {
char* text = getAllocatedMugenDefStringVariable(tScript, "def", "type");
turnStringLowercase(text);
MugenFontType ret;
if (!strcmp("bitmap", text)) {
ret = MUGEN_FONT_TYPE_BITMAP;
}
else if (!strcmp("truetype", text)) {
ret = MUGEN_FONT_TYPE_TRUETYPE;
}
else {
ret = MUGEN_FONT_TYPE_BITMAP;
logError("Unable to determine font type");
logErrorString(text);
recoverFromError();
}
freeMemory(text);
return ret;
}
static void setMugenFontDirectory(const char* tPath) {
(void)tPath;
setWorkingDirectory("/");
}
static void setMugenFontDirectory2(const char* tPath) {
char path[1024];
getPathToFile(path, tPath);
setWorkingDirectory(path);
}
static void resetMugenFontDirectory() {
setWorkingDirectory("/");
}
int hasMugenFont(int tKey)
{
return stl_map_contains(gMugenFontData.mFonts, tKey);
}
typedef struct {
uint8_t mMagic[12];
uint16_t mVersionHigh;
uint16_t mVersionLow;
uint32_t mTextureOffset;
uint32_t mTextureLength;
uint32_t mTextOffset;
uint32_t mTextLength;
uint8_t mComment[40];
} MugenFontHeader;
static void addMugenFont1(int tKey, const char* tPath) {
setMugenFontDirectory(tPath);
MugenFontHeader header;
Buffer b = fileToBuffer(tPath);
BufferPointer p = getBufferPointer(b);
readFromBufferPointer(&header, &p, sizeof(MugenFontHeader));
Buffer textureBuffer = makeBuffer((void*)(((uintptr_t)b.mData) + header.mTextureOffset), header.mTextureLength);
Buffer textBuffer = makeBuffer((void*)(((uintptr_t)b.mData) + header.mTextOffset), (b.mLength - header.mTextOffset));
MugenDefScript script;
loadMugenDefScriptFromBufferAndFreeBuffer(&script, textBuffer);
MugenFont& e = gMugenFontData.mFonts[tKey];
e.mSize = getMugenDefVectorIOrDefault(&script, "def", "size", Vector3DI(1, 1, 0));
e.mSpacing = getMugenDefVectorIOrDefault(&script, "def", "spacing", Vector3DI(0, 0, 0));
e.mOffset = getMugenDefVectorIOrDefault(&script, "def", "offset", Vector3DI(0, 0, 0));
e.mType = MUGEN_FONT_TYPE_ELECBYTE;
loadMugenElecbyteFont(&script, textureBuffer, &e);
unloadMugenDefScript(&script);
freeBuffer(b);
resetMugenFontDirectory();
}
static void addMugenFont2(int tKey, const char* tPath) {
MugenDefScript script;
loadMugenDefScript(&script, tPath);
setMugenFontDirectory2(tPath);
MugenFont& e = gMugenFontData.mFonts[tKey];
e.mType = getMugenFontTypeFromScript(&script);
e.mSize = getMugenDefVectorIOrDefault(&script, "def", "size", Vector3DI(1, 1, 0));
e.mSpacing = getMugenDefVectorIOrDefault(&script, "def", "spacing", Vector3DI(0, 0, 0));
e.mOffset = getMugenDefVectorIOrDefault(&script, "def", "offset", Vector3DI(0, 0, 0));
if (e.mType == MUGEN_FONT_TYPE_BITMAP) {
loadBitmapFont(&script, &e);
}
else if (e.mType == MUGEN_FONT_TYPE_TRUETYPE) {
loadMugenTruetypeFont(&script, &e);
}
else {
logError("Unimplemented font type.");
logErrorInteger(e.mType);
recoverFromError();
}
unloadMugenDefScript(&script);
resetMugenFontDirectory();
}
void addMugenFont(int tKey, const char* tPath) {
if(hasMugenFont(tKey))
{
removeMugenFont(tKey);
}
char path[1024];
if (strchr(tPath, '/')) {
sprintf(path, "assets/%s", tPath);
if (!isFile(path)) {
sprintf(path, "%s", tPath);
}
}
else {
sprintf(path, "assets/font/%s", tPath);
if (!isFile(path)) {
sprintf(path, "font/%s", tPath);
}
}
if (!isFile(path)) {
logWarningFormat("Unable to open font %s. Ignoring.", tPath);
return;
}
std::string ending = getFileExtension(path);
turnStringLowercase(ending);
if (ending == "def") {
addMugenFont2(tKey, path);
}
else if (ending == "fnt") {
addMugenFont1(tKey, path);
}
else {
logError("Unrecognized font file type.");
logErrorString(tPath);
recoverFromError();
}
}
void loadMugenTextHandler()
{
gMugenFontData.mFonts.clear();
}
static void unloadBitmapFont(MugenFont* tFont) {
auto& bitmapFont = std::get<MugenBitmapFont>(tFont->mData);
unloadMugenSpriteFile(&bitmapFont.mSprites);
}
static void unloadElecbyteFont(MugenFont* tFont) {
auto& elecbyteFont = std::get<MugenElecbyteFont>(tFont->mData);
unloadMugenSpriteFileSprite(&elecbyteFont.mSprite);
}
static void unloadMugenTruetypeFont(MugenFont* tFont) {
auto& truetypeFont = std::get<MugenTruetypeFont>(tFont->mData);
unloadTruetypeFont(truetypeFont.mFont);
}
static int unloadSingleFont(void* tCaller, MugenFont& tData) {
(void)tCaller;
MugenFont* font = &tData;
if (font->mType == MUGEN_FONT_TYPE_BITMAP) {
unloadBitmapFont(font);
} else if (font->mType == MUGEN_FONT_TYPE_ELECBYTE) {
unloadElecbyteFont(font);
} else if (font->mType == MUGEN_FONT_TYPE_TRUETYPE) {
unloadMugenTruetypeFont(font);
}
else {
logError("Unimplemented font type.");
logErrorInteger(font->mType);
recoverFromError();
}
return 1;
}
void unloadMugenFonts()
{
stl_int_map_remove_predicate(gMugenFontData.mFonts, unloadSingleFont);
}
void removeMugenFont(int tKey)
{
assert(hasMugenFont(tKey));
unloadSingleFont(NULL, gMugenFontData.mFonts[tKey]);
}
static MugenFont* getUsedMugenFontFromAvailable(int tFont)
{
if (stl_map_contains(gMugenFontData.mFonts, tFont)) {
return &gMugenFontData.mFonts[tFont];
}
else if (stl_map_contains(gMugenFontData.mFonts, 1)) { // try falling back to font 1 if it exists
return &gMugenFontData.mFonts[1];
}
else if (!gMugenFontData.mFonts.empty()) // just pick one
{
return &gMugenFontData.mFonts.begin()->second;
}
else {
logErrorFormat("[MugenTextHandler] Failed font fallback for font %d: No fonts loaded.", tFont);
abortSystem();
}
return nullptr;
}
int getMugenFontSizeY(int tKey)
{
const auto font = getUsedMugenFontFromAvailable(tKey);
return font->mSize.y;
}
int getMugenFontSpacingY(int tKey)
{
const auto font = getUsedMugenFontFromAvailable(tKey);
return font->mSpacing.y;
}
typedef struct {
char mText[1024];
char mDisplayText[1024];
Position mPosition;
MugenFont* mFont;
double mR;
double mG;
double mB;
double mScale;
MugenTextAlignment mAlignment;
GeoRectangle2D mRectangle;
double mTextBoxWidth;
Duration mBuildupDurationPerLetter;
Duration mBuildupNow;
int mIsVisible;
} MugenText;
static struct {
map<int, MugenText> mHandledTexts;
} gMugenTextHandler;
static void loadMugenTextHandlerActor(void* tData) {
(void) tData;
setProfilingSectionMarkerCurrentFunction();
gMugenTextHandler.mHandledTexts.clear();
}
static void unloadMugenTextHandlerActor(void* tData) {
(void)tData;
setProfilingSectionMarkerCurrentFunction();
gMugenTextHandler.mHandledTexts.clear();
}
static void updateSingleTextBuildup(MugenText* e) {
if (handleDurationAndCheckIfOver(&e->mBuildupNow, e->mBuildupDurationPerLetter)) {
if (!strcmp(e->mText, e->mDisplayText)) {
e->mBuildupDurationPerLetter = INF;
return;
}
auto l = strlen(e->mDisplayText);
e->mDisplayText[l] = e->mText[l];
e->mDisplayText[l + 1] = '\0';
}
}
static void updateSingleText(void* tCaller, MugenText& tData) {
(void)tCaller;
(void)tData;
MugenText* e = &tData;
updateSingleTextBuildup(e);
}
static void updateMugenTextHandler(void* tData) {
(void)tData;
setProfilingSectionMarkerCurrentFunction();
stl_int_map_map(gMugenTextHandler.mHandledTexts, updateSingleText);
}
static double getOriginalMugenFontFactor() {
const auto sz = getScreenSize();
return sz.y / 240.0;
}
static double getNewMugenFontFactor() {
const auto sz = getScreenSize();
return sz.y / 480.0;
}
typedef struct {
MugenSpriteFileSprite* mSprite;
Position mBasePosition;
MugenText* mText;
MugenFont* mFont;
} BitmapDrawCaller;
static void drawSingleBitmapSubSprite(MugenSpriteFileSubSprite& tSubSprite, BitmapDrawCaller& tCaller) {
double factor = getNewMugenFontFactor() * tCaller.mText->mScale;
Position p = tCaller.mBasePosition + (tSubSprite.mOffset * factor);
int minHeight = 0;
int maxHeight = tSubSprite.mTexture.mTextureSize.y - 1;
int upY = max(minHeight, min(maxHeight, (int)(tCaller.mText->mRectangle.mTopLeft.y - p.y)));
int downY = max(minHeight, min(maxHeight, upY + (int)(tCaller.mText->mRectangle.mBottomRight.y - (p.y + tCaller.mFont->mSize.y))));
if (upY == downY) return;
p.y = max(p.y, tCaller.mText->mRectangle.mTopLeft.y);
scaleDrawing(factor, p);
drawSprite(tSubSprite.mTexture, p, makeRectangleFromTexture(tSubSprite.mTexture));
scaleDrawing(1 / factor, p);
}
static set<int> getBitmapTextLinebreaks(char* tText, const Position& tStart, MugenFont* tFont, MugenSpriteFileGroup& tSpriteGroup, double tRightX, double tFactor) {
if (tRightX >= INF / 2) return set<int>();
set<int> ret;
Position p = tStart;
auto n = int(strlen(tText));
for (int i = 0; i < n; i++) {
int positionsRead = 0;
int doesBreak;
if (tText[i] == '\n') {
doesBreak = 1;
}
else {
char word[1024];
int items = sscanf(tText + i, "%1023s%n", word, &positionsRead);
if (items != 1) break;
for (int j = i; j < i + positionsRead; j++) {
if (stl_map_contains(tSpriteGroup.mSprites, (int)tText[j])) {
auto& sprite = tSpriteGroup.mSprites[(int)tText[j]];
p = vecAdd2D(p, vecScale(Vector3D(sprite.mOriginalTextureSize.x, 0, 0), tFactor));
p = vecAdd2D(p, vecScale(Vector3D(tFont->mSpacing.x, 0, 0), tFactor));
}
else {
p = vecAdd2D(p, vecScale(Vector3D(tFont->mSize.x, 0, 0), tFactor));
p = vecAdd2D(p, vecScale(Vector3D(tFont->mSpacing.x, 0, 0), tFactor));
}
}
doesBreak = (p.x > tRightX);
i += std::max(positionsRead - 1, 0);
}
if (doesBreak) {
auto skipIndex = i;
i -= positionsRead - 1;
while (i > 0 && i < n && tText[i] == ' ') i++;
i = max(0, i - 1);
if (ret.find(i) == ret.end()) {
ret.insert(i);
p.x = tStart.x;
}
else {
i = skipIndex;
}
}
}
return ret;
}
static void drawSingleBitmapText(MugenText* e) {
MugenFont* font = e->mFont;
auto& bitmapFont = std::get<MugenBitmapFont>(font->mData);
auto textLength = int(strlen(e->mDisplayText));
double factor = getNewMugenFontFactor() * e->mScale;
setDrawingBaseColorAdvanced(e->mR, e->mG, e->mB);
auto& spriteGroup = bitmapFont.mSprites.mGroups[0];
int i;
Position p = vecAdd2D(e->mPosition, vecScale(Vector3D(font->mOffset.x, font->mOffset.y, 0), factor));
Position start = p;
double rightX = p.x + e->mTextBoxWidth;
const auto breaks = getBitmapTextLinebreaks(e->mText, start, font, spriteGroup, rightX, factor);
for (i = 0; i < textLength; i++) {
if (stl_map_contains(spriteGroup.mSprites, (int)e->mDisplayText[i])) {
BitmapDrawCaller caller;
caller.mSprite = &spriteGroup.mSprites[(int)e->mDisplayText[i]];
caller.mBasePosition = p;
caller.mText = e;
caller.mFont = font;
for (auto& texture : caller.mSprite->mTextures)
{
drawSingleBitmapSubSprite(texture, caller);
}
p = vecAdd2D(p, vecScale(Vector3D(caller.mSprite->mOriginalTextureSize.x, 0, 0), factor));
p = vecAdd2D(p, vecScale(Vector3D(font->mSpacing.x, 0, 0), factor));
}
else {
p = vecAdd2D(p, vecScale(Vector3D(font->mSize.x, 0, 0), factor));
p = vecAdd2D(p, vecScale(Vector3D(font->mSpacing.x, 0, 0), factor));
}
if (breaks.find(i) != breaks.end()) {
p.x = start.x;
p = vecAdd2D(p, vecScale(Vector3D(0, font->mSize.y, 0), factor));
p = vecAdd2D(p, vecScale(Vector3D(0, font->mSpacing.y, 0), factor));
}
}
setDrawingBaseColorAdvanced(1, 1, 1);
}
static void drawSingleTruetypeText(MugenText* e) {
MugenFont* font = e->mFont;
auto& truetypeFont = std::get<MugenTruetypeFont>(font->mData);
drawTruetypeText(e->mDisplayText, truetypeFont.mFont, e->mPosition, font->mSize, Vector3D(e->mR, e->mG, e->mB), e->mTextBoxWidth, e->mRectangle);
}
typedef struct {
MugenElecbyteFontMapEntry* mMapEntry;
Position mBasePosition;
double mStartX;
int mPreviousSubSpriteOffsetY;
MugenText* mText;
MugenFont* mFont;
} ElecbyteDrawCaller;
static void drawSingleElecbyteSubSprite(MugenSpriteFileSubSprite& tSubSprite, ElecbyteDrawCaller& tCaller) {
if (tCaller.mMapEntry->mStartX >= tSubSprite.mOffset.x + tSubSprite.mTexture.mTextureSize.x) return;
if (tCaller.mMapEntry->mStartX + tCaller.mMapEntry->mWidth - 1 < tSubSprite.mOffset.x) return;
if (tSubSprite.mOffset.y > tCaller.mPreviousSubSpriteOffsetY) {
tCaller.mBasePosition.x = tCaller.mStartX;
}
tCaller.mPreviousSubSpriteOffsetY = tSubSprite.mOffset.y;
int minWidth = 0;
int maxWidth = tSubSprite.mTexture.mTextureSize.x - 1;
int leftX = max(minWidth, min(maxWidth, tCaller.mMapEntry->mStartX - tSubSprite.mOffset.x));
int rightX = max(minWidth, min(maxWidth, (tCaller.mMapEntry->mStartX + tCaller.mMapEntry->mWidth - 1) - tSubSprite.mOffset.x));
double factor = getOriginalMugenFontFactor() * tCaller.mText->mScale;
Position p = vecAdd2D(tCaller.mBasePosition, vecScale(Vector3D(0.f, tSubSprite.mOffset.y, 0.f), factor));
int minHeight = 0;
int maxHeight = tSubSprite.mTexture.mTextureSize.y - 1;
int upY = max(minHeight, min(maxHeight, (int)(tCaller.mText->mRectangle.mTopLeft.y - p.y)));
int downY = max(minHeight, min(maxHeight, upY + (int)(tCaller.mText->mRectangle.mBottomRight.y - (p.y + tCaller.mFont->mSize.y))));
if (upY == downY) return;
p.y = max(p.y, tCaller.mText->mRectangle.mTopLeft.y);
scaleDrawing(factor, p);
drawSprite(tSubSprite.mTexture, p, makeRectangle(leftX, upY, rightX - leftX, downY - upY));
scaleDrawing(1 / factor, p);
tCaller.mBasePosition.x += (rightX - leftX + 1) * factor;
}
static set<int> getElecbyteTextLinebreaks(char* tText, const Position& tStart, MugenFont* tFont, MugenElecbyteFont& tElecbyteFont, double tRightX, double tFactor) {
if (tRightX >= INF / 2) return set<int>();
set<int> ret;
Position p = tStart;
auto n = int(strlen(tText));
for (int i = 0; i < n; i++) {
int positionsRead = 0;
int doesBreak;
if (tText[i] == '\n') {
doesBreak = 1;
}
else {
char word[1024];
int items = sscanf(tText + i, "%1023s%n", word, &positionsRead);
if (items != 1) break;
for (int j = i; j < i + positionsRead; j++) {
if (tElecbyteFont.mMap[(uint8_t)tText[j]].mExists) {
MugenElecbyteFontMapEntry& mapEntry = tElecbyteFont.mMap[(uint8_t)tText[j]];
p = vecAdd2D(p, vecScale(Vector3D(mapEntry.mWidth, 0, 0), tFactor));
p = vecAdd2D(p, vecScale(Vector3D(tFont->mSpacing.x, 0, 0), tFactor));
}
else {
p = vecAdd2D(p, vecScale(Vector3D(tFont->mSize.x, 0, 0), tFactor));
p = vecAdd2D(p, vecScale(Vector3D(tFont->mSpacing.x, 0, 0), tFactor));
}
}
doesBreak = (p.x > tRightX);
i += std::max(positionsRead - 1, 0);
}
if (doesBreak) {
auto skipIndex = i;
i -= positionsRead - 1;
while (i > 0 && i < n && tText[i] == ' ') i++;
i = max(0, i - 1);
if (ret.find(i) == ret.end()) {
ret.insert(i);
p.x = tStart.x;
}
else {
i = skipIndex;
}
}
}
return ret;
}
static void drawSingleElecbyteText(MugenText* e) {
MugenFont* font = e->mFont;
auto& elecbyteFont = std::get<MugenElecbyteFont>(font->mData);
auto textLength = int(strlen(e->mDisplayText));
double factor = getOriginalMugenFontFactor() * e->mScale;
setDrawingBaseColorAdvanced(e->mR, e->mG, e->mB);
int i;
Position p = vecAdd2D(e->mPosition, Vector3D(font->mOffset.x, font->mOffset.y, 0));
Position start = p;
double rightX = p.x + e->mTextBoxWidth;
const auto breaks = getElecbyteTextLinebreaks(e->mText, start, font, elecbyteFont, rightX, factor);
for (i = 0; i < textLength; i++) {
if (elecbyteFont.mMap[(uint8_t)e->mDisplayText[i]].mExists) {
ElecbyteDrawCaller caller;
caller.mMapEntry = &elecbyteFont.mMap[(uint8_t)e->mDisplayText[i]];
caller.mBasePosition = p;
caller.mStartX = p.x;
caller.mPreviousSubSpriteOffsetY = INF;
caller.mText = e;
caller.mFont = font;
for (auto& texture : elecbyteFont.mSprite.mTextures)
{
drawSingleElecbyteSubSprite(texture, caller);
}
p = vecAdd2D(p, vecScale(Vector3D(caller.mMapEntry->mWidth, 0, 0), factor));
p = vecAdd2D(p, vecScale(Vector3D(font->mSpacing.x, 0, 0), factor));
}
else {
p = vecAdd2D(p, vecScale(Vector3D(font->mSize.x, 0, 0), factor));
p = vecAdd2D(p, vecScale(Vector3D(font->mSpacing.x, 0, 0), factor));
}
if (breaks.find(i) != breaks.end()) {
p.x = start.x;
p = vecAdd2D(p, vecScale(Vector3D(0, font->mSize.y, 0), factor));
p = vecAdd2D(p, vecScale(Vector3D(0, font->mSpacing.y, 0), factor));
}
}
setDrawingBaseColorAdvanced(1, 1, 1);
}
static void drawSingleText(void* tCaller, MugenText& tData) {
(void)tCaller;
MugenText* e = &tData;
if (!e->mIsVisible) return;
if (e->mFont->mType == MUGEN_FONT_TYPE_BITMAP) {
drawSingleBitmapText(e);
}
else if (e->mFont->mType == MUGEN_FONT_TYPE_TRUETYPE) {
drawSingleTruetypeText(e);
}
else if (e->mFont->mType == MUGEN_FONT_TYPE_ELECBYTE) {
drawSingleElecbyteText(e);
}
else {
logError("Unimplemented font type.");
logErrorInteger(e->mFont->mType);
recoverFromError();
}
}
static void drawMugenTextHandler(void* tData) {
(void)tData;
setProfilingSectionMarkerCurrentFunction();
stl_int_map_map(gMugenTextHandler.mHandledTexts, drawSingleText);
}
ActorBlueprint getMugenTextHandler() {
return makeActorBlueprint(loadMugenTextHandlerActor, unloadMugenTextHandlerActor, updateMugenTextHandler, drawMugenTextHandler);
};
void drawMugenText(char* tText, const Position& tPosition, int tFont) {
MugenText textData;
strcpy(textData.mText, tText);
strcpy(textData.mDisplayText, tText);
textData.mPosition = tPosition;
textData.mFont = &gMugenFontData.mFonts[tFont];
textData.mR = textData.mG = textData.mB = 1;
textData.mScale = 1;
textData.mAlignment = MUGEN_TEXT_ALIGNMENT_LEFT;
textData.mRectangle = GeoRectangle2D(-INF / 2, -INF / 2, INF, INF);
textData.mTextBoxWidth = INF;
textData.mBuildupDurationPerLetter = INF;
textData.mBuildupNow = 0;
textData.mIsVisible = 1;
drawSingleText(NULL, textData);
}
int addMugenText(const char* tText, const Position& tPosition, int tFont)
{
int id = stl_int_map_push_back(gMugenTextHandler.mHandledTexts, MugenText());
MugenText& e = gMugenTextHandler.mHandledTexts[id];
strcpy(e.mText, tText);
strcpy(e.mDisplayText, tText);
e.mFont = getUsedMugenFontFromAvailable(tFont);
e.mScale = 1;
e.mPosition = vecSub(tPosition, Vector3D(0, e.mFont->mSize.y * e.mScale, 0));
e.mR = e.mG = e.mB = 1;
e.mAlignment = MUGEN_TEXT_ALIGNMENT_LEFT;
e.mRectangle = GeoRectangle2D(-INF / 2, -INF / 2, INF, INF);
e.mTextBoxWidth = INF;
e.mBuildupDurationPerLetter = INF;
e.mBuildupNow = 0;
e.mIsVisible = 1;
return id;
}
int addMugenTextMugenStyle(const char * tText, const Position& tPosition, const Vector3DI& tFont)
{
int ret = addMugenText(tText, tPosition, tFont.x);
setMugenTextColor(ret, getMugenTextColorFromMugenTextColorIndex(tFont.y));
setMugenTextAlignment(ret, getMugenTextAlignmentFromMugenAlignmentIndex(tFont.z));
return ret;
}
void removeMugenText(int tID)
{
gMugenTextHandler.mHandledTexts.erase(tID);
}
void setMugenTextFont(int tID, int tFont)
{
MugenText* e = &gMugenTextHandler.mHandledTexts[tID];
e->mPosition = vecAdd2D(e->mPosition, Vector3D(0, e->mFont->mSize.y * e->mScale, 0));
e->mFont = getUsedMugenFontFromAvailable(tFont);
e->mPosition = vecSub(e->mPosition, Vector3D(0, e->mFont->mSize.y * e->mScale, 0));
}
static double getBitmapTextSize(MugenText* e) {
MugenFont* font = e->mFont;
auto& bitmapFont = std::get<MugenBitmapFont>(font->mData);
auto textLength = int(strlen(e->mText));
double factor = getOriginalMugenFontFactor() * e->mScale;
auto& spriteGroup = bitmapFont.mSprites.mGroups[0];
double sizeX = 0;
int i;
for (i = 0; i < textLength; i++) {
if (stl_map_contains(spriteGroup.mSprites, (int)e->mText[i])) {
auto& sprite = spriteGroup.mSprites[(int)e->mText[i]];
sizeX += sprite.mOriginalTextureSize.x * factor;
sizeX += font->mSpacing.x * factor;
}
else {
sizeX += font->mSize.x * factor;
sizeX += font->mSpacing.x * factor;
}
}
return sizeX;
}
static double getTruetypeTextSize(MugenText* e) {
auto textLength = strlen(e->mText);
return double(e->mFont->mSize.x*textLength);
}
static double getElecbyteTextSize(MugenText* e) {
MugenFont* font = e->mFont;
auto& elecbyteFont = std::get<MugenElecbyteFont>(font->mData);
auto textLength = int(strlen(e->mText));
double factor = getOriginalMugenFontFactor() * e->mScale;
int i;
double sizeX = 0;
for (i = 0; i < textLength; i++) {
if (elecbyteFont.mMap[(uint8_t)e->mText[i]].mExists) {
MugenElecbyteFontMapEntry& mapEntry = elecbyteFont.mMap[(uint8_t)e->mText[i]];
sizeX += mapEntry.mWidth*factor;
sizeX += font->mSpacing.x*factor;
}
else {
sizeX += font->mSize.x*factor;
sizeX += font->mSpacing.x*factor;
}
}
return sizeX;
}
static double getMugenTextSizeXInternal(MugenText* e) {
if (e->mFont->mType == MUGEN_FONT_TYPE_BITMAP) {
return getBitmapTextSize(e);
}
else if (e->mFont->mType == MUGEN_FONT_TYPE_TRUETYPE) {
return getTruetypeTextSize(e);
}
else if (e->mFont->mType == MUGEN_FONT_TYPE_ELECBYTE) {
return getElecbyteTextSize(e);
}
else {
logError("Unimplemented font type.");
logErrorInteger(e->mFont->mType);
recoverFromError();
return 0;
}
}
static double getMugenTextAlignmentOffsetX(MugenText* e, MugenTextAlignment tAlignment) {
double sizeX = getMugenTextSizeXInternal(e);
double ret = 0;
if (e->mAlignment == MUGEN_TEXT_ALIGNMENT_CENTER) {
ret += sizeX / 2;
}
else if (e->mAlignment == MUGEN_TEXT_ALIGNMENT_RIGHT) {
ret += sizeX;
}
if (tAlignment == MUGEN_TEXT_ALIGNMENT_CENTER) {
ret -= sizeX / 2;
}
else if (tAlignment == MUGEN_TEXT_ALIGNMENT_RIGHT) {
ret -= sizeX;
}
return ret;
}
void setMugenTextAlignment(int tID, MugenTextAlignment tAlignment)
{
MugenText* e = &gMugenTextHandler.mHandledTexts[tID];
e->mPosition.x += getMugenTextAlignmentOffsetX(e, tAlignment);
e->mAlignment = tAlignment;
}
void setMugenTextColor(int tID, Color tColor)
{
MugenText* e = &gMugenTextHandler.mHandledTexts[tID];
getRGBFromColor(tColor, &e->mR,&e->mG, &e->mB);
}
void setMugenTextColorRGB(int tID, double tR, double tG, double tB)
{
MugenText* e = &gMugenTextHandler.mHandledTexts[tID];
e->mR = tR;
e->mG = tG;
e->mB = tB;
}
void setMugenTextRectangle(int tID, const GeoRectangle2D& tRectangle)
{
MugenText* e = &gMugenTextHandler.mHandledTexts[tID];
e->mRectangle = tRectangle;
}
void setMugenTextPosition(int tID, const Position& tPosition)
{
MugenText* e = &gMugenTextHandler.mHandledTexts[tID];
e->mPosition = tPosition;
e->mPosition = vecSub(e->mPosition, Vector3D(0, e->mFont->mSize.y * e->mScale, 0));
MugenTextAlignment alignment = e->mAlignment;
e->mAlignment = MUGEN_TEXT_ALIGNMENT_LEFT;
setMugenTextAlignment(tID, alignment);
}
void addMugenTextPosition(int tID, const Position& tPosition)
{
auto pos = getMugenTextPositionReference(tID);
*pos += tPosition;
}
void setMugenTextTextBoxWidth(int tID, double tWidth)
{
MugenText* e = &gMugenTextHandler.mHandledTexts[tID];
e->mTextBoxWidth = tWidth;
}
void setMugenTextBuildup(int tID, Duration mBuildUpDurationPerLetter)