implement simple sfx playback for jumping

scene_man
En Yi 2023-10-09 21:28:30 +08:00
parent 0a4c700bf6
commit 2b79b5e6bc
5 changed files with 72 additions and 3 deletions

View File

@ -8,6 +8,8 @@
#include <emscripten/emscripten.h> #include <emscripten/emscripten.h>
#endif #endif
static SFX_t sfx_buffer[4] = {0};
static uint32_t sfx_counts[4] = {0};
Scene_t* scenes[1]; Scene_t* scenes[1];
static GameEngine_t engine = static GameEngine_t engine =
@ -15,7 +17,13 @@ static GameEngine_t engine =
.scenes = scenes, .scenes = scenes,
.max_scenes = 1, .max_scenes = 1,
.curr_scene = 0, .curr_scene = 0,
.assets = {0} .assets = {0},
.sfx_list = {
.sfx = sfx_buffer,
.n_sfx = 4,
.sfx_queue = sfx_counts,
.played_sfx = 0,
}
}; };
void update_loop(void) void update_loop(void)
@ -31,9 +39,11 @@ void update_loop(void)
int main(void) int main(void)
{ {
init_engine(&engine);
InitWindow(1280, 640, "raylib"); InitWindow(1280, 640, "raylib");
SetTargetFPS(60); SetTargetFPS(60);
InitAudioDevice();
init_engine(&engine);
init_memory_pools(); init_memory_pools();
init_assets(&engine.assets); init_assets(&engine.assets);
@ -47,6 +57,9 @@ int main(void)
#endif #endif
init_item_creation(&engine.assets); init_item_creation(&engine.assets);
add_sound(&engine.assets, "testsnd", "res/sound.ogg");
load_sfx(&engine, "testsnd", 0);
LevelScene_t scene; LevelScene_t scene;
scene.scene.engine = &engine; scene.scene.engine = &engine;
init_sandbox_scene(&scene); init_sandbox_scene(&scene);
@ -75,6 +88,7 @@ int main(void)
update_entity_manager(&scene.scene.ent_manager); update_entity_manager(&scene.scene.ent_manager);
// This is needed to advance time delta // This is needed to advance time delta
render_scene(&scene.scene); render_scene(&scene.scene);
update_sfx_list(&engine);
if (WindowShouldClose()) break; if (WindowShouldClose()) break;
} }
#endif #endif

View File

@ -65,4 +65,12 @@ Font* get_font(Assets_t* assets, const char* name);
LevelPack_t* get_level_pack(Assets_t* assets, const char* name); LevelPack_t* get_level_pack(Assets_t* assets, const char* name);
void draw_sprite(Sprite_t* spr, Vector2 pos, bool flip_x); void draw_sprite(Sprite_t* spr, Vector2 pos, bool flip_x);
typedef struct SFX
{
Sound* snd;
uint8_t plays;
uint8_t cooldown;
} SFX_t;
#endif // __ASSETS_H #endif // __ASSETS_H

View File

