VERY hacky positional audio
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
12
code/first.c
12
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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -59,6 +59,9 @@ struct World {
|
||||
U32 propCount;
|
||||
U32 hitboxCount;
|
||||
|
||||
Audio_Context *audio;
|
||||
U32 audio_handles[2];
|
||||
|
||||
//// Player
|
||||
Player player;
|
||||
|
||||
|
||||
@@ -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_
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user