summaryrefslogtreecommitdiff
path: root/gfx-iso/include/isogfx/isogfx.h
blob: e96606c96aeffc052fa954946e6bcd20020580d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * 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);

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