@ -3,6 +3,7 @@
void init_engine(GameEngine_t* engine) void init_engine(GameEngine_t* engine)
{ {
sc_queue_init(&engine->key_buffer); sc_queue_init(&engine->key_buffer);
memset(engine->sfx_list.sfx, 0, engine->sfx_list.n_sfx * sizeof(SFX_t));
} }
void deinit_engine(GameEngine_t* engine) void deinit_engine(GameEngine_t* engine)
@ -48,6 +49,38 @@ void change_scene(GameEngine_t* engine, unsigned int idx)
engine->scenes[engine->curr_scene]->state = SCENE_PLAYING; engine->scenes[engine->curr_scene]->state = SCENE_PLAYING;
} }
bool load_sfx(GameEngine_t* engine, const char* snd_name, uint32_t tag_idx)
{
if (tag_idx >= engine->sfx_list.n_sfx) return false;
Sound* snd = get_sound(&engine->assets, snd_name);
if (snd == NULL) return false;
engine->sfx_list.sfx[tag_idx].snd = snd;
engine->sfx_list.sfx[tag_idx].cooldown = 0;
engine->sfx_list.sfx[tag_idx].plays = 0;
}
void play_sfx(GameEngine_t* engine, unsigned int tag_idx)
{
if (tag_idx >= engine->sfx_list.n_sfx) return;
SFX_t* sfx = engine->sfx_list.sfx + tag_idx;
if (sfx->plays == 0 && sfx->snd != NULL)
{
PlaySound(*sfx->snd);
sfx->plays++;
engine->sfx_list.sfx_queue[engine->sfx_list.played_sfx++] = tag_idx;
}
}
void update_sfx_list(GameEngine_t* engine)
{
for (uint32_t i = 0; i< engine->sfx_list.played_sfx; ++i)
{
uint32_t tag_idx = engine->sfx_list.sfx_queue[i];
engine->sfx_list.sfx[tag_idx].plays = 0;
}
engine->sfx_list.played_sfx = 0;
}
//void init_scene(Scene_t* scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func) //void init_scene(Scene_t* scene, SceneType_t scene_type, system_func_t render_func, action_func_t action_func)
void init_scene(Scene_t* scene, system_func_t render_func, action_func_t action_func) void init_scene(Scene_t* scene, system_func_t render_func, action_func_t action_func)
{ {

View File

@ -7,15 +7,23 @@
typedef struct Scene Scene_t; typedef struct Scene Scene_t;
typedef struct SFXList
{
SFX_t* sfx;
uint32_t* sfx_queue;
uint32_t n_sfx;
uint32_t played_sfx;
} SFXList_t;
typedef struct GameEngine { typedef struct GameEngine {
Scene_t **scenes; Scene_t **scenes;
unsigned int max_scenes; unsigned int max_scenes;
unsigned int curr_scene; unsigned int curr_scene;
Assets_t assets; Assets_t assets;
SFXList_t sfx_list;
// Maintain own queue to handle key presses // Maintain own queue to handle key presses
struct sc_queue_32 key_buffer; struct sc_queue_32 key_buffer;
} GameEngine_t; } GameEngine_t;
void change_scene(GameEngine_t* engine, unsigned int idx);
//typedef enum SceneType { //typedef enum SceneType {
// LEVEL_SCENE = 0, // LEVEL_SCENE = 0,
@ -47,6 +55,11 @@ void init_engine(GameEngine_t* engine);
void deinit_engine(GameEngine_t* engine); void deinit_engine(GameEngine_t* engine);
void process_inputs(GameEngine_t* engine, Scene_t* scene); void process_inputs(GameEngine_t* engine, Scene_t* scene);
void change_scene(GameEngine_t* engine, unsigned int idx);
bool load_sfx(GameEngine_t* engine, const char* snd_name, uint32_t tag_idx);
void play_sfx(GameEngine_t* engine, unsigned int tag_idx);
void update_sfx_list(GameEngine_t* engine);
// Inline functions, for convenience // Inline functions, for convenience
extern void update_scene(Scene_t* scene); extern void update_scene(Scene_t* scene);
extern void render_scene(Scene_t* scene); extern void render_scene(Scene_t* scene);

View File

@ -362,6 +362,7 @@ void player_movement_input_system(Scene_t* scene)
// Check if possible to jump when jump is pressed // Check if possible to jump when jump is pressed
if (p_cjump->jump_released && p_pstate->jump_pressed && p_cjump->jumps > 0 && p_cjump->jump_ready) if (p_cjump->jump_released && p_pstate->jump_pressed && p_cjump->jumps > 0 && p_cjump->jump_ready)
{ {
play_sfx(scene->engine, 0);
p_cjump->jumps--; p_cjump->jumps--;
if (!in_water) if (!in_water)
{ {