-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMain.cpp
377 lines (312 loc) · 11.3 KB
/
Main.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
# include <Siv3D.hpp> // OpenSiv3D v0.6.15
// タイルの一辺の長さ(ピクセル)
inline constexpr Vec2 TileOffset{ 48, 24 };
// タイルの厚み(ピクセル)
inline constexpr int32 TileThickness = 17;
/*
インデックスとタイルの配置の関係 (N = 4)
(0, 0)
(0, 1) (1, 0)
(0, 2) (1, 1) (2, 0)
(0, 3) (1, 2) (2, 1) (3, 0)
(1, 3) (2, 2) (3, 1)
(2, 3) (3, 2)
(3, 3)
*/
/// @brief タイルのインデックスから、タイルの底辺中央の座標を計算します。
/// @param index タイルのインデックス
/// @param N マップの一辺のタイル数
/// @return タイルの底辺中央の座標
Vec2 ToTileBottomCenter(const Point& index, const int32 N)
{
const int32 i = index.manhattanLength();
const int32 xi = (i < (N - 1)) ? 0 : (i - (N - 1));
const int32 yi = (i < (N - 1)) ? i : (N - 1);
const int32 k = (index.manhattanDistanceFrom(Point{ xi, yi }) / 2);
const double posX = ((i < (N - 1)) ? (i * -TileOffset.x) : ((i - 2 * N + 2) * TileOffset.x));
const double posY = (i * TileOffset.y);
return{ (posX + TileOffset.x * 2 * k), posY };
}
/// @brief タイルのインデックスから、タイルの四角形を計算します。
/// @param index タイルのインデックス
/// @param N マップの一辺のタイル数
/// @return タイルの四角形
Quad ToTile(const Point& index, const int32 N)
{
const Vec2 bottomCenter = ToTileBottomCenter(index, N);
return Quad{
bottomCenter.movedBy(0, -TileThickness).movedBy(0, -TileOffset.y * 2),
bottomCenter.movedBy(0, -TileThickness).movedBy(TileOffset.x, -TileOffset.y),
bottomCenter.movedBy(0, -TileThickness),
bottomCenter.movedBy(0, -TileThickness).movedBy(-TileOffset.x, -TileOffset.y)
};
}
/// @brief 指定した列のタイルによって構成される四角形を計算します。
/// @param x 列インデックス
/// @param N マップの一辺のタイル数
/// @return 指定した列のタイルによって構成される四角形
Quad ToColumnQuad(const int32 x, const int32 N)
{
return{
ToTileBottomCenter(Point{ x, 0 }, N).movedBy(0, -TileThickness).movedBy(0, -TileOffset.y * 2),
ToTileBottomCenter(Point{ x, 0 }, N).movedBy(0, -TileThickness).movedBy(TileOffset.x, -TileOffset.y),
ToTileBottomCenter(Point{ x, (N - 1) }, N).movedBy(0, -TileThickness).movedBy(0, 0),
ToTileBottomCenter(Point{ x, (N - 1) }, N).movedBy(0, -TileThickness).movedBy(-TileOffset.x, -TileOffset.y)
};
}
/// @brief 指定した行のタイルによって構成される四角形を計算します。
/// @param y 行インデックス
/// @param N マップの一辺のタイル数
/// @return 指定した行のタイルによって構成される四角形
Quad ToRowQuad(const int32 y, const int32 N)
{
return{
ToTileBottomCenter(Point{ 0, y }, N).movedBy(0, -TileThickness).movedBy(-TileOffset.x, -TileOffset.y),
ToTileBottomCenter(Point{ 0, y }, N).movedBy(0, -TileThickness).movedBy(0, -TileOffset.y * 2),
ToTileBottomCenter(Point{ (N - 1), y }, N).movedBy(0, -TileThickness).movedBy(TileOffset.x, -TileOffset.y),
ToTileBottomCenter(Point{ (N - 1), y }, N).movedBy(0, -TileThickness).movedBy(0, 0)
};
}
/// @brief 各列のタイルによって構成される四角形の配列を作成します。
/// @param N マップの一辺のタイル数
/// @return 各列のタイルによって構成される四角形の配列
Array<Quad> MakeColumnQuads(const int32 N)
{
Array<Quad> quads;
for (int32 x = 0; x < N; ++x)
{
quads << ToColumnQuad(x, N);
}
return quads;
}
/// @brief 各行のタイルによって構成される四角形の配列を作成します。
/// @param N マップの一辺のタイル数
/// @return 各行のタイルによって構成される四角形の配列
Array<Quad> MakeRowQuads(const int32 N)
{
Array<Quad> quads;
for (int32 y = 0; y < N; ++y)
{
quads << ToRowQuad(y, N);
}
return quads;
}
/// @brief 指定した座標にあるタイルのインデックスを返します。
/// @param pos 座標
/// @param columnQuads 各列のタイルによって構成される四角形の配列
/// @param rowQuads 各行のタイルによって構成される四角形の配列
/// @return タイルのインデックス。指定した座標にタイルが無い場合は none
Optional<Point> ToIndex(const Vec2& pos, const Array<Quad>& columnQuads, const Array<Quad>& rowQuads)
{
int32 x = -1, y = -1;
// タイルの列インデックスを調べる
for (int32 i = 0; i < columnQuads.size(); ++i)
{
if (columnQuads[i].intersects(pos))
{
x = i;
break;
}
}
// タイルの行インデックスを調べる
for (int32 i = 0; i < rowQuads.size(); ++i)
{
if (rowQuads[i].intersects(pos))
{
y = i;
break;
}
}
// インデックスが -1 の場合、タイル上にはない
if ((x == -1) || (y == -1))
{
return none;
}
return Point{ x, y };
}
/// @brief 画像を読み込み、アルファ乗算済みのテクスチャを作成します。
/// @param path 画像ファイルのパス
/// @return アルファ乗算済みのテクスチャ
/// @remark 境界付近の品質を向上させるため、アルファ乗算済みのテクスチャを作成します。
/// @remark 描画時は `BlendState::Premultiplied` を指定してください。
[[nodiscard]]
Texture LoadPremultipliedTexture(FilePathView path)
{
Image image{ path };
Color* p = image.data();
const Color* const pEnd = (p + image.num_pixels());
while (p != pEnd)
{
p->r = static_cast<uint8>((static_cast<uint16>(p->r) * p->a) / 255);
p->g = static_cast<uint8>((static_cast<uint16>(p->g) * p->a) / 255);
p->b = static_cast<uint8>((static_cast<uint16>(p->b) * p->a) / 255);
++p;
}
return Texture{ image };
}
void Main()
{
// ウィンドウをリサイズする
Window::Resize(1280, 720);
// 背景を水色にする
Scene::SetBackground(ColorF{ 0.8, 0.9, 1.0 });
// https://kenney.nl/assets/isometric-roads
// からファイル一式をダウンロードし、「png」フォルダを App フォルダにコピーしてください。
// 各タイルのテクスチャ
Array<Texture> textures;
// png フォルダ内のファイルを列挙する
for (const auto& filePath : FileSystem::DirectoryContents(U"png/"))
{
// ファイル名が conifer と tree で始まるファイル(タイルではない)は除外する
if (const FilePath baseName = FileSystem::BaseName(filePath);
baseName.starts_with(U"conifer") || baseName.starts_with(U"tree"))
{
continue;
}
textures << LoadPremultipliedTexture(filePath);
}
// 全部で 88 種類のタイルが読み込まれれば正常
if (textures.size() != 88)
{
throw Error{ U"ファイルの配置が不正です。" };
}
// マップの一辺のタイル数
constexpr int32 N = 8;
// 各列の四角形
const Array<Quad> columnQuads = MakeColumnQuads(N);
// 各行の四角形
const Array<Quad> rowQuads = MakeRowQuads(N);
// タイルの種類
Grid<int32> grid(Size{ N, N });
// タイルメニューで選択されているタイルの種類
int32 tileTypeSelected = 30;
// マップ表示用の 2D カメラ
Camera2D camera{ Vec2{ 0, 0 }, 1.0 };
// タイルメニューの四角形
constexpr RoundRect TileMenuRoundRect = RectF{ 20, 20, (56 * 22), (50 * 4) }.stretched(10).rounded(8);
// マップにグリッドを表示するか
bool showGrid = false;
bool showIndex = false;
while (System::Update())
{
// 2D カメラを更新する
camera.update();
// タイルメニューの四角形の上にマウスカーソルがあるか
const bool onTileMenu = TileMenuRoundRect.mouseOver();
{
// 2D カメラによる座標変換を適用する
const auto tr = camera.createTransformer();
{
// 乗算済みアルファ用のブレンドステートを適用する
const ScopedRenderStates2D blend{ BlendState::Premultiplied };
// 上から順にタイルを描く
for (int32 i = 0; i < (N * 2 - 1); ++i)
{
// x の開始インデックス
const int32 xi = (i < (N - 1)) ? 0 : (i - (N - 1));
// y の開始インデックス
const int32 yi = (i < (N - 1)) ? i : (N - 1);
// 左から順にタイルを描く
for (int32 k = 0; k < (N - Abs(N - i - 1)); ++k)
{
// タイルのインデックス
const Point index{ (xi + k), (yi - k) };
// そのタイルの底辺中央の座標
const Vec2 pos = ToTileBottomCenter(index, N);
// 底辺中央を基準にタイルを描く
textures[grid[index]].draw(Arg::bottomCenter = pos);
}
}
}
// マウスカーソルがタイルメニュー上に無ければ
if (not onTileMenu)
{
// マウスカーソルがマップ上のどのタイルの上にあるかを取得する
if (const auto index = ToIndex(Cursor::PosF(), columnQuads, rowQuads))
{
// マウスカーソルがあるタイルを強調表示する
ToTile(*index, N).draw(ColorF{ 1.0, 0.2 });
// マウスの左ボタンが押されていたら
if (MouseL.pressed())
{
// タイルの種類を更新する
grid[*index] = tileTypeSelected;
}
}
}
// マップ上のグリッドを表示する
if (showGrid)
{
// 各列の四角形を描く
for (const auto& columnQuad : columnQuads)
{
columnQuad.drawFrame(2);
}
// 各行の四角形を描く
for (const auto& rowQuad : rowQuads)
{
rowQuad.drawFrame(2);
}
}
// マップ上のタイルのインデックスを表示する
if (showIndex)
{
for (int32 y = 0; y < N; ++y)
{
for (int32 x = 0; x < N; ++x)
{
const Point index{ x, y };
const Vec2 pos = ToTileBottomCenter(index, N).movedBy(0, -TileThickness);
PutText(U"{}"_fmt(index), pos.movedBy(0, -TileOffset.y - 3));
}
}
}
}
// 2D カメラの UI を表示する
camera.draw(Palette::Orange);
// タイルメニューを表示する
{
// 背景
TileMenuRoundRect.draw();
{
// 乗算済みアルファ用のブレンドステートを適用する
const ScopedRenderStates2D blend{ BlendState::Premultiplied };
// 各タイル
for (int32 y = 0; y < 4; ++y)
{
for (int32 x = 0; x < 22; ++x)
{
// タイルの長方形
const Rect rect{ (20 + x * 56), (20 + y * 50), 56, 50 };
// タイルの種類
const int32 tileType = (y * 22 + x);
// 現在選択されているタイルであれば
if (tileType == tileTypeSelected)
{
// 背景を灰色にする
rect.draw(ColorF{ 0.85 });
}
// タイルの上にマウスカーソルがあれば
if (rect.mouseOver())
{
// カーソルを手のアイコンにする
Cursor::RequestStyle(CursorStyle::Hand);
// 左クリックされたら
if (MouseL.down())
{
// 選択しているタイルの種類を更新する
tileTypeSelected = tileType;
}
}
// タイルを表示する
textures[tileType].scaled(0.5).drawAt(rect.center());
}
}
}
}
// マップ上のグリッドを表示するかのチェックボックス
SimpleGUI::CheckBox(showGrid, U"Show grid", Vec2{ 20, 240 }, 160);
// マップ上のインデックスを表示するかのチェックボックス
SimpleGUI::CheckBox(showIndex, U"Show index", Vec2{ 20, 280 }, 160);
}
}