From ef456c7e9e0648ebcc27ac9f2c1980e6daa1521c Mon Sep 17 00:00:00 2001 From: Alex Taber Date: Sat, 9 Sep 2017 00:12:16 -0400 Subject: [PATCH] Splash previews --- include/splashes.h | 2 ++ source/camera.c | 2 ++ source/draw.c | 7 +++--- source/fs.c | 4 ++-- source/main.c | 14 ++++++++---- source/splashes.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 9 deletions(-) diff --git a/include/splashes.h b/include/splashes.h index cc5ae4a..6d98d54 100644 --- a/include/splashes.h +++ b/include/splashes.h @@ -47,4 +47,6 @@ int splash_count; Result get_splashes(Splash_s** splashes_list, int *splash_count); void splash_install(Splash_s splash_to_install); void splash_delete(); +void load_splash_preview(Splash_s *splash); + #endif \ No newline at end of file diff --git a/source/camera.c b/source/camera.c index c6e05bb..0779f84 100644 --- a/source/camera.c +++ b/source/camera.c @@ -121,6 +121,8 @@ void take_picture(void) } pp2d_load_texture_memory(TEXTURE_QR, rgba8_buf, 400, 240); pp2d_draw_texture(TEXTURE_QR, 0, 0); + pp2d_draw_rectangle(0, 216, 400, 24, RGBA8(55, 122, 168, 255)); + pp2d_draw_text_center(GFX_TOP, 220, 0.5, 0.5, RGBA8(255, 255, 255, 255), "Hold \uE005 To Quit"); pp2d_end_draw(); free(rgba8_buf); pp2d_free_texture(TEXTURE_QR); diff --git a/source/draw.c b/source/draw.c index 396bd1a..ddb45ff 100644 --- a/source/draw.c +++ b/source/draw.c @@ -287,12 +287,13 @@ void draw_splash_interface(Splash_s *splashes_list, int splash_count, int select pp2d_end_draw(); return; } - Splash_s current_splash = splashes_list[selected_splash]; - if (preview_mode) { - // TODO: Splash Previews + pp2d_begin_draw(GFX_TOP); + pp2d_draw_texture_part(TEXTURE_PREVIEW, 0, 0, 0, 0, 400, 240); + pp2d_draw_on(GFX_BOTTOM); + pp2d_draw_texture_part(TEXTURE_PREVIEW, 0, 0, 40, 240, 320, 240); } else { draw_base_interface(); pp2d_draw_text_center(GFX_TOP, 4, 0.5, 0.5, COLOR_WHITE, "Splash mode"); diff --git a/source/fs.c b/source/fs.c index b34401c..0226342 100644 --- a/source/fs.c +++ b/source/fs.c @@ -115,7 +115,7 @@ u64 file_to_buf(FS_Path path, FS_Archive archive, char** buf) u64 size; FSFILE_GetSize(file, &size); - *buf = malloc(size); + *buf = calloc(1, size); FSFILE_Read(file, NULL, 0, *buf, size); FSFILE_Close(file); return size; @@ -139,7 +139,7 @@ u32 zip_file_to_buf(char *file_name, u16 *zip_path, char **buf) unz_file_info *file_info = malloc(sizeof(unz_file_info)); unzGetCurrentFileInfo(zip_handle, file_info, NULL, 0, NULL, 0, NULL, 0); file_size = file_info->uncompressed_size; - *buf = malloc(file_size); + *buf = calloc(1, file_size); unzOpenCurrentFile(zip_handle); unzReadCurrentFile(zip_handle, *buf, file_size); unzCloseCurrentFile(zip_handle); diff --git a/source/main.c b/source/main.c index 34074d9..531f9e2 100644 --- a/source/main.c +++ b/source/main.c @@ -159,10 +159,16 @@ int main(void) { if (!preview_mode) { - if (!current_theme->has_preview) - load_theme_preview(current_theme); - - preview_mode = current_theme->has_preview; + if (!splash_mode) + { + if (!current_theme->has_preview) + load_theme_preview(current_theme); + + preview_mode = current_theme->has_preview; + } else { + load_splash_preview(current_splash); + preview_mode = true; + } } else preview_mode = false; diff --git a/source/splashes.c b/source/splashes.c index 163b11d..f72a545 100644 --- a/source/splashes.c +++ b/source/splashes.c @@ -29,6 +29,61 @@ #include "themes.h" #include "pp2d/pp2d/pp2d.h" +void load_splash_preview(Splash_s *splash) +{ + //free the previously loaded preview. wont do anything if there wasnt one + pp2d_free_texture(TEXTURE_PREVIEW); + + char *preview_buffer = NULL; + u64 size = 0; + if (!(splash->is_zip)) + { + u16 path_to_preview[0x106] = {0}; + strucat(path_to_preview, splash->path); + struacat(path_to_preview, "/preview.png"); + size = file_to_buf(fsMakePath(PATH_UTF16, path_to_preview), ArchiveSD, &preview_buffer); + } else { + size = zip_file_to_buf("preview.png", splash->path, &preview_buffer); + } + + if (!size) + { + free(preview_buffer); + return; + } + + u8 * image = NULL; + unsigned int width = 0, height = 0; + + int result = lodepng_decode32(&image, &width, &height, (u8*)preview_buffer, size); + if (result == 0) // no error + { + for (u32 i = 0; i < width; i++) + { + for (u32 j = 0; j < height; j++) + { + u32 p = (i + j*width) * 4; + + u8 r = *(u8*)(image + p); + u8 g = *(u8*)(image + p + 1); + u8 b = *(u8*)(image + p + 2); + u8 a = *(u8*)(image + p + 3); + + *(image + p) = a; + *(image + p + 1) = b; + *(image + p + 2) = g; + *(image + p + 3) = r; + } + } + + pp2d_load_texture_memory(TEXTURE_PREVIEW, image, (u32)width, (u32)height); + } + + free(image); + free(preview_buffer); +} + + static void parse_smdh(Splash_s *splash, ssize_t textureID, u16 *splash_name) { char *info_buffer = NULL;