Added background music to scene

dev
Shariq Shah 5 years ago
parent 360a768952
commit ed3bba2930
  1. 2
      assets/scenes/scene_1.symtres
  2. BIN
      assets/sounds/scene_background_music_default.ogg
  3. 2
      src/common/version.h
  4. 52
      src/game/editor.c
  5. 62
      src/game/scene.c
  6. 5
      src/game/scene.h
  7. 6
      src/game/sound_source.c
  8. 2
      src/game/sound_source.h
  9. 1
      todo.txt

@ -1,6 +1,7 @@
Scene_Config Scene_Config
{ {
debug_draw_color : 0.800 0.400 0.100 1.000 debug_draw_color : 0.800 0.400 0.100 1.000
background_music_filename : sounds/scene_background_music_default.ogg
fog_type : 1 fog_type : 1
fog_density : 0.1000 fog_density : 0.1000
fog_color : 0.310 0.412 0.529 fog_color : 0.310 0.412 0.529
@ -9,6 +10,7 @@ Scene_Config
fog_max_distance : 450.0000 fog_max_distance : 450.0000
cleanup_func : scene_1_cleanup cleanup_func : scene_1_cleanup
init_func : scene_1_init init_func : scene_1_init
background_music_volume : 0.0100
debug_draw_enabled : false debug_draw_enabled : false
debug_draw_mode : 0 debug_draw_mode : 0
next_scene : test next_scene : test

@ -4,7 +4,7 @@
/* Auto generated version file. DO NOT MODIFY */ /* Auto generated version file. DO NOT MODIFY */
#define SYMMETRY_VERSION_MAJOR 0 #define SYMMETRY_VERSION_MAJOR 0
#define SYMMETRY_VERSION_MINOR 2 #define SYMMETRY_VERSION_MINOR 2
#define SYMMETRY_VERSION_REVISION 358 #define SYMMETRY_VERSION_REVISION 359
#define SYMMETRY_VERSION_BRANCH "dev" #define SYMMETRY_VERSION_BRANCH "dev"
#endif #endif

@ -2196,18 +2196,9 @@ void editor_window_property_inspector(struct nk_context* context, struct Editor*
{ {
if(strncmp(sound_source_filename_buffer, sound_source->source_buffer->filename, MAX_FILENAME_LEN) != 0) if(strncmp(sound_source_filename_buffer, sound_source->source_buffer->filename, MAX_FILENAME_LEN) != 0)
{ {
struct Sound_Source_Buffer* new_source_buffer = sound_source_buffer_create(sound, sound_source_filename_buffer, ST_WAV_STREAM); if(sound_source_buffer_set(sound, sound_source, sound_source_filename_buffer, ST_WAV) && playing)
if(new_source_buffer)
{
sound_source_buffer_stop_all(sound, sound_source->source_buffer);
sound_source_instance_destroy(sound, sound_source->source_instance);
sound_source->source_instance = sound_source_instance_create(sound, new_source_buffer, true);
sound_source->source_buffer = new_source_buffer;
sound_source->base.transform.is_modified = true; // Fake a transformation so that post-update the new sound source position is updated
if(playing)
sound_source_instance_play(sound, sound_source->source_instance); sound_source_instance_play(sound, sound_source->source_instance);
} }
}
sound_source_filename_copied = false; sound_source_filename_copied = false;
nk_contextual_close(context); nk_contextual_close(context);
} }
@ -2628,12 +2619,10 @@ void editor_window_settings_scene(struct nk_context* context, struct Editor* edi
next_scene_filename_copied = true; next_scene_filename_copied = true;
} }
nk_tooltip(context, "Enter the name of the scene file without extension");
int edit_flags = NK_EDIT_GOTO_END_ON_ACTIVATE | NK_EDIT_FIELD | NK_EDIT_SIG_ENTER; int edit_flags = NK_EDIT_GOTO_END_ON_ACTIVATE | NK_EDIT_FIELD | NK_EDIT_SIG_ENTER;
int next_scene_edit_state = nk_edit_string_zero_terminated(context, edit_flags, next_scene_filename_buffer, MAX_FILENAME_LEN, NULL); int next_scene_edit_state = nk_edit_string_zero_terminated(context, edit_flags, next_scene_filename_buffer, MAX_FILENAME_LEN, NULL);
if(next_scene_edit_state & NK_EDIT_COMMITED) if(next_scene_edit_state & NK_EDIT_COMMITED)
{ {
scene_save(scene, scene->filename, DIRT_INSTALL);
strncpy(scene->next_level_filename, next_scene_filename_buffer, MAX_FILENAME_LEN); strncpy(scene->next_level_filename, next_scene_filename_buffer, MAX_FILENAME_LEN);
nk_edit_unfocus(context); nk_edit_unfocus(context);
} }
@ -2656,15 +2645,10 @@ void editor_window_settings_scene(struct nk_context* context, struct Editor* edi
if(scene_init_edit_state & NK_EDIT_COMMITED) if(scene_init_edit_state & NK_EDIT_COMMITED)
{ {
if(scene_init_func_assign(scene, init_func_name_buffer)) if(scene_init_func_assign(scene, init_func_name_buffer))
{
nk_edit_unfocus(context); nk_edit_unfocus(context);
scene_save(scene, scene->filename, DIRT_INSTALL);
}
else else
{
init_func_name_copied = false; init_func_name_copied = false;
} }
}
else if(scene_init_edit_state & NK_EDIT_DEACTIVATED && init_func_name_copied) else if(scene_init_edit_state & NK_EDIT_DEACTIVATED && init_func_name_copied)
{ {
init_func_name_copied = false; init_func_name_copied = false;
@ -2684,20 +2668,44 @@ void editor_window_settings_scene(struct nk_context* context, struct Editor* edi
if(cleanup_func_edit_state & NK_EDIT_COMMITED) if(cleanup_func_edit_state & NK_EDIT_COMMITED)
{ {
if(scene_cleanup_func_assign(scene, cleanup_func_name_buffer)) if(scene_cleanup_func_assign(scene, cleanup_func_name_buffer))
{
scene_save(scene, scene->filename, DIRT_INSTALL);
nk_edit_unfocus(context); nk_edit_unfocus(context);
}
else else
{
cleanup_func_name_copied = false; cleanup_func_name_copied = false;
} }
}
else if(cleanup_func_edit_state & NK_EDIT_DEACTIVATED && cleanup_func_name_copied) else if(cleanup_func_edit_state & NK_EDIT_DEACTIVATED && cleanup_func_name_copied)
{ {
cleanup_func_name_copied = false; cleanup_func_name_copied = false;
} }
/* Background Music */
nk_layout_row_dynamic(context, row_height, 2);
nk_label(context, "Background Music Filename", LABEL_FLAGS_ALIGN_LEFT);
static char background_music_filename_buffer[MAX_FILENAME_LEN];
static bool background_music_filename_copied = false;
if(!background_music_filename_copied)
{
strncpy(background_music_filename_buffer, scene->background_music_buffer->filename, MAX_FILENAME_LEN);
background_music_filename_copied = true;
}
int background_music_filename_edit_state = nk_edit_string_zero_terminated(context, edit_flags, background_music_filename_buffer, MAX_FILENAME_LEN, NULL);
if(background_music_filename_edit_state & NK_EDIT_COMMITED)
{
if(scene_background_music_set(scene, background_music_filename_buffer))
nk_edit_unfocus;
else
background_music_filename_copied = false;
}
else
{
background_music_filename_copied = false;
}
nk_layout_row_dynamic(context, row_height, 1);
float new_volume = scene->background_music_volume;
if((new_volume = nk_propertyf(context, "Music Volume", 0.f, scene->background_music_volume, 1.f, 0.1f, 0.1f)) != scene->background_music_volume)
scene_background_music_volume_set(scene, new_volume);
/* Render Settings */ /* Render Settings */
struct Render_Settings* render_settings = &game_state->renderer->settings; struct Render_Settings* render_settings = &game_state->renderer->settings;
if(nk_tree_push(context, NK_TREE_TAB, "Fog", NK_MAXIMIZED)) if(nk_tree_push(context, NK_TREE_TAB, "Fog", NK_MAXIMIZED))

@ -104,6 +104,9 @@ void scene_init(struct Scene* scene)
editor_camera_init(game_state->editor, game_state->cvars); editor_camera_init(game_state->editor, game_state->cvars);
editor_init_entities(game_state->editor); editor_init_entities(game_state->editor);
scene->background_music_volume = 0.1f;
scene_background_music_set(scene, "sounds/scene_background_music_default.ogg");
if(game_state->game_mode == GAME_MODE_PAUSE) if(game_state->game_mode == GAME_MODE_PAUSE)
game_state->game_mode = GAME_MODE_GAME; game_state->game_mode = GAME_MODE_GAME;
scene->active_camera_index = game_state_get()->game_mode == GAME_MODE_GAME ? CAM_GAME : CAM_EDITOR; scene->active_camera_index = game_state_get()->game_mode == GAME_MODE_GAME ? CAM_GAME : CAM_EDITOR;
@ -113,6 +116,41 @@ void scene_init(struct Scene* scene)
scene_cleanup_func_assign(scene, "scene_func_stub"); scene_cleanup_func_assign(scene, "scene_func_stub");
} }
bool scene_background_music_set(struct Scene* scene, const char* filename)
{
struct Sound* sound = game_state_get()->sound;
scene->background_music_buffer = sound_source_buffer_create(sound, filename, ST_WAV_STREAM);
if(scene->background_music_buffer)
{
scene->background_music_instance = sound_source_instance_create(sound, scene->background_music_buffer, false);
if(scene->background_music_instance != 0)
{
sound_source_instance_volume_set(sound, scene->background_music_instance, scene->background_music_volume);
sound_source_instance_loop_set(sound, scene->background_music_instance, true);
sound_source_instance_play(sound, scene->background_music_instance);
return true;
}
else
{
log_error("scene:background_music_set", "Failed to create instance for scene background music file '%s'", filename);
return false;
}
}
else
{
log_error("scene:background_music_set", "Failed to create buffer from scene background music file '%s'", filename);
return false;
}
}
void scene_background_music_volume_set(struct Scene* scene, float new_volume)
{
struct Sound* sound = game_state_get()->sound;
scene->background_music_volume = fmaxf(0.f, new_volume);
if(sound_source_instance_is_valid(sound, scene->background_music_instance))
sound_source_instance_volume_set(sound, scene->background_music_instance, scene->background_music_volume);
}
bool scene_init_func_assign(struct Scene* scene, const char* init_func_name) bool scene_init_func_assign(struct Scene* scene, const char* init_func_name)
{ {
struct Game_State* game_state = game_state_get(); struct Game_State* game_state = game_state_get();
@ -216,6 +254,22 @@ bool scene_load(struct Scene* scene, const char* filename, int directory_type)
if(hashmap_value_exists(scene_data, "next_scene")) if(hashmap_value_exists(scene_data, "next_scene"))
strncpy(scene->next_level_filename, hashmap_value_exists(scene_data, "next_scene") ? hashmap_str_get(scene_data, "next_scene") : "NONE", MAX_FILENAME_LEN); strncpy(scene->next_level_filename, hashmap_value_exists(scene_data, "next_scene") ? hashmap_str_get(scene_data, "next_scene") : "NONE", MAX_FILENAME_LEN);
if(hashmap_value_exists(scene_data, "background_music_filename"))
{
const char* background_music_filename = hashmap_str_get(scene_data, "background_music_filename");
if(!scene_background_music_set(scene, background_music_filename))
{
log_error("scene:load", "Faield to set scene background music to '%s'. Reverting to default", background_music_filename);
scene_background_music_set(scene, "sounds/scene_background_default_music.ogg");
}
}
if(hashmap_value_exists(scene_data, "background_music_volume"))
{
float new_volume = hashmap_float_get(scene_data, "background_music_volume");
scene_background_music_volume_set(scene, new_volume);
}
num_objects_loaded++; num_objects_loaded++;
} }
break; break;
@ -329,6 +383,8 @@ bool scene_save(struct Scene* scene, const char* filename, int directory_type)
if(scene->init) hashmap_str_set(scene_data, "init_func", scene->init_func_name); if(scene->init) hashmap_str_set(scene_data, "init_func", scene->init_func_name);
if(scene->cleanup) hashmap_str_set(scene_data, "cleanup_func", scene->cleanup_func_name); if(scene->cleanup) hashmap_str_set(scene_data, "cleanup_func", scene->cleanup_func_name);
hashmap_str_set(scene_data, "next_scene", scene->next_level_filename != '\0' ? scene->next_level_filename : "NONE"); hashmap_str_set(scene_data, "next_scene", scene->next_level_filename != '\0' ? scene->next_level_filename : "NONE");
hashmap_str_set(scene_data, "background_music_filename", scene->background_music_buffer->filename);
hashmap_float_set(scene_data, "background_music_volume", scene->background_music_volume);
// Player // Player
struct Parser_Object* player_object = parser_object_new(parser, PO_PLAYER); struct Parser_Object* player_object = parser_object_new(parser, PO_PLAYER);
@ -465,6 +521,12 @@ void scene_destroy(struct Scene* scene)
player_destroy(&scene->player); player_destroy(&scene->player);
entity_reset(&scene->root_entity, 0); entity_reset(&scene->root_entity, 0);
scene->root_entity.flags &= ~EF_ACTIVE; scene->root_entity.flags &= ~EF_ACTIVE;
struct Sound* sound = game_state_get()->sound;
sound_source_instance_destroy(sound, scene->background_music_instance);
sound_source_buffer_destroy(sound, scene->background_music_buffer);
scene->background_music_buffer = NULL;
scene->background_music_instance = -1;
} }
void scene_update(struct Scene* scene, float dt) void scene_update(struct Scene* scene, float dt)

@ -29,6 +29,9 @@ struct Scene
int active_camera_index; int active_camera_index;
char init_func_name[MAX_HASH_KEY_LEN]; char init_func_name[MAX_HASH_KEY_LEN];
char cleanup_func_name[MAX_HASH_KEY_LEN]; char cleanup_func_name[MAX_HASH_KEY_LEN];
struct Sound_Source_Buffer* background_music_buffer;
int background_music_instance;
float background_music_volume;
Scene_Func init; Scene_Func init;
Scene_Func cleanup; Scene_Func cleanup;
}; };
@ -42,6 +45,8 @@ void scene_update_physics(struct Scene* scene, float fixed_dt);
void scene_post_update(struct Scene* scene); void scene_post_update(struct Scene* scene);
bool scene_cleanup_func_assign(struct Scene* scene, const char* cleanup_func_name); bool scene_cleanup_func_assign(struct Scene* scene, const char* cleanup_func_name);
bool scene_init_func_assign(struct Scene* scene, const char* init_func_name); bool scene_init_func_assign(struct Scene* scene, const char* init_func_name);
bool scene_background_music_set(struct Scene* scene, const char* filename);
void scene_background_music_volume_set(struct Scene* scene, float new_volume);
struct Entity* scene_entity_duplicate(struct Scene* scene, struct Entity* entity); struct Entity* scene_entity_duplicate(struct Scene* scene, struct Entity* entity);
struct Entity* scene_entity_create(struct Scene* scene, const char* name, struct Entity* parent); struct Entity* scene_entity_create(struct Scene* scene, const char* name, struct Entity* parent);

