Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
TilesetAsset.hpp
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <vector>
4#include <unordered_map>
5#include <glm/glm.hpp>
6#include "core/EngineAPI.hpp"
7#include <vulkan/vulkan.h>
8
10class ResourceManager;
11
12namespace Engine {
13
21 int id = -1;
23 std::string name;
25 std::string texturePath;
27 bool isSolid = false;
28
29 // --- Infinite palette grid position (persisted) ---
31 int gridX = 0;
33 int gridY = 0;
34
35 // --- Runtime atlas data (not persisted) ---
37 glm::vec4 atlasUV{ 0.f, 0.f, 1.f, 1.f };
38 };
39
49 std::string name;
51 std::string filePath;
53 int tileWidth = 16;
55 int tileHeight = 16;
56
61 std::vector<TileAsset> tiles;
62
63 // --- Runtime atlas (rebuilt when tileset changes) ---
66 std::vector<uint8_t> pixels;
67 int atlasWidth = 0;
68 int atlasHeight = 0;
70 VkDescriptorSet descriptorSet = VK_NULL_HANDLE;
71 bool valid = false;
72 };
74
75 // ------------------------------------------------------------------
76 // Disk I/O helpers
77 // ------------------------------------------------------------------
78
86 static TilesetAsset loadFromFile(const std::string& path);
87
93 static void saveToFile(const TilesetAsset& ts);
94
100 static TileAsset loadTileFile(const std::string& path);
101
107 static void saveTileFile(const TileAsset& tile, const std::string& path);
108
109 // ------------------------------------------------------------------
110 // Atlas building
111 // ------------------------------------------------------------------
112
119 void buildAtlas(VulkanRenderer& renderer);
120
125 void destroyAtlas(VulkanRenderer& renderer);
126 };
127
128 // ------------------------------------------------------------------
129 // Global in-memory cache (keyed by canonical file path)
130 // ------------------------------------------------------------------
131
136 ENGINE_API std::unordered_map<std::string, TilesetAsset>& getTilesetCache();
137
144 ENGINE_API TilesetAsset* loadOrGetTileset(const std::string& path, VulkanRenderer& renderer);
145
149 ENGINE_API void invalidateTilesetCache(const std::string& path = "");
150
151} // namespace Engine
#define ENGINE_API
Definition EngineAPI.hpp:10
Manages loading, caching, and GPU memory uploads for textures and glTF model meshes.
Definition ResourceManager.hpp:46
Manages core Vulkan pipeline states, device interaction, frames, buffers, and command submissions.
Definition VulkanRenderer.hpp:127
Definition Application.hpp:16
ENGINE_API TilesetAsset * loadOrGetTileset(const std::string &path, VulkanRenderer &renderer)
Loads a TilesetAsset from disk (or returns a cached copy). If the tileset file changes on disk,...
Definition TilesetAsset.cpp:34
ENGINE_API std::unordered_map< std::string, TilesetAsset > & getTilesetCache()
Returns a reference to the process-wide TilesetAsset cache. The TilemapSystem uses this to avoid relo...
Definition TilesetAsset.cpp:20
ENGINE_API void invalidateTilesetCache(const std::string &path="")
Evicts the specified entry (or all entries if path is empty) from the cache.
Definition TilesetAsset.cpp:25
Represents one tile slot in a tileset. Saved to disk as a small <name>.tile JSON file.
Definition TilesetAsset.hpp:19
int gridY
Row on the infinite palette grid (can be negative).
Definition TilesetAsset.hpp:33
bool isSolid
True = solid collider spawned for cells containing this tile.
Definition TilesetAsset.hpp:27
glm::vec4 atlasUV
Normalised UV rect in the runtime atlas: (u0, v0, u1, v1).
Definition TilesetAsset.hpp:37
int gridX
Column on the infinite palette grid (can be negative).
Definition TilesetAsset.hpp:31
std::string texturePath
Project-relative path to the tile's texture image.
Definition TilesetAsset.hpp:25
std::string name
Display name shown in the palette.
Definition TilesetAsset.hpp:23
Definition TilesetAsset.hpp:64
std::vector< uint8_t > pixels
Packed atlas pixel data (RGBA8, CPU side).
Definition TilesetAsset.hpp:66
int atlasWidth
Definition TilesetAsset.hpp:67
VkDescriptorSet descriptorSet
Vulkan descriptor set for the atlas texture (owned by ResourceManager).
Definition TilesetAsset.hpp:70
int atlasHeight
Definition TilesetAsset.hpp:68
bool valid
Definition TilesetAsset.hpp:71
A disk asset (.tileset JSON) that owns an ordered list of TileAssets. At runtime, a CPU-side atlas is...
Definition TilesetAsset.hpp:47
void destroyAtlas(VulkanRenderer &renderer)
Frees the GPU-side atlas resources if they exist.
Definition TilesetAsset.cpp:297
void buildAtlas(VulkanRenderer &renderer)
Rebuilds the CPU-side atlas from tiles[].texturePath entries, uploads it as a Vulkan texture,...
Definition TilesetAsset.cpp:205
std::string filePath
Absolute (or project-relative) path to the .tileset file.
Definition TilesetAsset.hpp:51
std::string name
Display name of the tileset.
Definition TilesetAsset.hpp:49
std::vector< TileAsset > tiles
Ordered list of tile slots. tiles[i].id == i. Index is the value stored in TilemapComponent::tiles[].
Definition TilesetAsset.hpp:61
static void saveTileFile(const TileAsset &tile, const std::string &path)
Saves a single TileAsset to a .tile JSON file.
Definition TilesetAsset.cpp:93
static TilesetAsset loadFromFile(const std::string &path)
Loads a TilesetAsset from a .tileset JSON file. Individual tile paths listed in the file are loaded a...
Definition TilesetAsset.cpp:114
AtlasCache atlas
Definition TilesetAsset.hpp:73
static TileAsset loadTileFile(const std::string &path)
Loads a single TileAsset from a .tile JSON file.
Definition TilesetAsset.cpp:63
static void saveToFile(const TilesetAsset &ts)
Saves the TilesetAsset to its filePath on disk (.tileset JSON). Does NOT write individual ....
Definition TilesetAsset.cpp:168
int tileWidth
Width of each tile cell in the packed atlas (pixels).
Definition TilesetAsset.hpp:53
int tileHeight
Height of each tile cell in the packed atlas (pixels).
Definition TilesetAsset.hpp:55