Loading...
Searching...
No Matches
TextureSerializer.cpp
Go to the documentation of this file.
1
//
2
// Created by denzel on 08/12/2025.
3
//
4
5
#
include
"TextureSerializer.h"
6
7
#
include
<
fstream
>
8
9
#
include
"json.hpp"
10
11
namespace
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
hellfire::TextureSerializer
Definition
TextureSerializer.h:35
hellfire::TextureSerializer::save_metadata
static bool save_metadata(const std::filesystem::path &texture_path, const TextureMetadata &meta)
Definition
TextureSerializer.cpp:16
hellfire
Definition
AssetManager.cpp:10
hellfire::get_meta_path
static std::filesystem::path get_meta_path(const std::filesystem::path &texture_path)
Definition
TextureSerializer.cpp:12
hellfire::TextureType
TextureType
Definition
Texture.h:13
hellfire::TextureMetadata
Metadata for texture assets.
Definition
TextureSerializer.h:16
hellfire::TextureMetadata::filter
FilterMode filter
Definition
TextureSerializer.h:23
hellfire::TextureMetadata::compressed
bool compressed
Definition
TextureSerializer.h:31
hellfire::TextureMetadata::wrap_v
WrapMode wrap_v
Definition
TextureSerializer.h:28
hellfire::TextureMetadata::FilterMode
FilterMode
Definition
TextureSerializer.h:22
hellfire::TextureMetadata::WrapMode
WrapMode
Definition
TextureSerializer.h:26
hellfire::TextureMetadata::wrap_u
WrapMode wrap_u
Definition
TextureSerializer.h:27
hellfire::TextureMetadata::compression_format
std::string compression_format
Definition
TextureSerializer.h:32
hellfire::TextureMetadata::srgb
bool srgb
Definition
TextureSerializer.h:19
hellfire::TextureMetadata::type
TextureType type
Definition
TextureSerializer.h:17
hellfire::TextureMetadata::generate_mipmaps
bool generate_mipmaps
Definition
TextureSerializer.h:18
engine
src
hellfire
serializers
TextureSerializer.cpp
Generated by
1.9.8