SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Toggle main menu visibility
Loading...
Searching...
No Matches
SHT.cpp
Go to the documentation of this file.
1
// ReSharper disable CppUseStructuredBinding
2
3
#include <
vtfpp/SHT.h
>
4
5
#include <BufferStream.h>
6
7
using namespace
sourcepp
;
8
using namespace
vtfpp
;
9
10
SHT::SHT
() :
opened
(true) {}
11
12
SHT::SHT
(std::span<const std::byte> shtData) {
13
BufferStreamReadOnly stream{shtData};
14
15
stream >> this->
version
;
16
17
this->
sequences
.resize(stream.read<uint32_t>());
18
for
(
auto
& sequence : this->
sequences
) {
19
stream >> sequence.id;
20
sequence.loop = stream.read<uint32_t>();
21
sequence.frames.resize(stream.read<uint32_t>());
22
stream >> sequence.durationTotal;
23
24
for
(
auto
& frame : sequence.frames) {
25
frame.duration = stream.read<
float
>();
26
for
(uint8_t i = 0; i < this->
getFrameBoundsCount
(); i++) {
27
auto
& bounds = frame.bounds[i];
28
stream >> bounds.x1 >> bounds.y1 >> bounds.x2 >> bounds.y2;
29
}
30
}
31
}
32
33
this->
opened
=
true
;
34
}
35
36
SHT::SHT
(
const
std::filesystem::path& shtPath)
37
:
SHT
(
fs
::readFileBuffer(shtPath)) {}
38
39
SHT::operator bool()
const
{
40
return
this->
opened
;
41
}
42
43
uint32_t
SHT::getVersion
()
const
{
44
return
this->
version
;
45
}
46
47
void
SHT::setVersion
(uint32_t v) {
48
if
(v != 0 && v != 1) {
49
return
;
50
}
51
this->
version
= v;
52
}
53
54
const
std::vector<SHT::Sequence>&
SHT::getSequences
()
const
{
55
return
this->
sequences
;
56
}
57
58
std::vector<SHT::Sequence>&
SHT::getSequences
() {
59
return
this->
sequences
;
60
}
61
62
const
SHT::Sequence
*
SHT::getSequenceFromID
(uint32_t
id
)
const
{
63
if
(
const
auto
pos = std::ranges::find_if(this->
sequences
, [
id
](
const
Sequence
& sequence) {
64
return
sequence.
id
== id;
65
}); pos != this->
sequences
.end()) {
66
return
&*pos;
67
}
68
return
nullptr
;
69
}
70
71
SHT::Sequence
*
SHT::getSequenceFromID
(uint32_t
id
) {
72
if
(
const
auto
pos = std::ranges::find_if(this->
sequences
, [
id
](
const
Sequence
& sequence) {
73
return
sequence.
id
== id;
74
}); pos != this->
sequences
.end()) {
75
return
&*pos;
76
}
77
return
nullptr
;
78
}
79
80
uint8_t
SHT::getFrameBoundsCount
()
const
{
81
return
this->
version
> 0 ? 4 : 1;
82
}
83
84
std::vector<std::byte>
SHT::bake
()
const
{
85
if
(!this->
opened
) {
86
return
{};
87
}
88
89
std::vector<std::byte> sheetData;
90
BufferStream stream{sheetData};
91
92
stream
93
.write<uint32_t>(this->
version
)
94
.write<uint32_t>(this->
sequences
.size());
95
96
for
(
const
auto
& sequence : this->
sequences
) {
97
stream
98
.write<uint32_t>(sequence.id)
99
.write<uint32_t>(sequence.loop)
100
.write<uint32_t>(sequence.frames.size())
101
.write<
float
>(sequence.durationTotal);
102
103
for
(
const
auto
& frame : sequence.frames) {
104
stream.write<
float
>(frame.duration);
105
106
for
(uint8_t i = 0; i < this->
getFrameBoundsCount
(); i++) {
107
auto
& bounds = frame.bounds[i];
108
stream << bounds.x1 << bounds.y1 << bounds.x2 << bounds.y2;
109
}
110
}
111
}
112
113
sheetData.resize(stream.tell());
114
return
sheetData;
115
}
116
117
bool
SHT::bake
(
const
std::filesystem::path& shtPath)
const
{
118
return
fs::writeFileBuffer
(shtPath, this->
bake
());
119
}
SHT.h
vtfpp::SHT::getSequenceFromID
const Sequence * getSequenceFromID(uint32_t id) const
Definition
SHT.cpp:62
vtfpp::SHT::version
uint32_t version
Definition
SHT.h:70
vtfpp::SHT::opened
bool opened
Definition
SHT.h:68
vtfpp::SHT::sequences
std::vector< Sequence > sequences
Definition
SHT.h:71
vtfpp::SHT::setVersion
void setVersion(uint32_t v)
Definition
SHT.cpp:47
vtfpp::SHT::getVersion
uint32_t getVersion() const
Definition
SHT.cpp:43
vtfpp::SHT::getFrameBoundsCount
uint8_t getFrameBoundsCount() const
Definition
SHT.cpp:80
vtfpp::SHT::bake
std::vector< std::byte > bake() const
Definition
SHT.cpp:84
vtfpp::SHT::getSequences
const std::vector< Sequence > & getSequences() const
Definition
SHT.cpp:54
vtfpp::SHT::SHT
SHT()
Definition
SHT.cpp:10
sourcepp::fs
Definition
FS.h:9
sourcepp::fs::writeFileBuffer
bool writeFileBuffer(const std::filesystem::path &filepath, std::span< const std::byte > buffer)
Definition
FS.cpp:37
sourcepp
Definition
LZMA.h:11
vtfpp
Definition
DistanceMapping.h:8
vtfpp::SHT::Sequence
Definition
SHT.h:14
vtfpp::SHT::Sequence::id
uint32_t id
Definition
SHT.h:35
src
vtfpp
SHT.cpp
Generated on
for SourcePP by
1.17.0