#define String(s) (s8){(u8*)s, lengthof(s)} typedef struct { u8 *buf; size len; } s8; #define MAX_FONT_SIZE 8 size txt_length(const char *text) { size length = 0; if(text != NULL){ while (*text++) length++; } return length; } s8 create_s8(const size sz) { return (s8){(u8*)RL_MALLOC(sz), sz}; } void free_s8(s8* str) { RL_FREE(str->buf); } s8 cstr_to_s8(const char* str) { size sz = txt_length(str) + 1; s8 final = (s8){(u8*)RL_MALLOC(sz), sz - 1}; memcpy(final.buf, str, sz); return final; } b32 equal_s8(const s8 one, const s8 other) { // Get OUTTA here if(one.len != other.len) return false; for(i32 i = 0; i < one.len; ++i){ if(one.buf[i] != other.buf[i]) return false; } return true; } void add_s8(s8 dst, const s8 src, size* pos) { memcpy(dst.buf + *pos, src.buf, src.len); *pos += src.len; } s8 load_file_text(const s8 path) { char* shr = LoadFileText((const char*)path.buf); s8 file = cstr_to_s8(shr); UnloadFileText(shr); return file; } // Exposed Functions // static Font font_slots[MAX_FONT_SIZE] = {0}; static s8 font_paths[MAX_FONT_SIZE] = {0}; i32 load_font(const char* path) { s8 path_str = cstr_to_s8(path); i32 current_idx = BLANK_DEFAULT; for(i32 i = 0; i < MAX_FONT_SIZE; ++i){ if(equal_s8(path_str, font_paths[current_idx])){ free_s8(&path_str); return i; } if(font_slots[i].texture.id <= 0){ current_idx = i; font_slots[current_idx] = LoadFont((const char*)path_str.buf); font_paths[current_idx] = path_str; break; } } //TODO: Handle slots a bit better. assert(current_idx != BLANK_DEFAULT); return current_idx; } void draw_text_font(i32 idx, const char* text, Vector2 pos, f32 size, Color col) { DrawTextEx(font_slots[idx], text, pos, size, 0.5f, col); } void draw_text_font_centered(i32 idx, const char* text, f32 y_offset, f32 size, Color col) { Vector2 vec = MeasureTextEx(font_slots[idx], text, size, 0.5f); Vector2 final_pos = (Vector2){(320.0f - vec.x) / 2.f, y_offset}; DrawTextEx(font_slots[idx], text, final_pos, size, 0.5f, col); DrawLineV((Vector2){final_pos.x, y_offset + vec.y - 2.f}, (Vector2){final_pos.x + vec.x, y_offset + vec.y - 2.f}, col); } void unload_font(i32 idx) { UnloadFont(font_slots[idx]); free_s8(&font_paths[idx]); } void unload_all_fonts(void) { for(int i = 0; i < MAX_FONT_SIZE; ++i){ if(font_slots[i].texture.id > 0) unload_font(i); } }