summaryrefslogtreecommitdiff
path: root/src/code/z_kanfont.c
blob: 280bfa3df8643f9cde92ff916631d102becbf025 (plain)
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
#include "kanread.h"
#include "message_data_static.h"
#include "printf.h"
#include "segment_symbols.h"
#include "translation.h"
#include "versions.h"
#include "dma.h"
#include "font.h"
#include "message.h"

/**
 * Loads a texture from kanji for the requested `character` into the character texture buffer
 * at `codePointIndex`. The value of `character` is the SHIFT-JIS encoding of the character.
 */
void Font_LoadCharWide(Font* font, u16 character, u16 codePointIndex) {
#if OOT_NTSC
    DMA_REQUEST_SYNC(&font->charTexBuf[codePointIndex],
                     (uintptr_t)_kanjiSegmentRomStart + Kanji_OffsetFromShiftJIS(character), FONT_CHAR_TEX_SIZE,
                     "../z_kanfont.c", UNK_LINE);
#endif
}

/**
 * Loads a texture from nes_font_static for the requested `character` into the character texture buffer
 * at `codePointIndex`. The value of `character` is the ASCII codepoint subtract ' '/0x20.
 */
void Font_LoadChar(Font* font, u8 character, u16 codePointIndex) {
    s32 offset = character * FONT_CHAR_TEX_SIZE;

    DMA_REQUEST_SYNC(&font->charTexBuf[codePointIndex], (uintptr_t)_nes_font_staticSegmentRomStart + offset,
                     FONT_CHAR_TEX_SIZE, "../z_kanfont.c", 93);
}

#if PLATFORM_IQUE
void Font_LoadCharCHN(Font* font, u16 character, u16 codePointIndex) {
    s32 offset = character * FONT_CHAR_TEX_SIZE;

    DMA_REQUEST_SYNC(&font->charTexBuf[codePointIndex], (uintptr_t)_nes_font_staticSegmentRomStart + offset,
                     FONT_CHAR_TEX_SIZE, "../z_kanfont.c", UNK_LINE);
}
#endif

/**
 * Loads a message box icon from message_static, such as the ending triangle/square or choice arrow into the
 * icon buffer.
 * The different icons are given in the MessageBoxIcon enum.
 */
void Font_LoadMessageBoxIcon(Font* font, u16 icon) {
    DMA_REQUEST_SYNC(font->iconBuf,
                     (uintptr_t)_message_staticSegmentRomStart + 4 * MESSAGE_STATIC_TEX_SIZE +
                         icon * FONT_CHAR_TEX_SIZE,
                     FONT_CHAR_TEX_SIZE, "../z_kanfont.c", 100);
}

/**
 * Loads a full set of character textures based on their ordering in the message with text id 0xFFFC into
 * the font buffer.
 */
