Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
SpriteCustomizer.cpp
Go to the documentation of this file.
1
11#include "RType/Logger.hpp"
12
13namespace rtp::client {
14
16 {
17 static SpriteCustomizer instance;
18 return instance;
19 }
20
22 : _spriteMappingPath("config/client/sprite_mappings.json")
23 {
25 }
26
28 {
29 try {
30 if (!std::filesystem::exists(_spriteMappingPath)) {
31 log::info("No sprite mapping file found at {}, using defaults", _spriteMappingPath.string());
32 return false;
33 }
34
36
37 if (_customSpriteMappings.empty()) {
38 log::warning("Sprite mappings file exists but is empty or invalid");
39 return false;
40 }
41
42 log::info("Loaded {} custom sprite mappings", _customSpriteMappings.size());
43 return true;
44 } catch (const std::exception& e) {
45 log::error("Failed to load sprite mappings: {}", e.what());
46 return false;
47 }
48 }
49
51 {
54 log::info("Sprite mappings reloaded");
55 }
56
57 bool SpriteCustomizer::hasCustomSprite(const std::string& entityName) const
58 {
59 bool found = _customSpriteMappings.find(entityName) != _customSpriteMappings.end();
60 // Log disabled to avoid spam - uncomment for debugging:
61 // if (!found) {
62 // log::debug("SpriteCustomizer: No custom sprite for '{}'. Available keys: {}",
63 // entityName, _customSpriteMappings.size());
64 // }
65 return found;
66 }
67
68 std::string SpriteCustomizer::getSpritePath(const std::string& entityName, const std::string& defaultPath) const
69 {
70 auto it = _customSpriteMappings.find(entityName);
71 if (it != _customSpriteMappings.end()) {
72 return it->second;
73 }
74 return defaultPath;
75 }
76
77 std::string SpriteCustomizer::getSpriteInfo(const std::string& entityName,
78 const std::string& defaultPath,
79 int& outLeft, int& outTop,
80 int& outWidth, int& outHeight) const
81 {
82 auto it = _customSpriteMappings.find(entityName);
83 if (it != _customSpriteMappings.end()) {
84 // Custom sprite: use full image (already resized to match original dimensions in ModMenu)
85 outLeft = 0;
86 outTop = 0;
87 // Keep outWidth and outHeight unchanged - custom sprite is already resized to match
88 log::debug("SpriteCustomizer: Using custom sprite '{}' with original dimensions {}x{}",
89 entityName, outWidth, outHeight);
90
91 return it->second;
92 }
93
94 // Default sprite: use provided coordinates
95 return defaultPath;
96 }
97
98 // Static variables for render systems
99 static void* s_worldRenderSystem = nullptr;
100 static void* s_uiRenderSystem = nullptr;
101
102 void SpriteCustomizer::setRenderSystems(void* worldRenderSystem, void* uiRenderSystem)
103 {
104 s_worldRenderSystem = worldRenderSystem;
105 s_uiRenderSystem = uiRenderSystem;
106 }
107
109 {
110 try {
112 auto* renderSystem = static_cast<client::RenderSystem*>(s_worldRenderSystem);
113 renderSystem->clearTextureCache();
114 log::info("SpriteCustomizer: Cleared world render system texture cache");
115 }
116
117 if (s_uiRenderSystem) {
118 auto* uiRenderSystem = static_cast<client::systems::UIRenderSystem*>(s_uiRenderSystem);
119 uiRenderSystem->clearTextureCache();
120 log::info("SpriteCustomizer: Cleared UI render system texture cache");
121 }
122 } catch (const std::exception& e) {
123 log::error("SpriteCustomizer: Error clearing texture caches: {}", e.what());
124 }
125 }
126
127} // namespace rtp::client
Logger declaration with support for multiple log levels.
Singleton class to manage custom sprite mappings globally.
std::filesystem::path _spriteMappingPath
static void clearTextureCaches()
Clear all texture caches in render systems This should be called when sprites are modified to ensure ...
void reloadMappings()
Reload sprite mappings (useful after modifications) This clears the cache and reloads from disk.
bool loadMappings()
Load sprite mappings from config file.
std::unordered_map< std::string, std::string > _customSpriteMappings
static void setRenderSystems(void *worldRenderSystem, void *uiRenderSystem)
Set render system pointers for cache clearing Called internally by Application during initialization.
std::string getSpriteInfo(const std::string &entityName, const std::string &defaultPath, int &outLeft, int &outTop, int &outWidth, int &outHeight) const
Get custom sprite info (path and coordinates for full sprite)
bool hasCustomSprite(const std::string &entityName) const
Check if an entity has a custom sprite.
std::string getSpritePath(const std::string &entityName, const std::string &defaultPath) const
Get the custom sprite path for an entity.
static SpriteCustomizer & getInstance()
Get the singleton instance.
R-Type client namespace.
static void * s_worldRenderSystem
static void * s_uiRenderSystem
std::unordered_map< std::string, std::string > loadSpriteMappings(const std::string &filepath="config/client/sprite_mappings.json")
Load sprite mappings from a JSON file.
void error(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an error message.
void warning(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log a warning message.
void info(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an informational message.
void debug(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log a debug message.