diff options
| author | Aldrik Ramaekers <aldrik@amftech.nl> | 2023-10-28 12:34:52 +0200 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrik@amftech.nl> | 2023-10-28 12:34:52 +0200 |
| commit | 5de0682c37fc8e9713bb566a637f19a3795abc4a (patch) | |
| tree | 3269724b9027205faaa5792262ced3b4f3f064f7 | |
| parent | 06d520eb39b2448ee08dce9010651a423115c798 (diff) | |
prep for synched audio
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | build/zombies.exe | bin | 1976257 -> 1978084 bytes | |||
| -rw-r--r-- | include/audio.h | 21 | ||||
| -rw-r--r-- | src/audio.c | 66 | ||||
| -rw-r--r-- | src/bullets.c | 6 | ||||
| -rw-r--r-- | src/game.c | 9 | ||||
| -rw-r--r-- | src/throwables.c | 4 |
7 files changed, 99 insertions, 9 deletions
@@ -3,6 +3,6 @@ main: mkdir -p "build/" cp -a "data/." "build/data" gcc -m64 -g -DMODE_DEBUG main.c -o build/zombies.exe -lSDL2_mixer -lSDL2 - # cp -a "build/." "C:/Manually installed programs/zombieshooter" + cp -a "build/." "C:/Manually installed programs/zombieshooter" ./build/zombies.exe -ip 127.0.0.1 -port 27015 diff --git a/build/zombies.exe b/build/zombies.exe Binary files differindex d3279d9..25fab8c 100644 --- a/build/zombies.exe +++ b/build/zombies.exe diff --git a/include/audio.h b/include/audio.h index 850e74e..92d460c 100644 --- a/include/audio.h +++ b/include/audio.h @@ -4,8 +4,27 @@ #include <projectbase/project_base.h> #define CHANNEL_THROWABLES 0 -#define CHANNEL_SHOOTING 0 +#define CHANNEL_SHOOTING 1 +typedef enum t_audio_event_type { + EVENT_SHOOT, + EVENT_RELOAD, + EVENT_BOUNCE_THROWABLE, +} audio_event_type; + +typedef struct t_audio_event { + bool active; + audio_event_type type; + u32 playerid; + vec3f position; +} audio_event; + +audio_event audio_events[20] = {0}; +int max_audio_events = 20; + +void add_audio_event_to_queue(audio_event_type event, u32 playerid, vec3f position); +void play_sounds_in_queue(); +void clear_sounds_in_queue(); void play_sound(int channel, Mix_Chunk* wav); void play_positioned_sound(int channel, Mix_Chunk* wav, vec3f pos, float max_audible_dist); 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); @@ -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); } } } |
