/*
 * Isometric rendering engine.
 */
#pragma once

#include <stdbool.h>
#include <stdint.h>

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);