Files
balatro-gba-chinese-jocker-…/include/graphic_utils.h
T
wuxu c7ef3c0047 Add Chinese text rendering support
Extend the bitmap font pipeline with the Chinese Joker glyph subset and encode supported UTF-8 text for Tonc TTE. Move the affine map clear of the expanded font, add a compile-time VRAM overlap guard, and cover every custom glyph with host tests.
2026-07-19 11:35:14 +08:00

546 lines
16 KiB
C

/**
* @file graphic_utils.h
*
* @brief Graphic utility functions
*
* This file contains general utils and wrappers that relate to
* graphics/video/vram and generally displaying things on the screen.
* Mostly wrappers and defines for using tonc.
*
* Note: the code here assumes we're working with a single screenblock
* which should be true for this entire game since a screenblock
* is enough to contain a full screen (and more)
* and there isn't any scrolling etc.
*/
#ifndef GRAPHIC_UTILS_H
#define GRAPHIC_UTILS_H
#include <tonc_math.h>
#include <tonc_video.h>
/**
* @name Graphics Utilities Constants
*
* Reminder:
* Screen Base Block is the base for the screenblock entries i.e. tilemap
* Character Base Block is the base for the tiles themselves
*
* @{
*/
#define MAIN_BG_SBB 31
#define MAIN_BG_CBB 1
#define TTE_SBB 30
#define TTE_CBB 0
#define AFFINE_BG_SBB 4
#define AFFINE_BG_CBB 2
#define PAL_ROW_LEN 16
#define NUM_PALETTES 16
/**
* @def TILE_SIZE
* @brief Tile size in pixels, both height and width as tiles are square
*/
#define TILE_SIZE 8
/**
* @def TILE_MEM_OBJ_CHARBLOCK0_IDX
* @brief The index for the first charblock of the object memory in tonc's tile_mem
*/
#define TILE_MEM_OBJ_CHARBLOCK0_IDX 4
/** @} */
/**
* @name TTE Palette constants
*
* @{
*/
/** @def TTE_BIT_UNPACK_OFFSET */
#define TTE_BIT_UNPACK_OFFSET 14
/** @def TTE_BIT_ON_CLR_IDX */
#define TTE_BIT_ON_CLR_IDX TTE_BIT_UNPACK_OFFSET + 1
/** @def TTE_GREEN_PB */
#define TTE_GREEN_PB 6 // 0x6
/** @def TTE_PURPLE_PB */
#define TTE_PURPLE_PB 7 // 0x7
/** @def TTE_DARK_GREEN_PB */
#define TTE_DARK_GREEN_PB 8 // 0x8
/** @def TTE_DARK_BLUE_PB */
#define TTE_DARK_BLUE_PB 9 // 0x9
/** @def TTE_BLACK_PB */
#define TTE_BLACK_PB 10 // 0xA
/** @def TTE_YELLOW_PB */
#define TTE_YELLOW_PB 11 // 0xB
/** @def TTE_BLUE_PB */
#define TTE_BLUE_PB 12 // 0xC
/** @def TTE_RED_PB */
#define TTE_RED_PB 13 // 0xD
/** @def TTE_WHITE_PB */
#define TTE_WHITE_PB 14 // 0xE
/** @def TTE_SPECIAL_PB_MULT_OFFSET */
#define TTE_SPECIAL_PB_MULT_OFFSET 0x1000
/**
* @def TTE_CHAR_SIZE
*
* @brief By default TTE characters occupy a single tile
*/
#define TTE_CHAR_SIZE TILE_SIZE
/** @} */
/**
* @name TTE Text formatting Tags
*
* @{
*/
/** @def TTE_GREEN_TAG */
#define TTE_GREEN_TAG "#{cx:0x6000}"
/** @def TTE_PURPLE_TAG */
#define TTE_PURPLE_TAG "#{cx:0x7000}"
/** @def TTE_GREEN_TAG */
#define TTE_DARK_GREEN_TAG "#{cx:0x8000}"
/** @def TTE_PURPLE_TAG */
#define TTE_DARK_BLUE_TAG "#{cx:0x9000}"
/** @def TTE_BLACK_TAG */
#define TTE_BLACK_TAG "#{cx:0xA000}"
/** @def TTE_YELLOW_TAG */
#define TTE_YELLOW_TAG "#{cx:0xB000}"
/** @def TTE_BLUE_TAG */
#define TTE_BLUE_TAG "#{cx:0xC000}"
/** @def TTE_RED_TAG */
#define TTE_RED_TAG "#{cx:0xD000}"
/** @def TTE_WHITE_TAG */
#define TTE_WHITE_TAG "#{cx:0xE000}"
/** @def TTE_DIAMOND_TAG */
#define TTE_DIAMOND_TAG TTE_YELLOW_TAG "Diamond "
/** @def TTE_HEART_TAG */
#define TTE_HEART_TAG TTE_RED_TAG "Hearts "
/** @def TTE_SPADE_TAG */
#define TTE_SPADE_TAG TTE_DARK_BLUE_TAG "Spade "
/** @def TTE_CLUB_TAG */
#define TTE_CLUB_TAG TTE_DARK_GREEN_TAG "Club "
/** @} */
/**
* @name Text colors
*
* @{
*/
/** @def TEXT_CLR_GREEN */
#define TEXT_CLR_GREEN RGB15(10, 27, 16)
/** @def TEXT_CLR_PURPLE */
#define TEXT_CLR_PURPLE RGB15(25, 15, 31)
/** @def TEXT_CLR_DARK_GREEN */
#define TEXT_CLR_DARK_GREEN RGB15(0, 14, 8)
/** @def TEXT_CLR_DARK_BLUE */
#define TEXT_CLR_DARK_BLUE RGB15(10, 5, 15)
/** @def TEXT_CLR_BLACK */
#define TEXT_CLR_BLACK RGB15(3, 5, 5) // ~ 0x1483
/** @def TEXT_CLR_YELLOW */
#define TEXT_CLR_YELLOW RGB15(31, 20, 0) // 0x029F
/** @def TEXT_CLR_BLUE */
#define TEXT_CLR_BLUE RGB15(0, 18, 31) // 0x7E40
/** @def TEXT_CLR_RED */
#define TEXT_CLR_RED RGB15(31, 9, 8) // 0x213F
/** @def TEXT_CLR_WHITE */
#define TEXT_CLR_WHITE CLR_WHITE
/** @} */
/**
* @name Dimensions for a screenblock.
*
* A 1024 size screenblock is arranged in a grid of 32x32 screen entries
* Interestingly since each block is 8x8 pixels, the 240x160 GBA screen
* is smaller than the screenblock, only the top left part of the screenblock
* is displayed on the screen.
*
* @{
*/
/** @def SE_ROW_LEN */
#define SE_ROW_LEN 32
/** @def SE_COL_LEN */
#define SE_COL_LEN 32
/** @} */
enum ScreenVertDir
{
SCREEN_UP = -1,
SCREEN_DOWN = 1
};
enum ScreenHorzDir
{
SCREEN_LEFT = -1,
SCREEN_RIGHT = 1
};
enum OverflowDir
{
OVERFLOW_LEFT = SCREEN_LEFT,
OVERFLOW_RIGHT = SCREEN_RIGHT
};
enum TextJustifyFlag
{
JUSTIFY_LEFT,
JUSTIFY_CENTER
};
/** @} */
// When making this, missed that it already exists in tonc_math.h
typedef RECT Rect;
// clang-format off
/**
* @brief Structure to represent arbitrary-sized 9-patches.
*
* While expanding a 3x3 rect is simple enough and has its purpose, this is an attempt
* to generalize the idea to a struct representing a 9-patch like this, such that areas
* 1, 2, 3 and 4 have no null dimensions and don't overlap:
* ```
* margins.left margins.right │ Such as:
* ╭───┴───╮ ╭─┴─╮ │
* ╔═══════╤═════╤═══╗ ╮ │ 1) margins.left + margins.right <= width (patch_rect)
* ║ 1 │ 6 │ 2 ║ ├─ margins.top │
* ╟───────┼─────┼───╢ ╯ │ 2) margins.top + margins.bottom <= height(patch_rect)
* ║ 5 │ 0 │ 7 ║ │
* ║ │ │ ║ │ 3) margins values are inclusive, in number of tiles
* ╟───────┼─────┼───╢ ╮ │
* ║ │ │ ║ │ │
* ║ 4 │ 8 │ 3 ║ ├─ margins.bottom │
* ║ │ │ ║ │ │
* ╚═══════╧═════╧═══╝ ╯ │
* ╰────────┬────────╯ │
* patch_rect │
* ```
*/
// clang-format on
typedef struct NinePatchRect
{
Rect patch_rect;
Rect margins;
} NinePatchRect;
INLINE void tte_colors_setup(void)
{
pal_bg_bank[TTE_GREEN_PB][TTE_BIT_ON_CLR_IDX] = TEXT_CLR_GREEN;
pal_bg_bank[TTE_PURPLE_PB][TTE_BIT_ON_CLR_IDX] = TEXT_CLR_PURPLE;
pal_bg_bank[TTE_DARK_GREEN_PB][TTE_BIT_ON_CLR_IDX] = TEXT_CLR_DARK_GREEN;
pal_bg_bank[TTE_DARK_BLUE_PB][TTE_BIT_ON_CLR_IDX] = TEXT_CLR_DARK_BLUE;
pal_bg_bank[TTE_BLACK_PB][TTE_BIT_ON_CLR_IDX] = TEXT_CLR_BLACK;
pal_bg_bank[TTE_YELLOW_PB][TTE_BIT_ON_CLR_IDX] = TEXT_CLR_YELLOW;
pal_bg_bank[TTE_BLUE_PB][TTE_BIT_ON_CLR_IDX] = TEXT_CLR_BLUE;
pal_bg_bank[TTE_RED_PB][TTE_BIT_ON_CLR_IDX] = TEXT_CLR_RED;
pal_bg_bank[TTE_WHITE_PB][TTE_BIT_ON_CLR_IDX] = TEXT_CLR_WHITE;
}
/**
* @brief Get the width of a rectangle
*
* @param rect a @ref Rect to measure
*
* @return The width of the rectangle.
*/
INLINE int rect_width(const Rect* rect)
{
return max(0, rect->right - rect->left + 1);
}
/**
* @brief Get the height of a rectangle
*
* @param rect a @ref Rect to measure
*
* @return The height of the rectangle, or 0 if rect->right < rect->left
*/
INLINE int rect_height(const Rect* rect)
{
return max(0, rect->bottom - rect->top + 1);
}
/**
* @brief Copies an SE rect vertically in direction by a single tile.
*
* bg_sbb is the SBB of the background in which to move the rect
* se_rect dimensions are in number of tiles.
*
* NOTE: This does not work with TTE_SBB, probably because it's 4BPP...
*
* If you are doing this operation you are probably doing this in the main
* background and you should use main_bg_se_copy_rect_1_tile_vert() instead.
*
* @param bg_sbb the SBB of the background in which to move the rect.
*
* @param se_rect dimensions are in number of tiles.
*
* @param direction must be either @ref SE_UP or @ref SE_DOWN.
*/
void bg_se_copy_rect_1_tile_vert(u16 bg_sbb, Rect se_rect, enum ScreenVertDir direction);
/**
* @brief Clears a rect in the main background.
*
* @param se_rect dimensions need to be in number of tiles.
*/
void main_bg_se_clear_rect(Rect se_rect);
/**
* @brief Copies a rect in the main background vertically in direction by a single tile.
*
* @param se_rect dimensions are in number of tiles.
*
* @param direction must be either @ref SE_UP or @ref SE_DOWN.
*/
void main_bg_se_copy_rect_1_tile_vert(Rect se_rect, enum ScreenVertDir direction);
/**
* @brief Copies a rect in the main background from se_rect to the position (x, y).
*
* @param se_rect dimensions are in number of tiles.
*
* @param dest_pos x and y are the coordinates in number of tiles.
*/
void main_bg_se_copy_rect(Rect se_rect, BG_POINT dest_pos);
/**
* @brief Copies a tile in the main background at se_tile_src to fill se_rect_dest.
*
* @param se_rect_dest dimensions are in number of tiles.
* @param se_tile_src x and y are the coordinates in number of tiles.
*/
void main_bg_se_copy_expand_tile(Rect se_rect_dest, BG_POINT se_tile_src);
/**
* @brief Copies a 9-patch and expands it to fit a passed rect.
*
* Performs the following operation:
*
* 1. The corners are copied
* 2. The sides are stretched
* 3. The center is stretched to fill `se_rect_dest` minus the patch's margins.
*
* @param se_rect_dest Destination for the 9-patch copy. Needs to be at least large enough to fit
* the corners and sides of the patch, (`margins.left + margins.right` by
* `margins.top + margins.bottom`) in which case only the corners are copied.
* @param src_9_ptch Pointer to the properties of the 9-patch we want to stretch.
*
* @sa NinePatchRect
*/
void main_bg_se_copy_expand_9_patch(Rect se_dest_rect, const NinePatchRect* src_9_ptch);
/**
* @brief Copies a 3x3 rect and expands it to fit a passed rect. Special case of the 9-patch.
*
* Performs the following operation:
*
* 1. The 3x3 rect is stretched to fill se_rect_dest.
* 2. The corners are copied
* 3. The sides are stretched
* 4. The center is filled.
*
* @param se_rect_dest destination for the 3x3 copy, if rect sides are length 2, then the sides are
* not copied, and the center is not filled (in the case of both top and bottom sides),
* and only the corners are copied.
* **But dest rect sides must be at least 2.**
*
* @param se_rect_src_3x3_top_left points to the top left corner of the source 3x3 rect.
*/
void main_bg_se_copy_expand_3x3_rect(Rect se_rect_dest, BG_POINT se_rect_src_3x3_top_left);
/**
* @brief Copies a 3-width SE rect and expands it to fit a passed rect.
*
* Performs the following operation:
*
* 1. The sides are stretched
* 2. The center is filled.
*
* @param se_rect_dest destination for the copy; if rect width is 2, the center is not
* filled and only the sides are copied.
* **But dest rect width must be at least 2.**
*
* @param src_row_left_pnt points to the leftmost tile of the source 3-width rect.
*/
void main_bg_se_copy_expand_3w_row(Rect se_dest_rect, BG_POINT src_row_left_pnt);
/**
* @brief Moves a rect in the main background vertically in direction by a single tile.
*
* Note that tiles in the previous location will be transparent (0x000)
* so maybe copy would be a better choice if you don't want to delete things
*
* @param se_rect dimensions are in number of tiles.
* @param direction must be either @ref SE_UP or @ref SE_DOWN.
*/
void main_bg_se_move_rect_1_tile_vert(Rect se_rect, enum ScreenVertDir direction);
/**
* @brief A wrapper for tte_erase_rect that would use the rect struct
*
* @param rect rectangle to erase
*/
void tte_erase_rect_wrapper(Rect rect);
/**
* @brief Changes rect->left so it fits the digits of num exactly when right aligned to rect->right.
* Assumes num is not negative.
*
* overflow_direction determines the direction the number will overflow
* if it's too large to fit inside the rect.
*
* Note that both rect->left and rect-right need to be defined, top and bottom don't matter
*
* @param rect is in number of pixels but should be a multiple of TILE_SIZE, so it's a whole number
* of tiles to fit TTE characters
*
* @param num number to display
*
* @param overflow_direction either OVERFLOW_LEFT or OVERFLOW_RIGHT.
*/
void update_text_rect_to_right_align_str(
Rect* rect,
const char* str,
enum OverflowDir overflow_direction
);
/**
* @brief Updates a rect so a string is centered within it.
*
* @param rect The rect provided, the provided values are used to determine the center
* and it is then updated so the string starting in rect->left is centered
* The rect is in number of pixels but should be a multiple of TTE_CHAR_SIZE
* so it's a whole number of tiles to fit TTE characters.
*
* @param str The string, the center of the string will be at the center of the updated rect.
*
* @param bias_direction Which direction to bias when the string can't be evenly centered
* with respect to char tiles.
* Examples:
* | |S|T|R| | - Can be evenly centered, bias has no effect
* | | |S|T|R| | - Bias right
* | |S|T|R| | | - Bias left
* |A|B|C|D| | - Bias left
* | |A|B|C|D| - Bias right
*/
void update_text_rect_to_center_str(Rect* rect, const char* str, enum ScreenHorzDir bias_direction);
/**
* @brief Copies 16 bit data from src to dst, applying a palette offset to the data.
*
* This is intended solely for use with tile8/8bpp data for dst and src.
* The palette offset allows the tiles to use a different location in the palette * memory
* This is useful because grit always loads the palette to the beginning of pal_bg_mem[]
*
* @param dst destination charblock
*
* @param src destination charblock
*
* @param wcount Number of words to copy
*
* @param palette_offset palette offset to shift to
*/
void memcpy16_tile8_with_palette_offset(u16* dst, const u16* src, uint hwcount, u8 palette_offset);
/**
* @brief Copies 32 bit data from src to dst, applying a palette offset to the data.
*
* This is intended solely for use with tile8/8bpp data for dst and src.
* The palette offset allows the tiles to use a different location in the palette memory
* This is useful because grit always loads the palette to the beginning of pal_bg_mem[]
*
* @param dst destination charblock
*
* @param src destination charblock
*
* @param wcount Number of words to copy
*
* @param palette_offset palette offset to shift to
*/
void memcpy32_tile8_with_palette_offset(u32* dst, const u32* src, uint wcount, u8 palette_offset);
/**
* @brief Toggles the visibility of the window layers.
*
* These windows are primarily used for the shadows on held jokers, consumables and cards.
*
* @param win0 the visibility state for window 0
* @param win1 the visibility state for window 1
*/
void toggle_windows(bool win0, bool win1);
/**
* @brief Justify a text with custom formatting tags according to the given
* justification and bias direction tags
*
* - The raw_text will be divided into Words, separated by 1 space, and 1 space only.
* - Each Word can have one or more formatting `{TAGS}` attached to it, and they MUST be right
* next to each other. It might not apply correctly otherwise if the line wraps.
* - `{TAGS}` must be one of TTE_BLACK_TAG, TTE_YELLOW_TAG, etc.
*
* @param raw_text The unformatted text. The '\n' character and custom formatting `{TAGS}` will
* not count towards the computed line widths, as they won't appear on screen.
* @param dst_rect Rectangle the justified text must fit into. Will overflow at the bottom if
* text is too long. Size in tiles
* @param justify_direction Align the text either to the left or center.
* @param bias_direction Used with `JUSTIFY_CENTER` only. Determines if lines that cannot be
* centered are to be slightly to the left or to the right.
* @param do_print does not actually print anything, only does the calculations to determine
* the number of lines taken by the paragraph
*
* @returns the number of lines the wrapped paragraph takes
*
* @sa update_text_rect_to_center_str
* @sa TTE_BLACK_TAG
*/
int tte_printf_justified_in_rect(
const char* raw_text,
Rect dst_rect,
enum TextJustifyFlag justify_direction,
enum ScreenHorzDir bias_direction,
bool do_print
);
#endif // GRAPHIC_UTILS_H