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