joeylib2/include/joey/asset.h

65 lines
2.4 KiB
C

// Sprite-style asset loading.
//
// A JoeyLib asset is a small bitmap with the same 4bpp packed pixel
// format as the rest of the library, plus an optional 16-entry $0RGB
// palette. Assets blit onto SurfaceT via surfaceBlit / surfaceBlitMasked
// (defined in draw.h).
//
// Two production paths:
//
// 1. Embedded -- declare a `const JoeyAssetT` directly in a .c file
// and bundle it into the executable. No allocation, no I/O. Best
// for small game assets that always ship with the binary.
//
// 2. File / memory -- joeyAssetLoadFile reads a .jas blob from disk;
// joeyAssetFromMem parses one already in RAM. Both allocate the
// JoeyAssetT plus its pixel buffer; release with joeyAssetFree.
//
// .jas binary format (little-endian, byte-stream so endianness of the
// host doesn't matter):
//
// offset bytes field
// ------ ----- --------------------------------------------
// 0 4 magic "JAS1"
// 4 2 width in pixels
// 6 2 height in pixels
// 8 1 hasPalette (0 or 1)
// 9 3 reserved (zero)
// 12 32 palette[16] of $0RGB; valid only if hasPalette
// 44 ... pixels: rowBytes * height where rowBytes = (width+1)/2
#ifndef JOEYLIB_ASSET_H
#define JOEYLIB_ASSET_H
#include "platform.h"
#include "surface.h"
#include "types.h"
typedef struct {
uint16_t width;
uint16_t height;
bool hasPalette;
uint16_t palette[16];
const uint8_t *pixels;
} JoeyAssetT;
// Allocates a new asset by reading a .jas file. Returns NULL if the
// file is missing, malformed, or too large.
JoeyAssetT *joeyAssetLoadFile(const char *path);
// Parses a .jas blob already in memory. Allocates a JoeyAssetT and
// copies the pixel run into a fresh buffer so the caller can free its
// own copy of the bytes after the call returns.
JoeyAssetT *joeyAssetFromMem(const uint8_t *data, uint32_t length);
// Releases an asset previously returned by a loader. Calling free on
// a static / embedded JoeyAssetT is forbidden -- those structs do not
// own their pixels and must not be passed here. NULL is OK.
void joeyAssetFree(JoeyAssetT *asset);
// Copies the asset's 16-entry palette into one of the surface's
// palette slots so that subsequent blits land on the right colors.
// No-op if the asset has no palette.
void joeyAssetApplyPalette(SurfaceT *dst, uint8_t paletteIndex, const JoeyAssetT *asset);
#endif