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
9 : opened(true)
10 , version(0) {}
11
12SHT::SHT(std::span<const std::byte> shtData) {
13 BufferStreamReadOnly stream{shtData.data(), shtData.size()};
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
36SHT::SHT(const std::string& shtPath)
37 : SHT(fs::readFileBuffer(shtPath)) {}
38
39SHT::operator bool() const {
40 return this->opened;
41}
42
43uint32_t SHT::getVersion() const {
44 return this->version;
45}
46
47void SHT::setVersion(uint32_t v) {
48 if (v != 0 && v != 1) {
49 return;
50 }
51 this->version = v;
52}
53
54const std::vector<SHT::Sequence>& SHT::getSequences() const {
55 return this->sequences;
56}
57
58std::vector<SHT::Sequence>& SHT::getSequences() {
59 return this->sequences;
60}
61
62const SHT::Sequence* SHT::getSequenceFromID(uint32_t id) const {
63 if (auto pos = std::find_if(this->sequences.begin(), this->sequences.end(), [id](const Sequence& sequence) {
64 return sequence.id == id;
65 }); pos != this->sequences.end()) {
66 return &*pos;
67 }
68 return nullptr;
69}
70
72 if (auto pos = std::find_if(this->sequences.begin(), this->sequences.end(), [id](const Sequence& sequence) {
73 return sequence.id == id;
74 }); pos != this->sequences.end()) {
75 return &*pos;
76 }
77 return nullptr;
78}
79
80uint8_t SHT::getFrameBoundsCount() const {
81 return (this->version > 0) ? 4 : 1;
82}
83
84std::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
117bool SHT::bake(const std::string& path) const {
118 return fs::writeFileBuffer(path, this->bake());
119}
Definition: SHT.h:13
const Sequence * getSequenceFromID(uint32_t id) const
Definition: SHT.cpp:62
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:47
uint32_t getVersion() const
Definition: SHT.cpp:43
uint8_t getFrameBoundsCount() const
Definition: SHT.cpp:80
std::vector< std::byte > bake() const
Definition: SHT.cpp:84
const std::vector< Sequence > & getSequences() const
Definition: SHT.cpp:54
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