Compare commits

...

5 Commits

Author SHA1 Message Date
En Yi 82b8a3b988 Implement simple animation system
Changelog:
- Add sprite map for player
- Add an animation system
- Add a placeholder player sprite transition function
2023-03-18 13:55:55 +08:00
En Yi ac6d93565b Add Sprite Component
Changelog:
- Move Sprite struct into components
- Add asset field in engine
- Update scene test code to add engine for assets
    - This fixes the crash when q is pressed
- Add sprite component to player
- Update render function to draw the sprite
2023-03-13 21:12:37 +08:00
En Yi 1ea1db3c60 Update the test to use the get functions 2023-03-13 20:07:52 +08:00
En Yi d01a6772ec Add convenience scripts
Changelog:
- Add build and run scripts for convenience
- Update lsan suppresion file
2023-03-11 22:50:17 +08:00
En Yi 340d507f14 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
2023-03-11 16:02:47 +08:00
19 changed files with 388 additions and 39 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);

61
assets_test.c 100644
View File

@ -0,0 +1,61 @@
#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");
add_sprite(&assets, "testspr1", tex);
Sprite_t* spr = get_sprite(&assets, "testspr1");
spr->origin = (Vector2){0, 0};
spr->frame_size = (Vector2){32, 32};
add_sprite(&assets, "testspr2", tex);
Sprite_t* spr2 = get_sprite(&assets, "testspr2");
spr2->frame_count = 4;
spr2->origin = (Vector2){0, 0};
spr2->frame_size = (Vector2){32, 32};
spr2->speed = 15;
add_sound(&assets, "testsnd", "res/sound.ogg");
Sound* snd = get_sound(&assets, "testsnd");
add_font(&assets, "testfont", "res/test_font.ttf");
Font* fnt = get_font(&assets, "testfont");
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);
}

3
build.sh 100755
View File

@ -0,0 +1,3 @@
#!/bin/sh
cd build/ && make -j4 && cd ..

View File

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

View File

@ -2,10 +2,11 @@
#define __COMPONENTS_H
#include "raylib.h"
#include <stdint.h>
#include "entity.h"
// TODO: Look at sc to use macros to auto generate functions
#define N_COMPONENTS 9
enum ComponentEnum
#define N_COMPONENTS 10
typedef enum ComponentEnum
{
CBBOX_COMP_T,
CTRANSFORM_COMP_T,
@ -16,8 +17,8 @@ enum ComponentEnum
CCONTAINER_T,
CHITBOXES_T,
CHURTBOX_T,
};
typedef enum ComponentEnum ComponentEnum_t;
CSPRITE_T,
}ComponentEnum_t;
typedef struct _CBBox_t
{
@ -112,6 +113,29 @@ typedef struct _CHurtbox_t
bool fragile;
}CHurtbox_t;
// 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 unsigned int (*sprite_transition_func_t)(Entity_t *ent); // Transition requires knowledge of the entity
typedef struct _CSprite_t
{
const char * const *sprites_map; //Array of all sprite names associated
Sprite_t* sprite;
sprite_transition_func_t transition_func;
Vector2 offset;
unsigned int current_idx;
}CSprite_t;
static inline void set_bbox(CBBox_t* p_bbox, unsigned int x, unsigned int y)
{
p_bbox->size.x = x;

View File

@ -14,13 +14,8 @@ static CPlayerState_t cplayerstate_buffer[1]; // Only player is expected to have
static CContainer_t ccontainer_buffer[MAX_COMP_POOL_SIZE];
static CHitBoxes_t chitboxes_buffer[MAX_COMP_POOL_SIZE];
static CHurtbox_t churtbox_buffer[MAX_COMP_POOL_SIZE];
static CSprite_t csprite_buffer[MAX_COMP_POOL_SIZE];
// Use hashmap as a Set
// Use list will be used to check if an object exist
// The alternative method to check the free list if idx is not there
// requires bound checking
// It's just easier on the mind overall
// If need to optimise memory, replace free_list with set and remove use_list
typedef struct MemPool
{
void * const buffer;
@ -42,6 +37,7 @@ static MemPool_t comp_mempools[N_COMPONENTS] =
{ccontainer_buffer, MAX_COMP_POOL_SIZE, sizeof(CContainer_t), NULL, {0}},
{chitboxes_buffer, MAX_COMP_POOL_SIZE, sizeof(CHitBoxes_t), NULL, {0}},
{churtbox_buffer, MAX_COMP_POOL_SIZE, sizeof(CHurtbox_t), NULL, {0}},
{csprite_buffer, MAX_COMP_POOL_SIZE, sizeof(CSprite_t), NULL, {0}},
};
static MemPool_t ent_mempool = {entity_buffer, MAX_COMP_POOL_SIZE, sizeof(Entity_t), NULL, {0}};

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);
}

