add cartridge system

This commit is contained in:
asrael 2025-09-27 11:03:36 -05:00
parent ff698730f1
commit 98ca54e920
25 changed files with 968 additions and 315 deletions

View file

@ -8,24 +8,78 @@
#define PXL8_MAX_TILEMAP_WIDTH 256
#define PXL8_MAX_TILEMAP_HEIGHT 256
#define PXL8_MAX_TILE_LAYERS 4
#define PXL8_CHUNK_SIZE 16
#define PXL8_CHUNK_MASK 15
typedef enum pxl8_tile_flags {
PXL8_TILE_FLIP_X = 1 << 0,
PXL8_TILE_FLIP_Y = 1 << 1,
PXL8_TILE_SOLID = 1 << 2,
PXL8_TILE_TRIGGER = 1 << 3,
PXL8_TILE_ANIMATED = 1 << 4,
PXL8_TILE_AUTOTILE = 1 << 5,
} pxl8_tile_flags;
typedef struct pxl8_tile {
u16 id;
u8 flags;
u8 palette_offset;
} pxl8_tile;
#define PXL8_TILE_ID_MASK 0x0000FFFF
#define PXL8_TILE_FLAGS_MASK 0x00FF0000
#define PXL8_TILE_PAL_MASK 0xFF000000
#define PXL8_TILE_ID_SHIFT 0
#define PXL8_TILE_FLAGS_SHIFT 16
#define PXL8_TILE_PAL_SHIFT 24
typedef u32 pxl8_tile;
static inline pxl8_tile pxl8_tile_pack(u16 id, u8 flags, u8 palette_offset) {
return (u32)id | ((u32)flags << 16) | ((u32)palette_offset << 24);
}
static inline u16 pxl8_tile_get_id(pxl8_tile tile) {
return tile & PXL8_TILE_ID_MASK;
}
static inline u8 pxl8_tile_get_flags(pxl8_tile tile) {
return (tile & PXL8_TILE_FLAGS_MASK) >> PXL8_TILE_FLAGS_SHIFT;
}
static inline u8 pxl8_tile_get_palette(pxl8_tile tile) {
return (tile & PXL8_TILE_PAL_MASK) >> PXL8_TILE_PAL_SHIFT;
}
typedef struct pxl8_tile_animation {
u16* frames;
u16 frame_count;
u16 current_frame;
f32 frame_duration;
f32 time_accumulator;
} pxl8_tile_animation;
typedef struct pxl8_tile_properties {
void* user_data;
u32 property_flags;
i16 collision_offset_x;
i16 collision_offset_y;
u16 collision_width;
u16 collision_height;
} pxl8_tile_properties;
typedef struct pxl8_autotile_rule {
u8 neighbor_mask;
u16 tile_id;
} pxl8_autotile_rule;
typedef struct pxl8_tile_chunk {
pxl8_tile tiles[PXL8_CHUNK_SIZE * PXL8_CHUNK_SIZE];
u32 chunk_x;
u32 chunk_y;
bool empty;
} pxl8_tile_chunk;
typedef struct pxl8_tilemap_layer {
pxl8_tile* tiles;
u32 width;
u32 height;
pxl8_tile_chunk** chunks;
u32 chunks_wide;
u32 chunks_high;
u32 chunk_count;
u32 allocated_chunks;
bool visible;
u8 opacity;
} pxl8_tilemap_layer;
@ -75,6 +129,15 @@ pxl8_tilemap* pxl8_tilemap_new(u32 width, u32 height, u32 tile_size);
void pxl8_tilemap_destroy(pxl8_tilemap* tilemap);
u16 pxl8_tilemap_get_tile_id(const pxl8_tilemap* tilemap, u32 layer, u32 x, u32 y);
void pxl8_tilemap_update(pxl8_tilemap* tilemap, f32 delta_time);
pxl8_result pxl8_tilemap_set_tile_auto(pxl8_tilemap* tilemap, u32 layer, u32 x, u32 y,
u16 base_tile_id, u8 flags);
void pxl8_tilemap_update_autotiles(pxl8_tilemap* tilemap, u32 layer, u32 x, u32 y, u32 w, u32 h);
u32 pxl8_tilemap_get_memory_usage(const pxl8_tilemap* tilemap);
void pxl8_tilemap_compress(pxl8_tilemap* tilemap);
#ifdef __cplusplus
}
#endif
#endif