@ -56,10 +56,10 @@ void sound_source_update_position(struct Sound* sound, struct Sound_Source* enti
} }
} }
void sound_source_buffer_set(struct Sound* sound, struct Sound_Source* entity, const char* filename, int type) bool sound_source_buffer_set(struct Sound* sound, struct Sound_Source* entity, const char* filename, int type)
{ {
if(strncmp(filename, entity->source_buffer->filename, MAX_FILENAME_LEN) == 0) if(strncmp(filename, entity->source_buffer->filename, MAX_FILENAME_LEN) == 0)
return; return true;
struct Sound_Source_Buffer* new_buffer = sound_source_buffer_create(sound, filename, type); struct Sound_Source_Buffer* new_buffer = sound_source_buffer_create(sound, filename, type);
if(new_buffer) if(new_buffer)
@ -69,10 +69,12 @@ void sound_source_buffer_set(struct Sound* sound, struct Sound_Source* entity, c
entity->type = type; entity->type = type;
entity->source_instance = sound_source_instance_create(sound, entity->source_buffer, true); entity->source_instance = sound_source_instance_create(sound, entity->source_buffer, true);
sound_source_apply_params_to_instance(sound, entity); sound_source_apply_params_to_instance(sound, entity);
return true;
} }
else else
{ {
log_error("sound_source:buffer_set", "Failed to set buffer for %s", entity->base.name); log_error("sound_source:buffer_set", "Failed to set buffer for %s", entity->base.name);
return false;
} }
} }

@ -11,7 +11,7 @@ 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_stop(struct Sound* sound, struct Sound_Source* entity);
bool sound_source_is_paused(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_update_position(struct Sound* sound, struct Sound_Source* entity);
void sound_source_buffer_set(struct Sound* sound, struct Sound_Source* entity, const char* filename, int type); bool 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_validate_instance(struct Sound* sound, struct Sound_Source* entity);
void sound_source_apply_params_to_instance(struct Sound* sound, struct Sound_Source* entity); void sound_source_apply_params_to_instance(struct Sound* sound, struct Sound_Source* entity);

@ -5,6 +5,7 @@ Todo:
- Disbale all player actions when scene cleared dialog or scene restart dialog are active - Disbale all player actions when scene cleared dialog or scene restart dialog are active
- Enemies getting hit by bullets - Enemies getting hit by bullets
- Memory utils that provide allocation tracking - Memory utils that provide allocation tracking
- Background music track per scene specified in scene properties
- Remove excessive repitition in scene and editor code that handles multiple entity types - Remove excessive repitition in scene and editor code that handles multiple entity types
- Allow switching to editor mode when game is in pause mode - Allow switching to editor mode when game is in pause mode
- Rendering Additions: - Rendering Additions:

Loading…
Cancel
Save