32
engine/assets.h 100644
View File

@ -0,0 +1,32 @@
#ifndef __ASSETS_H
#define __ASSETS_H
#include "sc/map/sc_map.h"
#include "components.h"
#include "raylib.h"
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

View File

@ -0,0 +1,8 @@
#include "assets_maps.h"
const char * const player_sprite_map[N_PLAYER_SPRITES] =
{
"plr_stand",
"plr_run",
};

View File

@ -0,0 +1,13 @@
#ifndef __ASSETS_MAPS_H
#define __ASSETS_MAPS_H
#define N_PLAYER_SPRITES 2
enum PlayerSpriteEnum
{
SPR_PLAYER_STAND = 0,
SPR_PLAYER_RUN
};
extern const char * const player_sprite_map[N_PLAYER_SPRITES];
#endif // __ASSETS_MAPS_H

View File

@ -1,6 +1,7 @@
#include "scene_impl.h"
#include "game_systems.h"
#include "constants.h"
#include "assets_maps.h"
#include "raylib.h"
#include "raymath.h"
#include <stdio.h>
@ -92,6 +93,12 @@ static void level_scene_render_func(Scene_t* scene)
};
DrawRectangleLinesEx(rec, 1.5, PURPLE);
}
CSprite_t* p_cspr = get_component(&scene->ent_manager, p_ent, CSPRITE_T);
if (p_cspr != NULL)
{
Vector2 pos = Vector2Add(p_ct->position, p_cspr->offset);
draw_sprite(p_cspr->sprite, pos);
}
}
for (size_t i=0; i<tilemap.n_tiles;++i)
@ -204,6 +211,10 @@ static void spawn_player(Scene_t *scene)
.width = p_bbox->size.x + 2,
.height = p_bbox->size.y - 1,
};
CSprite_t* p_cspr = add_component(&scene->ent_manager, p_ent, CSPRITE_T);
p_cspr->sprite = get_sprite(&scene->engine->assets, "plr_stand");
p_cspr->sprites_map = player_sprite_map;
p_cspr->transition_func = &player_sprite_transition_func;
}
static void toggle_block_system(Scene_t *scene)
@ -328,6 +339,7 @@ void init_level_scene(LevelScene_t *scene)
//sc_array_add(&scene->scene.systems, &update_tilemap_system);
sc_array_add(&scene->scene.systems, &state_transition_update_system);
sc_array_add(&scene->scene.systems, &player_ground_air_transition_system);
sc_array_add(&scene->scene.systems, &sprite_animation_system);
sc_array_add(&scene->scene.systems, &toggle_block_system);
// This avoid graphical glitch, not essential

View File

@ -3,6 +3,8 @@
#include "entManager.h"
#include "actions.h"
#include "sc/array/sc_array.h"
#include "assets.h"
typedef struct Scene Scene_t;
typedef struct GameEngine
@ -10,6 +12,7 @@ typedef struct GameEngine
Scene_t **scenes;
unsigned int max_scenes;
unsigned int curr_scene;
Assets_t assets;
}GameEngine_t;
void change_scene(GameEngine_t *engine, unsigned int idx);

