diff --git a/include/draw.h b/include/draw.h index 888e51e..1845deb 100644 --- a/include/draw.h +++ b/include/draw.h @@ -51,6 +51,7 @@ typedef enum { INSTALL_LOADING_REMOTE_THEMES, INSTALL_LOADING_REMOTE_SPLASHES, INSTALL_LOADING_REMOTE_PREVIEW, + INSTALL_LOADING_REMOTE_BGM, INSTALL_NONE, } InstallType; diff --git a/source/draw.c b/source/draw.c index e072e93..49a3105 100644 --- a/source/draw.c +++ b/source/draw.c @@ -204,6 +204,9 @@ void draw_install(InstallType type) case INSTALL_LOADING_REMOTE_PREVIEW: pp2d_draw_text_center(GFX_TOP, 120, 0.8, 0.8, COLOR_WHITE, "Downloading preview, please wait..."); break; + case INSTALL_LOADING_REMOTE_BGM: + pp2d_draw_text_center(GFX_TOP, 120, 0.8, 0.8, COLOR_WHITE, "Downloading BGM, please wait..."); + break; case INSTALL_SINGLE: pp2d_draw_text_center(GFX_TOP, 120, 0.8, 0.8, COLOR_WHITE, "Installing a single theme..."); break; diff --git a/source/loading.c b/source/loading.c index 69ca8fb..e131b5c 100644 --- a/source/loading.c +++ b/source/loading.c @@ -470,7 +470,7 @@ Result load_audio(Entry_s entry, audio_s *audio) ndspChnSetInterp(0, NDSP_INTERP_LINEAR); ndspChnSetRate(0, 44100); ndspChnSetFormat(0, NDSP_FORMAT_STEREO_PCM16); // Tremor outputs ogg files in 16 bit PCM stereo - ndspChnSetMix(0, audio->mix); // See mix comment above + ndspChnSetMix(0, audio->mix); // See mix comment above FILE *file = fmemopen(audio->filebuf, audio->filesize, "rb"); if(file != NULL) @@ -494,6 +494,8 @@ Result load_audio(Entry_s entry, audio_s *audio) DEBUG("Success!"); return MAKERESULT(RL_SUCCESS, RS_SUCCESS, RM_APPLICATION, RD_SUCCESS); } else { + free(audio->filebuf); + free(audio); DEBUG("File not found!\n"); return MAKERESULT(RL_FATAL, RS_NOTFOUND, RM_APPLICATION, RD_NOT_FOUND); } diff --git a/source/main.c b/source/main.c index cbffd31..d110597 100644 --- a/source/main.c +++ b/source/main.c @@ -435,20 +435,26 @@ int main(void) if(!preview_mode) { preview_mode = load_preview(*current_list, &preview_offset); - audio = calloc(sizeof(audio_s), 1); - load_audio(current_list->entries[current_list->selected_entry], audio); - play_audio(audio); + if(current_mode == MODE_THEMES) + { + audio = calloc(1, sizeof(audio_s)); + load_audio(current_list->entries[current_list->selected_entry], audio); + play_audio(audio); + } } else { preview_mode = false; - audio->stop = true; + if(current_mode == MODE_THEMES && audio) + audio->stop = true; } continue; } else if(preview_mode && kDown & (KEY_B | KEY_TOUCH)) { preview_mode = false; + if(current_mode == MODE_THEMES && audio) + audio->stop = true; continue; } } diff --git a/source/remote.c b/source/remote.c index 1a23f7b..e3b8c83 100644 --- a/source/remote.c +++ b/source/remote.c @@ -29,6 +29,7 @@ #include "draw.h" #include "fs.h" #include "unicode.h" +#include "music.h" #include "pp2d/pp2d/pp2d.h" static Instructions_s browser_instructions[MODE_AMOUNT] = { @@ -248,16 +249,14 @@ static void load_remote_list(Entry_List_s * list, json_int_t page, EntryMode mod } static u16 previous_path_preview[0x106] = {0}; -static bool load_remote_preview(Entry_List_s list, int * preview_offset) +static bool load_remote_preview(Entry_s * entry, int * preview_offset) { - Entry_s entry = list.entries[list.selected_entry]; - bool not_cached = true; - if(!memcmp(&previous_path_preview, &entry.path, 0x106*sizeof(u16))) return true; + if(!memcmp(&previous_path_preview, entry->path, 0x106*sizeof(u16))) return true; char * preview_png = NULL; - u32 preview_size = load_data("/preview.png", entry, &preview_png); + u32 preview_size = load_data("/preview.png", *entry, &preview_png); not_cached = !preview_size; @@ -267,7 +266,7 @@ static bool load_remote_preview(Entry_List_s list, int * preview_offset) preview_png = NULL; char * preview_url = NULL; - asprintf(&preview_url, THEMEPLAZA_PREVIEW_FORMAT, entry.tp_download_id); + asprintf(&preview_url, THEMEPLAZA_PREVIEW_FORMAT, entry->tp_download_id); draw_install(INSTALL_LOADING_REMOTE_PREVIEW); @@ -297,7 +296,7 @@ static bool load_remote_preview(Entry_List_s list, int * preview_offset) } // mark the new preview as loaded for optimisation - memcpy(&previous_path_preview, &entry.path, 0x106*sizeof(u16)); + memcpy(&previous_path_preview, entry->path, 0x106*sizeof(u16)); // free the previously loaded preview. wont do anything if there wasnt one pp2d_free_texture(TEXTURE_REMOTE_PREVIEW); @@ -316,7 +315,7 @@ static bool load_remote_preview(Entry_List_s list, int * preview_offset) if(not_cached) { u16 path[0x107] = {0}; - strucat(path, entry.path); + strucat(path, entry->path); struacat(path, "/preview.png"); remake_file(fsMakePath(PATH_UTF16, path), ArchiveSD, preview_size); buf_to_file(preview_size, fsMakePath(PATH_UTF16, path), ArchiveSD, preview_png); @@ -326,6 +325,39 @@ static bool load_remote_preview(Entry_List_s list, int * preview_offset) return ret; } +static u16 previous_path_bgm[0x106] = {0}; +static void load_remote_bgm(Entry_s * entry) +{ + if(!memcmp(&previous_path_bgm, entry->path, 0x106*sizeof(u16))) return; + + char * bgm_ogg = NULL; + u32 bgm_size = load_data("/bgm.ogg", *entry, &bgm_ogg); + + if(!bgm_size) + { + free(bgm_ogg); + bgm_ogg = NULL; + + char * bgm_url = NULL; + asprintf(&bgm_url, THEMEPLAZA_BGM_FORMAT, entry->tp_download_id); + + draw_install(INSTALL_LOADING_REMOTE_BGM); + + bgm_size = http_get(bgm_url, NULL, &bgm_ogg); + free(bgm_url); + + u16 path[0x107] = {0}; + strucat(path, entry->path); + struacat(path, "/bgm.ogg"); + remake_file(fsMakePath(PATH_UTF16, path), ArchiveSD, bgm_size); + buf_to_file(bgm_size, fsMakePath(PATH_UTF16, path), ArchiveSD, bgm_ogg); + + memcpy(&previous_path_bgm, entry->path, 0x106*sizeof(u16)); + } + + free(bgm_ogg); +} + static void download_remote_entry(Entry_s * entry, EntryMode mode) { char * download_url = NULL; @@ -460,6 +492,7 @@ bool themeplaza_browser(EntryMode mode) bool preview_mode = false; int preview_offset = 0; + audio_s * audio = NULL; Entry_List_s list = {0}; Entry_List_s * current_list = &list; @@ -496,6 +529,8 @@ bool themeplaza_browser(EntryMode mode) exit: quit = true; downloaded = false; + if(audio) + audio->stop = true; break; } @@ -539,14 +574,31 @@ bool themeplaza_browser(EntryMode mode) { toggle_preview: if(!preview_mode) - preview_mode = load_remote_preview(*current_list, &preview_offset); + { + preview_mode = load_remote_preview(current_entry, &preview_offset); + if(mode == MODE_THEMES) + { + load_remote_bgm(current_entry); + audio = calloc(1, sizeof(audio_s)); + load_audio(*current_entry, audio); + play_audio(audio); + } + } else + { preview_mode = false; + if(mode == MODE_THEMES && audio) + audio->stop = true; + } } else if(kDown & KEY_B) { if(preview_mode) + { preview_mode = false; + if(mode == MODE_THEMES && audio) + audio->stop = true; + } else break; } @@ -630,6 +682,8 @@ bool themeplaza_browser(EntryMode mode) if(preview_mode) { preview_mode = false; + if(mode == MODE_THEMES && audio) + audio->stop = true; continue; } else if(y < 24)