-
Notifications
You must be signed in to change notification settings - Fork 149
/
Gfx.cpp
468 lines (405 loc) · 13.5 KB
/
Gfx.cpp
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include "Gfx.h"
#include "Colour.h"
#include "Config.h"
#include "Console.h"
#include "Drawing/DrawSprite.h"
#include "Drawing/SoftwareDrawingEngine.h"
#include "Environment.h"
#include "ImageIds.h"
#include "Input.h"
#include "Interop/Interop.hpp"
#include "Localisation/LanguageFiles.h"
#include "Localisation/StringManager.h"
#include "PaletteMap.h"
#include "Ui.h"
#include "Ui/WindowManager.h"
#include <OpenLoco/Utility/Stream.hpp>
#include <algorithm>
#include <cassert>
#include <fstream>
#include <iostream>
#include <memory>
#include <numeric>
using namespace OpenLoco::Interop;
using namespace OpenLoco::Utility;
using namespace OpenLoco::Drawing;
using namespace OpenLoco::Ui;
namespace OpenLoco::Gfx
{
namespace TextDrawFlags
{
constexpr uint8_t inset = (1ULL << 0);
constexpr uint8_t outline = (1ULL << 1);
constexpr uint8_t dark = (1ULL << 2);
constexpr uint8_t extraDark = (1ULL << 3);
}
constexpr uint32_t kG1CountTemporary = 0x1000;
static loco_global<G1Element[G1ExpectedCount::kDisc + kG1CountTemporary + G1ExpectedCount::kObjects], 0x9E2424> _g1Elements;
// 0x009DA3E0
// Originally 0x009DA3E0 was an array of the image data pointers setup within 0x00452336
// We have removed that step and instead work directly on the images.
static constexpr std::array<uint32_t, 8> _noiseMaskImages = {
ImageIds::null,
ImageIds::noise_mask_1,
ImageIds::noise_mask_2,
ImageIds::noise_mask_3,
ImageIds::noise_mask_4,
ImageIds::noise_mask_5,
ImageIds::noise_mask_6,
ImageIds::noise_mask_7,
};
static std::unique_ptr<std::byte[]> _g1Buffer;
static loco_global<uint16_t, 0x112C824> _currentFontFlags;
static loco_global<int16_t, 0x0112C876> _currentFontSpriteBase;
static loco_global<uint8_t[224 * 4], 0x112C884> _characterWidths;
static loco_global<AdvancedColour[4], 0x1136594> _windowColours;
loco_global<uint32_t, 0x00E04324> _E04324;
loco_global<const uint8_t*, 0x009DA3D8> _noiseMaskImageData;
static PaletteMap::Buffer<8> _textColours{ 0 };
// 0x004FFAE8
ImageId applyGhostToImage(uint32_t imageIndex)
{
if (Config::get().old.constructionMarker)
{
return ImageId(imageIndex).withTranslucency(ExtColour::unk31);
}
else
{
return ImageId(imageIndex, ExtColour::unk2C);
}
}
static std::vector<G1Element> convertElements(const std::vector<G1Element32>& elements32)
{
auto elements = std::vector<G1Element>();
elements.reserve(elements32.size());
std::transform(
elements32.begin(),
elements32.end(),
std::back_inserter(elements),
[](G1Element32 src) { return G1Element(src); });
return elements;
}
// 0x0044733C
void loadG1()
{
auto g1Path = Environment::getPath(Environment::PathId::g1);
std::ifstream stream(g1Path, std::ios::in | std::ios::binary);
if (!stream)
{
throw std::runtime_error("Opening g1 file failed.");
}
G1Header header;
if (!readData(stream, header))
{
throw std::runtime_error("Reading g1 file header failed.");
}
if (header.numEntries != G1ExpectedCount::kDisc)
{
std::cout << "G1 element count doesn't match expected value: ";
std::cout << "Expected " << G1ExpectedCount::kDisc << "; Got " << header.numEntries << std::endl;
if (header.numEntries == G1ExpectedCount::kSteam)
{
std::cout << "Got Steam G1.DAT variant, will fix elements automatically." << std::endl;
}
}
// Read element headers
auto elements32 = std::vector<G1Element32>(header.numEntries);
if (!readData(stream, elements32.data(), header.numEntries))
{
throw std::runtime_error("Reading g1 element headers failed.");
}
auto elements = convertElements(elements32);
// Read element data
auto elementData = std::make_unique<std::byte[]>(header.totalSize);
if (!readData(stream, elementData.get(), header.totalSize))
{
throw std::runtime_error("Reading g1 elements failed.");
}
stream.close();
// The steam G1.DAT is missing two localised tutorial icons, and a smaller font variant
// This code copies the closest variants into their place, and moves other elements accordingly
if (header.numEntries == G1ExpectedCount::kSteam)
{
// Temporarily convert offsets to absolute indexes
for (size_t i = 0; i < elements.size(); i++)
{
if (elements[i].flags & G1ElementFlags::hasZoomSprites)
{
elements[i].zoomOffset = static_cast<int16_t>(i - elements[i].zoomOffset);
}
}
elements.resize(G1ExpectedCount::kDisc);
// Extra two tutorial images
std::copy_n(&elements[3549], header.numEntries - 3549, &elements[3551]);
std::copy_n(&elements[3551], 1, &elements[3549]);
std::copy_n(&elements[3551], 1, &elements[3550]);
// Extra font variant
std::copy_n(&elements[1788], 223, &elements[3898]);
// Restore relative offsets
for (size_t i = 0; i < elements.size(); i++)
{
if (elements[i].flags & G1ElementFlags::hasZoomSprites)
{
elements[i].zoomOffset = static_cast<int16_t>(i - elements[i].zoomOffset);
}
}
}
// Adjust memory offsets
for (auto& element : elements)
{
element.offset += (uintptr_t)elementData.get();
}
_g1Buffer = std::move(elementData);
std::copy(elements.begin(), elements.end(), _g1Elements.get());
}
// 0x004949BC
void initialiseCharacterWidths()
{
struct FontEntry
{
int16_t offset;
int8_t widthFudge;
};
constexpr std::array<FontEntry, 4> fonts = {
FontEntry{ Font::medium_normal, -1 },
FontEntry{ Font::medium_bold, -1 },
FontEntry{ Font::small, -1 },
FontEntry{ Font::large, 1 },
};
for (const auto& font : fonts)
{
// Supported character range is from 32 -> 255
for (auto i = 0; i < 224; ++i)
{
auto c = i + 32;
// TODO: Use an indirection to convert from character to imageId
auto* element = getG1Element(ImageIds::characters_medium_normal_space + i + font.offset);
if (element == nullptr)
{
continue;
}
auto width = element->width + font.widthFudge;
// Characters from 123 to 150 are unused
// Unsure why this zeros it out though since a negative width isn't an issue
if (c >= '{' && c <= '\x96')
{
width = 0;
}
_characterWidths[font.offset + i] = width;
}
}
// Vanilla setup scrolling text related globals here (unused)
}
// 0x00452336
void initialiseNoiseMaskMap()
{
call(0x00452336);
}
// 0x004CD406
void invalidateScreen()
{
invalidateRegion(0, 0, Ui::width(), Ui::height());
}
static std::unique_ptr<Drawing::SoftwareDrawingEngine> engine;
Drawing::SoftwareDrawingEngine& getDrawingEngine()
{
if (!engine)
{
engine = std::make_unique<Drawing::SoftwareDrawingEngine>();
}
return *engine;
}
/**
* 0x004C5C69
*
* @param left @<ax>
* @param top @<bx>
* @param right @<dx>
* @param bottom @<bp>
*/
void invalidateRegion(int32_t left, int32_t top, int32_t right, int32_t bottom)
{
getDrawingEngine().invalidateRegion(left, top, right, bottom);
}
// 0x004C5CFA
void render()
{
getDrawingEngine().render();
}
loco_global<char[512], 0x0112CC04> _byte_112CC04;
loco_global<char[512], 0x0112CE04> _byte_112CE04;
// 0x004CF63B
// TODO: Split this into two functions, one for rendering and one for processing messages.
void renderAndUpdate()
{
char backup1[512] = { 0 };
char backup2[512] = { 0 };
std::memcpy(backup1, _byte_112CC04, 512);
std::memcpy(backup2, _byte_112CE04, 512);
if (Ui::dirtyBlocksInitialised())
{
getDrawingEngine().render();
}
if (Input::hasFlag(Input::Flags::flag5))
{
Ui::processMessagesMini();
}
else
{
Ui::processMessages();
}
if (addr<0x005252AC, uint32_t>() != 0)
{
// sub_4058F5();
}
std::memcpy(_byte_112CC04, backup1, 512);
std::memcpy(_byte_112CE04, backup2, 512);
}
void render(Rect rect)
{
getDrawingEngine().render(rect);
}
/**
* 0x004C5DD5
* rct2: window_draw_all
*
* @param left @<ax>
* @param top @<bx>
* @param right @<dx>
* @param bottom @<bp>
*/
void render(int16_t left, int16_t top, int16_t right, int16_t bottom)
{
render(Rect::fromLTRB(left, top, right, bottom));
}
uint32_t recolour(uint32_t image)
{
return ImageIdFlags::remap | image;
}
uint32_t recolour(uint32_t image, ExtColour colour)
{
return ImageIdFlags::remap | (enumValue(colour) << 19) | image;
}
uint32_t recolour(uint32_t image, Colour colour)
{
return recolour(image, Colours::toExt(colour));
}
uint32_t recolour2(uint32_t image, Colour colour1, Colour colour2)
{
return ImageIdFlags::remap | ImageIdFlags::remap2 | (enumValue(colour1) << 19) | (enumValue(colour2) << 24) | image;
}
uint32_t recolour2(uint32_t image, ColourScheme colourScheme)
{
return recolour2(image, colourScheme.primary, colourScheme.secondary);
}
uint32_t recolourTranslucent(uint32_t image, ExtColour colour)
{
return ImageIdFlags::translucent | (enumValue(colour) << 19) | image;
}
loco_global<uint8_t*, 0x0050B860> _50B860;
G1Element* getG1Element(uint32_t imageId)
{
const auto id = getImageIndex(imageId);
if (id < _g1Elements.size())
{
return &_g1Elements[id];
}
return nullptr;
}
static loco_global<Drawing::PaletteEntry[256], 0x0113ED20> _113ED20;
// 0x0046E07B
void loadCurrency()
{
call(0x0046E07B);
}
// 0x004523F4
void loadPalette()
{
auto* g1Palette = getG1Element(ImageIds::default_palette);
uint8_t* colourData = g1Palette->offset;
for (auto i = g1Palette->xOffset; i < g1Palette->width; ++i)
{
_113ED20[i].b = *colourData++;
_113ED20[i].g = *colourData++;
_113ED20[i].r = *colourData++;
}
getDrawingEngine().updatePalette(_113ED20, 10, 236);
}
// 0x004530F8
ImageExtents getImagesMaxExtent(const ImageId baseImageId, const size_t numImages)
{
uint8_t bitmap[200][200] = {};
RenderTarget rt = {
/*.bits = */ reinterpret_cast<uint8_t*>(bitmap),
/*.x = */ -100,
/*.y = */ -100,
/*.width = */ 200,
/*.height = */ 200,
/*.pitch = */ 0,
/*.zoom_level = */ 0,
};
auto drawingCtx = Gfx::getDrawingEngine().getDrawingContext();
// Draw all the images ontop of the one bitmap
for (size_t i = 0; i < numImages; ++i)
{
drawingCtx.drawImage(rt, { 0, 0 }, baseImageId.withIndexOffset(i));
}
// Explore the bitmap to find the extents of the images drawn
int32_t spriteWidth = -1;
for (int32_t i = 99; i != 0; --i)
{
for (int32_t j = 0; j < 200; j++)
{
if (bitmap[j][100 - i] != 0)
{
spriteWidth = i;
break;
}
}
if (spriteWidth != -1)
break;
for (int32_t j = 0; j < 200; j++)
{
if (bitmap[j][100 + i] != 0)
{
spriteWidth = i;
break;
}
}
if (spriteWidth != -1)
break;
}
spriteWidth++;
int32_t spriteHeightNegative = -1;
for (int32_t i = 99; i != 0; --i)
{
for (int32_t j = 0; j < 200; j++)
{
if (bitmap[100 - i][j] != 0)
{
spriteHeightNegative = i;
break;
}
}
if (spriteHeightNegative != -1)
break;
}
spriteHeightNegative++;
int32_t spriteHeightPositive = -1;
for (int32_t i = 99; i != 0; --i)
{
for (int32_t j = 0; j < 200; j++)
{
if (bitmap[100 + i][j] != 0)
{
spriteHeightPositive = i;
break;
}
}
if (spriteHeightPositive != -1)
break;
}
spriteHeightPositive++;
return ImageExtents{ static_cast<uint8_t>(spriteWidth), static_cast<uint8_t>(spriteHeightNegative), static_cast<uint8_t>(spriteHeightPositive) };
}
}