Truncate Scores (#269)

* Fixed score truncation erasure, and centering for chips, mult, score and "temp score

* Added centering for money

* Added tests for truncate_uint_to_suffixed_str()

* Fixed u32 bug with get_digits

* Fixed score flames for large numbers

* Small change to update documentation

* Added 1 pixel on to score rect on the left to make room for full character tile + reset_top_left_panel_bottom_row() refactor

* Replaced directional defines with enums

* Updated ante_lut comment

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
MeirGavish
2025-12-02 08:28:36 +02:00
committed by GitHub
co-authored by Copilot
parent 07cab81ba1
commit 43d837571e
14 changed files with 411 additions and 148 deletions
+1 -1
View File
@@ -72,7 +72,7 @@ void blind_init();
void blind_set_boss_graphics(const unsigned int* tiles, const u16* palette);
int blind_get_requirement(enum BlindType type, int ante);
u32 blind_get_requirement(enum BlindType type, int ante);
int blind_get_reward(enum BlindType type);
u16 blind_get_color(enum BlindType type, enum BlindColorIndex index);
+47 -26
View File
@@ -50,16 +50,23 @@
#define SE_COL_LEN 32
// Since y direction goes from the top of the screen to the bottom
#define SCREEN_UP -1
#define SCREEN_DOWN 1
#define SCREEN_LEFT -1
#define SCREEN_RIGHT 1
enum ScreenVertDir
{
SCREEN_UP = -1,
SCREEN_DOWN = 1
};
#define SE_UP SCREEN_UP
#define SE_DOWN SCREEN_DOWN
enum ScreenHorzDir
{
SCREEN_LEFT = -1,
SCREEN_RIGHT = 1
};
#define OVERFLOW_LEFT SCREEN_LEFT
#define OVERFLOW_RIGHT SCREEN_RIGHT
enum OverflowDir
{
OVERFLOW_LEFT = SCREEN_LEFT,
OVERFLOW_RIGHT = SCREEN_RIGHT
};
// Tile size in pixels, both height and width as tiles are square
#define TILE_SIZE 8
@@ -79,25 +86,21 @@ SE main_bg_se_get_se(BG_POINT pos);
INLINE int rect_width(const Rect* rect)
{
/* Extra parens to avoid issues in case compiler turns INLINE into macro
* Not sure if necessary, could be just paranoia
*/
return (((rect)->right) - ((rect)->left) + 1);
return max(0, rect->right - rect->left + 1);
}
INLINE int rect_height(const Rect* rect)
{
return (((rect)->bottom) - ((rect)->top) + 1);
return max(0, rect->bottom - rect->top + 1);
}
/* 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
* direction must be either SE_UP or SE_DOWN.
* se_rect dimensions are in number of tiles.
*
* NOTE: This does not work with TTE_SBB, probably because it's 4BPP...
*/
void bg_se_copy_rect_1_tile_vert(u16 bg_sbb, Rect se_rect, int direction);
void bg_se_copy_rect_1_tile_vert(u16 bg_sbb, Rect se_rect, enum ScreenVertDir direction);
/* Clears a rect in the main background.
* The se_rect dimensions need to be in number of tiles.
@@ -105,10 +108,9 @@ void bg_se_copy_rect_1_tile_vert(u16 bg_sbb, Rect se_rect, int direction);
void main_bg_se_clear_rect(Rect se_rect);
/* Copies a rect in the main background vertically in direction by a single tile.
* direction must be either SE_UP or SE_DOWN.
* se_rect dimensions are in number of tiles.
*/
void main_bg_se_copy_rect_1_tile_vert(Rect se_rect, int direction);
void main_bg_se_copy_rect_1_tile_vert(Rect se_rect, enum ScreenVertDir direction);
/* Copies a rect in the main background from se_rect to the position (x, y).
* se_rect dimensions are in number of tiles.
@@ -134,27 +136,46 @@ void main_bg_se_copy_expand_3x3_rect(Rect se_rect_dest, BG_POINT se_rect_src_3x3
/* 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
* direction must be either SE_UP or SE_DOWN.
* se_rect dimensions are in number of tiles.
*/
void main_bg_se_move_rect_1_tile_vert(Rect se_rect, int direction);
void main_bg_se_move_rect_1_tile_vert(Rect se_rect, enum ScreenVertDir direction);
// A wrapper for tte_erase_rect that would use the rect struct
void tte_erase_rect_wrapper(Rect rect);
/* Changes rect->left so it fits the digits of num exactly when right aligned to rect->right.
* Assumes num is not negative.
/* Changes rect->left so it fits a string exactly when right aligned to rect->right.
*
* overflow_direction determines the direction the number will overflow
* if it's too large to fit inside the rect.
* Should be either OVERFLOW_LEFT or OVERFLOW_RIGHT.
* overflow_direction determines the direction the string will overflow
* if it's too large to fit inside the rect.
*
* The rect is in number of pixels but should be a multiple of TILE_SIZE
* 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
*
* Note that both rect->left and rect-right need to be defined, top and bottom don't matter
*/
void update_text_rect_to_right_align_num(Rect* rect, int num, int overflow_direction);
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);
/*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.
+40 -11
View File
@@ -3,9 +3,10 @@
#include <stdint.h>
#define GBLA_UNUSED __attribute__((unused))
static inline int get_digits(int n) // https://stackoverflow.com/questions/1068849/how-do-i-determine-the-number-of-digits-of-an-integer-in-c
static inline int u32_get_digits(uint32_t n) // https://stackoverflow.com/questions/1068849/how-do-i-determine-the-number-of-digits-of-an-integer-in-c
{
if (n < 10) return 1;
if (n < 100) return 2;
@@ -19,7 +20,7 @@ static inline int get_digits(int n) // https://stackoverflow.com/questions/10688
return 10;
}
static inline int get_digits_odd(int n)
static inline int get_digits_even(int n)
{
if (n < 100) return 1;
if (n < 10000) return 2;
@@ -28,15 +29,6 @@ static inline int get_digits_odd(int n)
return 5;
}
static inline int get_digits_even(int n)
{
if (n < 10) return 1;
if (n < 1000) return 2;
if (n < 100000) return 3;
if (n < 10000000) return 4;
return 5;
}
#define UNDEFINED -1
#define NUM_ELEM_IN_ARR(arr) (sizeof(arr) / sizeof((arr)[0]))
@@ -45,8 +37,45 @@ static inline int get_digits_even(int n)
#define UINT_MAX_DIGITS 10 // strlen(str(UINT32_MAX)) = strlen("4294967295")
#define UINT8_MAX_DIGITS 3 // strlen(str(UINT8_MAX)) = strlen("255")
#define ONE_K 1000
#define ONE_M 1000000
#define ONE_B 1000000000
#define ONE_K_ZEROS 3
#define ONE_M_ZEROS 6
#define ONE_B_ZEROS 9
// The suffix replaces everything past the third digit, e.g. "999K" -> "1M"
// so it needs at least this number of chars to be able to display any suffixed number
#define SUFFIXED_NUM_MIN_REQ_CHARS 4
int int_arr_max(int int_arr[], int size);
/**
* @brief Truncate an unsigned number into a suffixed string representation e.g. 12000 -> "12K"
* The least significant digits are rounded down e.g. 12345 -> "12K", 12987 -> "12K"
*
* @param num The number to truncate, can be anything from 0 to UINT32_MAX.
*
* @param num_req_chars The number of characters to constrain the string to.
* The function will use up as much characters as it can
* in order to maintain as much accuracy as possible.
* So numbers are not fully truncated if not necessary,
* e.g. 123123000 -> "123123K" for example value 7,
* and if num_req_chars > u32_get_digits(num) the number will not
* be truncated at all.
* Passing less than SUFFIXED_NUM_MIN_REQ_CHARS may result in an
* output string longer than num_req_chars but
* can be done to truncate 1000s -> "1K", 2000 -> "2K" etc.
* which wouldn't be otherwise.
*
* @param out_str An output buffer to write the resulting string to.
* Must be of size UINT_MAX_DIGITS + 1. + 1 for null-terminator.
* At that size the suffix character will always be accounted for since
* a number with more digits than UINT_MAX_DIGITS will not be handled nor truncated.
*/
void truncate_uint_to_suffixed_str(uint32_t num, int num_req_chars, char out_str[UINT_MAX_DIGITS + 1]);
uint32_t u32_protected_add (uint32_t a, uint32_t b);
uint16_t u16_protected_add (uint16_t a, uint16_t b);
uint32_t u32_protected_mult(uint32_t a, uint32_t b);