Techneck/code/source-c/assets.c

64 lines
2.0 KiB
C
Raw Normal View History

#include "assets.h"
#include "state.h"
#include <stdlib.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
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
);
}
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;
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;
return image;
}