From 520a8468929bf265359d8d573f38bff92bfd4aa1 Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Mon, 20 Jul 2026 21:23:54 +0200 Subject: [PATCH] Using the asset manager to load and store all types of assets. --- src/Makefile | 9 ++- src/asset_manager.c | 12 +++- src/headers/asset_manager.h | 30 ++++++++-- src/headers/texture_manager.h | 104 ---------------------------------- src/texture_manager.c | 70 ----------------------- 5 files changed, 40 insertions(+), 185 deletions(-) delete mode 100644 src/headers/texture_manager.h delete mode 100644 src/texture_manager.c diff --git a/src/Makefile b/src/Makefile index 71b75b9..c154f8d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,11 +1,10 @@ # Source files CORE_SOURCES := \ - asset_manager.c \ - texture_manager.c \ - event.c \ - vector2d.c \ linked_list.c \ - display.c + vector2d.c \ + asset_manager.c \ + display.c \ + event.c # Compiler and flags CORE_CC = gcc diff --git a/src/asset_manager.c b/src/asset_manager.c index f62bbd6..d56dece 100644 --- a/src/asset_manager.c +++ b/src/asset_manager.c @@ -1,9 +1,17 @@ #include "asset_manager.h" -void asset_manager_init(void) +void asset_manager_init(asset_manager_t *asset_manager) { } -void asset_manager_exit(void) +void asset_manager_exit(asset_manager_t *asset_manager) +{ +} + +texture_t *asset_manager_get_texture(asset_manager_t *asset_manager, const char *asset_name) +{ +} + +void asset_manager_draw_texture(texture_t *texture, rect_t *src_rect, rect_t *dst_rect, bool flip) { } diff --git a/src/headers/asset_manager.h b/src/headers/asset_manager.h index c433493..52fc530 100644 --- a/src/headers/asset_manager.h +++ b/src/headers/asset_manager.h @@ -1,11 +1,33 @@ #ifndef ASSET_MANAGER_H #define ASSET_MANAGER_H -#include "texture_manager.h" +#include +#include +#include "linked_list.h" +#include "vector2d.h" -void asset_manager_init(void); -void asset_manager_exit(void); +typedef SDL_Texture texture_t; -texture_t *load_image(const char *path); +typedef struct asset_t { + union { + texture_t *texture; + } data; + + enum { + TEXTURE + } type; + + size_t nb_refs; +} asset_t; + +typedef struct asset_manager_t { + linked_list_t assets; +} asset_manager_t; + +void asset_manager_init(asset_manager_t *asset_manager); +void asset_manager_exit(asset_manager_t *asset_manager); + +texture_t *asset_manager_get_texture(asset_manager_t *asset_manager, const char *asset_name); +void asset_manager_draw_texture(texture_t *texture, rect_t *src_rect, rect_t *dst_rect, bool flip); #endif // ASSET_MANAGER_H \ No newline at end of file diff --git a/src/headers/texture_manager.h b/src/headers/texture_manager.h deleted file mode 100644 index 5d5e412..0000000 --- a/src/headers/texture_manager.h +++ /dev/null @@ -1,104 +0,0 @@ -#ifndef TEXTURE_MANAGER_H -#define TEXTURE_MANAGER_H - -#include -#include "vector2d.h" -#include "linked_list.h" - -/** - * @brief This struct is the base for textures. - * - * @param name Name of the texture - * @param image Image of type image_t -*/ -typedef struct texture_t { - const char *name; - image_t *image; -} texture_t; - -/** - * @brief Create a texture with the given image and name. - * - * @param name Name of the texture - * @param image Texture of type image_t - * - * @return The created texture, NULL on error. -*/ -texture_t *create_texture(image_t *image, const char *name); - -/** - * @brief Destroy the given texture. - * - * @param texture Texture to destroy -*/ -void destroy_texture(texture_t *texture); - -/** - * @brief Draw the given texture from given src rect to dst rect. - * - * @param texture Texture to draw - * @param src Source rectangle of the texture to draw - * @param dst Destination of the drawing -*/ -void draw_texture(const texture_t *texture, const rect_t *src_rect, const rect_t *dst_rect, bool flip); - -/** - * @brief Texture manager struct. - * - * @param textures Textures loaded in memory -*/ -typedef struct texture_manager_t { - linked_list_t textures; -} texture_manager_t; - -/** - * @brief Initialise the given texture_manager. - * - * @param texture_manager Pointer to a texture manager -*/ -void texture_manager_init(texture_manager_t *texture_manager); - -/** - * @brief Load a texture from asset file in the texture manager. - * - * @param texture_manager Pointer to a texture manager - * @param name Name of the texture to add in the texture manager -*/ -void texture_manager_load_texture(texture_manager_t *texture_manager, const char *name); - -/** - * @brief Get a texture in the texture manager. - * - * @param texture_manager Pointer to a texture manager - * @param name Name of the texture to get - * - * @return The texture, NULL on error or if the texture is not found. -*/ -texture_t *texture_manager_get_texture(texture_manager_t *texture_manager, const char *name); - -/** - * @brief Remove a texture in the texture manager. - * Does nothing if the texture is not found. - * - * @param texture_manager Pointer to a texture manager - * @param name Name of the texture to remove -*/ -void texture_manager_remove_texture(texture_manager_t *texture_manager, const char *name); - -/** - * @brief Clear the texture manager. - * - * @param texture_manager Pointer to a texture manager -*/ -void texture_manager_clear(texture_manager_t *texture_manager); - -/** - * @brief Draw a texture in the texture manager. - * Do nothing if the texture is not found. - * - * @param texture_manager Pointer to a texture manager - * @param name Name of the texture to draw -*/ -void texture_manager_draw_texture(texture_manager_t *texture_manager, const char *name, const rect_t *src_rect, const rect_t *dst_rect, bool flip); - -#endif // TEXTURE_MANAGER_H \ No newline at end of file diff --git a/src/texture_manager.c b/src/texture_manager.c deleted file mode 100644 index e0255b8..0000000 --- a/src/texture_manager.c +++ /dev/null @@ -1,70 +0,0 @@ -#include -#include -#include -#include "texture_manager.h" - -texture_t *create_texture(image_t *image, const char *name) -{ - if(!image) return NULL; - - texture_t *texture = malloc(sizeof(texture_t)); - if (!texture) return NULL; - - texture->name = name; - texture->image = image; - - return texture; -} - -inline void destroy_texture(texture_t *texture) -{ - free(texture); -} - -inline void draw_texture(const texture_t *texture, const rect_t *src_rect, const rect_t *dst_rect, bool flip) -{ - (void)texture; - (void)src_rect; - (void)dst_rect; - (void)flip; - //display_subimage(texture->image, src_rect, dst_rect, flip); -} - -inline void texture_manager_init(texture_manager_t *texture_manager) -{ - linked_list_init(&texture_manager->textures, sizeof(texture_t), NULL); -} - -inline void texture_manager_load_texture(texture_manager_t *texture_manager, const char *name) -{ - texture_t *texture; - linked_list_push_back(&texture_manager->textures, texture); -} - -// Only for this file -// Arg : const char *name -static inline bool is_texture_name(elem_t *elem, va_list args) -{ - return !strcmp(((texture_t *)elem->data)->name, va_arg(args, const char*)); -} - -texture_t *texture_manager_get_texture(texture_manager_t *texture_manager, const char *name) -{ - texture_t *texture = linked_list_get_if(&texture_manager->textures, is_texture_name, name); - return texture; -} - -inline void texture_manager_remove_texture(texture_manager_t *texture_manager, const char *name) -{ - linked_list_remove_if(&texture_manager->textures, is_texture_name, name); -} - -inline void texture_manager_clear(texture_manager_t *texture_manager) -{ - linked_list_clear(&texture_manager->textures); -} - -inline void texture_manager_draw_texture(texture_manager_t *texture_manager, const char *name, const rect_t *src, const rect_t *dst, bool flip) -{ - draw_texture(texture_manager_get_texture(texture_manager, name), src, dst, flip); -}