add bgm preview to the themeplaza browser

and prevent some errors
This commit is contained in:
LiquidFenrir
2018-05-10 13:53:51 +02:00
parent 5520ec2b2f
commit 8022ed1682
5 changed files with 80 additions and 14 deletions

View File

@@ -51,6 +51,7 @@ typedef enum {
INSTALL_LOADING_REMOTE_THEMES, INSTALL_LOADING_REMOTE_THEMES,
INSTALL_LOADING_REMOTE_SPLASHES, INSTALL_LOADING_REMOTE_SPLASHES,
INSTALL_LOADING_REMOTE_PREVIEW, INSTALL_LOADING_REMOTE_PREVIEW,
INSTALL_LOADING_REMOTE_BGM,
INSTALL_NONE, INSTALL_NONE,
} InstallType; } InstallType;

View File

@@ -204,6 +204,9 @@ void draw_install(InstallType type)
case INSTALL_LOADING_REMOTE_PREVIEW: case INSTALL_LOADING_REMOTE_PREVIEW:
pp2d_draw_text_center(GFX_TOP, 120, 0.8, 0.8, COLOR_WHITE, "Downloading preview, please wait..."); pp2d_draw_text_center(GFX_TOP, 120, 0.8, 0.8, COLOR_WHITE, "Downloading preview, please wait...");
break; 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: case INSTALL_SINGLE:
pp2d_draw_text_center(GFX_TOP, 120, 0.8, 0.8, COLOR_WHITE, "Installing a single theme..."); pp2d_draw_text_center(GFX_TOP, 120, 0.8, 0.8, COLOR_WHITE, "Installing a single theme...");
break; break;

View File

@@ -470,7 +470,7 @@ Result load_audio(Entry_s entry, audio_s *audio)
ndspChnSetInterp(0, NDSP_INTERP_LINEAR); ndspChnSetInterp(0, NDSP_INTERP_LINEAR);
ndspChnSetRate(0, 44100); ndspChnSetRate(0, 44100);
ndspChnSetFormat(0, NDSP_FORMAT_STEREO_PCM16); // Tremor outputs ogg files in 16 bit PCM stereo 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"); FILE *file = fmemopen(audio->filebuf, audio->filesize, "rb");
if(file != NULL) if(file != NULL)
@@ -494,6 +494,8 @@ Result load_audio(Entry_s entry, audio_s *audio)
DEBUG("Success!"); DEBUG("Success!");
return MAKERESULT(RL_SUCCESS, RS_SUCCESS, RM_APPLICATION, RD_SUCCESS); return MAKERESULT(RL_SUCCESS, RS_SUCCESS, RM_APPLICATION, RD_SUCCESS);
} else { } else {
free(audio->filebuf);
free(audio);
DEBUG("File not found!\n"); DEBUG("File not found!\n");
return MAKERESULT(RL_FATAL, RS_NOTFOUND, RM_APPLICATION, RD_NOT_FOUND); return MAKERESULT(RL_FATAL, RS_NOTFOUND, RM_APPLICATION, RD_NOT_FOUND);
} }

View File

@@ -435,20 +435,26 @@ int main(void)
if(!preview_mode) if(!preview_mode)
{ {
preview_mode = load_preview(*current_list, &preview_offset); preview_mode = load_preview(*current_list, &preview_offset);
audio = calloc(sizeof(audio_s), 1); if(current_mode == MODE_THEMES)
load_audio(current_list->entries[current_list->selected_entry], audio); {
play_audio(audio); audio = calloc(1, sizeof(audio_s));
load_audio(current_list->entries[current_list->selected_entry], audio);
play_audio(audio);
}
} }
else else
{ {
preview_mode = false; preview_mode = false;
audio->stop = true; if(current_mode == MODE_THEMES && audio)
audio->stop = true;
} }
continue; continue;
} }
else if(preview_mode && kDown & (KEY_B | KEY_TOUCH)) else if(preview_mode && kDown & (KEY_B | KEY_TOUCH))
{ {
preview_mode = false; preview_mode = false;
if(current_mode == MODE_THEMES && audio)
audio->stop = true;
continue; continue;
} }
} }

View File

@@ -29,6 +29,7 @@
#include "draw.h" #include "draw.h"
#include "fs.h" #include "fs.h"
#include "unicode.h" #include "unicode.h"
#include "music.h"
#include "pp2d/pp2d/pp2d.h" #include "pp2d/pp2d/pp2d.h"
static Instructions_s browser_instructions[MODE_AMOUNT] = { 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 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; 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; 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; not_cached = !preview_size;
@@ -267,7 +266,7 @@ static bool load_remote_preview(Entry_List_s list, int * preview_offset)
preview_png = NULL; preview_png = NULL;
char * preview_url = 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); 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 // 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 // free the previously loaded preview. wont do anything if there wasnt one
pp2d_free_texture(TEXTURE_REMOTE_PREVIEW); 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) if(not_cached)
{ {
u16 path[0x107] = {0}; u16 path[0x107] = {0};
strucat(path, entry.path); strucat(path, entry->path);
struacat(path, "/preview.png"); struacat(path, "/preview.png");
remake_file(fsMakePath(PATH_UTF16, path), ArchiveSD, preview_size); remake_file(fsMakePath(PATH_UTF16, path), ArchiveSD, preview_size);
buf_to_file(preview_size, fsMakePath(PATH_UTF16, path), ArchiveSD, preview_png); 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; 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) static void download_remote_entry(Entry_s * entry, EntryMode mode)
{ {
char * download_url = NULL; char * download_url = NULL;
@@ -460,6 +492,7 @@ bool themeplaza_browser(EntryMode mode)
bool preview_mode = false; bool preview_mode = false;
int preview_offset = 0; int preview_offset = 0;
audio_s * audio = NULL;
Entry_List_s list = {0}; Entry_List_s list = {0};
Entry_List_s * current_list = &list; Entry_List_s * current_list = &list;
@@ -496,6 +529,8 @@ bool themeplaza_browser(EntryMode mode)
exit: exit:
quit = true; quit = true;
downloaded = false; downloaded = false;
if(audio)
audio->stop = true;
break; break;
} }
@@ -539,14 +574,31 @@ bool themeplaza_browser(EntryMode mode)
{ {
toggle_preview: toggle_preview:
if(!preview_mode) 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 else
{
preview_mode = false; preview_mode = false;
if(mode == MODE_THEMES && audio)
audio->stop = true;
}
} }
else if(kDown & KEY_B) else if(kDown & KEY_B)
{ {
if(preview_mode) if(preview_mode)
{
preview_mode = false; preview_mode = false;
if(mode == MODE_THEMES && audio)
audio->stop = true;
}
else else
break; break;
} }
@@ -630,6 +682,8 @@ bool themeplaza_browser(EntryMode mode)
if(preview_mode) if(preview_mode)
{ {
preview_mode = false; preview_mode = false;
if(mode == MODE_THEMES && audio)
audio->stop = true;
continue; continue;
} }
else if(y < 24) else if(y < 24)