Add asset enums & texture addition from Images

Enums is to reduce hardcoded indices

Texture can now be added from Images
scene_man
En Yi 2024-04-22 23:29:09 +08:00
parent 3961366ac4
commit 55ba03f2d6
2 changed files with 51 additions and 25 deletions

View File

@ -8,7 +8,7 @@
#include "zstd.h" #include "zstd.h"
#include <stdio.h> #include <stdio.h>
uint8_t n_loaded[6] = {0}; uint8_t n_loaded[N_ASSETS_TYPE] = {0};
// Hard limit number of // Hard limit number of
typedef struct TextureData typedef struct TextureData
@ -70,7 +70,7 @@ static void unload_level_pack(LevelPack_t pack)
// Maybe need a circular buffer?? // Maybe need a circular buffer??
Texture2D* add_texture(Assets_t* assets, const char* name, const char* path) Texture2D* add_texture(Assets_t* assets, const char* name, const char* path)
{ {
uint8_t tex_idx = n_loaded[0]; uint8_t tex_idx = n_loaded[AST_TEXTURE];
assert(tex_idx < MAX_TEXTURES); assert(tex_idx < MAX_TEXTURES);
Texture2D tex = LoadTexture(path); Texture2D tex = LoadTexture(path);
if (tex.width == 0 || tex.height == 0) return NULL; if (tex.width == 0 || tex.height == 0) return NULL;
@ -78,13 +78,13 @@ Texture2D* add_texture(Assets_t* assets, const char* name, const char* path)
textures[tex_idx].texture = tex; textures[tex_idx].texture = tex;
strncpy(textures[tex_idx].name, name, MAX_NAME_LEN); strncpy(textures[tex_idx].name, name, MAX_NAME_LEN);
sc_map_put_s64(&assets->m_textures, textures[tex_idx].name, tex_idx); sc_map_put_s64(&assets->m_textures, textures[tex_idx].name, tex_idx);
n_loaded[0]++; n_loaded[AST_TEXTURE]++;
return &textures[tex_idx].texture; return &textures[tex_idx].texture;
} }
Texture2D* add_texture_rres(Assets_t* assets, const char* name, const char* filename, const RresFileInfo_t* rres_file) Texture2D* add_texture_rres(Assets_t* assets, const char* name, const char* filename, const RresFileInfo_t* rres_file)
{ {
uint8_t tex_idx = n_loaded[0]; uint8_t tex_idx = n_loaded[AST_TEXTURE];
assert(tex_idx < MAX_TEXTURES); assert(tex_idx < MAX_TEXTURES);
int res_id = rresGetResourceId(rres_file->dir, filename); int res_id = rresGetResourceId(rres_file->dir, filename);
@ -102,7 +102,7 @@ Texture2D* add_texture_rres(Assets_t* assets, const char* name, const char* file
textures[tex_idx].texture = tex; textures[tex_idx].texture = tex;
strncpy(textures[tex_idx].name, name, MAX_NAME_LEN); strncpy(textures[tex_idx].name, name, MAX_NAME_LEN);
sc_map_put_s64(&assets->m_textures, textures[tex_idx].name, tex_idx); sc_map_put_s64(&assets->m_textures, textures[tex_idx].name, tex_idx);
n_loaded[0]++; n_loaded[AST_TEXTURE]++;
out_tex = &textures[tex_idx].texture; out_tex = &textures[tex_idx].texture;
} }
rresUnloadResourceChunk(chunk); rresUnloadResourceChunk(chunk);
@ -111,7 +111,7 @@ Texture2D* add_texture_rres(Assets_t* assets, const char* name, const char* file
Sound* add_sound_rres(Assets_t* assets, const char* name, const char* filename, const RresFileInfo_t* rres_file) Sound* add_sound_rres(Assets_t* assets, const char* name, const char* filename, const RresFileInfo_t* rres_file)
{ {
uint8_t snd_idx = n_loaded[2]; uint8_t snd_idx = n_loaded[AST_SOUND];
assert(snd_idx < MAX_SOUNDS); assert(snd_idx < MAX_SOUNDS);
int res_id = rresGetResourceId(rres_file->dir, filename); int res_id = rresGetResourceId(rres_file->dir, filename);
@ -128,55 +128,69 @@ Sound* add_sound_rres(Assets_t* assets, const char* name, const char* filename,
sfx[snd_idx].sound = snd; sfx[snd_idx].sound = snd;
strncpy(sfx[snd_idx].name, name, MAX_NAME_LEN); strncpy(sfx[snd_idx].name, name, MAX_NAME_LEN);
sc_map_put_s64(&assets->m_sounds, sfx[snd_idx].name, snd_idx); sc_map_put_s64(&assets->m_sounds, sfx[snd_idx].name, snd_idx);
n_loaded[2]++; n_loaded[AST_SOUND]++;
out_snd = &sfx[snd_idx].sound; out_snd = &sfx[snd_idx].sound;
} }
rresUnloadResourceChunk(chunk); rresUnloadResourceChunk(chunk);
return out_snd; return out_snd;
} }
Texture2D* add_texture_from_img(Assets_t* assets, const char* name, Image img)
{
uint8_t tex_idx = n_loaded[AST_TEXTURE];
assert(tex_idx < MAX_TEXTURES);
Texture2D tex = LoadTextureFromImage(img);
if (tex.width == 0 || tex.height == 0) return NULL;
textures[tex_idx].texture = tex;
strncpy(textures[tex_idx].name, name, MAX_NAME_LEN);
sc_map_put_s64(&assets->m_textures, textures[tex_idx].name, tex_idx);
n_loaded[AST_TEXTURE]++;
return &textures[tex_idx].texture;
}
Sprite_t* add_sprite(Assets_t* assets, const char* name, Texture2D* texture) Sprite_t* add_sprite(Assets_t* assets, const char* name, Texture2D* texture)
{ {
uint8_t spr_idx = n_loaded[1]; uint8_t spr_idx = n_loaded[AST_SPRITE];
assert(spr_idx < MAX_SPRITES); assert(spr_idx < MAX_SPRITES);
memset(sprites + spr_idx, 0, sizeof(SpriteData_t)); memset(sprites + spr_idx, 0, sizeof(SpriteData_t));
sprites[spr_idx].sprite.texture = texture; sprites[spr_idx].sprite.texture = texture;
strncpy(sprites[spr_idx].name, name, MAX_NAME_LEN); strncpy(sprites[spr_idx].name, name, MAX_NAME_LEN);
sc_map_put_s64(&assets->m_sprites, sprites[spr_idx].name, spr_idx); sc_map_put_s64(&assets->m_sprites, sprites[spr_idx].name, spr_idx);
n_loaded[1]++; n_loaded[AST_SPRITE]++;
return &sprites[spr_idx].sprite; return &sprites[spr_idx].sprite;
} }
Sound* add_sound(Assets_t* assets, const char* name, const char* path) Sound* add_sound(Assets_t* assets, const char* name, const char* path)
{ {
uint8_t snd_idx = n_loaded[2]; uint8_t snd_idx = n_loaded[AST_SOUND];
assert(snd_idx < MAX_SOUNDS); assert(snd_idx < MAX_SOUNDS);
sfx[snd_idx].sound = LoadSound(path); sfx[snd_idx].sound = LoadSound(path);
strncpy(sfx[snd_idx].name, name, MAX_NAME_LEN); strncpy(sfx[snd_idx].name, name, MAX_NAME_LEN);
sc_map_put_s64(&assets->m_sounds, sfx[snd_idx].name, snd_idx); sc_map_put_s64(&assets->m_sounds, sfx[snd_idx].name, snd_idx);
n_loaded[2]++; n_loaded[AST_SOUND]++;
return &sfx[snd_idx].sound; return &sfx[snd_idx].sound;
} }
Font* add_font(Assets_t* assets, const char* name, const char* path) Font* add_font(Assets_t* assets, const char* name, const char* path)
{ {
uint8_t fnt_idx = n_loaded[3]; uint8_t fnt_idx = n_loaded[AST_FONT];
assert(fnt_idx < MAX_FONTS); assert(fnt_idx < MAX_FONTS);
fonts[fnt_idx].font = LoadFont(path); fonts[fnt_idx].font = LoadFont(path);
strncpy(fonts[fnt_idx].name, name, MAX_NAME_LEN); strncpy(fonts[fnt_idx].name, name, MAX_NAME_LEN);
sc_map_put_s64(&assets->m_fonts, fonts[fnt_idx].name, fnt_idx); sc_map_put_s64(&assets->m_fonts, fonts[fnt_idx].name, fnt_idx);
n_loaded[3]++; n_loaded[AST_FONT]++;
return &fonts[fnt_idx].font; return &fonts[fnt_idx].font;
} }
EmitterConfig_t* add_emitter_conf(Assets_t* assets, const char* name) EmitterConfig_t* add_emitter_conf(Assets_t* assets, const char* name)
{ {
uint8_t emitter_idx = n_loaded[5]; uint8_t emitter_idx = n_loaded[AST_EMITTER_CONF];
assert(emitter_idx < MAX_EMITTER_CONF); assert(emitter_idx < MAX_EMITTER_CONF);
memset(emitter_confs + emitter_idx, 0, sizeof(EmitterConfData_t)); memset(emitter_confs + emitter_idx, 0, sizeof(EmitterConfData_t));
strncpy(emitter_confs[emitter_idx].name, name, MAX_NAME_LEN); strncpy(emitter_confs[emitter_idx].name, name, MAX_NAME_LEN);
sc_map_put_s64(&assets->m_emitter_confs, emitter_confs[emitter_idx].name, emitter_idx); sc_map_put_s64(&assets->m_emitter_confs, emitter_confs[emitter_idx].name, emitter_idx);
n_loaded[5]++; n_loaded[AST_EMITTER_CONF]++;
return &emitter_confs[emitter_idx].conf; return &emitter_confs[emitter_idx].conf;
} }
@ -185,7 +199,7 @@ LevelPack_t* add_level_pack(Assets_t* assets, const char* name, const char* path
FILE* file = fopen(path, "rb"); FILE* file = fopen(path, "rb");
if (file == NULL) return NULL; if (file == NULL) return NULL;
LevelPackData_t* pack_info = levelpacks + n_loaded[4]; LevelPackData_t* pack_info = levelpacks + n_loaded[AST_LEVELPACK];
fread(&pack_info->pack.n_levels, sizeof(uint32_t), 1, file); fread(&pack_info->pack.n_levels, sizeof(uint32_t), 1, file);
pack_info->pack.levels = calloc(pack_info->pack.n_levels, sizeof(LevelMap_t)); pack_info->pack.levels = calloc(pack_info->pack.n_levels, sizeof(LevelMap_t));
@ -201,10 +215,10 @@ LevelPack_t* add_level_pack(Assets_t* assets, const char* name, const char* path
} }
fclose(file); fclose(file);
uint8_t pack_idx = n_loaded[4]; uint8_t pack_idx = n_loaded[AST_LEVELPACK];
strncpy(pack_info->name, name, MAX_NAME_LEN); strncpy(pack_info->name, name, MAX_NAME_LEN);
sc_map_put_s64(&assets->m_levelpacks, levelpacks[pack_idx].name, pack_idx); sc_map_put_s64(&assets->m_levelpacks, levelpacks[pack_idx].name, pack_idx);
n_loaded[4]++; n_loaded[AST_LEVELPACK]++;
return &levelpacks[pack_idx].pack; return &levelpacks[pack_idx].pack;
} }
@ -212,7 +226,7 @@ LevelPack_t* add_level_pack(Assets_t* assets, const char* name, const char* path
static LevelPack_t* add_level_pack_zst(Assets_t* assets, const char* name, const uint8_t* zst_buffer, uint32_t len) static LevelPack_t* add_level_pack_zst(Assets_t* assets, const char* name, const uint8_t* zst_buffer, uint32_t len)
{ {
LevelPackData_t* pack_info = levelpacks + n_loaded[4]; LevelPackData_t* pack_info = levelpacks + n_loaded[AST_LEVELPACK];
size_t read = 0; size_t read = 0;
ZSTD_inBuffer input = { level_decompressor.in_buffer, read, 0 }; ZSTD_inBuffer input = { level_decompressor.in_buffer, read, 0 };
@ -350,10 +364,10 @@ load_end:
} }
pack_info->pack.n_levels = lvls; pack_info->pack.n_levels = lvls;
uint8_t pack_idx = n_loaded[4]; uint8_t pack_idx = n_loaded[AST_LEVELPACK];
strncpy(pack_info->name, name, MAX_NAME_LEN); strncpy(pack_info->name, name, MAX_NAME_LEN);
sc_map_put_s64(&assets->m_levelpacks, levelpacks[pack_idx].name, pack_idx); sc_map_put_s64(&assets->m_levelpacks, levelpacks[pack_idx].name, pack_idx);
n_loaded[4]++; n_loaded[AST_LEVELPACK]++;
return &levelpacks[pack_idx].pack; return &levelpacks[pack_idx].pack;
} }
@ -415,19 +429,19 @@ void init_assets(Assets_t* assets)
void free_all_assets(Assets_t* assets) void free_all_assets(Assets_t* assets)
{ {
for (uint8_t i = 0; i < n_loaded[0]; ++i) for (uint8_t i = 0; i < n_loaded[AST_TEXTURE]; ++i)
{ {
UnloadTexture(textures[i].texture); UnloadTexture(textures[i].texture);
} }
for (uint8_t i = 0; i < n_loaded[2]; ++i) for (uint8_t i = 0; i < n_loaded[AST_SOUND]; ++i)
{ {
UnloadSound(sfx[i].sound); UnloadSound(sfx[i].sound);
} }
for (uint8_t i = 0; i < n_loaded[3]; ++i) for (uint8_t i = 0; i < n_loaded[AST_FONT]; ++i)
{ {
UnloadFont(fonts[i].font); UnloadFont(fonts[i].font);
} }
for (uint8_t i = 0; i < n_loaded[4]; ++i) for (uint8_t i = 0; i < n_loaded[AST_LEVELPACK]; ++i)
{ {
unload_level_pack(levelpacks[i].pack); unload_level_pack(levelpacks[i].pack);
} }

View File

@ -5,6 +5,17 @@
#include "raylib.h" #include "raylib.h"
#include "rres.h" #include "rres.h"
#include "particle_sys.h" #include "particle_sys.h"
#define N_ASSETS_TYPE 6
typedef enum AssetType
{
AST_TEXTURE = 0,
AST_SPRITE,
AST_SOUND,
AST_FONT,
AST_LEVELPACK,
AST_EMITTER_CONF,
}AssetType_t;
typedef struct Assets typedef struct Assets
{ {
@ -49,6 +60,7 @@ void free_all_assets(Assets_t* assets);
void term_assets(Assets_t* assets); void term_assets(Assets_t* assets);
Texture2D* add_texture(Assets_t* assets, const char* name, const char* path); Texture2D* add_texture(Assets_t* assets, const char* name, const char* path);
Texture2D* add_texture_from_img(Assets_t* assets, const char* name, Image img);
Sound* add_sound(Assets_t * assets, const char* name, const char* path); Sound* add_sound(Assets_t * assets, const char* name, const char* path);
Font* add_font(Assets_t* assets, const char* name, const char* path); Font* add_font(Assets_t* assets, const char* name, const char* path);
LevelPack_t* add_level_pack(Assets_t* assets, const char* name, const char* path); LevelPack_t* add_level_pack(Assets_t* assets, const char* name, const char* path);