Skip to content

Commit

Permalink
Add Background music
Browse files Browse the repository at this point in the history
  • Loading branch information
networkfusion committed Oct 14, 2023
1 parent 7e5418a commit 0f2fe6b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/menu/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,31 @@ static void menu_init (boot_params_t *boot_params) {

component_background_init(BACKGROUND_CACHE);

if(menu->settings.bgm_enabled && file_exists(menu->settings.bgm_file_path)) {
// FIXME: Note that this needs to be paused if playing an MP3 from the player!!!
// FIXME: Loop the playback if it ends.
// Start playback.
mp3player_err_t err;

err = mp3player_init();
if (err != MP3PLAYER_OK) {
mp3player_deinit();
}
else {
err = mp3player_load(menu->settings.bgm_file_path);
if (err != MP3PLAYER_OK) {
mp3player_deinit();
} else {
sound_init_mp3_playback();
mp3player_mute(false);
err = mp3player_play();
if (err != MP3PLAYER_OK) {
mp3player_deinit();
}
}
}
}

menu->boot_params = boot_params;

bool default_directory_exists = directory_exists(menu->settings.default_directory);
Expand Down
9 changes: 6 additions & 3 deletions src/menu/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ static settings_t init = {
.hidden_files_enabled = false,
.default_directory = "/",
.use_saves_folder = true,
.bgm_enabled = false,
.bgm_file_path = "",

/* Beta feature flags */
.bgm_enabled = false,
.sound_enabled = false,
.rumble_enabled = true,
};
Expand All @@ -32,9 +33,10 @@ void settings_load (settings_t *settings) {
settings->hidden_files_enabled = mini_get_bool(ini, "menu", "show_hidden_files", init.hidden_files_enabled);
settings->default_directory = strdup(mini_get_string(ini, "menu", "default_directory", init.default_directory));
settings->use_saves_folder = mini_get_bool(ini, "menu", "use_saves_folder", init.use_saves_folder);
settings->bgm_enabled = mini_get_bool(ini, "menu", "bgm_enabled", init.bgm_enabled);
settings->bgm_file_path = strdup(mini_get_string(ini, "menu", "bgm_file_path", init.bgm_file_path));

/* Beta feature flags, they might not be in the file */
settings->bgm_enabled = mini_get_bool(ini, "menu_beta_flag", "bgm_enabled", init.bgm_enabled);
settings->sound_enabled = mini_get_bool(ini, "menu_beta_flag", "sound_enabled", init.sound_enabled);
settings->rumble_enabled = mini_get_bool(ini, "menu_beta_flag", "rumble_enabled", init.rumble_enabled);

Expand All @@ -49,9 +51,10 @@ void settings_save (settings_t *settings) {
mini_set_bool(ini, "menu", "show_hidden_files", settings->hidden_files_enabled);
mini_set_string(ini, "menu", "default_directory", settings->default_directory);
mini_set_bool(ini, "menu", "use_saves_folder", settings->use_saves_folder);
mini_set_bool(ini, "menu", "bgm_enabled", init.bgm_enabled);
mini_set_string(ini, "menu", "bgm_file_path", settings->bgm_file_path);

/* Beta feature flags, they should not save until production ready! */
// mini_set_bool(ini, "menu_beta_flag", "bgm_enabled", init.bgm_enabled);
// mini_set_bool(ini, "menu_beta_flag", "sound_enabled", init.sound_enabled);
// mini_set_bool(ini, "menu_beta_flag", "rumble_enabled", init.rumble_enabled);

Expand Down
3 changes: 3 additions & 0 deletions src/menu/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ typedef struct {
/** @brief Enable Background music */
bool bgm_enabled;

/** @brief Background music file path */
char *bgm_file_path;

/** @brief Enable Sounds */
bool sound_enabled;

Expand Down
15 changes: 15 additions & 0 deletions src/menu/views/browser.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,26 @@ static void set_default_directory (menu_t *menu) {
settings_save(&menu->settings);
}

static void set_bgm_file (menu_t *menu) {
if (file_has_extensions(menu->browser.entry->name, music_extensions)) {
path_t *path = path_clone_push(menu->browser.directory, menu->browser.entry->name);
free(menu->settings.bgm_file_path);
menu->settings.bgm_file_path = strdup(strip_sd_prefix(path_get(path)));
menu->settings.bgm_enabled = true;
settings_save(&menu->settings);
menu_show_error(menu, "Reboot N64 to take effect!");
}
else {
menu_show_error(menu, "Couldn't use this file!");
}
}

static component_context_menu_t entry_context_menu = {
.list = {
{ .text = "Show entry properties", .action = show_properties },
{ .text = "Delete selected entry", .action = delete_entry },
{ .text = "Set current directory as default", .action = set_default_directory },
{ .text = "Set current file as BGM", .action = set_bgm_file },
COMPONENT_CONTEXT_MENU_LIST_END,
}
};
Expand Down

0 comments on commit 0f2fe6b

Please sign in to comment.