Skip to content

Commit 880fa5a

Browse files
authored
Merge pull request #2088 from bztsrc/main
Add png chunk loading
2 parents b70316c + 9bfce67 commit 880fa5a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/cart.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,34 @@ void tic_cart_load(tic_cartridge* cart, const u8* buffer, s32 size)
8484
{
8585
memset(cart, 0, sizeof(tic_cartridge));
8686
const u8* end = buffer + size;
87+
u8 *chunk_cart = NULL;
88+
89+
// check if this cartridge is in PNG format
90+
if (!memcmp(buffer, "\x89PNG", 4))
91+
{
92+
s32 siz;
93+
const u8* ptr = buffer + 8;
94+
// iterate on chunks until we find a cartridge
95+
while (ptr < end)
96+
{
97+
siz = ((ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]);
98+
if (!memcmp(ptr + 4, "caRt", 4) && siz > 0)
99+
{
100+
chunk_cart = malloc(sizeof(tic_cartridge));
101+
if (chunk_cart)
102+
{
103+
size = tic_tool_unzip(chunk_cart, sizeof(tic_cartridge), ptr + 8, siz);
104+
buffer = chunk_cart;
105+
end = buffer + size;
106+
}
107+
break;
108+
}
109+
ptr += siz + 12;
110+
}
111+
// error, no TIC-80 cartridge chunk in PNG???
112+
if (!chunk_cart)
113+
return;
114+
}
87115

88116
#define LOAD_CHUNK(to) memcpy(&to, ptr, MIN(sizeof(to), chunk->size ? retro_le_to_cpu16(chunk->size) : TIC_BANK_SIZE))
89117

@@ -219,6 +247,9 @@ void tic_cart_load(tic_cartridge* cart, const u8* buffer, s32 size)
219247
}
220248
}
221249
}
250+
// if we have allocated the buffer from a PNG chunk
251+
if (chunk_cart)
252+
free(chunk_cart);
222253
}
223254

224255

0 commit comments

Comments
 (0)