View File

@ -1,6 +1,7 @@
#include "game_systems.h"
#include "AABB.h"
#include "constants.h"
#include "assets_maps.h"
#include <stdio.h>
static const Vector2 TILE_SZ = {TILE_SIZE, TILE_SIZE};
@ -888,6 +889,39 @@ void hitbox_update_system(Scene_t *scene)
}
}
void sprite_animation_system(Scene_t *scene)
{
unsigned int ent_idx;
CSprite_t* p_cspr;
sc_map_foreach(&scene->ent_manager.component_map[CSPRITE_T], ent_idx, p_cspr)
{
Entity_t *p_ent = get_entity(&scene->ent_manager, ent_idx);
// Update animation state
if (p_cspr->transition_func != NULL)
{
unsigned int spr_idx = p_cspr->transition_func(p_ent);
if (p_cspr->current_idx != spr_idx)
{
Sprite_t* new_spr = get_sprite(&scene->engine->assets, p_cspr->sprites_map[spr_idx]);
if (new_spr != NULL)
{
p_cspr->sprite = new_spr;
p_cspr->current_idx = spr_idx;
p_cspr->sprite->current_frame = 0;
}
}
}
// Animate it (handle frame count)
p_cspr->sprite->elapsed++;
if (p_cspr->sprite->elapsed == p_cspr->sprite->speed)
{
p_cspr->sprite->current_frame++;
p_cspr->sprite->current_frame %= p_cspr->sprite->frame_count;
p_cspr->sprite->elapsed = 0;
}
}
}
void init_level_scene_data(LevelSceneData_t *data)
{
sc_map_init_32(&data->collision_events, 128, 0);
@ -897,3 +931,8 @@ void term_level_scene_data(LevelSceneData_t *data)
{
sc_map_term_32(&data->collision_events);
}
unsigned int player_sprite_transition_func(Entity_t* ent)
{
return SPR_PLAYER_RUN;
}

View File

@ -14,4 +14,7 @@ void player_ground_air_transition_system(Scene_t* scene);
void state_transition_update_system(Scene_t *scene);
void update_tilemap_system(Scene_t *scene);
void hitbox_update_system(Scene_t *scene);
void sprite_animation_system(Scene_t *scene);
unsigned int player_sprite_transition_func(Entity_t* ent);
#endif // __GAME_SYSTEMS_H

View File

@ -1 +1,2 @@
leak:X11
leak:radeonsi_dri

3
run.sh 100755
View File

@ -0,0 +1,3 @@
#!/bin/sh
LSAN_OPTIONS=suppressions=./lsan_supp.txt ./build/$1

View File

@ -6,14 +6,40 @@
// Maintain own queue to handle key presses
struct sc_queue_32 key_buffer;
Scene_t *scenes[1];
static GameEngine_t engine =
{
.scenes = scenes,
.max_scenes = 1,
.curr_scene = 0,
.assets = {0}
};
int main(void)
{
sc_queue_init(&key_buffer);
InitWindow(1280, 640, "raylib");
SetTargetFPS(60);
init_memory_pools();
init_assets(&engine.assets);
Texture2D* tex = add_texture(&engine.assets, "plr_tex", "res/test_tex.png");
Sprite_t* spr = add_sprite(&engine.assets, "plr_stand", tex);
spr->origin = (Vector2){0, 0};
spr->frame_size = (Vector2){32, 32};
spr = add_sprite(&engine.assets, "plr_run", tex);
spr->frame_count = 4;
spr->origin = (Vector2){0, 0};
spr->frame_size = (Vector2){32, 32};
spr->speed = 15;
LevelScene_t scene;
scene.scene.engine = &engine;
init_level_scene(&scene);
scenes[0] = &scene.scene;
change_scene(&engine, 0);
while(true)
{