diff options
| author | 3gg <3gg@shellblade.net> | 2025-07-14 08:52:15 -0700 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-07-14 08:52:15 -0700 |
| commit | 6cefdd40dd62f098874bd706720db731341bc957 (patch) | |
| tree | e38a31025af970b25a9068f0ec5a36ac0945085a /include | |
| parent | 39ab6c2bce7434e45c3957168278cacf9dcd19f5 (diff) | |
Move file format defs to separate header
Diffstat (limited to 'include')
| -rw-r--r-- | include/isogfx/asset.h | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/include/isogfx/asset.h b/include/isogfx/asset.h new file mode 100644 index 0000000..298c469 --- /dev/null +++ b/include/isogfx/asset.h | |||
| @@ -0,0 +1,132 @@ | |||
| 1 | /* | ||
| 2 | * File format definitions for isogfx assets. | ||
| 3 | * | ||
| 4 | * All formats are defined so that they can be simply read into memory and used | ||
| 5 | * by the engine. Likewise, a tool that generates these formats can generate | ||
| 6 | * the structures in memory and then write them as is to a file. | ||
| 7 | */ | ||
| 8 | #pragma once | ||
| 9 | |||
| 10 | #include <assert.h> | ||
| 11 | #include <stdint.h> | ||
| 12 | |||
| 13 | // Maximum length of path strings in .TS and .TM files. | ||
| 14 | #define ISOGFX_MAX_PATH_LENGTH 128 | ||
| 15 | |||
| 16 | // ----------------------------------------------------------------------------- | ||
| 17 | // Tile set (TS) file format. | ||
| 18 | // ----------------------------------------------------------------------------- | ||
| 19 | |||
| 20 | typedef struct Ts_Tile { | ||
| 21 | uint16_t width; // Tile width in pixels. | ||
| 22 | uint16_t height; // Tile height in pixels. | ||
| 23 | Pixel pixels[1]; // Count: width * height. | ||
| 24 | } Ts_Tile; | ||
| 25 | |||
| 26 | typedef struct Ts_TileSet { | ||
| 27 | uint16_t num_tiles; | ||
| 28 | uint16_t max_tile_width; // Maximum tile width in pixels. | ||
| 29 | uint16_t max_tile_height; // Maximum tile height in pixels. | ||
| 30 | Ts_Tile tiles[1]; // Count: num_tiles. | ||
| 31 | } Ts_TileSet; | ||
| 32 | |||
| 33 | // ----------------------------------------------------------------------------- | ||
| 34 | // Tile map (TM) file format. | ||
| 35 | // ----------------------------------------------------------------------------- | ||
| 36 | |||
| 37 | typedef struct Tm_Layer { | ||
| 38 | union { | ||
| 39 | char tileset_path[ISOGFX_MAX_PATH_LENGTH]; // Relative to the Tm_Map file. | ||
| 40 | }; | ||
| 41 | Tile tiles[1]; // Count: world_width * world_height. | ||
| 42 | } Tm_Layer; | ||
| 43 | |||
| 44 | typedef struct Tm_Map { | ||
| 45 | uint16_t world_width; // World width in number of tiles. | ||
| 46 | uint16_t world_height; // World height in number of tiles. | ||
| 47 | uint16_t base_tile_width; | ||
| 48 | uint16_t base_tile_height; | ||
| 49 | uint16_t num_layers; | ||
| 50 | Tm_Layer layers[1]; // Count: num_layers. | ||
| 51 | } Tm_Map; | ||
| 52 | |||
| 53 | // ----------------------------------------------------------------------------- | ||
| 54 | // Sprite sheet file format. | ||
| 55 | // ----------------------------------------------------------------------------- | ||
| 56 | |||
| 57 | /// A row of sprites in a sprite sheet. | ||
| 58 | /// | ||
| 59 | /// Each row in a sprite sheet can have a different number of columns. | ||
| 60 | /// | ||
| 61 | /// The pixels of the row follow a "sprite-major" order. It contains the | ||
| 62 | /// 'sprite_width * sprite_height' pixels for the first column/sprite, then the | ||
| 63 | /// second column/sprite, etc. | ||
| 64 | /// | ||
| 65 | /// Pixels are 8-bit indices into the sprite sheet's colour palette. | ||
| 66 | typedef struct Ss_Row { | ||
| 67 | uint16_t num_cols; /// Number of columns in this row. | ||
| 68 | uint8_t pixels[1]; /// Count: num_cols * sprite_width * sprite_height. | ||
| 69 | } Ss_Row; | ||
| 70 | |||
| 71 | typedef struct Ss_Palette { | ||
| 72 | uint16_t num_colours; | ||
| 73 | Pixel colours[1]; /// Count: num_colors. | ||
| 74 | } Ss_Palette; | ||
| 75 | |||
| 76 | /// Sprite sheet top-level data definition. | ||
| 77 | /// | ||
| 78 | /// Sprite width and height are assumed constant throughout the sprite sheet. | ||
| 79 | typedef struct Ss_SpriteSheet { | ||
| 80 | uint16_t sprite_width; /// Sprite width in pixels. | ||
| 81 | uint16_t sprite_height; /// Sprite height in pixels. | ||
| 82 | uint16_t num_rows; | ||
| 83 | Ss_Palette palette; /// Variable size. | ||
| 84 | Ss_Row rows[1]; /// Count: num_rows. Variable offset. | ||
| 85 | } Ss_SpriteSheet; | ||
| 86 | |||
| 87 | // ----------------------------------------------------------------------------- | ||
| 88 | // Data accessors. | ||
| 89 | // ----------------------------------------------------------------------------- | ||
| 90 | |||
| 91 | /// Return the next tile in the tile set. | ||
| 92 | static inline const Ts_Tile* ts_tileset_get_next_tile( | ||
| 93 | const Ts_TileSet* tileset, const Ts_Tile* tile) { | ||
| 94 | assert(tileset); | ||
| 95 | assert(tile); | ||
| 96 | return (const Ts_Tile*)((const uint8_t*)tile + sizeof(Ts_Tile) + | ||
| 97 | ((tile->width * tile->height - 1) * sizeof(Pixel))); | ||
| 98 | } | ||
| 99 | |||
| 100 | /// Return the next layer in the tile map. | ||
| 101 | static inline const Tm_Layer* tm_map_get_next_layer( | ||
| 102 | const Tm_Map* map, const Tm_Layer* layer) { | ||
| 103 | assert(map); | ||
| 104 | assert(layer); | ||
| 105 | return (const Tm_Layer*)((const uint8_t*)layer + sizeof(Tm_Layer) + | ||
| 106 | ((map->world_width * map->world_height - 1) * | ||
| 107 | sizeof(Tile))); | ||
| 108 | } | ||
| 109 | |||
| 110 | /// Return the ith row in the sprite sheet. | ||
| 111 | static inline const Ss_Row* get_sprite_sheet_row( | ||
| 112 | const Ss_SpriteSheet* sheet, int row) { | ||
| 113 | assert(sheet); | ||
| 114 | assert(row >= 0); | ||
| 115 | assert(row < sheet->num_rows); | ||
| 116 | // Skip over the palette. | ||
| 117 | const Ss_Row* rows = | ||
| 118 | (const Ss_Row*)(&sheet->palette.colours[0] + sheet->palette.num_colours); | ||
| 119 | return &rows[row]; | ||
| 120 | } | ||
| 121 | |||
| 122 | /// Return the ith sprite in the row. | ||
| 123 | static inline const uint8_t* get_sprite_sheet_sprite( | ||
| 124 | const Ss_SpriteSheet* sheet, const Ss_Row* row, int col) { | ||
| 125 | assert(sheet); | ||
| 126 | assert(row); | ||
| 127 | assert(col >= 0); | ||
| 128 | assert(col < row->num_cols); | ||
| 129 | const int sprite_offset = col * sheet->sprite_width * sheet->sprite_height; | ||
| 130 | const uint8_t* sprite = &row->pixels[sprite_offset]; | ||
| 131 | return sprite; | ||
| 132 | } | ||
