Files
wschod/game/src/resources.c
T

94 lines
2.5 KiB
C

#include "resources.h"
Resources resources;
void
Resources_add_program (const char *v_src, const char *f_src)
{
resources.programs = realloc (resources.programs, sizeof (unsigned int) * resources.programs_len + 1);
resources.programs[resources.programs_len++] = program_create (v_src, f_src);
}
void
Resources_load_program (const char *v_path, const char *f_path)
{
FILE *v_file = fopen (v_path, "r");
if (v_file == NULL)
{
WARN ("File `%s' does not exist. Not loading program.", v_path);
return;
}
fseek (v_file, 0L, SEEK_END);
size_t v_len = ftell (v_file) + 1;
char *v_src = calloc (v_len, sizeof (char));
rewind (v_file);
INFO ("Read %zu bytes from vertex source.", fread (v_src, sizeof (char), v_len, v_file));
FILE *f_file = fopen (f_path, "r");
if (f_file == NULL)
{
WARN ("File `%s' does not exist. Not loading program.", f_path);
return;
}
fseek (f_file, 0L, SEEK_END);
size_t f_len = ftell (f_file) + 1;
char *f_src = calloc (f_len, sizeof (char));
rewind (f_file);
INFO ("Read %zu bytes from fragment source.", fread (f_src, sizeof (char), f_len, f_file));
Resources_add_program (v_src, f_src);
free (v_src);
free (f_src);
}
void
Resources_add_texture (int width, int height, unsigned char *data)
{
resources.textures = realloc (resources.textures, sizeof (unsigned int) * resources.textures_len + 1);
resources.textures[resources.textures_len++] = texture_create (width, height, data);
}
void
Resources_load_texture (const char *path)
{
SDL_Surface *surface = IMG_Load (path);
if (surface == NULL)
{
WARN ("Failed to load texture from path %s: %s", path, SDL_GetError ());
}
Resources_add_texture (surface->w, surface->h, surface->pixels);
SDL_DestroySurface (surface);
}
void
Resources_init ()
{
resources.programs_len = 0;
resources.programs = malloc (sizeof (unsigned int));
Resources_load_program ("res/shaders/default.vert", "res/shaders/default.frag");
Resources_load_program ("res/shaders/chunk.vert", "res/shaders/chunk.frag");
resources.textures_len = 0;
resources.textures = malloc (sizeof (unsigned int));
Resources_load_texture ("res/textures/dirt.png");
Resources_load_texture ("res/textures/drygrass.png");
Resources_load_texture ("res/textures/grass.png");
Resources_load_texture ("res/textures/rock_cement.png");
Resources_load_texture ("res/textures/asphalt_gravel.png");
}
void
Resources_free ()
{
free (resources.programs);
free (resources.textures);
}