Skip to content

Commit 2cc226b

Browse files
authored
Informs retroarch that PNG extension is also supported (#2832)
* Informs retroarch that PNG extension is also supported * 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. * libpng CMakelist - order may matter * target_link_libraries(tic80core PRIVATE png)
1 parent 6a23ce9 commit 2cc226b

File tree

6 files changed

+50
-46
lines changed

6 files changed

+50
-46
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ include(cmake/gif.cmake)
152152
include(cmake/blipbuf.cmake)
153153
include(cmake/zlib.cmake)
154154
include(cmake/zip.cmake)
155+
include(cmake/png.cmake)
155156
include(cmake/tools.cmake)
156157

157158
include(cmake/lua.cmake)
@@ -170,7 +171,6 @@ include(cmake/core.cmake)
170171
include(cmake/wave.cmake)
171172
include(cmake/argparse.cmake)
172173
include(cmake/naett.cmake)
173-
include(cmake/png.cmake)
174174
include(cmake/studio.cmake)
175175

176176
include(cmake/sdl.cmake)

cmake/core.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ set(TIC80CORE_SRC
2929
${TIC80CORE_DIR}/ext/fft.c
3030
${TIC80CORE_DIR}/ext/kiss_fft.c
3131
${TIC80CORE_DIR}/ext/kiss_fftr.c
32+
${TIC80CORE_DIR}/ext/png.c
3233
)
3334

3435
if(BUILD_DEPRECATED)
@@ -55,6 +56,7 @@ target_include_directories(tic80core
5556
${CMAKE_SOURCE_DIR}/include
5657
${CMAKE_SOURCE_DIR}/src)
5758

59+
target_link_libraries(tic80core PRIVATE png)
5860
target_link_libraries(tic80core PRIVATE blipbuf)
5961

6062
if(BUILD_WITH_ZLIB)

cmake/studio.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set(TIC80STUDIO_SRC
1313
${TIC80LIB_DIR}/studio/fs.c
1414
${TIC80LIB_DIR}/ext/md5.c
1515
${TIC80LIB_DIR}/ext/json.c
16+
${TIC80LIB_DIR}/ext/png.c
1617
)
1718

1819
if(BUILD_EDITORS)
@@ -28,7 +29,6 @@ if(BUILD_EDITORS)
2829
${TIC80LIB_DIR}/studio/net.c
2930
${TIC80LIB_DIR}/ext/history.c
3031
${TIC80LIB_DIR}/ext/gif.c
31-
${TIC80LIB_DIR}/ext/png.c
3232
)
3333
endif()
3434

src/cart.c

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <stdlib.h>
3333
#include "tic_assert.h"
3434
#include "tools.h"
35+
#include "ext/png.h"
3536

3637
typedef enum
3738
{
@@ -81,6 +82,42 @@ static s32 chunkSize(const Chunk* chunk)
8182
return chunk->size == 0 && (chunk->type == CHUNK_CODE || chunk->type == CHUNK_BINARY) ? TIC_BANK_SIZE : retro_le_to_cpu16(chunk->size);
8283
}
8384

85+
static png_buffer getRawCartFromPng(png_buffer buffer)
86+
{
87+
png_buffer zip = png_decode(buffer);
88+
89+
if (zip.size)
90+
{
91+
png_buffer buf = png_create(sizeof(tic_cartridge));
92+
93+
buf.size = tic_tool_unzip(buf.data, buf.size, zip.data, zip.size);
94+
free(zip.data);
95+
96+
if(buf.size)
97+
return buf;
98+
99+
free(buf.data);
100+
}
101+
102+
return (png_buffer){.data = NULL, .size = 0};
103+
}
104+
105+
tic_cartridge* loadPngCart(png_buffer buffer)
106+
{
107+
png_buffer buf = getRawCartFromPng(buffer);
108+
109+
if(buf.data)
110+
{
111+
tic_cartridge* cart = malloc(sizeof(tic_cartridge));
112+
tic_cart_load(cart, buf.data, buf.size);
113+
free(buf.data);
114+
115+
return cart;
116+
}
117+
118+
return NULL;
119+
}
120+
84121
void tic_cart_load(tic_cartridge* cart, const u8* buffer, s32 size)
85122
{
86123
memset(cart, 0, sizeof(tic_cartridge));
@@ -90,27 +127,16 @@ void tic_cart_load(tic_cartridge* cart, const u8* buffer, s32 size)
90127
// check if this cartridge is in PNG format
91128
if (!memcmp(buffer, "\x89PNG", 4))
92129
{
93-
s32 siz;
94-
const u8* ptr = buffer + 8;
95-
// iterate on chunks until we find a cartridge
96-
while (ptr < end)
130+
png_buffer buf = getRawCartFromPng((png_buffer){.data = (u8*)buffer, .size = size});
131+
132+
if(buf.data)
97133
{
98-
siz = ((ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]);
99-
if (!memcmp(ptr + 4, "caRt", 4) && siz > 0)
100-
{
101-
chunk_cart = malloc(sizeof(tic_cartridge));
102-
if (chunk_cart)
103-
{
104-
size = tic_tool_unzip(chunk_cart, sizeof(tic_cartridge), ptr + 8, siz);
105-
buffer = chunk_cart;
106-
end = buffer + size;
107-
}
108-
break;
109-
}
110-
ptr += siz + 12;
134+
chunk_cart = buf.data;
135+
buffer = buf.data;
136+
size = buf.size;
137+
end = buffer + size;
111138
}
112-
// error, no TIC-80 cartridge chunk in PNG???
113-
if (!chunk_cart)
139+
else
114140
return;
115141
}
116142

src/studio/studio.c

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,30 +1531,6 @@ bool project_ext(const char* name)
15311531
return false;
15321532
}
15331533

1534-
tic_cartridge* loadPngCart(png_buffer buffer)
1535-
{
1536-
png_buffer zip = png_decode(buffer);
1537-
1538-
if (zip.size)
1539-
{
1540-
png_buffer buf = png_create(sizeof(tic_cartridge));
1541-
1542-
buf.size = tic_tool_unzip(buf.data, buf.size, zip.data, zip.size);
1543-
free(zip.data);
1544-
1545-
if(buf.size)
1546-
{
1547-
tic_cartridge* cart = malloc(sizeof(tic_cartridge));
1548-
tic_cart_load(cart, buf.data, buf.size);
1549-
free(buf.data);
1550-
1551-
return cart;
1552-
}
1553-
}
1554-
1555-
return NULL;
1556-
}
1557-
15581534
void studioRomSaved(Studio* studio)
15591535
{
15601536
updateTitle(studio);

src/system/libretro/tic80_libretro.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ RETRO_API void retro_get_system_info(struct retro_system_info *info)
336336
memset(info, 0, sizeof(*info));
337337
info->library_name = TIC_NAME;
338338
info->library_version = TIC_VERSION;
339-
info->valid_extensions = "tic";
339+
info->valid_extensions = "tic|png";
340340
info->need_fullpath = false;
341341
info->block_extract = false;
342342
}

0 commit comments

Comments
 (0)