-
Notifications
You must be signed in to change notification settings - Fork 148
/
gfx.cpp
158 lines (141 loc) · 3.73 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
#include "../interop/interop.hpp"
#include "../environment.h"
#include "../ui.h"
#include "gfx.h"
using namespace openloco::interop;
namespace openloco::gfx
{
loco_global<drawpixelinfo_t, 0x0050B884> _screen_dpi;
loco_global_array<g1_element_t, LOCO_G1_ELEMENT_COUNT, 0x9E2424> _g1Elements;
drawpixelinfo_t& screen_dpi()
{
return _screen_dpi;
}
// 0x0044733C
void load_g1()
{
auto g1Path = environment::get_path(environment::path_id::g1);
FILE * file;
gfx::g1_header_t header;
void * g1Buffer;
file = fopen(g1Path.make_preferred().u8string().c_str(), "rb");
try
{
if (file == nullptr)
{
throw std::exception("Opening g1 file failed.");
}
if (fread(&header, 8, 1, file) != 1)
{
throw std::exception("Reading g1 file header failed.");
}
// Read element headers
if (fread(_g1Elements, header.num_entries * sizeof(g1_element_t), 1, file) != 1)
{
throw std::exception("Reading g1 element headers failed.");
}
// Read element data
g1Buffer = malloc(header.total_size);
if (fread(g1Buffer, header.total_size, 1, file) != 1)
{
free(g1Buffer);
throw std::exception("Reading g1 elements failed.");
}
// Adjust memory offsets
for (uint32_t i = 0; i < header.num_entries; i++)
{
_g1Elements[i].offset += (int32_t)g1Buffer;
}
fclose(file);
}
catch (const std::exception &e)
{
if (file == nullptr)
{
fclose(file);
}
throw std::runtime_error(e.what());
}
}
// 0x00447485
// edi: dpi
// ebp: fill
void clear(drawpixelinfo_t &dpi, uint32_t fill)
{
registers regs;
regs.edi = (int32_t)&dpi;
regs.ebp = (int32_t)fill;
call(0x00447485, regs);
}
void clear_single(drawpixelinfo_t &dpi, uint8_t paletteId)
{
auto fill = (paletteId << 24) | (paletteId << 16) | (paletteId << 8) | paletteId;
clear(dpi, fill);
}
// 0x00494B3F
// al: colour
// bx: string id
// cx: x
// dx: y
// esi: args
// edi: dpi
void draw_string_494B3F(
drawpixelinfo_t &dpi,
int16_t x,
int16_t y,
uint8_t colour,
string_id stringId,
const void * args)
{
registers regs;
regs.al = colour;
regs.bx = stringId;
regs.cx = x;
regs.dx = y;
regs.esi = (int32_t)args;
regs.edi = (int32_t)&dpi;
call(0x00494B3F, regs);
}
// 0x00494BBF
// al: colour
// bx: string id
// cx: x
// dx: y
// esi: args
// edi: dpi
// bp: width
void draw_string_494BBF(
drawpixelinfo_t &dpi,
int16_t x,
int16_t y,
int16_t width,
uint8_t colour,
string_id stringId,
const void * args)
{
registers regs;
regs.al = colour;
regs.bx = stringId;
regs.cx = x;
regs.dx = y;
regs.esi = (int32_t)args;
regs.edi = (int32_t)&dpi;
regs.bp = width;
call(0x00494BBF, regs);
}
// 0x004CD406
void invalidate_screen()
{
set_dirty_blocks(0, 0, ui::width(), ui::height());
}
// 0x004C5C69
void set_dirty_blocks(int32_t left, int32_t top, int32_t right, int32_t bottom)
{
registers regs;
regs.ax = left;
regs.bx = top;
regs.dx = right;
regs.bp = bottom;
call(0x004C5C69, regs);
}
}