aboutsummaryrefslogtreecommitdiff
path: root/src/gunner/text.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gunner/text.c')
-rw-r--r--src/gunner/text.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/src/gunner/text.c b/src/gunner/text.c
new file mode 100644
index 0000000..0812ff1
--- /dev/null
+++ b/src/gunner/text.c
@@ -0,0 +1,116 @@
+#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, vec2 pos, i32 size, Color col)
+{
+ Vector2 position = (Vector2){FROM_FIXED(pos.x), FROM_FIXED(pos.y)};
+ DrawTextEx(font_slots[idx], text, position, (f32)size, 1.0f, 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);
+
+ }
+} \ No newline at end of file