Compare commits

...

2 Commits

Author SHA1 Message Date
En Yi e318c65135 Add some more sfx 2023-10-10 21:57:45 +08:00
En Yi 2b79b5e6bc implement simple sfx playback for jumping 2023-10-09 21:28:30 +08:00
7 changed files with 136 additions and 16 deletions

View File

@ -8,6 +8,8 @@
#include <emscripten/emscripten.h> #include <emscripten/emscripten.h>
#endif #endif
static SFX_t sfx_buffer[N_SFX] = {0};
static uint32_t sfx_counts[N_SFX] = {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 = N_SFX,
.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,17 @@ int main(void)
#endif #endif
init_item_creation(&engine.assets); init_item_creation(&engine.assets);
add_sound(&engine.assets, "snd_jump", "res/jump.ogg");
add_sound(&engine.assets, "snd_land", "res/land.ogg");
add_sound(&engine.assets, "snd_wdrop", "res/water_land.ogg");
add_sound(&engine.assets, "snd_bland", "res/boulder_move.ogg");
add_sound(&engine.assets, "snd_bubble", "res/bubble.ogg");
load_sfx(&engine, "snd_jump", PLAYER_JMP_SFX);
load_sfx(&engine, "snd_land", PLAYER_LAND_SFX);
load_sfx(&engine, "snd_wdrop", WATER_IN_SFX);
load_sfx(&engine, "snd_bland", BOULDER_LAND_SFX);
load_sfx(&engine, "snd_bubble", BUBBLE_SFX);
LevelScene_t scene; LevelScene_t scene;
scene.scene.engine = &engine; scene.scene.engine = &engine;
init_sandbox_scene(&scene); init_sandbox_scene(&scene);
@ -75,6 +96,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

@ -0,0 +1,36 @@
#ifndef _ASSETS_TAG_H
#define _ASSETS_TAG_H
typedef enum EntityTag {
NO_ENT_TAG = 0,
PLAYER_ENT_TAG,
ENEMY_ENT_TAG,
CRATES_ENT_TAG,
CHEST_ENT_TAG,
BOULDER_ENT_TAG,
LEVEL_END_TAG,
DESTRUCTABLE_ENT_TAG,
DYNMEM_ENT_TAG,
} EntityTag_t;
#define N_SFX 18
typedef enum SFXTag {
PLAYER_JMP_SFX = 0,
PLAYER_LAND_SFX,
PLAYER_RUN_SFX,
PLAYER_LADDER_SFX,
PLAYER_WATER_RUN_SFX,
WATER_IN_SFX,
WATER_OUT_SFX,
WOOD_LAND_SFX,
WOOD_DESTROY_SFX,
METAL_LAND_SFX,
METAL_DESTROY_SFX,
BOULDER_LAND_SFX,
BOULDER_DESTROY_SFX,
ARROW_RELEASE_SFX,
ARROW_DESTROY_SFX,
BOMB_RELEASE_SFX,
EXPLOSION_SFX,
BUBBLE_SFX,
} SFXTag_t;
#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

@ -1,19 +1,7 @@
#ifndef __ENT_IMPL_H #ifndef __ENT_IMPL_H
#define __ENT_IMPL_H #define __ENT_IMPL_H
#include "assets.h" #include "assets.h"
#include "assets_tag.h"
typedef enum EntityTag {
NO_ENT_TAG = 0,
PLAYER_ENT_TAG,
ENEMY_ENT_TAG,
CRATES_ENT_TAG,
CHEST_ENT_TAG,
BOULDER_ENT_TAG,
LEVEL_END_TAG,
DESTRUCTABLE_ENT_TAG,
DYNMEM_ENT_TAG,
} EntityTag_t;
bool init_player_creation(const char* info_file, Assets_t* assets); bool init_player_creation(const char* info_file, Assets_t* assets);
bool init_player_creation_rres(const char* rres_file, const char* file, Assets_t* assets); bool init_player_creation_rres(const char* rres_file, const char* file, Assets_t* assets);

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, PLAYER_JMP_SFX);
p_cjump->jumps--; p_cjump->jumps--;
if (!in_water) if (!in_water)
{ {
@ -385,6 +386,12 @@ void player_movement_input_system(Scene_t* scene)
p_cjump->jump_ready = false; p_cjump->jump_ready = false;
p_cjump->jump_released = false; p_cjump->jump_released = false;
} }
else if (p_mstate->ground_state == 0b01)
{
// the else if check is to prevent playing the landing sfx
// on first frame jumps
play_sfx(scene->engine, PLAYER_LAND_SFX);
}
} }
} }
@ -898,6 +905,7 @@ void moveable_update_system(Scene_t* scene)
p_moveable->gridmove = false; p_moveable->gridmove = false;
p_bbox->solid = true; p_bbox->solid = true;
p_ctransform->movement_mode = REGULAR_MOVEMENT; p_ctransform->movement_mode = REGULAR_MOVEMENT;
play_sfx(scene->engine, BOULDER_LAND_SFX);
} }
else if (remaining_distance > 0.1) else if (remaining_distance > 0.1)
{ {
@ -1345,6 +1353,17 @@ void state_transition_update_system(Scene_t* scene)
{ {
p_ctransform->active = true; p_ctransform->active = true;
} }
if (p_mstate->ground_state == 0b01)
{
if (p_ent->m_tag == BOULDER_ENT_TAG)
{
play_sfx(scene->engine, BOULDER_LAND_SFX);
}
}
if (p_mstate->water_state == 0b01)
{
play_sfx(scene->engine, WATER_IN_SFX);
}
} }
} }
@ -1741,6 +1760,7 @@ void airtimer_update_system(Scene_t* scene)
{ {
p_air->curr_count--; p_air->curr_count--;
p_air->curr_ftimer = p_air->max_ftimer; p_air->curr_ftimer = p_air->max_ftimer;
play_sfx(scene->engine, BUBBLE_SFX);
} }
else else
{ {