Allow changing sound pitch

scene_man
En Yi 2024-05-01 17:08:29 +08:00
parent 1094e13c0a
commit 41e4b34869
3 changed files with 32 additions and 9 deletions

View File

@ -98,24 +98,46 @@ bool load_sfx(GameEngine_t* engine, const char* snd_name, uint32_t tag_idx)
return true; return true;
} }
void play_sfx(GameEngine_t* engine, unsigned int tag_idx) void play_sfx_pitched(GameEngine_t* engine, unsigned int tag_idx, float pitch)
{ {
if (tag_idx >= engine->sfx_list.n_sfx) return; if (tag_idx >= engine->sfx_list.n_sfx) return;
SFX_t* sfx = engine->sfx_list.sfx + tag_idx; SFX_t* sfx = engine->sfx_list.sfx + tag_idx;
if (sfx->plays == 0 && sfx->snd != NULL) if (sfx->snd != NULL)
{
SetSoundPitch(*sfx->snd, pitch);
//if (sfx->snd != NULL)
{ {
PlaySound(*sfx->snd); PlaySound(*sfx->snd);
sfx->plays++; sfx->plays++;
engine->sfx_list.sfx_queue[engine->sfx_list.played_sfx++] = tag_idx; }
//SetSoundPitch(*sfx->snd, 0.0f);
}
}
void play_sfx(GameEngine_t* engine, unsigned int tag_idx)
{
play_sfx_pitched(engine, tag_idx, 0.0f);
}
void stop_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->snd != NULL && IsSoundPlaying(*sfx->snd))
{
StopSound(*sfx->snd);
//sfx->plays--;
} }
} }
void update_sfx_list(GameEngine_t* engine) void update_sfx_list(GameEngine_t* engine)
{ {
for (uint32_t i = 0; i< engine->sfx_list.played_sfx; ++i) for (uint32_t i = 0; i< engine->sfx_list.n_sfx; ++i)
{ {
uint32_t tag_idx = engine->sfx_list.sfx_queue[i]; if (!IsSoundPlaying(*engine->sfx_list.sfx->snd))
engine->sfx_list.sfx[tag_idx].plays = 0; {
engine->sfx_list.sfx[i].plays = 0;
}
} }
engine->sfx_list.played_sfx = 0; engine->sfx_list.played_sfx = 0;
} }

View File

@ -63,6 +63,7 @@ void process_inputs(GameEngine_t* engine, Scene_t* scene);
void change_scene(GameEngine_t* engine, unsigned int idx); void change_scene(GameEngine_t* engine, unsigned int idx);
bool load_sfx(GameEngine_t* engine, const char* snd_name, uint32_t tag_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 play_sfx(GameEngine_t* engine, unsigned int tag_idx);
void play_sfx_pitched(GameEngine_t* engine, unsigned int tag_idx, float pitch);
void update_sfx_list(GameEngine_t* engine); void update_sfx_list(GameEngine_t* engine);
// Inline functions, for convenience // Inline functions, for convenience

View File

@ -520,7 +520,7 @@ void player_movement_input_system(Scene_t* scene)
// on first frame jumps // on first frame jumps
// This is also why this is done here instead of in the // This is also why this is done here instead of in the
// state transition function // state transition function
play_sfx(scene->engine, PLAYER_LAND_SFX); play_sfx_pitched(scene->engine, PLAYER_LAND_SFX, 0.8f + rand() * 0.4f / (float)RAND_MAX);
} }
} }
} }