-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathDC6.cpp
More file actions
191 lines (161 loc) · 6.89 KB
/
DC6.cpp
File metadata and controls
191 lines (161 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <cstring>
#include "Abyss/Streams/StreamReader.h"
#include "DC6.h"
#include "Abyss/Singletons.h"
namespace Abyss::DataTypes {
DC6::DC6(const std::string_view path)
: _version(0), _flags(0), _encoding(0), _directions(0), _framesPerDirection(0), _texture(nullptr, &SDL_DestroyTexture), _blendMode(Enums::BlendMode::None) {
auto stream = Singletons::getFileProvider().loadFile(path);
Streams::StreamReader sr(stream);
_version = sr.readUInt32();
_flags = sr.readUInt32();
_encoding = sr.readUInt32();
sr.readBytes(_termination);
_directions = sr.readUInt32();
_framesPerDirection = sr.readUInt32();
const auto frameCount = _directions * _framesPerDirection;
_framePointers.resize(frameCount);
for (auto &framePointer : _framePointers) {
framePointer = sr.readUInt32();
}
for ([[maybe_unused]] auto &framePointer : _framePointers) {
_frames.emplace_back(sr);
}
}
DC6::DC6(const std::string_view path, const Palette &palette) : DC6(path) { setPalette(palette); }
uint32_t DC6::getVersion() const { return _version; }
uint32_t DC6::getFlags() const { return _flags; }
uint32_t DC6::getEncoding() const { return _encoding; }
std::array<uint8_t, DC6TerminationSize> DC6::getTermination() const { return _termination; }
uint32_t DC6::getDirections() const { return _directions; }
uint32_t DC6::getFramesPerDirection() const { return _framesPerDirection; }
std::vector<uint32_t> DC6::getFramePointers() const { return _framePointers; }
uint32_t DC6::getFrameCount() const { return _framesPerDirection; }
void DC6::setPalette(const Palette &palette) {
enum class eScanlineType { EndOfLine, RunOfTransparentPixels, RunOfOpaquePixels };
auto scanlineType = [](const std::byte b) -> eScanlineType {
if (b == DC6EndOfScanline)
return eScanlineType::EndOfLine;
if (static_cast<int>(b & DC6EndOfScanline) > 0)
return eScanlineType::RunOfTransparentPixels;
return eScanlineType::RunOfOpaquePixels;
};
uint32_t textureWidth = 1;
uint32_t textureHeight = 1;
for (const auto &frame : _frames) {
textureWidth += frame.getWidth();
textureHeight = std::max(textureHeight, frame.getHeight());
}
_texture.reset(SDL_CreateTexture(Singletons::getRendererProvider().getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING,
static_cast<int>(textureWidth), static_cast<int>(textureHeight)));
if (!_texture)
throw std::runtime_error(SDL_GetError());
void *pixels;
int pitch;
if (SDL_LockTexture(_texture.get(), nullptr, &pixels, &pitch))
throw std::runtime_error(SDL_GetError());
std::memset(pixels, 0, textureHeight * pitch);
auto originX = 0;
for (const auto &frame : _frames) {
auto process = true;
auto x = originX;
auto y = frame.getHeight() - 1;
auto offset = 0;
const auto &frameData = frame.getFrameData();
while (process) {
if (offset >= frameData.size()) {
throw std::runtime_error("Data overrun while decoding DC6 frame.");
}
const auto b = frameData[offset];
offset++;
switch (scanlineType(b)) {
case eScanlineType::EndOfLine: {
if (y == 0) {
process = false;
break;
}
y--;
x = originX;
break;
}
case eScanlineType::RunOfTransparentPixels: {
const auto &transparentPixels = b & DC6MaxRunLength;
x += static_cast<int>(transparentPixels);
break;
}
case eScanlineType::RunOfOpaquePixels: {
for (auto i = 0; i < static_cast<int>(b); i++) {
if (offset >= frameData.size()) {
throw std::runtime_error("Data overrun while decoding DC6 frame.");
}
const auto paletteIndex = static_cast<int>(frameData[offset]);
const auto &paletteColor = palette.getEntries()[paletteIndex];
const auto pixel = static_cast<uint32_t>(paletteColor.getBlue() << 24 | paletteColor.getGreen() << 16 | paletteColor.getRed() << 8 | 0xFF);
*(static_cast<uint32_t *>(pixels) + y * (pitch / 4) + x + i) = pixel;
offset++;
}
x += static_cast<int>(b);
break;
}
}
}
_frameRects.emplace_back(SDL_Rect{originX, 0, static_cast<int>(frame.getWidth()), static_cast<int>(frame.getHeight())});
originX += static_cast<int>(frame.getWidth());
}
SDL_UnlockTexture(_texture.get());
setBlendMode(_blendMode);
}
void DC6::draw(const uint32_t frameIdx, const int x, const int y) const {
if (frameIdx >= _frameRects.size())
throw std::runtime_error("Invalid frame index");
const auto &frameRect = _frameRects[frameIdx];
const auto &frame = _frames[frameIdx];
const SDL_Rect destRect{x + frame.getXOffset(), y + frame.getYOffset() - frameRect.h, frameRect.w, frameRect.h};
SDL_RenderCopy(Singletons::getRendererProvider().getRenderer(), _texture.get(), &frameRect, &destRect);
}
void DC6::draw(uint32_t frameIdx, const int x, int y, const int framesX, const int framesY) const {
for (auto fy = 0; fy < framesY; fy++) {
auto orgX = x;
const auto yAdjust = _frameRects[frameIdx].h;
const auto xAdjust = _frameRects[frameIdx].w;
for (auto fx = 0; fx < framesX; fx++) {
draw(frameIdx, orgX, y + yAdjust);
frameIdx++;
orgX += xAdjust;
}
y += yAdjust;
}
}
void DC6::setBlendMode(const Enums::BlendMode blendMode) {
this->_blendMode = blendMode;
switch (blendMode) {
default:
case Enums::BlendMode::None:
SDL_SetTextureBlendMode(_texture.get(), SDL_BLENDMODE_NONE);
break;
case Enums::BlendMode::Blend:
SDL_SetTextureBlendMode(_texture.get(), SDL_BLENDMODE_BLEND);
break;
case Enums::BlendMode::Add:
SDL_SetTextureBlendMode(_texture.get(), SDL_BLENDMODE_ADD);
break;
case Enums::BlendMode::Mod:
SDL_SetTextureBlendMode(_texture.get(), SDL_BLENDMODE_MOD);
break;
}
}
void DC6::getFrameSize(const uint32_t frameIdx, int &frameWidth, int &frameHeight) const {
if (frameIdx >= _frameRects.size())
throw std::runtime_error("Invalid frame index");
const auto &[x, y, w, h] = _frameRects[frameIdx];
frameWidth = w;
frameHeight = h;
}
void DC6::getFrameOffset(const uint32_t frameIdx, int &offsetX, int &offsetY) const {
if (frameIdx >= _frames.size())
throw std::runtime_error("Invalid frame index");
const auto &frame = _frames[frameIdx];
offsetX = frame.getXOffset();
offsetY = frame.getYOffset();
}
} // namespace Abyss::DataTypes