Separated texture type from asset manager for other asset types.
This commit is contained in:
parent
46f13015d5
commit
1fa9d1cb87
|
|
@ -4,6 +4,7 @@ CORE_SOURCES := \
|
|||
vector2d.c \
|
||||
inputs.c \
|
||||
asset_manager.c \
|
||||
texture.c \
|
||||
event_bus.c \
|
||||
ecs.c \
|
||||
display.c \
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
#include "asset_manager.h"
|
||||
|
||||
#include <SDL3/SDL_render.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include "memory_alloc.h"
|
||||
#include "linked_list.h"
|
||||
#include "display.h"
|
||||
#include "texture.h"
|
||||
#include "memory_alloc.h"
|
||||
#include "errors.h"
|
||||
|
||||
int create_asset(asset_t *asset, va_list args)
|
||||
|
|
@ -141,70 +140,7 @@ static inline bool condition_is_asset_header_def_name(elem_t *elem, va_list args
|
|||
return !strcmp(((asset_header_def_t *)elem->data)->name, va_arg(args, const char *));
|
||||
}
|
||||
|
||||
static inline int load_texture(asset_t *asset, asset_header_def_t *asset_header_def, FILE *asset_file)
|
||||
{
|
||||
if(fseek(asset_file, asset_header_def->start, SEEK_SET))
|
||||
{
|
||||
error_printf("Failed to seek in asset file : %d", errno);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
size_t width, height;
|
||||
|
||||
if(fread(&width, sizeof(width), 1, asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(fread(&height, sizeof(height), 1, asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
SDL_Surface *tmp_surface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_RGB565);
|
||||
if(!tmp_surface)
|
||||
{
|
||||
error_printf("Failed to create temporary surface : %s", SDL_GetError());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(!SDL_LockSurface(tmp_surface))
|
||||
{
|
||||
error_printf("Failed to lock temporary surface : %s", SDL_GetError());
|
||||
SDL_DestroySurface(tmp_surface);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, asset_file) != width * height)
|
||||
{
|
||||
error_printf("Failed to read asset file.");
|
||||
SDL_DestroySurface(tmp_surface);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(!SDL_SetSurfaceColorKey(tmp_surface, true, 0xF81F))
|
||||
{
|
||||
error_printf("Failed to set color key of temporary surface : %s", SDL_GetError());
|
||||
SDL_DestroySurface(tmp_surface);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
asset->data = SDL_CreateTextureFromSurface(renderer, tmp_surface);
|
||||
if(!asset->data)
|
||||
{
|
||||
error_printf("Failed to create texture from temporary surface : %s", SDL_GetError());
|
||||
SDL_DestroySurface(tmp_surface);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
SDL_DestroySurface(tmp_surface);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int asset_manager_load_asset(asset_manager_t *asset_manager, const char *asset_name, asset_type_t asset_type)
|
||||
asset_t *asset_manager_load_asset(asset_manager_t *asset_manager, const char *asset_name, asset_type_t asset_type)
|
||||
{
|
||||
assert("Asset manager cannot be NULL" && asset_manager);
|
||||
|
||||
|
|
@ -215,8 +151,6 @@ int asset_manager_load_asset(asset_manager_t *asset_manager, const char *asset_n
|
|||
((asset_t *)asset_elem->data)->nb_refs++;
|
||||
|
||||
debug_printf("Asset \"%s\" has already been loaded, nb refs : %lu", ((asset_t *)asset_elem->data)->name, ((asset_t *)asset_elem->data)->nb_refs);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -225,12 +159,12 @@ int asset_manager_load_asset(asset_manager_t *asset_manager, const char *asset_n
|
|||
if(!asset_header_def_elem)
|
||||
{
|
||||
error_printf("Asset doesn't exist in asset file : %s", asset_name);
|
||||
return EXIT_FAILURE;
|
||||
return NULL;
|
||||
}
|
||||
asset_header_def_t *asset_header_def = asset_header_def_elem->data;
|
||||
|
||||
asset_elem = create_elem(&asset_manager->assets, asset_type);
|
||||
if(!asset_elem) return_failure_int;
|
||||
if(!asset_elem) return_failure_ptr;
|
||||
asset_t *asset = asset_elem->data;
|
||||
|
||||
asset->name = calloc(strlen(asset_name) + 1, sizeof(char));
|
||||
|
|
@ -238,22 +172,28 @@ int asset_manager_load_asset(asset_manager_t *asset_manager, const char *asset_n
|
|||
{
|
||||
error_printf("Failed to allocate memory for asset name : %d", errno);
|
||||
destroy_asset(asset);
|
||||
return EXIT_FAILURE;
|
||||
return NULL;
|
||||
}
|
||||
strcpy(asset->name, asset_name);
|
||||
asset->type = asset_type;
|
||||
|
||||
if(fseek(asset_manager->asset_file, asset_header_def->start, SEEK_SET))
|
||||
{
|
||||
error_printf("Failed to seek in asset file : %d", errno);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
switch(asset_type)
|
||||
{
|
||||
case ASSET_TEXTURE:
|
||||
if(load_texture(asset, asset_header_def, asset_manager->asset_file)) return_failure_int;
|
||||
asset->data = texture_load(asset_manager->asset_file);
|
||||
if(!asset->data) return_failure_ptr;
|
||||
break;
|
||||
|
||||
default:
|
||||
error_printf("Asset type doesn't exists : %d", asset_type);
|
||||
destroy_asset(asset);
|
||||
|
||||
return EXIT_FAILURE;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
asset->nb_refs++;
|
||||
|
|
@ -261,9 +201,9 @@ int asset_manager_load_asset(asset_manager_t *asset_manager, const char *asset_n
|
|||
linked_list_push_back(&asset_manager->assets, asset_elem);
|
||||
|
||||
debug_printf("Asset \"%s\" has been loaded, nb refs : %lu", asset->name, asset->nb_refs);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
return asset_elem->data;
|
||||
}
|
||||
|
||||
int asset_manager_let_go_asset(asset_manager_t *asset_manager, const char *asset_name)
|
||||
|
|
|
|||
|
|
@ -1,13 +1,8 @@
|
|||
#ifndef ASSET_MANAGER_H
|
||||
#define ASSET_MANAGER_H
|
||||
|
||||
#include <SDL3/SDL_render.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include "linked_list.h"
|
||||
#include "vector2d.h"
|
||||
|
||||
typedef SDL_Texture texture_t;
|
||||
|
||||
typedef enum asset_type_t {
|
||||
ASSET_TEXTURE
|
||||
|
|
@ -44,7 +39,7 @@ int asset_manager_init(asset_manager_t *asset_manager);
|
|||
int asset_manager_exit(asset_manager_t *asset_manager);
|
||||
int asset_manager_collect_garbage(asset_manager_t *asset_manager);
|
||||
|
||||
int asset_manager_load_asset(asset_manager_t *asset_manager, const char *asset_name, asset_type_t asset_type);
|
||||
asset_t *asset_manager_load_asset(asset_manager_t *asset_manager, const char *asset_name, asset_type_t asset_type);
|
||||
int asset_manager_let_go_asset(asset_manager_t *asset_manager, const char *asset_name);
|
||||
|
||||
asset_t *asset_manager_get(asset_manager_t *asset_manager, const char *asset_name);
|
||||
|
|
|
|||
|
|
@ -2,8 +2,9 @@
|
|||
#define DISPLAY_H
|
||||
|
||||
#include <SDL3/SDL_render.h>
|
||||
#include <stdlib.h>
|
||||
#include "asset_manager.h"
|
||||
#include "texture.h"
|
||||
#include "vector2d.h"
|
||||
|
||||
#define C_BLACK 0, 0, 0
|
||||
#define C_RED 255, 0, 0
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
#ifndef TEXTURE_H
|
||||
#define TEXTURE_H
|
||||
|
||||
#include <SDL3/SDL_render.h>
|
||||
#include "asset_manager.h"
|
||||
|
||||
typedef SDL_Texture texture_t;
|
||||
|
||||
texture_t *texture_load(FILE *asset_file);
|
||||
|
||||
#endif // TEXTURE_H
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
#include "texture.h"
|
||||
|
||||
#include "asset_manager.h"
|
||||
#include "display.h"
|
||||
#include "errors.h"
|
||||
|
||||
texture_t *texture_load(FILE *asset_file)
|
||||
{
|
||||
size_t width, height;
|
||||
|
||||
if(fread(&width, sizeof(width), 1, asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(fread(&height, sizeof(height), 1, asset_file) != 1)
|
||||
{
|
||||
error_printf("Failed to read in asset file.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SDL_Surface *tmp_surface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_RGB565);
|
||||
if(!tmp_surface)
|
||||
{
|
||||
error_printf("Failed to create temporary surface : %s", SDL_GetError());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(!SDL_LockSurface(tmp_surface))
|
||||
{
|
||||
error_printf("Failed to lock temporary surface : %s", SDL_GetError());
|
||||
SDL_DestroySurface(tmp_surface);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, asset_file) != width * height)
|
||||
{
|
||||
error_printf("Failed to read asset file.");
|
||||
SDL_DestroySurface(tmp_surface);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(!SDL_SetSurfaceColorKey(tmp_surface, true, 0xF81F))
|
||||
{
|
||||
error_printf("Failed to set color key of temporary surface : %s", SDL_GetError());
|
||||
SDL_DestroySurface(tmp_surface);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
texture_t *texture = SDL_CreateTextureFromSurface(renderer, tmp_surface);
|
||||
if(!texture)
|
||||
{
|
||||
error_printf("Failed to create texture from temporary surface : %s", SDL_GetError());
|
||||
SDL_DestroySurface(tmp_surface);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SDL_DestroySurface(tmp_surface);
|
||||
|
||||
return texture;
|
||||
}
|
||||
Loading…
Reference in New Issue