diff --git a/code/core/impl/math.c b/code/core/impl/math.c index 36d0876..afdaf88 100644 --- a/code/core/impl/math.c +++ b/code/core/impl/math.c @@ -47,6 +47,11 @@ V3f V3f_Sub(V3f a, V3f b) { return result; } +F32 V2f_Dot(V2f a, V2f b) { + F32 result = (a.x * b.x) + (a.y * b.y); + return result; +} + F32 V3f_Dot(V3f a, V3f b) { F32 result = (a.x * b.x) + (a.y * b.y) + (a.z * b.z); return result; diff --git a/code/core/math.h b/code/core/math.h index f49cc00..64a9ae6 100644 --- a/code/core/math.h +++ b/code/core/math.h @@ -118,6 +118,7 @@ function V3f V3f_Neg(V3f x); function V3f V3f_Scale(V3f x, F32 s); function V3f V3f_Sub(V3f a, V3f b); +function F32 V2f_Dot(V2f a, V2f b); function F32 V3f_Dot(V3f a, V3f b); function F32 V4f_Dot(V4f a, V4f b); diff --git a/code/first.c b/code/first.c index aff318b..d541795 100644 --- a/code/first.c +++ b/code/first.c @@ -25,8 +25,6 @@ #include "game/impl/npc.c" #include "game/impl/bandit.c" - - int main(int argc, char **argv) { (void) argc; @@ -64,7 +62,8 @@ int main(int argc, char **argv) Audio_Context *audio = M_ArenaPush(game->arena, Audio_Context); Audio_Init(game->arena, audio, 0.15f); - Audio_Play(audio, 1); + U32 bgm = Audio_Play(audio, 0); + U32 saloon_music = Audio_Play(audio, 1); SDL_ResumeAudioStreamDevice(audio->stream); @@ -84,6 +83,11 @@ int main(int argc, char **argv) World *world = M_ArenaPush(arena, World); LoadWorld(arena, world); game->world = world; + + world->audio = audio; + world->audio_handles[0] = bgm; + world->audio_handles[1] = saloon_music; + world->arena = arena; world->random = Random_Seed(29237489723847); world->npcCount = 127; @@ -333,7 +337,7 @@ int main(int argc, char **argv) } if(!game->editor.enabled) { - UpdateWorld(1.0f / 60.0f, game->world); + UpdateWorld(1.0f / 250.0f, game->world); game->camera.p.x = game->world->player.collision.pos.x; game->camera.p.y = game->world->player.collision.pos.y; diff --git a/code/game/impl/player.c b/code/game/impl/player.c index 678b2cc..a98b698 100644 --- a/code/game/impl/player.c +++ b/code/game/impl/player.c @@ -164,6 +164,16 @@ void PlayerUpdate(F32 delta, Player *player) { dir = V2f_Scale(NormaliseV2F(dir), PLAYER_SPEED*delta); player->collision.pos.x += dir.x; player->collision.pos.y += dir.y; + + V2f test; + test.x = player->collision.pos.x - 14; + test.y = player->collision.pos.y - 14; + + F32 len = V2f_Dot(test, test); + F32 vol = Max(0.0f, 1.0f - (len / 25.0f)); + + Audio_ChangeVolume(player->world->audio, player->world->audio_handles[1], vol); + Audio_ChangeVolume(player->world->audio, player->world->audio_handles[0], 1.0f - vol); } void PlayerDraw(D_Context *draw, Player *player) { diff --git a/code/game/world.h b/code/game/world.h index 3cc1e3d..df710c3 100644 --- a/code/game/world.h +++ b/code/game/world.h @@ -59,6 +59,9 @@ struct World { U32 propCount; U32 hitboxCount; + Audio_Context *audio; + U32 audio_handles[2]; + //// Player Player player; diff --git a/code/os/audio.h b/code/os/audio.h index a8b70e4..11cdea5 100644 --- a/code/os/audio.h +++ b/code/os/audio.h @@ -13,6 +13,8 @@ struct Audio_Track { Audio_Data *data; + F32 volume; + U32 n_played; U32 next; // to play if playing, free otherwise }; @@ -34,6 +36,8 @@ struct Audio_Context { }; function void Audio_Init(M_Arena *arena, Audio_Context *audio, F32 volume); -function void Audio_Play(Audio_Context *audio, U32 index); +function U32 Audio_Play(Audio_Context *audio, U32 index); + +function void Audio_ChangeVolume(Audio_Context *audio, U32 handle, F32 volume); #endif // LD_OS_AUDIO_H_ diff --git a/code/os/core.c b/code/os/core.c index 324c21c..1b36388 100644 --- a/code/os/core.c +++ b/code/os/core.c @@ -48,8 +48,8 @@ internal void SDL_SubmitAudio(void *user, SDL_AudioStream *stream, int needed, i U32 off = track->n_played << 1; // played n frames, thus double to n samples for (U32 f = 0; f < play; ++f) { - *l0++ += cast(F32) (track->data->samples[off + (2 * f) + 0]) * audio->volume; - *r0++ += cast(F32) (track->data->samples[off + (2 * f) + 1]) * audio->volume; + *l0++ += cast(F32) (track->data->samples[off + (2 * f) + 0]) * track->volume * audio->volume; + *r0++ += cast(F32) (track->data->samples[off + (2 * f) + 1]) * track->volume * audio->volume; } track->n_played += play; @@ -136,7 +136,6 @@ void Audio_Init(M_Arena *arena, Audio_Context *audio, F32 volume) { } audio->n_tracks = 16; - //audio->tracks = M_ArenaPush(arena, Audio_Track, audio->n_tracks); for (U32 it = 1; it < audio->n_tracks; ++it) { audio->tracks[it].next = it + 1; @@ -153,19 +152,30 @@ void Audio_Init(M_Arena *arena, Audio_Context *audio, F32 volume) { } } -void Audio_Play(Audio_Context *audio, U32 index) { +U32 Audio_Play(Audio_Context *audio, U32 index) { + U32 result = 0; if (audio->free != 0 && SDL_LockAudioStream(audio->stream)) { - U32 idx = audio->free; + result = audio->free; - Audio_Track *track = &audio->tracks[idx]; + Audio_Track *track = &audio->tracks[result]; audio->free = track->next; track->playing = true; track->data = &audio->sounds[index]; track->n_played = 0; + track->volume = 1.0f; track->next = audio->head; - audio->head = idx; + audio->head = result; SDL_UnlockAudioStream(audio->stream); } + + return result; +} + +void Audio_ChangeVolume(Audio_Context *audio, U32 handle, F32 volume) { + if (SDL_LockAudioStream(audio->stream)) { + audio->tracks[handle].volume = volume; + SDL_UnlockAudioStream(audio->stream); + } }