Loading...
Searching...
No Matches
TextureSerializer.cpp
Go to the documentation of this file.
1//
2// Created by denzel on 08/12/2025.
3//
4
6
7#include <fstream>
8
9#include "json.hpp"
10
11namespace hellfire {
12 static std::filesystem::path get_meta_path(const std::filesystem::path& texture_path) {
13 return texture_path.string() + ".meta";
14 }
15
16 bool TextureSerializer::save_metadata(const std::filesystem::path &texture_path, const TextureMetadata &meta) {
17 nlohmann::json j;
18
19 j["type"] = static_cast<int>(meta.type);
20 j["generate_mipmaps"] = meta.generate_mipmaps;
21 j["srgb"] = meta.srgb;
22 j["filter"] = static_cast<int>(meta.filter);
23 j["wrap_u"] = static_cast<int>(meta.wrap_u);
24 j["wrap_v"] = static_cast<int>(meta.wrap_v);
25 j["compressed"] = meta.compressed;
26 j["compression_format"] = meta.compression_format;
27
28 std::ofstream file(get_meta_path(texture_path));
29 if (!file) return false;
30
31 file << j.dump(2);
32 return file.good();
33 }
34
35 std::optional<TextureMetadata> TextureSerializer::load_metadata(const std::filesystem::path &texture_path) {
36 const auto meta_path = get_meta_path(texture_path);
37 if (!std::filesystem::exists(meta_path)) {
38 return std::nullopt; // No metadata, use defaults
39 }
40
41 std::ifstream file(meta_path);
42 if (!file) return std::nullopt;
43
44 try {
45 nlohmann::json j;
46 file >> j;
47
48 TextureMetadata meta;
49 meta.type = static_cast<TextureType>(j.value("type", 0));
50 meta.generate_mipmaps = j.value("generate_mipmaps", true);
51 meta.srgb = j.value("srgb", true);
52 meta.filter = static_cast<TextureMetadata::FilterMode>(j.value("filter", 2));
53 meta.wrap_u = static_cast<TextureMetadata::WrapMode>(j.value("wrap_u", 0));
54 meta.wrap_v = static_cast<TextureMetadata::WrapMode>(j.value("wrap_v", 0));
55 meta.compressed = j.value("compressed", false);
56 meta.compression_format = j.value("compression_format", "");
57
58 return meta;
59 } catch (...) {
60 return std::nullopt;
61 }
62 }
63} // hellfire
static bool save_metadata(const std::filesystem::path &texture_path, const TextureMetadata &meta)
static std::filesystem::path get_meta_path(const std::filesystem::path &texture_path)
TextureType
Definition Texture.h:13
Metadata for texture assets.