rogged/libs/tileset/tileset.h
NotAShelf ceb657add8
tileset: initial GPU texture atlas w/ procedural sprite painting
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I7b6991f7342362033ad72ab4700fdb9f6a6a6964
2026-04-28 15:33:12 +03:00

91 lines
3.4 KiB
C

#ifndef TILESET_H
#define TILESET_H
#include "settings.h"
#include <raylib.h>
// Maximum number of tiles that can be registered in a single atlas
#define MAX_TILE_ID 32
// Tile IDs for map tiles (variants for visual variety)
#define TILE_WALL_0 0
#define TILE_WALL_1 1
#define TILE_FLOOR_0 2
#define TILE_FLOOR_1 3
#define TILE_FLOOR_2 4
#define TILE_FLOOR_3 5
#define TILE_STAIRS_SPRITE 6
// Sprite IDs for entities
#define SPRITE_PLAYER 7
#define SPRITE_PLAYER_WALK_0 8
#define SPRITE_PLAYER_WALK_1 9
#define SPRITE_PLAYER_ATTACK 10
#define SPRITE_ENEMY_GOBLIN 11
#define SPRITE_ENEMY_GOBLIN_WALK_0 12
#define SPRITE_ENEMY_GOBLIN_WALK_1 13
#define SPRITE_ENEMY_GOBLIN_ATTACK 14
#define SPRITE_ENEMY_SKELETON 15
#define SPRITE_ENEMY_SKELETON_WALK_0 16
#define SPRITE_ENEMY_SKELETON_WALK_1 17
#define SPRITE_ENEMY_SKELETON_ATTACK 18
#define SPRITE_ENEMY_ORC 19
#define SPRITE_ENEMY_ORC_WALK_0 20
#define SPRITE_ENEMY_ORC_WALK_1 21
#define SPRITE_ENEMY_ORC_ATTACK 22
#define SPRITE_ITEM_POTION 23
#define SPRITE_ITEM_WEAPON 24
#define SPRITE_ITEM_ARMOR 25
// Door tiles
#define TILE_DOOR_CLOSED_SPRITE 26
#define TILE_DOOR_OPEN_SPRITE 27
// Effect/status sprites
#define SPRITE_EFFECT_BURN 28
#define SPRITE_EFFECT_POISON 29
#define SPRITE_EFFECT_BLOCK 30
#define SPRITE_SLASH_EFFECT 31
// Total count of defined tiles
#define NUM_TILE_IDS 32
// Tileset encapsulates a GPU texture atlas with sub-rectangle regions per tile ID.
// The atlas is built at startup by painting into a RenderTexture, then finalized
// into a regular Texture2D for efficient drawing via DrawTexturePro.
typedef struct {
RenderTexture2D render_target; // RenderTexture for painting (valid before finalize)
Texture2D atlas; // GPU texture (valid after finalize)
int tile_w; // width of each tile in pixels
int tile_h; // height of each tile in pixels
Rectangle regions[MAX_TILE_ID]; // sub-rectangles within atlas for each tile ID
int tile_count; // number of registered tiles
int atlas_cols; // number of columns in the atlas grid
int atlas_rows; // number of rows in the atlas grid
int finalized; // 1 after tileset_finalize called, 0 otherwise
} Tileset;
// Initialize a tileset with the given tile dimensions.
// Computes atlas grid size based on MAX_TILE_ID and allocates a RenderTexture.
// Returns 0 on failure (e.g., RenderTexture allocation failed), non-zero on success.
int tileset_init(Tileset *ts, int tile_w, int tile_h);
// Register a tile ID with its atlas region.
// The region is computed automatically based on tile_w/tile_h and the ID index.
// Returns 0 if the ID is out of bounds or already registered, non-zero on success.
int tileset_register(Tileset *ts, int id);
// Finalize the tileset: converts the internal RenderTexture into a regular Texture2D
// suitable for DrawTexturePro. After this call, painting functions must not be used.
// Returns 0 on failure, non-zero on success.
int tileset_finalize(Tileset *ts);
// Get the atlas sub-rectangle for a given tile ID.
// Returns a zeroed Rectangle if the ID is invalid or not registered.
Rectangle tileset_get_region(const Tileset *ts, int id);
// Destroy a tileset, unloading the atlas texture and zeroing the struct.
// Safe to call on a zero-initialized or already-destroyed tileset.
void tileset_destroy(Tileset *ts);
#endif // TILESET_H