txt.c (1170B)
1 // Copyright 2022, Brian Swetland <swetland@frotz.net> 2 // Licensed under the Apache License, Version 2.0 3 4 #include <stdio.h> 5 #include <stdarg.h> 6 7 #include <gfx/gfx.h> 8 #include <compiler.h> 9 #include <hw/platform.h> 10 11 #define CH_WIDTH 8 12 #define CH_HEIGHT 16 13 14 void txt_init(txt_surface_t *ts, gfx_surface_t *gs) { 15 ts->gs = gs; 16 ts->cw = gs->width / CH_WIDTH; 17 ts->ch = gs->height / CH_HEIGHT; 18 ts->cx = 0; 19 ts->cy = 0; 20 }; 21 22 void txt_goto(txt_surface_t *ts, uint32_t cx, uint32_t cy) { 23 if (cx >= ts->cw) { 24 cx = ts->cw - 1; 25 } 26 if (cy >= ts->ch) { 27 cx = ts->ch - 1; 28 } 29 ts->cx = cx; 30 ts->cy = cy; 31 } 32 33 void txt_putc(txt_surface_t *ts, uint32_t ch) { 34 if ((ch == '\r') || (ch == '\n')) { 35 newline: 36 ts->cx = 0; 37 ts->cy++; 38 if (ts->cy >= ts->ch) { 39 ts->cy = 0; 40 } 41 return; 42 } 43 gfx_putc(ts->gs, ts->cx * CH_WIDTH, ts->cy * CH_HEIGHT, ch); 44 ts->cx++; 45 if (ts->cx >= ts->cw) { 46 goto newline; 47 } 48 } 49 50 void txt_puts(txt_surface_t *ts, const char *s) { 51 while (*s) { 52 txt_putc(ts, *s++); 53 } 54 } 55 56 void txt_printf(txt_surface_t *ts, const char *fmt, ...) { 57 char msg[128]; 58 va_list ap; 59 va_start(ap, fmt); 60 vsnprintf(msg, sizeof(msg), fmt, ap); 61 va_end(ap); 62 txt_puts(ts, msg); 63 }