From 4ffa1895390066f2bbd347c0689a1b667a21fd24 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Fri, 27 Jun 2025 10:17:41 -0700 Subject: Initial commit --- include/isogfx/backend.h | 28 ++++++++++ include/isogfx/isogfx.h | 136 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 include/isogfx/backend.h create mode 100644 include/isogfx/isogfx.h (limited to 'include') diff --git a/include/isogfx/backend.h b/include/isogfx/backend.h new file mode 100644 index 0000000..172991d --- /dev/null +++ b/include/isogfx/backend.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +typedef struct Gfx Gfx; +typedef struct IsoGfx IsoGfx; + +typedef struct IsoBackend IsoBackend; + +/// Initialize the backend. +IsoBackend* IsoBackendInit(const IsoGfx*); + +/// Shut down the backend. +void IsoBackendShutdown(IsoBackend**); + +/// Notify the backend of a window resize event. +/// This allows the backend to determine how to position and scale the iso +/// screen buffer on the graphics window. +void IsoBackendResizeWindow(IsoBackend*, const IsoGfx*, int width, int height); + +/// Render the iso screen to the graphics window. +void IsoBackendRender(const IsoBackend*, const IsoGfx*); + +/// Map window coordinates to iso space coordinates. +/// This takes into account any possible resizing done by the backend in +/// response to calls to IsoBackendResizeWindow(). +bool IsoBackendGetMousePosition( + const IsoBackend*, double window_x, double window_y, double* x, double* y); diff --git a/include/isogfx/isogfx.h b/include/isogfx/isogfx.h new file mode 100644 index 0000000..3421a7b --- /dev/null +++ b/include/isogfx/isogfx.h @@ -0,0 +1,136 @@ +/* + * Isometric rendering engine. + */ +#pragma once + +#include +#include + +typedef struct IsoGfx IsoGfx; + +/// Sprite sheet handle. +typedef uint16_t SpriteSheet; + +/// Sprite handle. +typedef uint16_t Sprite; + +/// Tile handle. +typedef uint16_t Tile; + +/// Colour channel. +typedef uint8_t Channel; + +typedef struct Pixel { + Channel r, g, b, a; +} Pixel; + +typedef enum TileDescType { + TileFromColour, + TileFromFile, + TileFromMemory, +} TileDescType; + +typedef struct TileDesc { + TileDescType type; + int width; /// Tile width in pixels. + int height; /// Tile height in pixels. + union { + Pixel colour; /// Constant colour tile. + struct { + const char* path; + } file; + struct { + const uint8_t* data; /// sizeof(Pixel) * width * height + } mem; + }; +} TileDesc; + +typedef struct WorldDesc { + int tile_width; /// Base tile width in pixels. + int tile_height; /// Base tile height in pixels. + int world_width; /// World width in tiles. + int world_height; /// World height in tiles. + int max_num_tiles; /// 0 for an implementation-defined default. +} WorldDesc; + +typedef struct IsoGfxDesc { + int screen_width; /// Screen width in pixels. + int screen_height; /// Screen height in pixels. + int max_num_sprites; /// 0 for an implementation-defined default. + int sprite_sheet_pool_size_bytes; /// 0 for an implementation-defined default. +} IsoGfxDesc; + +/// Create a new isometric graphics engine. +IsoGfx* isogfx_new(const IsoGfxDesc*); + +/// Destroy the isometric graphics engine. +void isogfx_del(IsoGfx**); + +/// Create an empty world. +bool isogfx_make_world(IsoGfx*, const WorldDesc*); + +/// Load a world from a tile map (.TM) file. +bool isogfx_load_world(IsoGfx*, const char* filepath); + +/// Return the world's width. +int isogfx_world_width(const IsoGfx*); + +/// Return the world's height. +int isogfx_world_height(const IsoGfx*); + +/// Create a new tile. +Tile isogfx_make_tile(IsoGfx*, const TileDesc*); + +/// Set the tile at position (x,y). +void isogfx_set_tile(IsoGfx*, int x, int y, Tile); + +/// Set the tiles in positions in the range (x0,y0) - (x1,y1). +void isogfx_set_tiles(IsoGfx*, int x0, int y0, int x1, int y1, Tile); + +/// Load a sprite sheet (.SS) file. +bool isogfx_load_sprite_sheet(IsoGfx*, const char* filepath, SpriteSheet*); + +/// Create an animated sprite. +Sprite isogfx_make_sprite(IsoGfx*, SpriteSheet); + +/// Destroy the sprite. +void isogfx_del_sprite(IsoGfx*, Sprite); + +/// Destroy all the sprites. +void isogfx_del_sprites(IsoGfx*); + +/// Set the sprite's position. +void isogfx_set_sprite_position(IsoGfx*, Sprite, int x, int y); + +/// Set the sprite's current animation. +void isogfx_set_sprite_animation(IsoGfx*, Sprite, int animation); + +/// Update the renderer. +/// +/// Currently this updates the sprite animations. +void isogfx_update(IsoGfx*, double t); + +/// Render the world. +void isogfx_render(IsoGfx*); + +/// Draw/overlay a tile at position (x,y). +/// +/// This function just renders a tile at position (x,y) and should be called +/// after isogfx_render() to obtain the correct result. To set the tile at +/// position (x,y) instead, use isogfx_set_tile(). +void isogfx_draw_tile(IsoGfx*, int x, int y, Tile); + +/// Resize the virtual screen's dimensions. +bool isogfx_resize(IsoGfx*, int screen_width, int screen_height); + +/// Get the virtual screen's dimensions. +void isogfx_get_screen_size(const IsoGfx*, int* width, int* height); + +/// Return a pointer to the virtual screen's colour buffer. +/// +/// Call after each call to isogfx_render() to retrieve the render output. +const Pixel* isogfx_get_screen_buffer(const IsoGfx*); + +/// Translate Cartesian to isometric coordinates. +void isogfx_pick_tile( + const IsoGfx*, double xcart, double ycart, int* xiso, int* yiso); -- cgit v1.2.3