* 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>
80 lines
2.1 KiB
C
80 lines
2.1 KiB
C
#include "util.h"
|
|
#include <limits.h>
|
|
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
|
|
int int_arr_max(int int_arr[], int size)
|
|
{
|
|
int max = INT_MIN;
|
|
for (int i = 0; i < size; i++)
|
|
{
|
|
if (int_arr[i] > max)
|
|
{
|
|
max = int_arr[i];
|
|
}
|
|
}
|
|
|
|
return max;
|
|
}
|
|
|
|
void truncate_uint_to_suffixed_str(uint32_t num, int num_req_chars, char out_str_buff[UINT_MAX_DIGITS + 1])
|
|
{
|
|
bool inevitable_overflow = num_req_chars < SUFFIXED_NUM_MIN_REQ_CHARS;
|
|
if (inevitable_overflow)
|
|
{
|
|
num_req_chars = SUFFIXED_NUM_MIN_REQ_CHARS;
|
|
}
|
|
|
|
int num_digits = u32_get_digits(num);
|
|
int overflow_size = num_digits - num_req_chars;
|
|
char* suffix = "";
|
|
|
|
/* If there is overflow, divide by the next suffixed power of 10
|
|
* to truncate the number back within num_req_chars.
|
|
* UINT32_MAX is in the billions so no need to check larger numbers
|
|
* or perform complex mathematical operations.
|
|
*/
|
|
if (overflow_size >= ONE_M_ZEROS)
|
|
{
|
|
num /= ONE_B;
|
|
suffix = "B";
|
|
}
|
|
else if (overflow_size >= ONE_K_ZEROS)
|
|
{
|
|
num /= ONE_M;
|
|
suffix = "M";
|
|
}
|
|
else if (overflow_size > 0
|
|
|| (inevitable_overflow && num_digits == SUFFIXED_NUM_MIN_REQ_CHARS))
|
|
// Special case - alleviate inevitable overflow for 1000s and truncate them to "1K"s
|
|
{
|
|
num /= ONE_K;
|
|
suffix = "K";
|
|
}
|
|
|
|
snprintf(out_str_buff, UINT_MAX_DIGITS + 1, "%lu%s", num, suffix);
|
|
}
|
|
|
|
// Avoid uint overflow when add/multiplying score
|
|
|
|
uint32_t u32_protected_add(uint32_t a, uint32_t b)
|
|
{
|
|
return (a > (UINT32_MAX - b)) ? UINT32_MAX : (a + b);
|
|
}
|
|
|
|
uint16_t u16_protected_add(uint16_t a, uint16_t b)
|
|
{
|
|
return (a > (UINT16_MAX - b)) ? UINT16_MAX : (a + b);
|
|
}
|
|
|
|
uint32_t u32_protected_mult(uint32_t a, uint32_t b)
|
|
{
|
|
return (a == 0 || b == 0) ? 0 : (a > (UINT32_MAX / b) ? UINT32_MAX : a * b);
|
|
}
|
|
|
|
uint16_t u16_protected_mult(uint16_t a, uint16_t b)
|
|
{
|
|
return (a == 0 || b == 0) ? 0 : (a > (UINT16_MAX / b) ? UINT16_MAX : a * b);
|
|
|
|
}
|