2025-09-24 00:39:44 -05:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "pxl8_types.h"
|
|
|
|
|
#include "pxl8_gfx.h"
|
|
|
|
|
#include "pxl8_tilesheet.h"
|
|
|
|
|
|
|
|
|
|
#define PXL8_TILE_SIZE 16
|
|
|
|
|
#define PXL8_MAX_TILEMAP_WIDTH 256
|
|
|
|
|
#define PXL8_MAX_TILEMAP_HEIGHT 256
|
|
|
|
|
#define PXL8_MAX_TILE_LAYERS 4
|
2025-09-27 11:03:36 -05:00
|
|
|
#define PXL8_CHUNK_SIZE 16
|
|
|
|
|
#define PXL8_CHUNK_MASK 15
|
2025-09-24 00:39:44 -05:00
|
|
|
|
|
|
|
|
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,
|
2025-09-27 11:03:36 -05:00
|
|
|
PXL8_TILE_ANIMATED = 1 << 4,
|
|
|
|
|
PXL8_TILE_AUTOTILE = 1 << 5,
|
2025-09-24 00:39:44 -05:00
|
|
|
} pxl8_tile_flags;
|
|
|
|
|
|
2025-09-27 11:03:36 -05:00
|
|
|
#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;
|
2025-09-24 00:39:44 -05:00
|
|
|
|
|
|
|
|
typedef struct pxl8_tilemap_layer {
|
2025-09-27 11:03:36 -05:00
|
|
|
pxl8_tile_chunk** chunks;
|
|
|
|
|
u32 chunks_wide;
|
|
|
|
|
u32 chunks_high;
|
|
|
|
|
u32 chunk_count;
|
|
|
|
|
u32 allocated_chunks;
|
2025-09-24 00:39:44 -05:00
|
|
|
bool visible;
|
|
|
|
|
u8 opacity;
|
|
|
|
|
} pxl8_tilemap_layer;
|
|
|
|
|
|
|
|
|
|
typedef struct pxl8_tilemap {
|
|
|
|
|
pxl8_tilemap_layer layers[PXL8_MAX_TILE_LAYERS];
|
|
|
|
|
u32 width;
|
|
|
|
|
u32 height;
|
|
|
|
|
u32 tile_size;
|
|
|
|
|
u32 active_layers;
|
|
|
|
|
|
|
|
|
|
pxl8_tilesheet* tilesheet;
|
|
|
|
|
|
|
|
|
|
i32 camera_x;
|
|
|
|
|
i32 camera_y;
|
|
|
|
|
} pxl8_tilemap;
|
|
|
|
|
|
|
|
|
|
typedef struct pxl8_tilemap_view {
|
|
|
|
|
i32 x, y;
|
|
|
|
|
i32 width, height;
|
|
|
|
|
i32 tile_start_x, tile_start_y;
|
|
|
|
|
i32 tile_end_x, tile_end_y;
|
|
|
|
|
} pxl8_tilemap_view;
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
pxl8_result pxl8_tilemap_init(pxl8_tilemap* tilemap, u32 width, u32 height, u32 tile_size);
|
|
|
|
|
pxl8_result pxl8_tilemap_set_tilesheet(pxl8_tilemap* tilemap, pxl8_tilesheet* tilesheet);
|
|
|
|
|
void pxl8_tilemap_free(pxl8_tilemap* tilemap);
|
|
|
|
|
|
|
|
|
|
pxl8_result pxl8_tilemap_set_tile(pxl8_tilemap* tilemap, u32 layer, u32 x, u32 y, u16 tile_id, u8 flags);
|
|
|
|
|
pxl8_tile pxl8_tilemap_get_tile(const pxl8_tilemap* tilemap, u32 layer, u32 x, u32 y);
|
|
|
|
|
|
|
|
|
|
void pxl8_tilemap_set_camera(pxl8_tilemap* tilemap, i32 x, i32 y);
|
|
|
|
|
void pxl8_tilemap_get_view(const pxl8_tilemap* tilemap, const pxl8_gfx_ctx* gfx, pxl8_tilemap_view* view);
|
|
|
|
|
|
|
|
|
|
void pxl8_tilemap_render(const pxl8_tilemap* tilemap, pxl8_gfx_ctx* gfx);
|
|
|
|
|
void pxl8_tilemap_render_layer(const pxl8_tilemap* tilemap, pxl8_gfx_ctx* gfx, u32 layer);
|
|
|
|
|
void pxl8_tilemap_render_tile(const pxl8_tilemap* tilemap, pxl8_gfx_ctx* gfx, u16 tile_id, i32 x, i32 y, u8 flags);
|
|
|
|
|
|
|
|
|
|
bool pxl8_tilemap_is_solid(const pxl8_tilemap* tilemap, u32 x, u32 y);
|
|
|
|
|
bool pxl8_tilemap_check_collision(const pxl8_tilemap* tilemap, i32 x, i32 y, i32 w, i32 h);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2025-09-27 11:03:36 -05:00
|
|
|
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);
|
|
|
|
|
|
2025-09-24 00:39:44 -05:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
2025-09-27 11:03:36 -05:00
|
|
|
#endif
|