From 48ccab3ce295b5dcd64260e320f4357ccc73b13b Mon Sep 17 00:00:00 2001 From: imsys <911254+imsys@users.noreply.github.com> Date: Thu, 11 Sep 2025 03:34:01 -0300 Subject: [PATCH 1/4] Informs retroarch that PNG extension is also supported --- src/system/libretro/tic80_libretro.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/system/libretro/tic80_libretro.c b/src/system/libretro/tic80_libretro.c index fc3fcb1e0..5591134fe 100644 --- a/src/system/libretro/tic80_libretro.c +++ b/src/system/libretro/tic80_libretro.c @@ -336,7 +336,7 @@ RETRO_API void retro_get_system_info(struct retro_system_info *info) memset(info, 0, sizeof(*info)); info->library_name = TIC_NAME; info->library_version = TIC_VERSION; - info->valid_extensions = "tic"; + info->valid_extensions = "tic|png"; info->need_fullpath = false; info->block_extract = false; } From 433fde1d1563caf31d780d527b551c0e773c0814 Mon Sep 17 00:00:00 2001 From: imsys <911254+imsys@users.noreply.github.com> Date: Sun, 14 Sep 2025 19:27:11 -0300 Subject: [PATCH 2/4] Loading PNG cart from cart.c (fix PNG load in libretro) Moved loadPngCart from studio.c to cart.c The standalone binary loads carts from this stack: studio.c: studio_create() console.c: initConsole() console.c: cmdLoadCart() cmdLoadCart() had a working PNG cart load, but libretro uses `tic.c: tic80_load()` directlty, but that had a incompete PNG load. So I tried to combine and bring the aproach from cmdLoadCart() to tic80_load() by letting them both use the same PNG loading function. --- cmake/core.cmake | 1 + cmake/libretro.cmake | 2 +- cmake/studio.cmake | 2 +- src/cart.c | 64 +++++++++++++++++++++++++++++++------------- src/studio/studio.c | 24 ----------------- 5 files changed, 48 insertions(+), 45 deletions(-) diff --git a/cmake/core.cmake b/cmake/core.cmake index 19ac5a4d0..4c12bb52f 100644 --- a/cmake/core.cmake +++ b/cmake/core.cmake @@ -29,6 +29,7 @@ set(TIC80CORE_SRC ${TIC80CORE_DIR}/ext/fft.c ${TIC80CORE_DIR}/ext/kiss_fft.c ${TIC80CORE_DIR}/ext/kiss_fftr.c + ${TIC80CORE_DIR}/ext/png.c ) if(BUILD_DEPRECATED) diff --git a/cmake/libretro.cmake b/cmake/libretro.cmake index 54fa9b3dc..a86adcaaa 100644 --- a/cmake/libretro.cmake +++ b/cmake/libretro.cmake @@ -46,6 +46,6 @@ if(BUILD_LIBRETRO) target_compile_definitions(tic80_libretro PRIVATE __LIBRETRO__=TRUE ) - target_link_libraries(tic80_libretro tic80core) + target_link_libraries(tic80_libretro tic80core png) set_target_properties(tic80_libretro PROPERTIES PREFIX "") endif() \ No newline at end of file diff --git a/cmake/studio.cmake b/cmake/studio.cmake index d55d27176..7d32891ca 100644 --- a/cmake/studio.cmake +++ b/cmake/studio.cmake @@ -13,6 +13,7 @@ set(TIC80STUDIO_SRC ${TIC80LIB_DIR}/studio/fs.c ${TIC80LIB_DIR}/ext/md5.c ${TIC80LIB_DIR}/ext/json.c + ${TIC80LIB_DIR}/ext/png.c ) if(BUILD_EDITORS) @@ -28,7 +29,6 @@ if(BUILD_EDITORS) ${TIC80LIB_DIR}/studio/net.c ${TIC80LIB_DIR}/ext/history.c ${TIC80LIB_DIR}/ext/gif.c - ${TIC80LIB_DIR}/ext/png.c ) endif() diff --git a/src/cart.c b/src/cart.c index 5fec3ff66..3108c0a8c 100644 --- a/src/cart.c +++ b/src/cart.c @@ -32,6 +32,7 @@ #include #include "tic_assert.h" #include "tools.h" +#include "ext/png.h" typedef enum { @@ -81,6 +82,42 @@ static s32 chunkSize(const Chunk* chunk) return chunk->size == 0 && (chunk->type == CHUNK_CODE || chunk->type == CHUNK_BINARY) ? TIC_BANK_SIZE : retro_le_to_cpu16(chunk->size); } +static png_buffer getRawCartFromPng(png_buffer buffer) +{ + png_buffer zip = png_decode(buffer); + + if (zip.size) + { + png_buffer buf = png_create(sizeof(tic_cartridge)); + + buf.size = tic_tool_unzip(buf.data, buf.size, zip.data, zip.size); + free(zip.data); + + if(buf.size) + return buf; + + free(buf.data); + } + + return (png_buffer){.data = NULL, .size = 0}; +} + +tic_cartridge* loadPngCart(png_buffer buffer) +{ + png_buffer buf = getRawCartFromPng(buffer); + + if(buf.data) + { + tic_cartridge* cart = malloc(sizeof(tic_cartridge)); + tic_cart_load(cart, buf.data, buf.size); + free(buf.data); + + return cart; + } + + return NULL; +} + void tic_cart_load(tic_cartridge* cart, const u8* buffer, s32 size) { memset(cart, 0, sizeof(tic_cartridge)); @@ -90,27 +127,16 @@ void tic_cart_load(tic_cartridge* cart, const u8* buffer, s32 size) // check if this cartridge is in PNG format if (!memcmp(buffer, "\x89PNG", 4)) { - s32 siz; - const u8* ptr = buffer + 8; - // iterate on chunks until we find a cartridge - while (ptr < end) + png_buffer buf = getRawCartFromPng((png_buffer){.data = (u8*)buffer, .size = size}); + + if(buf.data) { - siz = ((ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]); - if (!memcmp(ptr + 4, "caRt", 4) && siz > 0) - { - chunk_cart = malloc(sizeof(tic_cartridge)); - if (chunk_cart) - { - size = tic_tool_unzip(chunk_cart, sizeof(tic_cartridge), ptr + 8, siz); - buffer = chunk_cart; - end = buffer + size; - } - break; - } - ptr += siz + 12; + chunk_cart = buf.data; + buffer = buf.data; + size = buf.size; + end = buffer + size; } - // error, no TIC-80 cartridge chunk in PNG??? - if (!chunk_cart) + else return; } diff --git a/src/studio/studio.c b/src/studio/studio.c index 30cbff259..08cb95a28 100644 --- a/src/studio/studio.c +++ b/src/studio/studio.c @@ -1531,30 +1531,6 @@ bool project_ext(const char* name) return false; } -tic_cartridge* loadPngCart(png_buffer buffer) -{ - png_buffer zip = png_decode(buffer); - - if (zip.size) - { - png_buffer buf = png_create(sizeof(tic_cartridge)); - - buf.size = tic_tool_unzip(buf.data, buf.size, zip.data, zip.size); - free(zip.data); - - if(buf.size) - { - tic_cartridge* cart = malloc(sizeof(tic_cartridge)); - tic_cart_load(cart, buf.data, buf.size); - free(buf.data); - - return cart; - } - } - - return NULL; -} - void studioRomSaved(Studio* studio) { updateTitle(studio); From fa846df04062c6e760d369149ac6f5e66c00b05c Mon Sep 17 00:00:00 2001 From: imsys <911254+imsys@users.noreply.github.com> Date: Sun, 14 Sep 2025 20:21:20 -0300 Subject: [PATCH 3/4] libpng CMakelist - order may matter --- CMakeLists.txt | 2 +- cmake/libretro.cmake | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cabd9ea79..47f7262cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,6 +152,7 @@ include(cmake/gif.cmake) include(cmake/blipbuf.cmake) include(cmake/zlib.cmake) include(cmake/zip.cmake) +include(cmake/png.cmake) include(cmake/tools.cmake) include(cmake/lua.cmake) @@ -170,7 +171,6 @@ include(cmake/core.cmake) include(cmake/wave.cmake) include(cmake/argparse.cmake) include(cmake/naett.cmake) -include(cmake/png.cmake) include(cmake/studio.cmake) include(cmake/sdl.cmake) diff --git a/cmake/libretro.cmake b/cmake/libretro.cmake index a86adcaaa..54fa9b3dc 100644 --- a/cmake/libretro.cmake +++ b/cmake/libretro.cmake @@ -46,6 +46,6 @@ if(BUILD_LIBRETRO) target_compile_definitions(tic80_libretro PRIVATE __LIBRETRO__=TRUE ) - target_link_libraries(tic80_libretro tic80core png) + target_link_libraries(tic80_libretro tic80core) set_target_properties(tic80_libretro PROPERTIES PREFIX "") endif() \ No newline at end of file From ca7a66e6a29b01312aade9cbd39a8381421f30ea Mon Sep 17 00:00:00 2001 From: imsys <911254+imsys@users.noreply.github.com> Date: Sun, 14 Sep 2025 21:19:46 -0300 Subject: [PATCH 4/4] target_link_libraries(tic80core PRIVATE png) --- cmake/core.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/cmake/core.cmake b/cmake/core.cmake index 4c12bb52f..f51117145 100644 --- a/cmake/core.cmake +++ b/cmake/core.cmake @@ -56,6 +56,7 @@ target_include_directories(tic80core ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src) +target_link_libraries(tic80core PRIVATE png) target_link_libraries(tic80core PRIVATE blipbuf) if(BUILD_WITH_ZLIB)