From ee13448c4a2b5e798f2474e8703fafd7bb919179 Mon Sep 17 00:00:00 2001 From: Shariq Shah Date: Wed, 5 Feb 2020 15:31:13 +1100 Subject: [PATCH] Re-implemented parts of sound source property inspector --- assets/entities/turret.symtres | 4 +- src/common/version.h | 2 +- src/game/editor.c | 69 ++++++++++++++++++++++++++++------ src/game/enemy.c | 2 +- src/game/entity.c | 7 ++-- src/game/entity.h | 1 - src/game/scene.c | 5 +-- src/game/sound_source.c | 9 +++-- src/game/sound_source.h | 7 +++- todo.txt | 4 +- 10 files changed, 82 insertions(+), 28 deletions(-) diff --git a/assets/entities/turret.symtres b/assets/entities/turret.symtres index b93205c..f2a7a74 100644 --- a/assets/entities/turret.symtres +++ b/assets/entities/turret.symtres @@ -35,7 +35,7 @@ Entity loop : false sound_min_distance : 0.0000 active : true - playing : false + paused : true position : 0.000 0.000 0.000 bouding_box_min : -0.500 -0.500 -0.500 source_filename : sounds/bullet_1.wav @@ -56,7 +56,7 @@ Entity loop : true sound_min_distance : 0.0000 active : true - playing : true + paused : false position : 0.000 0.000 0.000 bouding_box_min : -0.500 -0.500 -0.500 source_filename : sounds/windy_ambience.ogg diff --git a/src/common/version.h b/src/common/version.h index b1c6de8..3eb9ea1 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 325 +#define SYMMETRY_VERSION_REVISION 326 #define SYMMETRY_VERSION_BRANCH "dev" #endif \ No newline at end of file diff --git a/src/game/editor.c b/src/game/editor.c index ab09517..e95eaf0 100755 --- a/src/game/editor.c +++ b/src/game/editor.c @@ -32,6 +32,7 @@ #include "console.h" #include "debug_vars.h" #include "../common/version.h" +#include "sound_source.h" #include #include @@ -243,6 +244,15 @@ void editor_render(struct Editor* editor, struct Camera * active_camera) } } break; + case ET_SOUND_SOURCE: + { + struct Sound_Source* sound_source = (struct Sound_Source*)editor->selected_entity; + quat rot = { 0.f, 0.f, 0.f, 1.f }; + quat_axis_angle(&rot, &UNIT_X, 90.f); + im_circle(sound_source->min_distance, 32, false, abs_pos, rot, editor->selected_entity_color, 5); + im_circle(sound_source->max_distance, 32, false, abs_pos, rot, editor->selected_entity_color, 5); + } + break; } /* Draw bounding box for selected entity */ @@ -2029,28 +2039,65 @@ void editor_window_property_inspector(struct nk_context* context, struct Editor* { struct Sound* sound = game_state->sound; struct Sound_Source* sound_source = (struct Sound_Source*)entity; + bool sound_params_modified = false; + if(nk_tree_push(context, NK_TREE_TAB, "Sound Source", NK_MAXIMIZED)) { nk_layout_row_dynamic(context, row_height, 2); - nk_label(context, "Playing", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE); - int is_playing = sound_source_instance_is_paused(sound, sound_source->source_instance); + nk_label(context, "Playing", LABEL_FLAGS_ALIGN_LEFT); + int is_playing = !sound_source_is_paused(sound, sound_source); int playing = nk_check_label(context, "", is_playing); if(is_playing && !playing) - sound_source_instance_pause(sound, sound_source->source_instance); + sound_source_pause(sound, sound_source); else if(!is_playing && playing) - sound_source_instance_play(sound, sound_source->source_instance); - + sound_source_play(sound, sound_source); - nk_label(context, "Loop", NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE); - int is_looping = sound_source_instance_loop_get(sound, sound_source->source_instance); + nk_label(context, "Loop", LABEL_FLAGS_ALIGN_LEFT); + int is_looping = sound_source->loop; int looping = nk_check_label(context, "", is_looping); if(is_looping != looping) - sound_source_instance_loop_set(sound, sound_source->source_instance, looping); + { + sound_source->loop = (bool)looping; + sound_params_modified = true; + } nk_layout_row_dynamic(context, row_height, 1); - float volume = sound_source_instance_volume_get(sound, sound_source->source_instance); - volume = nk_propertyf(context, "Volume", 0.f, volume, 10.f, 0.5f, 0.1f); - sound_source_instance_volume_set(sound, sound_source->source_instance, volume); + float volume = nk_propertyf(context, "Volume", 0.f, sound_source->volume, 100.f, 0.5f, 0.1f); + if(volume != sound_source->volume) + { + sound_source->volume = volume; + sound_params_modified = true; + } + + float min_distance = nk_propertyf(context, "Min Distance", 0.f, sound_source->min_distance, sound_source->max_distance, 0.5f, 0.1f); + if(min_distance != sound_source->min_distance) + { + sound_source->min_distance = min_distance; + sound_params_modified = true; + } + + float max_distance = nk_propertyf(context, "Max Distance", sound_source->min_distance, sound_source->max_distance, FLT_MAX, 0.5f, 0.1f); + if(max_distance != sound_source->max_distance) + { + sound_source->max_distance = max_distance; + sound_params_modified = true; + } + + float combo_width = nk_widget_width(context), combo_height = row_height * 4; + nk_layout_row_dynamic(context, row_height, 2); + nk_label(context, "Attenuation", LABEL_FLAGS_ALIGN_LEFT); + int new_attenuation = nk_combo_string(context, "None\0Inverse\0Linear\0Exponential", sound_source->attenuation_type, 4, row_height, nk_vec2(combo_width, combo_height)); + if(new_attenuation != sound_source->attenuation_type) + { + sound_source->attenuation_type = new_attenuation; + sound_params_modified = true; + } + + if(sound_params_modified) + { + sound_source_validate_instance(sound, sound_source); + sound_source_apply_params_to_instance(sound, sound_source); + } nk_layout_row_dynamic(context, row_height, 2); static char sound_source_filename_buffer[MAX_FILENAME_LEN]; diff --git a/src/game/enemy.c b/src/game/enemy.c index 96c64a3..2d86d07 100644 --- a/src/game/enemy.c +++ b/src/game/enemy.c @@ -66,7 +66,7 @@ void enemy_init(struct Enemy* enemy, int type) 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); + sound_source_buffer_set(game_state_get()->sound, enemy->weapon_sound, sound_filename, type); } void enemy_static_mesh_set(struct Enemy* enemy, const char* geometry_filename, int material_type) diff --git a/src/game/entity.c b/src/game/entity.c index 6ab4bb4..09e1a86 100755 --- a/src/game/entity.c +++ b/src/game/entity.c @@ -20,6 +20,7 @@ #include "texture.h" #include "enemy.h" #include "event.h" +#include "sound_source.h" #include #include @@ -254,7 +255,7 @@ bool entity_write(struct Entity* entity, struct Parser_Object* object, bool writ { struct Sound_Source* sound_source = (struct Sound_Source*)entity; hashmap_str_set(entity_data, "source_filename", sound_source->source_buffer->filename); - hashmap_bool_set(entity_data, "playing", sound_source->playing); + hashmap_bool_set(entity_data, "paused", sound_source_is_paused(game_state_get()->sound, sound_source)); hashmap_int_set(entity_data, "sound_type", sound_source->type); hashmap_bool_set(entity_data, "loop", sound_source->loop); hashmap_float_set(entity_data, "volume", sound_source->volume); @@ -420,7 +421,6 @@ struct Entity* entity_read(struct Parser_Object* object, struct Entity* parent_e struct Sound_Source_Buffer* default_source_buffer = sound_source->source_buffer; uint default_source_instance = sound_source->source_instance; - if(hashmap_value_exists(object->data, "playing")) sound_source->playing = hashmap_bool_get(object->data, "playing"); if(hashmap_value_exists(object->data, "loop")) sound_source->loop = hashmap_bool_get(object->data, "loop"); if(hashmap_value_exists(object->data, "sound_min_distance")) sound_source->min_distance = hashmap_float_get(object->data, "sound_min_distance"); if(hashmap_value_exists(object->data, "sound_max_distance")) sound_source->max_distance = hashmap_float_get(object->data, "sound_max_distance"); @@ -447,7 +447,8 @@ struct Entity* entity_read(struct Parser_Object* object, struct Entity* parent_e sound_source_instance_volume_set(sound, sound_source->source_instance, sound_source->volume); sound_update_3d(sound); - if(sound_source->playing) + bool paused = hashmap_value_exists(object->data, "paused") ? hashmap_bool_get(object->data, "paused") : false; + if(!paused) sound_source_instance_play(sound, sound_source->source_instance); //Stop the default sound source from playing now that we have loaded the actual buffer diff --git a/src/game/entity.h b/src/game/entity.h index beb43b8..6cf8633 100755 --- a/src/game/entity.h +++ b/src/game/entity.h @@ -112,7 +112,6 @@ struct Sound_Source { struct Entity base; int type; - bool playing; bool loop; uint source_instance; float min_distance; diff --git a/src/game/scene.c b/src/game/scene.c index 75aa26f..40ff4c0 100755 --- a/src/game/scene.c +++ b/src/game/scene.c @@ -655,7 +655,6 @@ struct Sound_Source* scene_sound_source_create(struct Scene* scene, const char* new_sound_source->loop = loop; new_sound_source->min_distance = 0.f; new_sound_source->max_distance = 10.f; - new_sound_source->playing = play; new_sound_source->attenuation_type = SA_LINEAR; new_sound_source->rolloff_factor = 0.95f; new_sound_source->volume = 1.f; @@ -667,7 +666,7 @@ struct Sound_Source* scene_sound_source_create(struct Scene* scene, const char* sound_source_instance_volume_set(sound, new_sound_source->source_instance, new_sound_source->volume); sound_update_3d(sound); - if(new_sound_source->playing) sound_source_instance_play(sound, new_sound_source->source_instance); + if(play) sound_source_instance_play(sound, new_sound_source->source_instance); } else { @@ -1178,7 +1177,7 @@ struct Entity* scene_entity_duplicate(struct Scene* scene, struct Entity* entity case ET_SOUND_SOURCE: { struct Sound_Source* sound_source = (struct Sound_Source*)entity; - struct Sound_Source* new_sound_source = scene_sound_source_create(scene, entity->name, entity->transform.parent, sound_source->source_buffer->filename, sound_source->type, sound_source->loop, sound_source->playing); + struct Sound_Source* new_sound_source = scene_sound_source_create(scene, entity->name, entity->transform.parent, sound_source->source_buffer->filename, sound_source->type, sound_source->loop, !sound_source_is_paused(game_state_get()->sound, sound_source)); if(!new_sound_source) return new_entity; new_sound_source->min_distance = sound_source->min_distance; diff --git a/src/game/sound_source.c b/src/game/sound_source.c index 76d1c2b..7457a8b 100644 --- a/src/game/sound_source.c +++ b/src/game/sound_source.c @@ -4,9 +4,6 @@ #include "transform.h" #include "../common/log.h" -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)) @@ -78,3 +75,9 @@ void sound_source_buffer_set(struct Sound* sound, struct Sound_Source* entity, c } } } + +bool sound_source_is_paused(struct Sound* sound, struct Sound_Source* entity) +{ + sound_source_validate_instance(sound, entity); + return sound_source_instance_is_paused(sound, entity->source_instance); +} diff --git a/src/game/sound_source.h b/src/game/sound_source.h index 99e3c25..038cedf 100644 --- a/src/game/sound_source.h +++ b/src/game/sound_source.h @@ -1,13 +1,18 @@ #ifndef SOUND_SOURCE_H #define SOUND_SOURCE_H +#include + struct Sound_Source; 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); +bool sound_source_is_paused(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); +void sound_source_buffer_set(struct Sound* sound, struct Sound_Source* entity, const char* filename, int type); +void sound_source_validate_instance(struct Sound* sound, struct Sound_Source* entity); +void sound_source_apply_params_to_instance(struct Sound* sound, struct Sound_Source* entity); #endif \ No newline at end of file diff --git a/todo.txt b/todo.txt index 2b5f3d8..76e135e 100644 --- a/todo.txt +++ b/todo.txt @@ -5,7 +5,6 @@ Todo: - Level-wide events and responses - Main menu as a level or just have a pause menu where game's options etc can be changed? - Implement separate property window for player related variables that can be shown in the editor similar to renderer settings etc - - Add all sound source properties to propery inspector - Fix rotate gizmo's origin not being set to the selected entity - Player shooting - Player jump cooldown, don't allow jump until a certian time interval has passed, even if we're grounded @@ -402,4 +401,5 @@ Done: * Implement turret state machine * Fix Turret losing target at diagonals * Add turret properties to property inspector - * Add another ambient sound_source entity as child to enemy entity \ No newline at end of file + * Add another ambient sound_source entity as child to enemy entity + * Add all sound source properties to propery inspector \ No newline at end of file