-
Notifications
You must be signed in to change notification settings - Fork 2
/
Palette.cpp
255 lines (217 loc) · 6.38 KB
/
Palette.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
#include "Palette.hpp"
#include <cstring>
int shared_yoffs[7] = {};
const int num_tiles_spec = 41;
const char *tiles_spec[1][48] = {
{ // -1 (Special)
"/specs/0.bmp", // Wall,
"/specs/1.bmp", // ChairDown,
"/specs/2.bmp", // ChairLeft,
"/specs/3.bmp", // ChairRight,
"/specs/4.bmp", // ChairUp,
"/specs/5.bmp", // ChairDownRight,
"/specs/6.bmp", // ChairUpLeft,
"/specs/7.bmp", // ChairAll,
"/specs/8.bmp", // UnknownDoor,
"/specs/9.bmp", // Chest,
"/specs/10.bmp", // SpecUnknown1,
"/specs/11.bmp", // SpecUnknown2,
"/specs/12.bmp", // SpecUnknown3,
"/specs/13.bmp", // SpecUnknown4,
"/specs/14.bmp", // SpecUnknown5,
"/specs/15.bmp", // SpecUnknown6,
"/specs/16.bmp", // BankVault,
"/specs/17.bmp", // NPCBoundary,
"/specs/18.bmp", // MapEdge,
"/specs/19.bmp", // FakeWall,
"/specs/20.bmp", // Board1,
"/specs/21.bmp", // Board2,
"/specs/22.bmp", // Board3,
"/specs/23.bmp", // Board4,
"/specs/24.bmp", // Board5,
"/specs/25.bmp", // Board6,
"/specs/26.bmp", // Board7,
"/specs/27.bmp", // Board8,
"/specs/28.bmp", // Jukebox,
"/specs/29.bmp", // Jump,
"/specs/30.bmp", // Water,
"/specs/31.bmp", // SpecUnknown7,
"/specs/32.bmp", // Arena,
"/specs/33.bmp", // AmbientSource,
"/specs/34.bmp", // Spikes1,
"/specs/35.bmp", // Spikes2,
"/specs/36.bmp", // Spikes3,
"/specs/warp.bmp", // Warp,
"/specs/item.bmp", // Item spawn
"/specs/npc.bmp", // NPC,
"/specs/sign.bmp", // Sign,
"/specs/genericitems.bmp", // Items bubble,
"/specs/signcaptioned.bmp", // Sign bubble,
"/specs/chestcaptioned.bmp", // Chest bubble,
"/specs/warpcaptioned.bmp", // Warp bubble,
"/specs/doorcaptioned.bmp", // Door bubble,
"/specs/lockeddoorcaptioned.bmp", // Locked door bubble
"/specs/npccaptioned.bmp" // NPC bubble
}
};
static constexpr int divup(int n, int d)
{
return (n + (d - 1)) / d;
}
static constexpr int res = 16;
static constexpr int tilemap_size = divup(640, res) * divup(200000, res);
static bool* g_tilemap;
void Palette::Populate(GFX_Loader &loader)
{
if (!this->tiles.empty())
{
return;
}
if (this->file > 0)
loader.Prepare(this->file);
Tile tile;
int local_num_tiles = (this->file >= 0) ? (loader.CountBitmaps(this->file) - 1) : num_tiles_spec;
// gfx003 has one extra resource
if (this->file == 3)
--local_num_tiles;
if (this->file == -1 || this->file == 3)
{
for (int i = 0; i < local_num_tiles; ++i)
{
tile.id = i;
tile.x = (i % 10) << 6;
tile.y = (i / 10) << 5;
tile.w = 64;
tile.h = 32;
tiles.push_back(tile);
this->height = std::max(this->height, tile.y + tile.h);
}
}
else
{
if (!g_tilemap)
g_tilemap = new bool[tilemap_size];
std::memset(g_tilemap, 0, tilemap_size);
bool* tilemap = g_tilemap;
int cy = 0;
for (int i = 1; i <= local_num_tiles; ++i)
{
auto bmp_info = loader.Info(this->file, i);
if (this->file == 3 || this->file == 6)
{
if (bmp_info.width >= 120)
bmp_info.width /= 4;
}
if (bmp_info.width > 640)
bmp_info.width = 640;
int draw_x = 0, draw_y = 0;
for (; cy < divup(200000, res); ++cy)
{
for (int cx = 0; cx < divup(640, res); ++cx)
{
for (int y = 0; y < divup(bmp_info.height, res); ++y)
{
for (int x = 0; x < divup(bmp_info.width, res); ++x)
{
if (cy+y >= divup(200000, res) || cx+x >= divup(640, res) || tilemap[(cy+y) * divup(640, res) + (cx+x)])
{
goto next_check;
}
}
}
draw_x = cx;
draw_y = cy;
goto found_ya;
next_check:
;
}
}
continue;
found_ya:
tile.id = i;
tile.x = draw_x * res;
tile.y = draw_y * res;
tile.w = bmp_info.width;
tile.h = bmp_info.height;
tiles.push_back(tile);
for (int y = 0; y < divup(tile.h, res); ++y)
for (int x = 0; x < divup(tile.w, res); ++x)
tilemap[(draw_y+y) * divup(640, res) + (draw_x+x)] = true;
this->height = std::max(this->height, tile.y + tile.h);
}
}
this->height += 64;
}
void Palette::Click(int x, int y)
{
y -= 32;
selected_tile = 0;
for (std::list<Palette::Tile>::iterator i = this->tiles.begin(); i != this->tiles.end(); ++i)
{
if (x > i->x && x <= i->x + i->w
&& y > i->y && y <= i->y + i->h)
{
selected_tile = i->id;
return;
}
}
}
int Palette::RightClick(int x, int y)
{
y -= 32;
for (std::list<Palette::Tile>::iterator i = this->tiles.begin(); i != this->tiles.end(); ++i)
{
if (x > i->x && x <= i->x + i->w
&& y > i->y && y <= i->y + i->h)
{
return i->id;
}
}
return 0;
}
void Pal_Renderer::Render()
{
int target_h = this->target.Height();
std::deque<std::function<void()>> drawcmd;
for (std::list<Palette::Tile>::iterator i = this->pal->tiles.begin(); i != this->pal->tiles.end(); ++i)
{
int draw_x = i->x;
int draw_y = i->y - yoff + 32;
if (draw_y + i->h >= 0 && draw_y < target_h)
{
a5::Bitmap& bmp = [&]() -> a5::Bitmap&
{
if (this->pal->file >= 0)
return this->gfxloader.Load(this->pal->file, i->id, this->animation_state);
else
return this->gfxloader.LoadRaw(tiles_spec[std::abs(this->pal->file)-1][i->id]);
}();
if (i->id == this->pal->selected_tile)
{
drawcmd.emplace_back([this, &bmp, draw_x, draw_y]()
{
if (this->pal->layer == 7)
{
this->target.BlitTinted(bmp, a5::RGBA(255, 255, 255, 160), draw_x, draw_y);
}
else
{
this->target.Blit(bmp, draw_x, draw_y);
this->target.BlitTinted(bmp, a5::RGBA(160, 160, 160, 255), draw_x, draw_y);
}
});
}
else
{
drawcmd.emplace_back([this, &bmp, draw_x, draw_y]()
{
this->target.Blit(bmp, draw_x, draw_y);
});
}
}
}
al_hold_bitmap_drawing(true);
for (const auto& cmd : drawcmd)
cmd();
al_hold_bitmap_drawing(false);
}