Implement assets and assets management

Internal Changelog:
- Use string-int mapping for assets tracking
- static alloc assets (so there is a hard limit for now)
- Add simple assets test code
- Implement functions to load in assets
- Current assets:
    - Texture + Sprites
    - Sound
    - Font
scene_man
En Yi 2023-03-11 16:02:47 +08:00
parent 92cb1855ba
commit 340d507f14
7 changed files with 254 additions and 29 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.cache/
build/
compile_commands.json
res/

View File

@ -118,3 +118,21 @@ target_link_libraries(menu_test
${GAME_LIBS}
)
add_executable(assets_test
assets_test.c
)
target_include_directories(assets_test
PRIVATE
${CMAKE_CURRENT_LIST_DIR}
${RAYLIB_DIR}/include
)
target_compile_options(assets_test PRIVATE -fsanitize=address -gdwarf-4)
target_link_options(assets_test PRIVATE -fsanitize=address -gdwarf-4)
target_link_directories(assets_test
PRIVATE
${RAYLIB_DIR}/lib
)
target_link_libraries(assets_test
${GAME_LIBS}
)

View File

@ -1,29 +0,0 @@
#ifndef __ASSETS_H
#define __ASSETS_H
#include "sc/map/sc_map.h"
#include "raylib.h"
typedef struct Animation
{
Image* sprite;
int frame_count;
int current_frame;
int speed;
Vector2 size;
char* name;
}Animation_t;
typedef struct Assets
{
}Assets_t;
#endif // __ASSETS_H
void add_texture(Assets_t *assets, char *name, char *path);
void add_animation(Assets_t *assets, char *name, char *path);
void add_sound(Assets_t *assets, char *name, char *path);
void add_font(Assets_t *assets, char *name, char *path);
Image* get_texture(Assets_t *assets);
Animation_t* get_animation(Assets_t *assets);
Sound* get_sound(Assets_t *assets);
Font* get_font(Assets_t *assets);

57
assets_test.c 100644
View File

@ -0,0 +1,57 @@
#include "assets.h"
#include <stdio.h>
#include <unistd.h>
#include "raylib.h"
int main(void)
{
InitWindow(1280, 640, "raylib");
InitAudioDevice();
SetTargetFPS(60);
Assets_t assets;
init_assets(&assets);
Texture2D* tex = add_texture(&assets, "testtex", "res/test_tex.png");
Sprite_t* spr = add_sprite(&assets, "testspr1", tex);
spr->origin = (Vector2){0, 0};
spr->frame_size = (Vector2){32, 32};
Sprite_t* spr2 = add_sprite(&assets, "testspr2", tex);
spr2->frame_count = 4;
spr2->origin = (Vector2){0, 0};
spr2->frame_size = (Vector2){32, 32};
spr2->speed = 15;
Sound* snd = add_sound(&assets, "testsnd", "res/sound.ogg");
Font* fnt = add_font(&assets, "testfont", "res/test_font.ttf");
while(!WindowShouldClose())
{
if (IsKeyReleased(KEY_C))
{
PlaySound(*snd);
}
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTextEx(*fnt, "Press C to play a sound", (Vector2){64, 64}, 24, 1, RED);
// Draw the static Sprite and animated Sprite
draw_sprite(spr, (Vector2){64,128});
draw_sprite(spr2, (Vector2){64,180});
EndDrawing();
// Update the animated Sprite
spr2->elapsed++;
if (spr2->elapsed == spr2->speed)
{
spr2->current_frame++;
spr2->current_frame %= spr2->frame_count;
spr2->elapsed = 0;
}
}
CloseWindow();
term_assets(&assets);
}

View File