void Font_LoadOrderedFont(Font* font) {
    s32 size;
    s32 len;
    s32 codePointIndex;
    s32 fontBufIndex;
    u32 offset;
    const char* messageDataStart;
    u16* msgBufWide;

#if OOT_NTSC && !PLATFORM_IQUE
    messageDataStart = (const char*)_jpn_message_data_staticSegmentStart;
    font->msgOffset = _message_0xFFFC_jpn - messageDataStart;
    size = font->msgLength = _message_0xFFFD_jpn - _message_0xFFFC_jpn;
    len = (u32)size / 2;
    DMA_REQUEST_SYNC(font->msgBufWide, (uintptr_t)_jpn_message_data_staticSegmentRomStart + font->msgOffset, size,
                     "../z_kanfont.c", UNK_LINE);

    PRINTF("msg_data=%x,  msg_data0=%x   jj=%x\n", font->msgOffset, font->msgLength, len);

    fontBufIndex = 0;
    for (codePointIndex = 0; font->msgBufWide[codePointIndex] != MESSAGE_WIDE_END; codePointIndex++) {
        if (len < codePointIndex) {
            PRINTF(T("ERROR!!  エラー!!!  error───!!!!\n", "ERROR!!  Error!!!  error───!!!!\n"));
            return;
        }

        if (font->msgBufWide[codePointIndex] != MESSAGE_WIDE_NEWLINE) {
            offset = Kanji_OffsetFromShiftJIS(font->msgBufWide[codePointIndex]);
            DMA_REQUEST_SYNC(&font->fontBuf[fontBufIndex * 8], (uintptr_t)_kanjiSegmentRomStart + offset,
                             FONT_CHAR_TEX_SIZE, "../z_kanfont.c", UNK_LINE);
            fontBufIndex += FONT_CHAR_TEX_SIZE / 8;
        }
    }
#elif OOT_PAL
    messageDataStart = (const char*)_nes_message_data_staticSegmentStart;
    font->msgOffset = _message_0xFFFC_nes - messageDataStart;
    size = font->msgLength = _message_0xFFFD_nes - _message_0xFFFC_nes;
    len = size;
    DMA_REQUEST_SYNC(font->msgBuf, (uintptr_t)_nes_message_data_staticSegmentRomStart + font->msgOffset, len,
                     "../z_kanfont.c", 122);

    PRINTF("msg_data=%x,  msg_data0=%x   jj=%x\n", font->msgOffset, font->msgLength, len * 1);

    fontBufIndex = 0;
    for (codePointIndex = 0; font->msgBuf[codePointIndex] != MESSAGE_END; codePointIndex++) {
        if (codePointIndex > (len * 1)) {
            PRINTF(T("ERROR!!  エラー!!!  error───!!!!\n", "ERROR!!  Error!!!  error───!!!!\n"));
            return;
        }

        if (font->msgBuf[codePointIndex] != MESSAGE_NEWLINE) {
            PRINTF("nes_mes_buf[%d]=%d\n", codePointIndex, font->msgBuf[codePointIndex]);

            offset = (font->msgBuf[codePointIndex] - ' ') * FONT_CHAR_TEX_SIZE;
            DMA_REQUEST_SYNC(font->fontBuf + fontBufIndex * 8, (uintptr_t)_nes_font_staticSegmentRomStart + offset,
                             FONT_CHAR_TEX_SIZE, "../z_kanfont.c", 134);
            fontBufIndex += FONT_CHAR_TEX_SIZE / 8;
        }
    }
#elif PLATFORM_IQUE
    messageDataStart = (const char*)_jpn_message_data_staticSegmentStart;
    font->msgOffset = _message_0xFFFC_jpn - messageDataStart;
    size = font->msgLength = _message_0xFFFD_jpn - _message_0xFFFC_jpn;
    len = (u32)size / 2;
    DMA_REQUEST_SYNC(font->msgBufWide, (uintptr_t)_jpn_message_data_staticSegmentRomStart + font->msgOffset, size,
                     "../z_kanfont.c", UNK_LINE);

    PRINTF("msg_data=%x,  msg_data0=%x   jj=%x\n", font->msgOffset, font->msgLength, len);

    // Workaround for EGCS internal compiler error (see docs/compilers.md)
    msgBufWide = font->msgBufWide;
    fontBufIndex = 0;
    for (codePointIndex = 0; msgBufWide[codePointIndex] != MESSAGE_WIDE_END; codePointIndex++) {
        if (len < codePointIndex) {
            PRINTF(T("ERROR!!  エラー!!!  error───!!!!\n", "ERROR!!  Error!!!  error───!!!!\n"));
            return;
        }

        if (msgBufWide[codePointIndex] != MESSAGE_WIDE_NEWLINE) {
            offset = Kanji_OffsetFromShiftJIS(msgBufWide[codePointIndex]);
            DMA_REQUEST_SYNC(&font->fontBuf[fontBufIndex * 8], (uintptr_t)_kanjiSegmentRomStart + offset,
                             FONT_CHAR_TEX_SIZE, "../z_kanfont.c", UNK_LINE);
            fontBufIndex += FONT_CHAR_TEX_SIZE / 8;
        }
    }
#endif
}