Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
SpriteMapConfig.hpp
Go to the documentation of this file.
1
10#ifndef RTYPE_CONFIG_SPRITEMAPCONFIG_HPP_
11#define RTYPE_CONFIG_SPRITEMAPCONFIG_HPP_
12
13#include "SimpleJsonParser.hpp"
14#include <unordered_map>
15#include <string>
16#include <fstream>
17#include <sstream>
18
19namespace rtp::config {
20
26 inline std::unordered_map<std::string, std::string> loadSpriteMappings(
27 const std::string& filepath = "config/client/sprite_mappings.json")
28 {
29 std::unordered_map<std::string, std::string> mappings;
30
31 std::ifstream file(filepath);
32 if (!file.is_open()) {
33 return mappings; // Return empty map if file doesn't exist
34 }
35
36 std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
37 file.close();
38
39 SimpleJson parser;
40 if (!parser.parseContent(content)) {
41 return mappings; // Return empty map if parse fails
42 }
43
44 mappings = parser.getData();
45 return mappings;
46 }
47
54 inline bool saveSpriteMappings(
55 const std::unordered_map<std::string, std::string>& mappings,
56 const std::string& filepath = "config/client/sprite_mappings.json")
57 {
58 try {
59 std::stringstream ss;
60 ss << "{\n";
61 bool first = true;
62 for (const auto& [key, path] : mappings) {
63 if (!first) ss << ",\n";
64 ss << " \"" << key << "\": \"" << path << "\"";
65 first = false;
66 }
67 ss << "\n}";
68
69 std::ofstream file(filepath);
70 if (!file.is_open()) {
71 return false;
72 }
73
74 file << ss.str();
75 return true;
76 } catch (...) {
77 return false;
78 }
79 }
80
81} // namespace rtp::config
82
83#endif // RTYPE_CONFIG_SPRITEMAPCONFIG_HPP_
bool parseContent(const std::string &jsonContent)
const std::unordered_map< std::string, std::string > & getData() const
File : SpriteMapConfig.hpp License: MIT Author : GitHub Copilot Date : 18/01/2026.
bool saveSpriteMappings(const std::unordered_map< std::string, std::string > &mappings, const std::string &filepath="config/client/sprite_mappings.json")
Save sprite mappings to a JSON file.
std::unordered_map< std::string, std::string > loadSpriteMappings(const std::string &filepath="config/client/sprite_mappings.json")
Load sprite mappings from a JSON file.