Using the asset manager to load and store all types of assets.
This commit is contained in:
parent
5ab3d30407
commit
520a846892
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,33 @@
|
|||
#ifndef ASSET_MANAGER_H
|
||||
#define ASSET_MANAGER_H
|
||||
|
||||
#include "texture_manager.h"
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <stdbool.h>
|
||||
#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
|
||||
|
|
@ -1,104 +0,0 @@
|
|||
#ifndef TEXTURE_MANAGER_H
|
||||
#define TEXTURE_MANAGER_H
|
||||
|
||||
#include <image.h>
|
||||
#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
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
#include <string.h>
|
||||
#include <memory_alloc.h>
|
||||
#include <display.h>
|
||||
#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);
|
||||
}
|
||||
Loading…
Reference in New Issue