gfx.h (2302B)
1 // Copyright 2022, Brian Swetland <swetland@frotz.net> 2 // Licensed under the Apache License, Version 2.0 3 4 #pragma once 5 6 #include <stdint.h> 7 8 typedef struct gfx_surface gfx_surface_t; 9 10 struct gfx_surface { 11 uint32_t width; 12 uint32_t height; 13 uint32_t stride; 14 void *pixels; 15 16 uint32_t pixfmt; 17 uint32_t fgcolor; 18 uint32_t bgcolor; 19 uint32_t reserved; 20 21 void (*plot)(gfx_surface_t* gs, uint32_t x, uint32_t y, uint32_t pxl); 22 void (*hline)(gfx_surface_t* gs, uint32_t x0, uint32_t y, uint32_t x1, uint32_t pxl); 23 void (*putc)(gfx_surface_t* gs, uint32_t x, uint32_t y, uint32_t ch); 24 uint32_t (*color)(gfx_surface_t* gs, uint32_t argb8888); 25 }; 26 27 #define PIXFMT_RGB565 0x03000565 28 29 void gfx_init_display(gfx_surface_t *gs); 30 void gfx_init(gfx_surface_t* gs); 31 32 static inline void gfx_plot(gfx_surface_t* gs, uint32_t x, uint32_t y) { 33 gs->plot(gs, x, y, gs->fgcolor); 34 } 35 36 static inline void gfx_hline(gfx_surface_t* gs, uint32_t x0, uint32_t y, uint32_t x1) { 37 gs->hline(gs, x0, y, x1, gs->fgcolor); 38 } 39 40 static inline void gfx_putc(gfx_surface_t* gs, uint32_t x, uint32_t y, uint32_t ch) { 41 gs->putc(gs, x, y, ch); 42 } 43 44 static inline uint32_t gfx_color(gfx_surface_t* gs, uint32_t argb8888) { 45 return gs->color(gs, argb8888); 46 } 47 48 void gfx_fill(gfx_surface_t* gs, uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t pxl); 49 void gfx_clear(gfx_surface_t* gs, uint32_t pxl); 50 void gfx_puts(gfx_surface_t* gs, uint32_t x, uint32_t y, const char* s); 51 void gfx_printf(gfx_surface_t* gs, uint32_t x, uint32_t y, const char* fmt, ...); 52 void gfx_setcolor(gfx_surface_t* gs, uint32_t fg_argb8888, uint32_t bg_argb8888); 53 54 typedef struct txt_surface { 55 gfx_surface_t *gs; 56 uint32_t cw; 57 uint32_t ch; 58 uint32_t cx; 59 uint32_t cy; 60 } txt_surface_t; 61 62 void txt_init(txt_surface_t *ts, gfx_surface_t *gs); 63 void txt_puts(txt_surface_t *ts, const char *s); 64 void txt_printf(txt_surface_t *ts, const char *fmt, ...); 65 void txt_goto(txt_surface_t *ts, uint32_t cx, uint32_t cy); 66 67 68 #define C_BLACK 0x00000000 69 #define C_WHITE 0x00FFFFFF 70 #define C_GRAY25 0x00404040 71 #define C_GRAY50 0x00808080 72 #define C_GRAY75 0x00C0C0C0 73 #define C_RED 0x00FF0000 74 #define C_GREEN 0x0000FF00 75 #define C_BLUE 0x000000FF 76 #define C_YELLOW 0x00FFFF00 77 #define C_PURPLE 0x00FF00FF 78 #define C_CYAN 0x0000FFFF