From ddfbbbfa2d0ed84d60edd71c88987cb75de5c960 Mon Sep 17 00:00:00 2001 From: Shariq Shah Date: Mon, 20 Jan 2020 15:50:14 +1100 Subject: [PATCH] Sound source properties are now applied to each newly created instance --- src/common/version.h | 2 +- src/game/enemy.c | 31 +++++++++++++++++++++++----- src/game/enemy.h | 2 ++ src/game/entity.h | 2 +- src/game/scene.c | 2 +- src/game/sound_source.c | 45 ++++++++++++++++++++++++++++++++++++----- src/game/sound_source.h | 3 ++- src/system/sound.c | 4 ++-- src/system/sound.h | 2 +- todo.txt | 4 ++-- 10 files changed, 78 insertions(+), 19 deletions(-) diff --git a/src/common/version.h b/src/common/version.h index 62793b2..b0f6c1d 100755 --- a/src/common/version.h +++ b/src/common/version.h @@ -4,7 +4,7 @@ /* Auto generated version file. DO NOT MODIFY */ #define SYMMETRY_VERSION_MAJOR 0 #define SYMMETRY_VERSION_MINOR 1 -#define SYMMETRY_VERSION_REVISION 314 +#define SYMMETRY_VERSION_REVISION 315 #define SYMMETRY_VERSION_BRANCH "dev" #endif \ No newline at end of file diff --git a/src/game/enemy.c b/src/game/enemy.c index 9d81088..ea0ff96 100644 --- a/src/game/enemy.c +++ b/src/game/enemy.c @@ -53,8 +53,28 @@ void enemy_init(struct Enemy* enemy, int type) if(!mesh) log_error("enemy:init", "Failed to add mesh from file for %s", enemy->base.name); - enemy->mesh->base.flags |= EF_TRANSIENT; - enemy->weapon_sound->base.flags |= EF_TRANSIENT; + if(enemy->mesh) enemy->mesh->base.flags |= EF_TRANSIENT; + if(enemy->weapon_sound) enemy->weapon_sound->base.flags |= EF_TRANSIENT; +} + +void enemy_weapon_sound_set(struct Enemy* enemy, const char* sound_filename, int type) +{ + sound_source_buffer_set(game_state_get()->sound, sound_filename, type); +} + +void enemy_static_mesh_set(struct Enemy* enemy, const char* geometry_filename, int material_type) +{ + struct Scene* scene = game_state_get()->scene; + char mesh_name[MAX_ENTITY_NAME_LEN]; + memset(mesh_name, '\0', sizeof(char) * MAX_ENTITY_NAME_LEN); + snprintf(mesh_name, MAX_ENTITY_NAME_LEN, "%s_Mesh", enemy->base.name); + + struct Static_Mesh* new_mesh = scene_static_mesh_create(scene, mesh_name, enemy, geometry_filename, material_type); + if(new_mesh) + { + if(enemy->mesh) scene_static_mesh_remove(scene, enemy->mesh); + enemy->mesh = new_mesh; + } } void enemy_update(struct Enemy* enemy, struct Scene* scene, float dt) @@ -75,8 +95,9 @@ void enemy_update(struct Enemy* enemy, struct Scene* scene, float dt) void enemy_reset(struct Enemy* enemy) { - entity_reset(enemy, enemy->base.id); - enemy->base.flags = EF_NONE; + enemy->type = -1; + enemy->damage = 0; + enemy->health = 0; } struct Enemy* enemy_read(struct Parser_Object* object, const char* name, struct Entity* parent_entity) @@ -89,7 +110,7 @@ struct Enemy* enemy_read(struct Parser_Object* object, const char* name, struct if(enemy_type != -1) { - new_enemy = scene_enemy_create(scene, name, parent_entity, enemy_type); + new_enemy = scene_enemy_create(scene, name, parent_entity, enemy_type); // Create enemy with default values then read and update from file if necessary if(!new_enemy) return new_enemy; if(hashmap_value_exists(object->data, "health")) new_enemy->health = hashmap_int_get(object->data, "health"); diff --git a/src/game/enemy.h b/src/game/enemy.h index d54083e..725ff92 100644 --- a/src/game/enemy.h +++ b/src/game/enemy.h @@ -12,5 +12,7 @@ void enemy_update(struct Enemy* enemy, struct Scene* scene, float dt); void enemy_reset(struct Enemy* enemy); struct Enemy* enemy_read(struct Parser_Object* object, const char* name, struct Entity* parent_entity); void enemy_write(struct Enemy* enemy, struct Hashmap* entity_data); +void enemy_weapon_sound_set(struct Enemy* enemy, const char* sound_filename, int type); +void enemy_static_mesh_set(struct Enemy* enemy, const char* geometry_filename, int material_type); #endif \ No newline at end of file diff --git a/src/game/entity.h b/src/game/entity.h index 36d2923..e426eaa 100755 --- a/src/game/entity.h +++ b/src/game/entity.h @@ -59,7 +59,7 @@ enum Entity_Flags EF_ACTIVE = 1 << 0, EF_SELECTED_IN_EDITOR = 1 << 1, EF_MARKED_FOR_DELETION = 1 << 2, - EF_TRANSIENT = 1 << 3, + EF_TRANSIENT = 1 << 3, // Do not save the entity when saving the scene. The entity will still be saved if it is individually saved to file EF_HIDE_IN_EDITOR_SCENE_HIERARCHY = 1 << 4, EF_SKIP_RENDER = 1 << 5, EF_IGNORE_RAYCAST = 1 << 6 diff --git a/src/game/scene.c b/src/game/scene.c index bd851e5..8591473 100755 --- a/src/game/scene.c +++ b/src/game/scene.c @@ -413,7 +413,7 @@ void scene_post_update(struct Scene* scene) if(sound_source->base.transform.is_modified) { - sound_source_update(sound, sound_source); + sound_source_update_position(sound, sound_source); sound_source->base.transform.is_modified = false; } } diff --git a/src/game/sound_source.c b/src/game/sound_source.c index 1e1a5a1..76d1c2b 100644 --- a/src/game/sound_source.c +++ b/src/game/sound_source.c @@ -4,17 +4,30 @@ #include "transform.h" #include "../common/log.h" -static void sound_source_validate_instance(struct Sound* sound, struct Sound_Source* entity) +static void sound_source_validate_instance(struct Sound* sound, struct Sound_Source* entity); +static void sound_source_apply_params_to_instance(struct Sound* sound, struct Sound_Source* entity); + +void sound_source_validate_instance(struct Sound* sound, struct Sound_Source* entity) { if(!sound_source_instance_is_valid(sound, entity->source_instance)) { entity->source_instance = sound_source_instance_create(sound, entity->source_buffer, true); - vec3 abs_position = { 0.f }; - transform_get_absolute_position(entity, &abs_position); - sound_source_instance_update_position(sound, entity->source_instance, abs_position); + sound_source_apply_params_to_instance(sound, entity); } } +void sound_source_apply_params_to_instance(struct Sound* sound, struct Sound_Source* entity) +{ + // This function assumes that the handle to the sound source is valid + vec3 abs_position = { 0.f }; + transform_get_absolute_position(entity, &abs_position); + sound_source_instance_update_position(sound, entity->source_instance, abs_position); + sound_source_instance_loop_set(sound, entity->source_instance, entity->loop); + sound_source_instance_min_max_distance_set(sound, entity->source_instance, entity->min_distance, entity->max_distance); + sound_source_instance_attenuation_set(sound, entity->source_instance, entity->attenuation_type, entity->rolloff_factor); + sound_source_instance_volume_set(sound, entity->source_instance, entity->volume); +} + void sound_source_play(struct Sound* sound, struct Sound_Source* entity) { sound_source_validate_instance(sound, entity); @@ -34,7 +47,7 @@ void sound_source_stop(struct Sound* sound, struct Sound_Source* entity) sound_source_instance_stop(sound, entity->source_instance); } -void sound_source_update(struct Sound* sound, struct Sound_Source* entity) +void sound_source_update_position(struct Sound* sound, struct Sound_Source* entity) { if(sound_source_instance_is_valid(sound, entity->source_instance)) { @@ -43,3 +56,25 @@ void sound_source_update(struct Sound* sound, struct Sound_Source* entity) sound_source_instance_update_position(sound, entity->source_instance, abs_position); } } + +void sound_source_buffer_set(struct Sound* sound, struct Sound_Source* entity, const char* filename, int type) +{ + if(entity->source_buffer) + { + struct Sound_Source_Buffer* new_buffer = sound_source_buffer_create(sound, filename, type); + if(new_buffer) + { + sound_source_instance_destroy(sound, entity->source_instance); + sound_source_buffer_destroy(sound, entity->source_buffer); + + entity->source_buffer = new_buffer; + entity->type = type; + entity->source_instance = sound_source_instance_create(sound, entity->source_buffer, true); + sound_source_apply_params_to_instance(sound, entity); + } + else + { + log_error("sound_source:buffer_set", "Failed to set buffer for %s", entity->base.name); + } + } +} diff --git a/src/game/sound_source.h b/src/game/sound_source.h index a0e7a27..99e3c25 100644 --- a/src/game/sound_source.h +++ b/src/game/sound_source.h @@ -7,6 +7,7 @@ struct Sound; void sound_source_play(struct Sound* sound, struct Sound_Source* entity); void sound_source_pause(struct Sound* sound, struct Sound_Source* entity); void sound_source_stop(struct Sound* sound, struct Sound_Source* entity); -void sound_source_update(struct Sound* sound, struct Sound_Source* entity); +void sound_source_update_position(struct Sound* sound, struct Sound_Source* entity); +void sound_source_buffer_set(struct Sound* sound, struct Sound_Source* entity); #endif \ No newline at end of file diff --git a/src/system/sound.c b/src/system/sound.c index eca9ab1..306f40a 100755 --- a/src/system/sound.c +++ b/src/system/sound.c @@ -195,7 +195,7 @@ struct Sound_Source_Buffer* sound_source_buffer_create(struct Sound* sound, cons if(!filename) return NULL; - struct Sound_Source_Buffer* source = sound_source_buffer(sound, filename); + struct Sound_Source_Buffer* source = sound_source_buffer_get(sound, filename); // See if we've already loaded this file otherwise, get the next empty slot. // If we can't find an empty slot, print error and return NULL @@ -269,7 +269,7 @@ struct Sound_Source_Buffer* sound_source_buffer_create(struct Sound* sound, cons return source; } -struct Sound_Source_Buffer* sound_source_buffer(struct Sound* sound, const char* name) +struct Sound_Source_Buffer* sound_source_buffer_get(struct Sound* sound, const char* name) { struct Sound_Source_Buffer* source = NULL; for(int i = 0; i < MAX_SOUND_BUFFERS; i++) diff --git a/src/system/sound.h b/src/system/sound.h index 2116e76..fc5ca74 100755 --- a/src/system/sound.h +++ b/src/system/sound.h @@ -70,7 +70,7 @@ bool sound_source_instance_loop_get(struct Sound* sound, uint source_instance); bool sound_source_instance_is_paused(struct Sound* sound, uint source_instance); struct Sound_Source_Buffer* sound_source_buffer_create(struct Sound* sound, const char* filename, int type); -struct Sound_Source_Buffer* sound_source_buffer(struct Sound* sound, const char* name); +struct Sound_Source_Buffer* sound_source_buffer_get(struct Sound* sound, const char* name); int sound_source_buffer_play_3d(struct Sound* sound, struct Sound_Source_Buffer* source, vec3 position); int sound_source_buffer_play_clocked_3d(struct Sound* sound, struct Sound_Source_Buffer* source, float delay, vec3 position); void sound_source_buffer_destroy(struct Sound* sound, struct Sound_Source_Buffer* source); diff --git a/todo.txt b/todo.txt index 0f02bb1..f83bb52 100644 --- a/todo.txt +++ b/todo.txt @@ -1,6 +1,5 @@ Todo: - Imlement reading/writing enemy mesh and weapon sound to file and resetting it in code - - Apply sound source properties to source instance whenever a new instance is created - Enemy ray casting and shooting - Player shooting - Player jump cooldown, don't allow jump until a certian time interval has passed, even if we're grounded @@ -401,4 +400,5 @@ Done: * Brought back sprinting by fixing a bug where player movement related variables were written to file as floats but read back as ints * Move player movement related variables from function to player struct and load them from config file * Screen mouse coordinates to world-coordinates for aiming - * Sound source entity functions that automatically track if handles are valid and create/update as necessary \ No newline at end of file + * Sound source entity functions that automatically track if handles are valid and create/update as necessary + * Apply sound source properties to source instance whenever a new instance is created \ No newline at end of file