pxl8/client/src/gfx/pxl8_tilemap.h

124 lines
4.1 KiB
C
Raw Normal View History

2025-09-24 00:39:44 -05:00
#pragma once
#include "pxl8_gfx.h"
#include "pxl8_tilesheet.h"
#include "pxl8_types.h"
2025-09-24 00:39:44 -05:00
2025-12-03 09:41:33 -06:00
#define PXL8_TILE_SIZE 8
2025-09-24 00:39:44 -05:00
#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 current_frame;
f32 frame_duration;
2025-10-04 04:13:48 -05:00
u16 frame_count;
u16* frames;
2025-09-27 11:03:36 -05:00
f32 time_accumulator;
} pxl8_tile_animation;
typedef struct pxl8_tile_properties {
i16 collision_offset_x;
i16 collision_offset_y;
u16 collision_height;
2025-10-04 04:13:48 -05:00
u16 collision_width;
u32 property_flags;
void* user_data;
2025-09-27 11:03:36 -05:00
} pxl8_tile_properties;
typedef struct pxl8_autotile_rule {
u8 neighbor_mask;
u16 tile_id;
} pxl8_autotile_rule;
typedef struct pxl8_tile_chunk {
u32 chunk_x;
u32 chunk_y;
bool empty;
2025-10-04 04:13:48 -05:00
pxl8_tile tiles[PXL8_CHUNK_SIZE * PXL8_CHUNK_SIZE];
2025-09-27 11:03:36 -05:00
} pxl8_tile_chunk;
2025-09-24 00:39:44 -05:00
2025-10-04 04:13:48 -05:00
typedef struct pxl8_tilemap_layer pxl8_tilemap_layer;
typedef struct pxl8_tilemap pxl8_tilemap;
2025-09-24 00:39:44 -05:00
typedef struct pxl8_tilemap_view {
2025-10-04 04:13:48 -05:00
i32 height;
2025-09-24 00:39:44 -05:00
i32 tile_end_x, tile_end_y;
2025-10-04 04:13:48 -05:00
i32 tile_start_x, tile_start_y;
i32 width;
i32 x, y;
2025-09-24 00:39:44 -05:00
} pxl8_tilemap_view;
#ifdef __cplusplus
extern "C" {
#endif
2025-10-04 04:13:48 -05:00
pxl8_tilemap* pxl8_tilemap_create(u32 width, u32 height, u32 tile_size);
void pxl8_tilemap_destroy(pxl8_tilemap* tilemap);
2025-11-27 15:48:11 -06:00
2025-11-01 12:39:59 -05:00
u32 pxl8_tilemap_get_height(const pxl8_tilemap* tilemap);
2025-11-27 15:48:11 -06:00
u32 pxl8_tilemap_get_memory_usage(const pxl8_tilemap* tilemap);
2025-11-01 12:39:59 -05:00
pxl8_tile pxl8_tilemap_get_tile(const pxl8_tilemap* tilemap, u32 layer, u32 x, u32 y);
2025-11-27 15:48:11 -06:00
u16 pxl8_tilemap_get_tile_id(const pxl8_tilemap* tilemap, u32 layer, u32 x, u32 y);
2025-11-01 12:39:59 -05:00
u32 pxl8_tilemap_get_tile_size(const pxl8_tilemap* tilemap);
void* pxl8_tilemap_get_tile_user_data(const pxl8_tilemap* tilemap, u16 tile_id);
2025-11-27 15:48:11 -06:00
void pxl8_tilemap_get_view(const pxl8_tilemap* tilemap, const pxl8_gfx* gfx, pxl8_tilemap_view* view);
2025-11-01 12:39:59 -05:00
u32 pxl8_tilemap_get_width(const pxl8_tilemap* tilemap);
2025-11-27 15:48:11 -06:00
void pxl8_tilemap_set_camera(pxl8_tilemap* tilemap, i32 x, i32 y);
pxl8_result pxl8_tilemap_set_tile(pxl8_tilemap* tilemap, u32 layer, u32 x, u32 y, u16 tile_id, u8 flags);
pxl8_result pxl8_tilemap_set_tile_auto(pxl8_tilemap* tilemap, u32 layer, u32 x, u32 y, u16 base_tile_id, u8 flags);
2025-11-01 12:39:59 -05:00
void pxl8_tilemap_set_tile_user_data(pxl8_tilemap* tilemap, u16 tile_id, void* user_data);
2025-11-27 15:48:11 -06:00
pxl8_result pxl8_tilemap_set_tilesheet(pxl8_tilemap* tilemap, pxl8_tilesheet* tilesheet);
pxl8_result pxl8_tilemap_load_ase(pxl8_tilemap* tilemap, const char* filepath, u32 layer);
2025-10-04 04:13:48 -05:00
2025-09-28 13:10:29 -05:00
bool pxl8_tilemap_check_collision(const pxl8_tilemap* tilemap, i32 x, i32 y, i32 w, i32 h);
void pxl8_tilemap_compress(pxl8_tilemap* tilemap);
bool pxl8_tilemap_is_solid(const pxl8_tilemap* tilemap, u32 x, u32 y);
2025-10-04 04:13:48 -05:00
void pxl8_tilemap_render(const pxl8_tilemap* tilemap, pxl8_gfx* gfx);
void pxl8_tilemap_render_layer(const pxl8_tilemap* tilemap, pxl8_gfx* gfx, u32 layer);
2025-11-27 15:48:11 -06:00
void pxl8_tilemap_render_tile(const pxl8_tilemap* tilemap, pxl8_gfx* gfx, u16 tile_id, i32 x, i32 y, u8 flags);
2025-09-27 11:03:36 -05:00
void pxl8_tilemap_update(pxl8_tilemap* tilemap, f32 delta_time);
2025-11-27 15:48:11 -06:00
void pxl8_tilemap_update_autotiles(pxl8_tilemap* tilemap, u32 layer, u32 x, u32 y, u32 w, u32 h);
2025-09-27 11:03:36 -05:00
2025-09-24 00:39:44 -05:00
#ifdef __cplusplus
}
2025-09-27 11:03:36 -05:00
#endif