diff --git a/scene_test.c b/scene_test.c index a0df98b..5e9b9b1 100644 --- a/scene_test.c +++ b/scene_test.c @@ -8,6 +8,8 @@ #include #endif +static SFX_t sfx_buffer[4] = {0}; +static uint32_t sfx_counts[4] = {0}; Scene_t* scenes[1]; static GameEngine_t engine = @@ -15,7 +17,13 @@ static GameEngine_t engine = .scenes = scenes, .max_scenes = 1, .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) @@ -31,9 +39,11 @@ void update_loop(void) int main(void) { - init_engine(&engine); InitWindow(1280, 640, "raylib"); SetTargetFPS(60); + InitAudioDevice(); + + init_engine(&engine); init_memory_pools(); init_assets(&engine.assets); @@ -47,6 +57,9 @@ int main(void) #endif init_item_creation(&engine.assets); + add_sound(&engine.assets, "testsnd", "res/sound.ogg"); + load_sfx(&engine, "testsnd", 0); + LevelScene_t scene; scene.scene.engine = &engine; init_sandbox_scene(&scene); @@ -75,6 +88,7 @@ int main(void) update_entity_manager(&scene.scene.ent_manager); // This is needed to advance time delta render_scene(&scene.scene); + update_sfx_list(&engine); if (WindowShouldClose()) break; } #endif diff --git a/scenes/engine/assets.h b/scenes/engine/assets.h index af0a477..2642218 100644 --- a/scenes/engine/assets.h +++ b/scenes/engine/assets.h @@ -65,4 +65,12 @@ Font* get_font(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); + +typedef struct SFX +{ + Sound* snd; + uint8_t plays; + uint8_t cooldown; +} SFX_t; + #endif // __ASSETS_H diff --git a/scenes/engine/engine.c b/scenes/engine/engine.c index 8bf843a..6e0181f 100644 --- a/scenes/engine/engine.c +++ b/scenes/engine/engine.c @@ -3,6 +3,7 @@ void init_engine(GameEngine_t* engine) { 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) @@ -48,6 +49,38 @@ void change_scene(GameEngine_t* engine, unsigned int idx) 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, system_func_t render_func, action_func_t action_func) { diff --git a/scenes/engine/engine.h b/scenes/engine/engine.h index 8f39ed8..501e0be 100644 --- a/scenes/engine/engine.h +++ b/scenes/engine/engine.h @@ -7,15 +7,23 @@ 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 { Scene_t **scenes; unsigned int max_scenes; unsigned int curr_scene; Assets_t assets; + SFXList_t sfx_list; // Maintain own queue to handle key presses struct sc_queue_32 key_buffer; } GameEngine_t; -void change_scene(GameEngine_t* engine, unsigned int idx); //typedef enum SceneType { // LEVEL_SCENE = 0, @@ -47,6 +55,11 @@ void init_engine(GameEngine_t* engine); void deinit_engine(GameEngine_t* engine); 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 extern void update_scene(Scene_t* scene); extern void render_scene(Scene_t* scene); diff --git a/scenes/game_systems.c b/scenes/game_systems.c index dd7d760..89afc37 100644 --- a/scenes/game_systems.c +++ b/scenes/game_systems.c @@ -362,6 +362,7 @@ void player_movement_input_system(Scene_t* scene) // 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) { + play_sfx(scene->engine, 0); p_cjump->jumps--; if (!in_water) {