summaryrefslogtreecommitdiff
path: root/src/boot_O2/gfxprint.c
blob: a6f84a7a4e0cfb9954561be50a8333f0577c112f (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
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
#include "global.h"

extern u16 sGfxPrintFontTLUT[64];
extern u16 sGfxPrintUnkTLUT[16];
extern u8 sGfxPrintUnkData[8];
extern u8 sGfxPrintFontData[2048];

#define gDPSetPrimColorMod(pkt, m, l, rgba)                                                    \
    {                                                                                          \
        Gfx* _g = (Gfx*)(pkt);                                                                 \
                                                                                               \
        _g->words.w0 = (_SHIFTL(G_SETPRIMCOLOR, 24, 8) | _SHIFTL(m, 8, 8) | _SHIFTL(l, 0, 8)); \
        _g->words.w1 = (rgba);                                                                 \
    }

void GfxPrint_InitDlist(GfxPrint* this) {
    s32 width = 16;
    s32 height = 256;
    s32 i;

    gDPPipeSync(this->dlist++);
    gDPSetOtherMode(this->dlist++,
                    G_AD_DISABLE | G_CD_DISABLE | G_CK_NONE | G_TC_FILT | G_TF_BILERP | G_TT_IA16 | G_TL_TILE |
                        G_TD_CLAMP | G_TP_NONE | G_CYC_1CYCLE | G_PM_NPRIMITIVE,
                    G_AC_NONE | G_ZS_PRIM | G_RM_XLU_SURF | G_RM_XLU_SURF2);
    gDPSetCombineMode(this->dlist++, G_CC_DECALRGBA, G_CC_DECALRGBA);
    gDPLoadTextureBlock_4b(this->dlist++, sGfxPrintFontData, G_IM_FMT_CI, width, height, 0, G_TX_NOMIRROR | G_TX_WRAP,
                           G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMASK, G_TX_NOMASK, G_TX_NOLOD, G_TX_NOLOD);
    gDPLoadTLUT(this->dlist++, 64, 256, sGfxPrintFontTLUT);

    for (i = 1; i < 4; i++) {
        gDPSetTile(this->dlist++, G_IM_FMT_CI, G_IM_SIZ_4b, 1, 0, i * 2, i, G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMASK,
                   G_TX_NOLOD, G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMASK, G_TX_NOLOD);
        gDPSetTileSize(this->dlist++, i * 2, 0, 0, 60, 1020);
    }

    gDPSetPrimColorMod(this->dlist++, 0, 0, this->color.rgba);

    gDPLoadMultiTile_4b(this->dlist++, sGfxPrintUnkData, 0, 1, G_IM_FMT_CI, 2, 8, 0, 0, 1, 7, 4,
                        G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMIRROR | G_TX_WRAP, 1, 3, G_TX_NOLOD, G_TX_NOLOD);

    gDPLoadTLUT(this->dlist++, 16, 320, sGfxPrintUnkTLUT);

    for (i = 1; i < 4; i++) {
        gDPSetTile(this->dlist++, G_IM_FMT_CI, G_IM_SIZ_4b, 1, 0, i * 2 + 1, 4, G_TX_NOMIRROR | G_TX_WRAP, 3,
                   G_TX_NOLOD, G_TX_NOMIRROR | G_TX_WRAP, 1, G_TX_NOLOD);
        gDPSetTileSize(this->dlist++, i * 2 + 1, 0, 0, 4, 28);
    }
}

void GfxPrint_SetColor(GfxPrint* this, u32 r, u32 g, u32 b, u32 a) {
    this->color.r = r;
    this->color.g = g;
    this->color.b = b;
    this->color.a = a;
    gDPPipeSync(this->dlist++);
    gDPSetPrimColorMod(this->dlist++, 0, 0, this->color.rgba);
}

void GfxPrint_SetPosPx(GfxPrint* this, s32 x, s32 y) {
    this->posX = this->baseX + (x << 2);
    this->posY = this->baseY + (y << 2);
}

void GfxPrint_SetPos(GfxPrint* this, s32 x, s32 y) {
    GfxPrint_SetPosPx(this, x << 3, y << 3);
}

void GfxPrint_SetBasePosPx(GfxPrint* this, s32 x, s32 y) {
    this->baseX = x << 2;
    this->baseY = y << 2;
}

/* regalloc in the final gSPTextureRectangle */
#ifdef NON_MATCHING
void GfxPrint_PrintCharImpl(GfxPrint* this, u8 c) {
    u32 tile = (c & 0xFF) * 2;

    if (this->flag & GFXPRINT_UPDATE_MODE) {
        this->flag &= ~GFXPRINT_UPDATE_MODE;

        gDPPipeSync(this->dlist++);
        if (this->flag & GFXPRINT_USE_RGBA16) {
            gDPSetTextureLUT(this->dlist++, G_TT_RGBA16);
            gDPSetCycleType(this->dlist++, G_CYC_2CYCLE);
            gDPSetRenderMode(this->dlist++, G_RM_OPA_CI, G_RM_XLU_SURF2);
            gDPSetCombineMode(this->dlist++, G_CC_INTERFERENCE, G_CC_PASS2);
        } else {
            gDPSetTextureLUT(this->dlist++, G_TT_IA16);
            gDPSetCycleType(this->dlist++, G_CYC_1CYCLE);
            gDPSetRenderMode(this->dlist++, G_RM_XLU_SURF, G_RM_XLU_SURF2);
            gDPSetCombineMode(this->dlist++, G_CC_MODULATEIDECALA_PRIM, G_CC_MODULATEIDECALA_PRIM);
        }
    }

    if (this->flag & GFXPRINT_FLAG4) {
        gDPSetPrimColorMod(this->dlist++, 0, 0, 0);

        gSPTextureRectangle(this->dlist++, this->posX + 4, this->posY + 4, this->posX + 4 + 32, this->posY + 4 + 32,
                            (c & 3) << 1, (u16)(c & 4) * 64, (u16)(c >> 3) * 256, 1024, 1024);

        gDPSetPrimColorMod(this->dlist++, 0, 0, this->color.rgba);
    }

    gSPTextureRectangle(this->dlist++, this->posX, this->posY, this->posX + 32, this->posY + 32, (u16)(tile & 7),
                        (u16)(c & 4) * 64, (u16)(c >> 3) * 256, 1024, 1024);

    this->posX += 32;
}
#else
#pragma GLOBAL_ASM("asm/non_matchings/boot/gfxprint/GfxPrint_PrintCharImpl.s")
#endif

#pragma GLOBAL_ASM("asm/non_matchings/boot/gfxprint/GfxPrint_PrintChar.s")

void GfxPrint_PrintStringWithSize(GfxPrint* this, const void* buffer, size_t charSize, size_t charCount) {
    const char* str = (const char*)buffer;
    size_t count = charSize * charCount;

    while (count) {
        GfxPrint_PrintChar(this, *str++);
        count--;
    }
}

void GfxPrint_PrintString(GfxPrint* this, const char* str) {
    while (*str) {
        GfxPrint_PrintChar(this, *(str++));
    }
}

GfxPrint* GfxPrint_Callback(GfxPrint* this, const char* str, size_t size) {
    GfxPrint_PrintStringWithSize(this, str, sizeof(char), size);
    return this;
}

void GfxPrint_Init(GfxPrint* this) {
    this->flag &= ~GFXPRINT_OPEN;

    this->callback = GfxPrint_Callback;

    this->dlist = NULL;
    this->posX = 0;
    this->posY = 0;
    this->baseX = 0;
    this->baseY = 0;
    this->color.rgba = 0;
    this->flag &= ~GFXPRINT_FLAG1;
    this->flag &= ~GFXPRINT_USE_RGBA16;
    this->flag |= GFXPRINT_FLAG4;
    this->flag |= GFXPRINT_UPDATE_MODE;
}

void GfxPrint_Destroy(GfxPrint* this) {
}

void GfxPrint_Open(GfxPrint* this, Gfx* dlist) {
    if (!(this->flag & GFXPRINT_OPEN)) {
        this->flag |= GFXPRINT_OPEN;
        this->dlist = dlist;
        GfxPrint_InitDlist(this);
    }
}

Gfx* GfxPrint_Close(GfxPrint* this) {
    Gfx* ret;

    this->flag &= ~GFXPRINT_OPEN;
    ret = this->dlist;
    this->dlist = NULL;
    return ret;
}

s32 GfxPrint_VPrintf(GfxPrint* this, const char* fmt, va_list args) {
    return PrintUtils_VPrintf((PrintCallback*)&this->callback, fmt, args);
}

s32 GfxPrint_Printf(GfxPrint* this, const char* fmt, ...) {
    s32 ret;
    va_list args;
    va_start(args, fmt);

    ret = GfxPrint_VPrintf(this, fmt, args);

    va_end(args);

    return ret;
}