Techneck/code/source-c/assets.c

148 lines
4.9 KiB
C

#include "assets.h"
#include "state.h"
#include <stdlib.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
// TODO: This can be done better.
uint32_t tc_image_id_counter = 1;
tc_asset_storage_s tc_init_asset_storage(char *asset_folder_path)
{
tc_asset_storage_s assets;
assets.images_capacity = 256;
assets.num_images = 0;
assets.images = calloc(sizeof(tc_image_s *), assets.images_capacity);
return assets;
}
void tc_upload_image(tc_image_s *image)
{
glGenTextures(1, &image->gl_identifier);
glBindTexture(GL_TEXTURE_2D, image->gl_identifier);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
image->width, image->height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
image->pixels
);
image->is_uploaded = true;
}
tc_image_s * tc_load_image_from_disk(char *path)
{
tc_image_s *image = malloc(sizeof(tc_image_s));
image->attributes_capacity = 8;
image->attributes =
calloc(sizeof(tc_asset_attribute_s), image->attributes_capacity);
image->num_attributes = 0;
image->pixels = stbi_load(path, &image->width, &image->height, NULL, 4);
image->len_name = 0;
image->name = NULL;
image->x = 0;
image->y = 0;
image->is_subimage = false;
image->superimage = NULL;
image->image_id = tc_image_id_counter;
++tc_image_id_counter;
if(tc_game_state_g.asset_storage.num_images >= tc_game_state_g.asset_storage.images_capacity)
{
tc_game_state_g.asset_storage.images_capacity *= 2;
tc_game_state_g.asset_storage.images =
realloc(tc_game_state_g.asset_storage.images,
sizeof(tc_image_s *) * tc_game_state_g.asset_storage.images_capacity
);
}
tc_game_state_g.asset_storage.images[tc_game_state_g.asset_storage.num_images] = image;
++tc_game_state_g.asset_storage.num_images;
return image;
}
tc_image_s * tc_create_subimage(tc_image_s *superimage,
uint32_t x, uint32_t y,
uint32_t width, uint32_t height
) {
if(!superimage->is_uploaded)
{
puts("Superimages must have been uploaded before creating a subimage!");
return NULL;
}
tc_image_s *image = malloc(sizeof(tc_image_s));
image->attributes_capacity = 8;
image->attributes =
calloc(sizeof(tc_asset_attribute_s), image->attributes_capacity);
image->num_attributes = 0;
image->pixels = NULL;
image->len_name = 0;
image->name = NULL;
image->x = x;
image->y = y;
image->is_subimage = true;
image->width = width;
image->height = height;
image->superimage = superimage;
image->image_id = tc_image_id_counter;
++tc_image_id_counter;
if(tc_game_state_g.asset_storage.num_images >= tc_game_state_g.asset_storage.images_capacity)
{
tc_game_state_g.asset_storage.images_capacity *= 2;
tc_game_state_g.asset_storage.images =
realloc(tc_game_state_g.asset_storage.images,
sizeof(tc_image_s *) * tc_game_state_g.asset_storage.images_capacity
);
}
tc_game_state_g.asset_storage.images[tc_game_state_g.asset_storage.num_images] = image;
++tc_game_state_g.asset_storage.num_images;
return tc_game_state_g.asset_storage.images[tc_game_state_g.asset_storage.num_images-1];
}
tc_image_s * tc_resolve_id_to_image(uint32_t id)
{
uint32_t image_index = 0;
while(image_index < tc_game_state_g.asset_storage.num_images)
{
if(tc_game_state_g.asset_storage.images[image_index]->image_id == id)
return tc_game_state_g.asset_storage.images[image_index];
++image_index;
}
return NULL;
}
tc_image_s * tc_resolve_name_to_image(char *name)
{
uint32_t image_index = 0;
while(image_index < tc_game_state_g.asset_storage.num_images)
{
if(tc_game_state_g.asset_storage.images[image_index]->name == NULL)
{
++image_index;
continue;
}
if(!strcmp(tc_game_state_g.asset_storage.images[image_index]->name, name))
return tc_game_state_g.asset_storage.images[image_index];
++image_index;
}
return NULL;
}