/** * @file util.h */ #pragma once /** * @file util.h * @author C. Hölzl * @brief util header file */ #ifdef __cplusplus extern "C" { #endif #include #include #include #include #define NAME_LEN 2 #define MAX(x,y) ((x)>(y)?(x):(y)) #define MIN(x,y) ((x)<(y)?(x):(y)) #define ABS(x) ((x)<0?(-(x)):(x)) #define MOD(x,y) (((y)==0)?0:((s32)((x)+(y)))%(y)) #define SIGN(a) ((a)/abs(a)) #define ARGB6(a,r,g,b) ( ((a) << 5) | ((r)|((g)<<5)|((b)<<10))) #define HALF_BRIGHT_COLOR(color) ARGB16((color >> 15),\ (color & 0x1F)/2,\ ((color >> 5) & 0x1F)/2,\ ((color >> 10) & 0x1F)/2) #define TRANSPARENT ARGB16(0,0,0,0) #define DARK_DARK_WHITE ARGB16(1,7,7,7) #define DARK_WHITE ARGB16(1,15,15,15) #define WHITE ARGB16(1,29,29,30) #define DARK_BLUE ARGB16(1,7,8,10) #define BLUE ARGB16(1,11,16,21) #define DARK_RED ARGB16(1,12,6,7) #define RED ARGB16(1,23,12,13) #define DARK_ORANGE ARGB16(1,13,8,7) #define ORANGE ARGB16(1,25,16,14) #define DARK_YELLOW ARGB16(1,15,13,9) #define YELLOW ARGB16(1,29,25,17) #define DARK_GREEN ARGB16(1,10,12,9) #define GREEN ARGB16(1,20,23,17) #define DARK_VIOLET ARGB16(1,11,9,11) #define VIOLET ARGB16(1,11,9,11) #define P_TRANSPARENT 31 #define P_WHITE 16 #define P_DARK_WHITE 17 #define P_DARK_DARK_WHITE 18 #define P_BLUE 19 #define P_DARK_BLUE 20 #define P_RED 21 #define P_DARK_RED 22 #define P_ORANGE 23 #define P_DARK_ORANGE 24 #define P_YELLOW 25 #define P_DARK_YELLOW 26 #define P_GREEN 27 #define P_DARK_GREEN 28 #define P_VIOLET 29 #define P_DARK_VIOLET 30 #define SET_PERSONAL_PALETTE(PTR)\ do {\ PTR[P_TRANSPARENT] = TRANSPARENT;\ PTR[P_WHITE] = WHITE;\ PTR[P_DARK_WHITE] = DARK_WHITE;\ PTR[P_DARK_DARK_WHITE] = DARK_DARK_WHITE;\ PTR[P_BLUE] = BLUE;\ PTR[P_DARK_BLUE] = DARK_BLUE;\ PTR[P_RED] = RED;\ PTR[P_DARK_RED] = DARK_RED;\ PTR[P_ORANGE] = ORANGE;\ PTR[P_DARK_ORANGE] = DARK_ORANGE;\ PTR[P_YELLOW] = YELLOW;\ PTR[P_DARK_YELLOW] = DARK_YELLOW;\ PTR[P_GREEN] = GREEN;\ PTR[P_DARK_GREEN] = DARK_GREEN;\ PTR[P_VIOLET] = VIOLET;\ PTR[P_DARK_VIOLET] = DARK_VIOLET;\ } while (false) typedef enum {GGAME, GGAME_ALT, GMENU} gmode_t; /* * Initialize random number generator */ static inline void rand_init(u32 seed) { srand(seed); } /* * Generate a random number within range */ static inline u32 rand_value(u32 range) { return ((u32)((float32) range * rand() / (RAND_MAX + 1.0))); } typedef enum { TA_LEFT, TA_RIGHT, TA_CENTER} textalign_t; #define PNAME {PersonalData->name[0],PersonalData->name[1],0} #define SCORE_TAB_SIZE 16 #define SECTIME_MAX_VALUE 9999 typedef struct { char name[NAME_LEN + 1]; u32 time; } pscore_t; pscore_t highscore[SCORE_TAB_SIZE]; #define POWERDWN() powerOn(PM_SYSTEM_PWR) static inline void write_score() { FILE* file = fopen("/nfh8_score.txt", "w+"); if (file != NULL) { u32 i; for (i = 0; i < SCORE_TAB_SIZE; ++i) { fprintf(file, "%2s %u\n", highscore[i].name, highscore[i].time); } fclose(file); } } static inline void insert_score(const pscore_t* score) { u32 i = SCORE_TAB_SIZE - 1; //pscore_t tmp = {"++",SECTIME_MAX_VALUE}; while (i < SCORE_TAB_SIZE && score->time < highscore[i].time) { if (i - 1 >= 0) { strncpy(highscore[i].name, highscore[i - 1].name, NAME_LEN); highscore[i].time = highscore[i - 1].time; } strncpy(highscore[i - 1].name, score->name, NAME_LEN); highscore[i - 1].time = score->time; ++i; } } static inline pscore_t* get_score() { return highscore; } static inline void read_score() { FILE* file = fopen("/nfh8_score.txt", "r"); if (file != NULL) { u32 i; for (i = 0; i < SCORE_TAB_SIZE; ++i) { u8 r = fscanf(file, "%2s %u\n", highscore[i].name, &(highscore[i].time)); if (r != 2 || strnlen(highscore[i].name, NAME_LEN + 1) == 0 || highscore[i].time > SECTIME_MAX_VALUE) { //strncpy(highscore[i].name, "--", NAME_LEN); highscore[i].time = SECTIME_MAX_VALUE; } } fclose(file); } else { u32 i; for (i = 0; i < SCORE_TAB_SIZE; ++i) { strncpy(highscore[i].name, "", NAME_LEN); highscore[i].time = SECTIME_MAX_VALUE; } } } static inline u16 char_to_tile(char c) { if (c >= 'A' && c <= 'Z') { return (c - 'A') + 1; } if (c >= 'a' && c <= 'z') { return (c - 'a') + 39; } if (c >= '1' && c <= '9') { return (c - '1') + 27; } if (c == '0') { return 36; } switch (c) { case '[': return 37; case ']': return 38; case '!': return 65; case '.': return 66; case '?': return 67; case '@': return 68; case '#': return 69; case '$': return 70; case '%': return 71; case '^': return 72; case '&': return 73; case '(': return 74; case ')': return 75; case ':': return 76; case ';': return 77; case ',': return 78; case '\'': return 79; case '+': return 81; case '-': return 82; case '=': return 83; case '{': return 84; case '}': return 85; case '\\': return 86; case '/': return 87; case '<': return 88; case '>': return 89; case '`': return 90; case '"': return 91; case ' ': return 0; default: return 0; } } #ifdef __cplusplus } #endif