diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2025-04-28 23:44:30 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2025-04-28 23:44:30 -0400 |
| commit | 9257256b79efa18a8e329be1d9ee6a1d3c05bbc9 (patch) | |
| tree | 36e633a77a4414688c96720342f4a7598fb96e85 | |
| parent | de6ed6f8e8c4a2d139b4e81b68db7408dbe435d1 (diff) | |
| download | volute-9257256b79efa18a8e329be1d9ee6a1d3c05bbc9.zip | |
render icon
| -rw-r--r-- | renderer.c | 32 |
1 files changed, 31 insertions, 1 deletions
@@ -9,6 +9,15 @@ #include "renderer.h" +#define expect(x) do { \ + if (!(x)) { \ + fprintf(stderr, "Fatal error: %s:%d: assertion '%s' failed\n", \ + __FILE__, __LINE__, #x); \ + abort(); \ + } \ + } while (0) + + enum window { WIDTH = 640, HEIGHT = 480, @@ -52,7 +61,7 @@ static void render_command(mu_Command *cmd); static void clip(mu_Rect rect); static void draw_rect(mu_Rect rect, mu_Color color); static void draw_text(mu_Font font, mu_Vec2 pos, mu_Color color, const char *str); - +static void draw_icon(int id, mu_Rect r); static SDL_Window *window = NULL; static SDL_Renderer *renderer = NULL; @@ -208,6 +217,9 @@ render_command(mu_Command *cmd) { break; case MU_COMMAND_TEXT: { draw_text(cmd->text.font, cmd->text.pos, cmd->text.color, cmd->text.str); } + break; case MU_COMMAND_ICON: { + draw_icon(cmd->icon.id, cmd->icon.rect); + } } } @@ -260,6 +272,24 @@ draw_text(mu_Font font, mu_Vec2 pos, mu_Color color, const char *str) { SDL_FreeSurface(surface); } +static void +draw_icon(int id, mu_Rect r) { + SDL_Texture *texture; + SDL_Rect rect; + + expect(id >= 0 && id < icon_list.idx); + texture = SDL_CreateTextureFromSurface(renderer, icon_list.items[id]); + if (!texture) { + fprintf(stderr, "%s\n", SDL_GetError()); + return; + } + rect = (SDL_Rect) {r.x, r.y, r.w, r.h}; + if (SDL_RenderCopy(renderer, texture, NULL, &rect) != 0) { + fprintf(stderr, "%s\n", SDL_GetError()); + } + SDL_DestroyTexture(texture); +} + void r_get_window_size(int *w, int*h) { if (SDL_GetRendererOutputSize(renderer, w, h) != 0) { |