From fa7007d73b72c852440e037ce4a48ff58f812f2a Mon Sep 17 00:00:00 2001 From: Dragorn421 Date: Mon, 1 Nov 2021 17:23:49 +0100 Subject: [PATCH] Document `z_moji.c` (#926) * Document `z_moji.c` * Run formatter * Use `CHAR_HEIGHT` instead of `CHAR_WIDTH` for y pos * `> X - 1` -> `>= X` * `const char*` for string argument * Type palettes and textures with `u64` * Remove unused variable * Use `CHAR_WIDTH` and `CHAR_HEIGHT` more * Explicit documentation on `gMojiFontTex` --- data/z_text.data.s | 4 +- include/variables.h | 5 +- src/code/z_moji.c | 127 +++++++++++++++++++++++++++----------------- 3 files changed, 84 insertions(+), 52 deletions(-) diff --git a/data/z_text.data.s b/data/z_text.data.s index 9fd2ccaedf..c20dd6cca6 100644 --- a/data/z_text.data.s +++ b/data/z_text.data.s @@ -11,10 +11,10 @@ # temporary file name, rename to something more appropriate when decompiled -glabel gLetterTLUT +glabel gMojiFontTLUTs .incbin "baserom.z64", 0xBA18E0, 0x80 -glabel gFontFF +glabel gMojiFontTex .incbin "baserom.z64", 0xBA1960, 0x400 # Unused diff --git a/include/variables.h b/include/variables.h index 3fa5077b93..d6cc3463a9 100644 --- a/include/variables.h +++ b/include/variables.h @@ -206,8 +206,9 @@ extern u16 gSramSlotOffsets[]; //extern ? D_8012A6A4; //extern ? D_8012A704; //extern ? D_8012A71C; -extern u8 gLetterTLUT[4][32]; // original name: "moji_tlut" -extern u8 gFontFF[]; // original name: "font_ff" +// 4 16-colors palettes +extern u64 gMojiFontTLUTs[4][4]; // original name: "moji_tlut" +extern u64 gMojiFontTex[]; // original name: "font_ff" //extern ? D_8012AC90; //extern ? D_8012ACA0; //extern ? D_8012AD20; diff --git a/src/code/z_moji.c b/src/code/z_moji.c index 7c200f63dd..b10298b684 100644 --- a/src/code/z_moji.c +++ b/src/code/z_moji.c @@ -1,100 +1,131 @@ +/** + * Unused. A very simple utility for drawing text on screen. + */ + #include "global.h" +// how big to draw the characters on screen +#define DISP_CHAR_WIDTH 8 +#define DISP_CHAR_HEIGHT 8 + +// gMojiFontTex is a TEX_CHAR_COLS x TEX_CHAR_ROWS grid of characters, +// each character being TEX_CHAR_WIDTH x TEX_CHAR_HEIGHT in size. +// Each spot on the grid contains 4 characters, which are revealed by using different TLUTs. + +#define TEX_CHAR_WIDTH 8 +#define TEX_CHAR_HEIGHT 8 + +#define TEX_CHAR_COLS 2 +#define TEX_CHAR_ROWS 16 + +// A character `c = 0bRRRRRCTT` maps to row 0bRRRRR, column 0bC and TLUT 0bTT. +#define GET_CHAR_TLUT_INDEX(c) (c & 3) +// `/ 4` matches the `& 4` (`(c & 4) / 4` is the column the character is in) +#define GET_TEX_CHAR_S(c) ((u16)(c & 4) * ((1 << 5) * TEX_CHAR_WIDTH / 4)) +#define GET_TEX_CHAR_T(c) ((u16)(c >> 3) * ((1 << 5) * TEX_CHAR_HEIGHT)) + u32 sFontColorRed = 255; u32 sFontColorGreen = 255; u32 sFontColorBlue = 255; u32 sFontColorAlpha = 255; -s32 D_80120120 = 0; -s32 D_80120124 = 0; -UNK_TYPE D_8015FFC0; -UNK_TYPE D_8015FFC4; +s32 sScreenPosX = 0; +s32 sScreenPosY = 0; -void func_8007B910(u32 red, u32 green, u32 blue, u32 alpha) { +s32 sCurTLUTIndex; + +void Moji_SetColor(u32 red, u32 green, u32 blue, u32 alpha) { sFontColorRed = red; sFontColorGreen = green; sFontColorBlue = blue; sFontColorAlpha = alpha; } -void func_8007B934(s32 arg0, s32 arg1) { - if (arg0 > 39) { - D_80120120 = 39 * 8; - } else if (arg0 < 0) { - D_80120120 = 0; +void Moji_SetPosition(s32 gridX, s32 gridY) { + if (gridX >= SCREEN_WIDTH / DISP_CHAR_WIDTH) { + sScreenPosX = SCREEN_WIDTH - DISP_CHAR_WIDTH; + } else if (gridX < 0) { + sScreenPosX = 0; } else { - D_80120120 = arg0 * 8; + sScreenPosX = gridX * DISP_CHAR_WIDTH; } - if (arg1 > 29) { - D_80120124 = 29 * 8; - } else if (arg1 < 0) { - D_80120124 = 0; + if (gridY >= SCREEN_HEIGHT / DISP_CHAR_HEIGHT) { + sScreenPosY = SCREEN_HEIGHT - DISP_CHAR_HEIGHT; + } else if (gridY < 0) { + sScreenPosY = 0; } else { - D_80120124 = arg1 * 8; + sScreenPosY = gridY * DISP_CHAR_HEIGHT; } } -void func_8007B9A4(GraphicsContext* gfxCtx, u8 arg1) { +void Moji_DrawChar(GraphicsContext* gfxCtx, char c) { s32 pad[2]; OPEN_DISPS(gfxCtx, "../z_moji.c", 86); - if ((u32)gLetterTLUT & 0xF) { - osSyncPrintf("moji_tlut --> %X\n", gLetterTLUT); + if ((u32)gMojiFontTLUTs & 0xF) { + osSyncPrintf("moji_tlut --> %X\n", gMojiFontTLUTs); } - if (D_8015FFC0 != (arg1 & 3)) { - gDPLoadTLUT(POLY_OPA_DISP++, 16, 256, &gLetterTLUT[arg1 & 3]); - D_8015FFC0 = arg1 & 3; + if (sCurTLUTIndex != GET_CHAR_TLUT_INDEX(c)) { + gDPLoadTLUT(POLY_OPA_DISP++, 16, 256, &gMojiFontTLUTs[GET_CHAR_TLUT_INDEX(c)]); + sCurTLUTIndex = GET_CHAR_TLUT_INDEX(c); } - - gSPTextureRectangle(POLY_OPA_DISP++, D_80120120 << 2, D_80120124 << 2, (D_80120120 + 8) << 2, (D_80120124 + 8) << 2, - G_TX_RENDERTILE, (u16)(arg1 & 4) * 64, (u16)(arg1 >> 3) * 256, 1 << 10, 1 << 10); + gSPTextureRectangle(POLY_OPA_DISP++, sScreenPosX << 2, sScreenPosY << 2, (sScreenPosX + DISP_CHAR_WIDTH) << 2, + (sScreenPosY + DISP_CHAR_HEIGHT) << 2, G_TX_RENDERTILE, GET_TEX_CHAR_S(c), GET_TEX_CHAR_T(c), + (1 << 10) * TEX_CHAR_WIDTH / DISP_CHAR_WIDTH, (1 << 10) * TEX_CHAR_HEIGHT / DISP_CHAR_HEIGHT); CLOSE_DISPS(gfxCtx, "../z_moji.c", 123); } -void func_8007BBA8(GraphicsContext* gfxCtx, u8* arg1) { +/** + * Does not work as is in most cases. + * Can work if the render mode, combiner and possibly other settings are set correctly. + * For example this works with the render mode used in `GfxPrint_InitDlist`, + * and `G_CC_MODULATEI_PRIM` for both combiner cycles. + */ +void Moji_DrawString(GraphicsContext* gfxCtx, const char* str) { s32 i; OPEN_DISPS(gfxCtx, "../z_moji.c", 137); - if ((u32)gFontFF & 0xF) { - osSyncPrintf("font_ff --> %X\n", gFontFF); + if ((u32)gMojiFontTex & 0xF) { + osSyncPrintf("font_ff --> %X\n", gMojiFontTex); } gDPPipeSync(POLY_OPA_DISP++); gDPSetPrimColor(POLY_OPA_DISP++, 0, 0, sFontColorRed, sFontColorGreen, sFontColorBlue, sFontColorAlpha); - gDPLoadTextureBlock_4b(POLY_OPA_DISP++, (s32)gFontFF, G_IM_FMT_CI, 16, 128, 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); + gDPLoadTextureBlock_4b(POLY_OPA_DISP++, (s32)gMojiFontTex, G_IM_FMT_CI, TEX_CHAR_COLS * TEX_CHAR_WIDTH, + TEX_CHAR_ROWS * TEX_CHAR_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); - D_8015FFC0 = -1; + sCurTLUTIndex = -1; - for (i = 0; arg1[i] != 0; i++) { - switch (arg1[i]) { - case 9: - D_80120120 = (((D_80120120 / 8) / 8) + 1) * 8 * 8; - if (D_80120120 >= SCREEN_WIDTH) { - D_80120120 = 0; - D_80120124 += 8; - if (D_80120124 >= SCREEN_HEIGHT) { - D_80120124 = 0; + for (i = 0; str[i] != '\0'; i++) { + switch (str[i]) { + case '\t': + sScreenPosX = (((sScreenPosX / DISP_CHAR_WIDTH) / 8) + 1) * DISP_CHAR_WIDTH * 8; + if (sScreenPosX >= SCREEN_WIDTH) { + sScreenPosX = 0; + sScreenPosY += DISP_CHAR_HEIGHT; + if (sScreenPosY >= SCREEN_HEIGHT) { + sScreenPosY = 0; } } break; - case 10: - case 13: - D_80120120 = 0; - D_80120124 += 8; - if (D_80120124 >= SCREEN_HEIGHT) { - D_80120124 = 0; + case '\n': + case '\r': + sScreenPosX = 0; + sScreenPosY += DISP_CHAR_HEIGHT; + if (sScreenPosY >= SCREEN_HEIGHT) { + sScreenPosY = 0; } break; default: - func_8007B9A4(gfxCtx, arg1[i]); - D_80120120 += 8; + Moji_DrawChar(gfxCtx, str[i]); + sScreenPosX += DISP_CHAR_WIDTH; } }