forked from diasurgical/DevilutionX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreencode_dun_cels.cpp
More file actions
323 lines (299 loc) · 9.77 KB
/
reencode_dun_cels.cpp
File metadata and controls
323 lines (299 loc) · 9.77 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "levels/reencode_dun_cels.hpp"
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <limits>
#include <memory>
#include <span>
#include <utility>
#include <vector>
#include <SDL_endian.h>
#include "levels/dun_tile.hpp"
#include "utils/attributes.h"
#include "utils/endian_read.hpp"
#include "utils/endian_write.hpp"
#include "utils/format_int.hpp"
#include "utils/log.hpp"
namespace devilution {
namespace {
DVL_ALWAYS_INLINE void ReencodeDungeonCelsLeftTriangleLower(uint8_t *&dst, const uint8_t *&src)
{
unsigned width = 0;
for (unsigned i = 0; i < 8; ++i) {
src += 2; // Skips the two zero bytes (aka bloat).
width += 2;
std::memcpy(dst, src, width);
src += width;
dst += width;
width += 2;
std::memcpy(dst, src, width);
src += width;
dst += width;
}
}
DVL_ALWAYS_INLINE void ReencodeDungeonCelsLeftTriangle(uint8_t *&dst, const uint8_t *&src)
{
ReencodeDungeonCelsLeftTriangleLower(dst, src);
unsigned width = DunFrameWidth;
for (unsigned i = 0; i < 7; ++i) {
src += 2; // Skips the two zero bytes (aka bloat).
width -= 2;
std::memcpy(dst, src, width);
src += width;
dst += width;
width -= 2;
std::memcpy(dst, src, width);
src += width;
dst += width;
}
src += 2; // Skips the two zero bytes (aka bloat).
width -= 2;
std::memcpy(dst, src, width);
dst += width;
}
DVL_ALWAYS_INLINE void ReencodeDungeonCelsRightTriangleLower(uint8_t *&dst, const uint8_t *&src)
{
unsigned width = 0;
for (unsigned i = 0; i < 8; ++i) {
width += 2;
std::memcpy(dst, src, width);
src += width + 2; // Skips the two zero bytes (aka bloat).
dst += width;
width += 2;
std::memcpy(dst, src, width);
src += width;
dst += width;
}
}
DVL_ALWAYS_INLINE void ReencodeDungeonCelsRightTriangle(uint8_t *&dst, const uint8_t *&src)
{
ReencodeDungeonCelsRightTriangleLower(dst, src);
unsigned width = DunFrameWidth;
for (unsigned i = 0; i < 7; ++i) {
width -= 2;
std::memcpy(dst, src, width);
src += width + 2; // Skips the two zero bytes (aka bloat).
dst += width;
width -= 2;
std::memcpy(dst, src, width);
src += width;
dst += width;
}
width -= 2;
std::memcpy(dst, src, width);
dst += width;
}
DVL_ALWAYS_INLINE void ReencodeDungeonCelsLeftTrapezoid(uint8_t *&dst, const uint8_t *&src)
{
ReencodeDungeonCelsLeftTriangleLower(dst, src);
std::memcpy(dst, src, DunFrameWidth * 16);
dst += DunFrameWidth * 16;
}
DVL_ALWAYS_INLINE void ReencodeDungeonCelsRightTrapezoid(uint8_t *&dst, const uint8_t *&src)
{
ReencodeDungeonCelsRightTriangleLower(dst, src);
std::memcpy(dst, src, DunFrameWidth * 16);
dst += DunFrameWidth * 16;
}
DVL_ALWAYS_INLINE void RenderTransparentSquare(uint8_t *dst, const uint8_t *src)
{
for (unsigned i = 0; i < DunFrameHeight; ++i, dst -= 2 * DunFrameWidth) {
uint_fast8_t drawWidth = DunFrameWidth;
while (drawWidth > 0) {
auto v = static_cast<int8_t>(*src++);
if (v > 0) {
std::memcpy(dst, src, v);
src += v;
} else {
v = static_cast<int8_t>(-v);
}
dst += v;
drawWidth -= v;
}
}
}
DVL_ALWAYS_INLINE void ExtractFoliageLeftTriangle(uint8_t *&dst, uint8_t *src)
{
for (int w = 2, y = 31; y >= 16; --y, w += 2, src -= DunFrameWidth) {
std::memcpy(dst, src + (DunFrameWidth - w), w);
std::memset(src + (DunFrameWidth - w), 0, w);
dst += w;
}
for (int w = 30, y = 15; y > 0; --y, w -= 2, src -= DunFrameWidth) {
std::memcpy(dst, src + (DunFrameWidth - w), w);
std::memset(src + (DunFrameWidth - w), 0, w);
dst += w;
}
}
DVL_ALWAYS_INLINE void ExtractFoliageRightTriangle(uint8_t *&dst, uint8_t *src)
{
for (int w = 2, y = 31; y >= 16; --y, w += 2, src -= DunFrameWidth) {
std::memcpy(dst, src, w);
std::memset(src, 0, w);
dst += w;
}
for (int w = 30, y = 15; y > 0; --y, w -= 2, src -= DunFrameWidth) {
std::memcpy(dst, src, w);
std::memset(src, 0, w);
dst += w;
}
}
DVL_ALWAYS_INLINE void ExtractFoliageTransparentSquare(uint8_t *&dst, const uint8_t *src)
{
// The bottom 16 lines are always transparent, foliage only
// applies to the upper half of the tile.
src -= DunFrameHeight * 16;
for (int y = 16; y > 0; --y, src -= 2 * DunFrameWidth) {
unsigned transparentRun = 0;
unsigned solidRun = 0;
for (int x = 0; x < DunFrameWidth; ++x) {
if (*src++ != 0) {
if (transparentRun != 0) {
*dst++ = static_cast<uint8_t>(-static_cast<int8_t>(transparentRun));
transparentRun = 0;
}
++solidRun;
} else {
if (solidRun != 0) {
*dst++ = solidRun;
std::memcpy(dst, src - solidRun, solidRun);
dst += solidRun;
solidRun = 0;
}
++transparentRun;
}
}
if (transparentRun != 0) {
*dst++ = static_cast<uint8_t>(-static_cast<int8_t>(transparentRun));
} else if (solidRun != 0) {
*dst++ = solidRun;
std::memcpy(dst, src - solidRun, solidRun);
dst += solidRun;
}
}
}
DVL_ALWAYS_INLINE void ReencodeFloorWithFoliage(uint8_t *&dst, const uint8_t *&src, TileType tileType)
{
uint8_t surface[DunFrameWidth * DunFrameHeight] {};
uint8_t *surfaceLastLine = &surface[DunFrameWidth * (DunFrameHeight - 1)];
RenderTransparentSquare(surfaceLastLine, src);
if (tileType == TileType::LeftTriangle) {
ExtractFoliageLeftTriangle(dst, surfaceLastLine);
} else {
ExtractFoliageRightTriangle(dst, surfaceLastLine);
}
ExtractFoliageTransparentSquare(dst, surfaceLastLine);
}
size_t GetReencodedSize(const uint8_t *dungeonCels, std::span<std::pair<uint16_t, DunFrameInfo>> frames)
{
size_t result = (2 + frames.size()) * 4;
const auto *srcOffsets = reinterpret_cast<const uint32_t *>(dungeonCels);
for (const auto &[frame, info] : frames) {
size_t frameSize;
switch (info.type) {
case TileType::TransparentSquare: {
const uint32_t srcFrameBegin = SDL_SwapLE32(srcOffsets[frame]);
if (info.isFloor()) {
uint8_t out[1024];
uint8_t *outIt = out;
const uint8_t *src = &dungeonCels[srcFrameBegin];
const TileType newType = info.isFloorLeft() ? TileType::LeftTriangle : TileType::RightTriangle;
ReencodeFloorWithFoliage(outIt, src, newType);
frameSize = outIt - out;
} else {
const uint32_t srcFrameEnd = SDL_SwapLE32(srcOffsets[frame + 1]);
frameSize = srcFrameEnd - srcFrameBegin;
}
} break;
case TileType::Square: {
frameSize = DunFrameWidth * DunFrameHeight;
} break;
case TileType::LeftTriangle:
case TileType::RightTriangle:
frameSize = ReencodedTriangleFrameSize;
break;
case TileType::LeftTrapezoid:
case TileType::RightTrapezoid:
frameSize = ReencodedTrapezoidFrameSize;
break;
}
result += frameSize;
}
return result;
}
} // namespace
void ReencodeDungeonCels(std::unique_ptr<std::byte[]> &dungeonCels, std::span<std::pair<uint16_t, DunFrameInfo>> frames)
{
const auto *srcData = reinterpret_cast<const uint8_t *>(dungeonCels.get());
const auto *srcOffsets = reinterpret_cast<const uint32_t *>(srcData);
int numFoliage = 0;
LogVerbose("Re-encoding dungeon CELs: {} frames, {} bytes",
FormatInteger(SDL_SwapLE32(srcOffsets[0])),
FormatInteger(SDL_SwapLE32(srcOffsets[SDL_SwapLE32(srcOffsets[0]) + 1])));
const size_t outSize = GetReencodedSize(srcData, frames);
std::unique_ptr<std::byte[]> result { new std::byte[outSize] };
auto *const resultPtr = reinterpret_cast<uint8_t *>(result.get());
WriteLE32(resultPtr, static_cast<uint32_t>(frames.size()));
uint8_t *lookup = resultPtr + 4;
uint8_t *out = resultPtr + (2 + frames.size()) * 4; // number of frames, frame offsets, file size
for (const auto &[frame, info] : frames) {
WriteLE32(lookup, static_cast<uint32_t>(out - resultPtr));
lookup += 4;
const uint32_t srcFrameBegin = SDL_SwapLE32(srcOffsets[frame]);
const uint8_t *src = &srcData[srcFrameBegin];
switch (info.type) {
case TileType::TransparentSquare: {
if (info.isFloor()) {
const TileType newType = info.isFloorLeft() ? TileType::LeftTriangle : TileType::RightTriangle;
ReencodeFloorWithFoliage(out, src, newType);
++numFoliage;
} else {
const uint32_t srcFrameEnd = SDL_SwapLE32(srcOffsets[frame + 1]);
const uint32_t size = srcFrameEnd - srcFrameBegin;
std::memcpy(out, src, size);
out += size;
}
} break;
case TileType::Square:
std::memcpy(out, src, DunFrameWidth * DunFrameHeight);
out += DunFrameWidth * DunFrameHeight;
break;
case TileType::LeftTriangle:
ReencodeDungeonCelsLeftTriangle(out, src);
break;
case TileType::RightTriangle:
ReencodeDungeonCelsRightTriangle(out, src);
break;
case TileType::LeftTrapezoid:
ReencodeDungeonCelsLeftTrapezoid(out, src);
break;
case TileType::RightTrapezoid:
ReencodeDungeonCelsRightTrapezoid(out, src);
break;
}
}
WriteLE32(lookup, static_cast<uint32_t>(outSize));
const auto *dstOffsets = reinterpret_cast<const uint32_t *>(resultPtr);
LogVerbose(" Re-encoded dungeon CELs: {} frames, {} bytes. Extracted {} foliage tiles.",
FormatInteger(SDL_SwapLE32(dstOffsets[0])),
FormatInteger(SDL_SwapLE32(dstOffsets[SDL_SwapLE32(dstOffsets[0]) + 1])),
FormatInteger(numFoliage));
dungeonCels = std::move(result);
}
std::vector<std::pair<uint16_t, uint16_t>> ComputeCelBlockAdjustments(std::span<std::pair<uint16_t, DunFrameInfo>> frames)
{
std::vector<std::pair<uint16_t, uint16_t>> celBlockAdjustments;
uint16_t lastFrameIndex = 0;
uint16_t adjustment = 0;
for (auto &[frame, info] : frames) {
uint16_t diff = frame - lastFrameIndex - 1;
if (diff > 0) celBlockAdjustments.emplace_back(frame, adjustment);
adjustment += diff;
lastFrameIndex = frame;
}
if (adjustment > 0) {
celBlockAdjustments.emplace_back(std::numeric_limits<uint16_t>::max(), adjustment);
}
return celBlockAdjustments;
}
} // namespace devilution