@ -1,6 +1,7 @@
add_subdirectory(EC)
add_library(lib_scenes STATIC
engine.c
assets.c
editor_scene.c
menu_scene.c
game_systems.c

133
engine/assets.c 100644
View File

@ -0,0 +1,133 @@
#include "assets.h"
#include "assert.h"
#define MAX_TEXTURES 16
#define MAX_SPRITES 16
#define MAX_SOUNDS 16
#define MAX_FONTS 4
uint8_t free_idx[4] = {0};
// Hard limit number of
static Texture2D textures[MAX_TEXTURES];
static Font fonts[MAX_FONTS];
static Sound sfx[MAX_SOUNDS];
static Sprite_t sprites[MAX_SPRITES];
// Maybe need a circular buffer??
Texture2D* add_texture(Assets_t *assets, char *name, char *path)
{
uint8_t tex_idx = free_idx[0];
assert(tex_idx < MAX_TEXTURES);
textures[tex_idx] = LoadTexture(path);
sc_map_put_s64(&assets->m_textures, name, tex_idx);
free_idx[0]++;
return textures + tex_idx;
}
Sprite_t* add_sprite(Assets_t *assets, char *name, Texture2D* texture)
{
uint8_t spr_idx = free_idx[1];
assert(spr_idx < MAX_SPRITES);
memset(sprites + spr_idx, 0, sizeof(Sprite_t));
sprites[spr_idx].texture = texture;
sc_map_put_s64(&assets->m_sprites, name, spr_idx);
free_idx[1]++;
return sprites + spr_idx;
}
Sound* add_sound(Assets_t *assets, char *name, char *path)
{
uint8_t snd_idx = free_idx[2];
assert(snd_idx < MAX_SOUNDS);
sfx[snd_idx] = LoadSound(path);
sc_map_put_s64(&assets->m_sounds, name, snd_idx);
free_idx[2]++;
return sfx + snd_idx;
}
Font* add_font(Assets_t *assets, char *name, char *path)
{
uint8_t fnt_idx = free_idx[3];
assert(fnt_idx < MAX_FONTS);
fonts[fnt_idx] = LoadFont(path);
sc_map_put_s64(&assets->m_fonts, name, fnt_idx);
free_idx[3]++;
return fonts + fnt_idx;
}
void init_assets(Assets_t *assets)
{
sc_map_init_s64(&assets->m_fonts, MAX_FONTS, 0);
sc_map_init_s64(&assets->m_sprites, MAX_SPRITES, 0);
sc_map_init_s64(&assets->m_textures, MAX_TEXTURES, 0);
sc_map_init_s64(&assets->m_sounds, MAX_SOUNDS, 0);
}
void free_all_assets(Assets_t *assets)
{
sc_map_clear_s64(&assets->m_textures);
sc_map_clear_s64(&assets->m_fonts);
sc_map_clear_s64(&assets->m_sounds);
sc_map_clear_s64(&assets->m_sprites);
memset(free_idx, 0, sizeof(free_idx));
}
void term_assets(Assets_t *assets)
{
free_all_assets(assets);
sc_map_term_s64(&assets->m_textures);
sc_map_term_s64(&assets->m_fonts);
sc_map_term_s64(&assets->m_sounds);
sc_map_term_s64(&assets->m_sprites);
}
Texture2D* get_texture(Assets_t *assets, const char *name)
{
uint8_t tex_idx = sc_map_get_s64(&assets->m_textures, name);
if (sc_map_found(&assets->m_textures))
{
return textures + tex_idx;
}
return NULL;
}
Sprite_t* get_sprite(Assets_t *assets, const char *name)
{
uint8_t spr_idx = sc_map_get_s64(&assets->m_sprites, name);
if (sc_map_found(&assets->m_sprites))
{
return sprites + spr_idx;
}
return NULL;
}
Sound* get_sound(Assets_t *assets, const char *name)
{
uint8_t snd_idx = sc_map_get_s64(&assets->m_sounds, name);
if (sc_map_found(&assets->m_sounds))
{
return sfx + snd_idx;
}
return NULL;
}
Font* get_font(Assets_t *assets, const char *name)
{
uint8_t fnt_idx = sc_map_get_s64(&assets->m_fonts, name);
if (sc_map_found(&assets->m_fonts))
{
return fonts + fnt_idx;
}
return NULL;
}
void draw_sprite(Sprite_t *spr, Vector2 pos)
{
Rectangle rec = {
spr->origin.x + spr->frame_size.x * spr->current_frame,
spr->origin.y,
spr->frame_size.x,
spr->frame_size.y
};
DrawTextureRec(*spr->texture, rec, pos, WHITE);
}

44
engine/assets.h 100644
View File

@ -0,0 +1,44 @@
#ifndef __ASSETS_H
#define __ASSETS_H
#include "sc/map/sc_map.h"
#include "raylib.h"
// Credits to bedroomcoders.co.uk for this
typedef struct Sprite
{
Texture2D* texture;
Vector2 frame_size;
Vector2 origin;
int frame_count;
int current_frame;
int elapsed;
int speed;
char* name;
}Sprite_t;
typedef struct Assets
{
struct sc_map_s64 m_textures;
struct sc_map_s64 m_sounds;
struct sc_map_s64 m_fonts;
struct sc_map_s64 m_sprites;
}Assets_t;
void init_assets(Assets_t *assets);
void free_all_assets(Assets_t *assets);
void term_assets(Assets_t *assets);
Texture2D* add_texture(Assets_t *assets, char *name, char *path);
Sprite_t* add_sprite(Assets_t *assets, char *name, Texture2D* texture);
Sound* add_sound(Assets_t *assets, char *name, char *path);
Font* add_font(Assets_t *assets, char *name, char *path);
Texture2D* get_texture(Assets_t *assets, const char *name);
Sprite_t* get_sprite(Assets_t *assets, const char *name);
Sound* get_sound(Assets_t *assets, const char *name);
Font* get_font(Assets_t *assets, const char *name);
void draw_sprite(Sprite_t *spr, Vector2 pos);
#endif // __ASSETS_H