summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrik@amftech.nl>2023-10-28 12:34:52 +0200
committerAldrik Ramaekers <aldrik@amftech.nl>2023-10-28 12:34:52 +0200
commit5de0682c37fc8e9713bb566a637f19a3795abc4a (patch)
tree3269724b9027205faaa5792262ced3b4f3f064f7 /src
parent06d520eb39b2448ee08dce9010651a423115c798 (diff)
prep for synched audio
Diffstat (limited to 'src')
-rw-r--r--src/audio.c66
-rw-r--r--src/bullets.c6
-rw-r--r--src/game.c9
-rw-r--r--src/throwables.c4
4 files changed, 78 insertions, 7 deletions
diff --git a/src/audio.c b/src/audio.c
index ed25d4c..4a10241 100644
--- a/src/audio.c
+++ b/src/audio.c
@@ -1,5 +1,71 @@
#include "../include/audio.h"
+void add_audio_event_to_queue(audio_event_type event, u32 playerid, vec3f position) {
+ for (int i = 0; i < max_audio_events; i++) {
+ if (audio_events[i].active) continue;
+
+ audio_events[i] = (audio_event){.active = true, .playerid = playerid, .position = position, .type = event};
+ return;
+ }
+}
+
+static int get_channel_from_audio_event_type(audio_event_type event) {
+ switch (event)
+ {
+ case EVENT_BOUNCE_THROWABLE: return CHANNEL_THROWABLES;
+ case EVENT_SHOOT: return CHANNEL_SHOOTING;
+ case EVENT_RELOAD: return CHANNEL_SHOOTING;
+
+ default: return 0;
+ }
+}
+
+static Mix_Chunk* get_sample_from_audio_event_type(audio_event_type event, u32 playerid) {
+ player* p = get_player_by_id(playerid);
+ if (!p) return 0;
+
+ switch (event)
+ {
+ case EVENT_BOUNCE_THROWABLE: return wav_throwable_bounce;
+ case EVENT_SHOOT: {
+ switch (p->guntype)
+ {
+ case GUN_MP5: return wav_shoot_mp5;
+ default:
+ break;
+ }
+ }
+ case EVENT_RELOAD: {
+ switch (p->guntype)
+ {
+ case GUN_MP5: return wav_reload_mp5;
+ default:
+ break;
+ }
+ }
+
+ default:
+ break;
+ }
+}
+
+void play_sounds_in_queue() {
+ for (int i = 0; i < max_audio_events; i++) {
+ if (!audio_events[i].active) continue;
+
+ play_positioned_sound(
+ get_channel_from_audio_event_type(audio_events[i].type),
+ get_sample_from_audio_event_type(audio_events[i].type, audio_events[i].playerid),
+ audio_events[i].position, 20);
+ }
+}
+
+void clear_sounds_in_queue() {
+ for (int i = 0; i < max_audio_events; i++) {
+ audio_events[i].active = false;
+ }
+}
+
void play_sound(int channel, Mix_Chunk* wav) {
Mix_PlayChannel(channel, wav, 0);
}
diff --git a/src/bullets.c b/src/bullets.c
index d1c8d97..f5d5198 100644
--- a/src/bullets.c
+++ b/src/bullets.c
@@ -21,14 +21,14 @@ void shoot(platform_window* window, u32 id, float dirx, float diry) {
if (bullets_to_shoot > p->ammo_in_mag) bullets_to_shoot = p->ammo_in_mag;
p->ammo_in_mag -= bullets_to_shoot;
if (p->ammo_in_mag == 0) {
- play_positioned_sound(CHANNEL_SHOOTING, wav_reload_mp5, (vec3f){.x = p->playerx, .y = p->playery, .z = p->height}, 10);
+ add_audio_event_to_queue(EVENT_RELOAD, p->id, (vec3f){.x = p->playerx, .y = p->playery, .z = p->height});
p->interact_state = INTERACT_RELOADING;
p->sec_since_interact_state_change = 0;
return;
}
- play_positioned_sound(CHANNEL_SHOOTING, wav_shoot_mp5, (vec3f){.x = p->playerx, .y = p->playery, .z = p->height}, 20);
-
+ add_audio_event_to_queue(EVENT_SHOOT, p->id, (vec3f){.x = p->playerx, .y = p->playery, .z = p->height});
+
for (int i = 0; i < bullets_to_shoot; i++)
{
map_info info = get_map_info(window);
diff --git a/src/game.c b/src/game.c
index 06c7df4..993d82e 100644
--- a/src/game.c
+++ b/src/game.c
@@ -190,13 +190,19 @@ void update_server(platform_window* window) {
broadcast_zombies = platform_get_time(TIME_FULL, TIME_NS) - broadcast_zombies;
update_throwables_server(window);
+ clear_throwables();
-
broadcast_stamp = platform_get_time(TIME_FULL, TIME_NS);
broadcast_to_clients(create_protocol_user_list());
broadcast_to_clients(create_protocol_zombie_list());
broadcast_to_clients(create_protocol_bullets_list());
broadcast_to_clients(create_protocol_drop_list());
+
+ // play sounds locally and send them to clients.
+ play_sounds_in_queue();
+
+ clear_sounds_in_queue();
+
update_timer = 0.0f;
}
@@ -279,7 +285,6 @@ void update_client(platform_window* window) {
void update_game(platform_window* window) {
clear_bullets();
- clear_throwables();
if (global_state.server) {
update_server(window);
diff --git a/src/throwables.c b/src/throwables.c
index 33f6137..1ad761f 100644
--- a/src/throwables.c
+++ b/src/throwables.c
@@ -96,13 +96,13 @@ void update_throwables_server(platform_window* window) {
throwables[i].direction.z = -throwables[i].direction.z*0.7;
throwables[i].bounces++;
- play_positioned_sound(CHANNEL_THROWABLES, wav_throwable_bounce, b.position, 8);
+ add_audio_event_to_queue(EVENT_BOUNCE_THROWABLE, b.player_id, b.position);
if (throwables[i].bounces >= 3) throwables[i].direction.z = 0;
}
if (check_if_throwable_collided_with_object(&throwables[i], window, oldpos, throwables[i].position, &throwables[i].direction)) {
- play_positioned_sound(CHANNEL_THROWABLES, wav_throwable_bounce, b.position, 8);
+ add_audio_event_to_queue(EVENT_BOUNCE_THROWABLE, b.player_id, b.position);
}
}
}