VERY hacky positional audio

This commit is contained in:
2025-10-06 22:17:41 +01:00
parent 59f643b72c
commit df2c02a6a9
7 changed files with 49 additions and 12 deletions

View File

@@ -47,6 +47,11 @@ V3f V3f_Sub(V3f a, V3f b) {
return result; 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 V3f_Dot(V3f a, V3f b) {
F32 result = (a.x * b.x) + (a.y * b.y) + (a.z * b.z); F32 result = (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
return result; return result;

View File

@@ -118,6 +118,7 @@ function V3f V3f_Neg(V3f x);
function V3f V3f_Scale(V3f x, F32 s); function V3f V3f_Scale(V3f x, F32 s);
function V3f V3f_Sub(V3f a, V3f b); 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 V3f_Dot(V3f a, V3f b);
function F32 V4f_Dot(V4f a, V4f b); function F32 V4f_Dot(V4f a, V4f b);

View File

@@ -25,8 +25,6 @@
#include "game/impl/npc.c" #include "game/impl/npc.c"
#include "game/impl/bandit.c" #include "game/impl/bandit.c"
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
(void) argc; (void) argc;
@@ -64,7 +62,8 @@ int main(int argc, char **argv)
Audio_Context *audio = M_ArenaPush(game->arena, Audio_Context); Audio_Context *audio = M_ArenaPush(game->arena, Audio_Context);
Audio_Init(game->arena, audio, 0.15f); 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); SDL_ResumeAudioStreamDevice(audio->stream);
@@ -84,6 +83,11 @@ int main(int argc, char **argv)
World *world = M_ArenaPush(arena, World); World *world = M_ArenaPush(arena, World);
LoadWorld(arena, world); LoadWorld(arena, world);
game->world = world; game->world = world;
world->audio = audio;
world->audio_handles[0] = bgm;
world->audio_handles[1] = saloon_music;
world->arena = arena; world->arena = arena;
world->random = Random_Seed(29237489723847); world->random = Random_Seed(29237489723847);
world->npcCount = 127; world->npcCount = 127;
@@ -333,7 +337,7 @@ int main(int argc, char **argv)
} }
if(!game->editor.enabled) { 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.x = game->world->player.collision.pos.x;
game->camera.p.y = game->world->player.collision.pos.y; game->camera.p.y = game->world->player.collision.pos.y;

View File

@@ -164,6 +164,16 @@ void PlayerUpdate(F32 delta, Player *player) {
dir = V2f_Scale(NormaliseV2F(dir), PLAYER_SPEED*delta); dir = V2f_Scale(NormaliseV2F(dir), PLAYER_SPEED*delta);
player->collision.pos.x += dir.x; player->collision.pos.x += dir.x;
player->collision.pos.y += dir.y; 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) { void PlayerDraw(D_Context *draw, Player *player) {

View File

@@ -59,6 +59,9 @@ struct World {
U32 propCount; U32 propCount;
U32 hitboxCount; U32 hitboxCount;
Audio_Context *audio;
U32 audio_handles[2];
//// Player //// Player
Player player; Player player;

View File

@@ -13,6 +13,8 @@ struct Audio_Track {
Audio_Data *data; Audio_Data *data;
F32 volume;
U32 n_played; U32 n_played;
U32 next; // to play if playing, free otherwise 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_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_ #endif // LD_OS_AUDIO_H_

View File

@@ -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 U32 off = track->n_played << 1; // played n frames, thus double to n samples
for (U32 f = 0; f < play; ++f) { for (U32 f = 0; f < play; ++f) {
*l0++ += cast(F32) (track->data->samples[off + (2 * f) + 0]) * 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]) * audio->volume; *r0++ += cast(F32) (track->data->samples[off + (2 * f) + 1]) * track->volume * audio->volume;
} }
track->n_played += play; track->n_played += play;
@@ -136,7 +136,6 @@ void Audio_Init(M_Arena *arena, Audio_Context *audio, F32 volume) {
} }
audio->n_tracks = 16; audio->n_tracks = 16;
//audio->tracks = M_ArenaPush(arena, Audio_Track, audio->n_tracks);
for (U32 it = 1; it < audio->n_tracks; ++it) { for (U32 it = 1; it < audio->n_tracks; ++it) {
audio->tracks[it].next = it + 1; 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)) { 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; audio->free = track->next;
track->playing = true; track->playing = true;
track->data = &audio->sounds[index]; track->data = &audio->sounds[index];
track->n_played = 0; track->n_played = 0;
track->volume = 1.0f;
track->next = audio->head; track->next = audio->head;
audio->head = idx; audio->head = result;
SDL_UnlockAudioStream(audio->stream); 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);
}
} }