Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add png chunk loading #2088

Merged
merged 1 commit into from
Jan 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/cart.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,34 @@ void tic_cart_load(tic_cartridge* cart, const u8* buffer, s32 size)
{
memset(cart, 0, sizeof(tic_cartridge));
const u8* end = buffer + size;
u8 *chunk_cart = NULL;

// 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)
{
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;
}
// error, no TIC-80 cartridge chunk in PNG???
if (!chunk_cart)
return;
}

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

Expand Down Expand Up @@ -219,6 +247,9 @@ void tic_cart_load(tic_cartridge* cart, const u8* buffer, s32 size)
}
}
}
// if we have allocated the buffer from a PNG chunk
if (chunk_cart)
free(chunk_cart);
}


Expand Down