aboutsummaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorThomas Guillermo Albers Raviola <thomas@thomaslabs.org>2026-01-16 23:26:01 +0100
committerThomas Guillermo Albers Raviola <thomas@thomaslabs.org>2026-01-16 23:26:01 +0100
commitc15c603b35e86fd42e6db94a7382ba7a1ddadd6e (patch)
treeab56040ce4768564bef5fbe9e65fe4c025cea6ce /util.c
Initial commit
Diffstat (limited to 'util.c')
-rw-r--r--util.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..d91aff7
--- /dev/null
+++ b/util.c
@@ -0,0 +1,61 @@
+#include "game.h"
+#include <stdio.h>
+#include <stdarg.h>
+
+SDL_Texture *load_texture(const char *name)
+{
+ int i;
+ SDL_Surface *surface;
+ SDL_Texture *texture;
+
+ struct TextureContainer *container;
+ struct Resources *res = &game->resources;
+
+ if(game->resources.num_textures >= MAX_TEXTURES)
+ return NULL;
+
+ for(i = 0; i < res->num_stored_textures; i ++)
+ {
+ if(!strcmp(res->contained_textures[i].name, name))
+ return res->contained_textures[i].texture;
+ }
+
+ surface = IMG_Load(name);
+ texture = SDL_CreateTextureFromSurface(game->renderer, surface);
+ SDL_FreeSurface(surface);
+
+ container = &res->contained_textures[res->num_stored_textures ++];
+ strcpy(container->name, name);
+ container->texture = texture;
+
+ res->textures[game->resources.num_textures ++] = texture;
+
+ return texture;
+}
+
+Mix_Chunk *load_sound(const char *name)
+{
+ Mix_Chunk *c;
+ struct Resources *res = &game->resources;
+ c = Mix_LoadWAV(name);
+
+ if(!c)
+ die("%s could not be opened: %s", name, Mix_GetError());
+
+ res->audios[res->num_audios ++] = c;
+
+ return c;
+}
+
+void die(const char *error, ...)
+{
+ va_list args;
+
+ va_start(args, error);
+ vfprintf(stderr, error, args);
+ va_end(args);
+
+ free(game);
+ SDL_Quit();
+ exit(1);
+}