aboutsummaryrefslogtreecommitdiffstats
path: root/renderer.c
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2025-04-29 00:21:51 -0400
committerSam Anthony <sam@samanthony.xyz>2025-04-29 00:21:51 -0400
commitbb356388f2b4f140d68c991737348eeb5a226496 (patch)
treea04400a955dc332f0a1139e242a3925b0cd691e7 /renderer.c
parent9cf4d1b6b8a4a1d063978dd31eb2f87ea41e9edd (diff)
downloadvolute-bb356388f2b4f140d68c991737348eeb5a226496.zip
cache icon textures
Diffstat (limited to 'renderer.c')
-rw-r--r--renderer.c36
1 files changed, 17 insertions, 19 deletions
diff --git a/renderer.c b/renderer.c
index b05418a..158f7e2 100644
--- a/renderer.c
+++ b/renderer.c
@@ -49,7 +49,7 @@ static const char key_map[256] = {
[SDLK_BACKSPACE & 0xff] = MU_KEY_BACKSPACE,
};
-mu_stack(SDL_Surface *, ICONLIST_SIZE) icon_list;
+mu_stack(SDL_Texture *, ICONLIST_SIZE) icon_list;
static void print_info(void);
@@ -111,7 +111,7 @@ r_init(mu_Context *ctx, const char *title) {
void
r_free(void) {
while (icon_list.idx-- > 0) {
- SDL_FreeSurface(icon_list.items[icon_list.idx]);
+ SDL_DestroyTexture(icon_list.items[icon_list.idx]);
}
}
@@ -281,20 +281,14 @@ draw_text(mu_Font font, mu_Vec2 pos, mu_Color color, const char *str) {
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) {
+ if (SDL_RenderCopy(renderer, icon_list.items[id], NULL, &rect) != 0) {
fprintf(stderr, "%s\n", SDL_GetError());
}
- SDL_DestroyTexture(texture);
}
void
@@ -306,26 +300,30 @@ r_get_window_size(int *w, int*h) {
int
r_push_icon(const char *path) {
- SDL_Surface **p;
+ SDL_Surface *surface;
+
+ expect(icon_list.idx < ICONLIST_SIZE);
- if (icon_list.idx >= ICONLIST_SIZE) {
- fprintf(stderr, "icon list overflow\n");
+ surface = IMG_Load(path);
+ if (!surface) {
+ fprintf(stderr, "failed to load %s: %s\n", path, SDL_GetError());
return 1;
}
- p = icon_list.items + icon_list.idx;
- *p = IMG_Load(path);
- if (*p == NULL) {
- fprintf(stderr, "failed to load %s: %s\n", path, SDL_GetError());
+ icon_list.items[icon_list.idx] = SDL_CreateTextureFromSurface(renderer, surface);
+ if (!icon_list.items[icon_list.idx]) {
+ fprintf(stderr, "%s\n", SDL_GetError());
+ SDL_FreeSurface(surface);
return 1;
}
icon_list.idx++;
+ SDL_FreeSurface(surface);
return 0;
}
void
r_pop_icon(void) {
- if (icon_list.idx >= 0) {
+ if (icon_list.idx <= 0) {
return;
}
- SDL_FreeSurface(icon_list.items[--icon_list.idx]);
+ SDL_DestroyTexture(icon_list.items[--icon_list.idx]);
}