* 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>
85 lines
3.2 KiB
C
85 lines
3.2 KiB
C
#ifndef UTIL_H
|
|
#define UTIL_H
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
#define GBLA_UNUSED __attribute__((unused))
|
|
|
|
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;
|
|
if (n < 1000) return 3;
|
|
if (n < 10000) return 4;
|
|
if (n < 100000) return 5;
|
|
if (n < 1000000) return 6;
|
|
if (n < 10000000) return 7;
|
|
if (n < 100000000) return 8;
|
|
if (n < 1000000000) return 9;
|
|
return 10;
|
|
}
|
|
|
|
static inline int get_digits_even(int n)
|
|
{
|
|
if (n < 100) return 1;
|
|
if (n < 10000) return 2;
|
|
if (n < 1000000) return 3;
|
|
if (n < 100000000) return 4;
|
|
return 5;
|
|
}
|
|
|
|
#define UNDEFINED -1
|
|
|
|
#define NUM_ELEM_IN_ARR(arr) (sizeof(arr) / sizeof((arr)[0]))
|
|
|
|
#define INT_MAX_DIGITS 11 // strlen(str(INT_MAX)) = strlen("-2147483647")
|
|
#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);
|
|
uint16_t u16_protected_mult(uint16_t a, uint16_t b);
|
|
|
|
#endif // UTIL_H
|