/
Texture.hpp
304 lines (170 loc) · 4.61 KB
/
Texture.hpp
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
#pragma once
class Texture
{
protected:
dxlib_handle handle;
public:
/// <summary> ハンドルから画像を生成します </summary>
Texture(const dxlib_handle handle) : handle(handle) {}
Texture() : handle(DX_NONE_GRAPH) {}
Texture(const String &file, const uint32 width, const uint32 height)
{
handle = LoadGraph(file.c_str());
resize(width, height);
}
/// <summary> 画像のサイズを取得します </summary>
Size<uint32> getSize()
{
int width, height;
GetGraphSize(handle, &width, &height);
return Size<uint32>(width, height);
}
/// <summary> 反転した画像を複製します </summary>
Texture copyMirror()
{
auto size = getSize();
dxlib_handle texture = MakeScreen(size.width, size.height, true);
SwapScreen::Begin(texture);
DrawTurnGraph(0, 0, handle, true);
SwapScreen::End();
return Texture(texture);
}
/// <summary> 回転した画像を複製します </summary>
Texture copyRotate(const double radian)
{
auto size = getSize();
dxlib_handle texture = MakeScreen(size.width, size.height, true);
SwapScreen::Begin(texture);
DrawRotaGraph(size.width *0.5, size.height*0.5, 1, radian, handle, true);
SwapScreen::End();
return Texture(texture);
}
/// <summary> 画像を 3D で扱えるように修正します </summary>
Texture &use3D(bool alpha = true) {
uint32 width = getSize().width, height = getSize().height;
uint32 size = std::max(width, height);
// そのままでもいい場合
if (width == height && size >= 8 && (size - 1 & size))
{
return *this;
}
int count = 3;
while (true)
{
uint32 _ = static_cast<uint32>(std::pow(2, count++));
if (_ >= size)
{
size = _ < 8 ? 8 : _;
break;
}
}
dxlib_handle screen = MakeScreen(size, size, alpha);
SetDrawScreen(screen);
DrawExtendGraph(0, 0, size, size, handle, alpha);
DeleteGraph(handle);
return *this = Texture(screen);
}
Texture(const String &file)
{
handle = LoadGraph(file.c_str());
}
void draw(const double x, const double y)
{
DrawGraphF((float)x, (float)y, handle, true);
}
void draw(const double x1, const double y1, const double x2, const double y2)
{
DrawExtendGraphF(static_cast<double>(x1), static_cast<double>(y1), static_cast<double>(x2), static_cast<double>(y2), handle, true);
}
/// <summary> 座標とサイズを指定して描画します </summary>
void draw(const Vec2 &pos, const Size<double> &size)
{
DrawExtendGraphF(pos.x, pos.y, pos.x + size.width, pos.y + size.height, handle, true);
}
/// <summary> 範囲を指定して描画します </summary>
void draw(const Rect &rect)
{
draw(rect.pos(), rect.size());
}
void drawCenter(const Vec2 ¢er, const Size<double> &size)
{
draw(Vec2(center.x - size.width / 2, center.y - size.height / 2), size);
}
/// <summary> 中心座標とサイズから描画します </summary>
void draw(const Vec2 ¢er, const double width, const double height)
{
Vec2 min(center.x - width / 2, center.y - height / 2);
Vec2 max(center.x + width / 2, center.y + height / 2);
DrawExtendGraphF(min.x, min.y, max.x, max.y, handle, true);
}
dxlib_handle getHandle() const
{
return handle;
}
void resize(const uint32 width, const uint32 height)
{
dxlib_handle screen = MakeScreen(width, height, true);
auto _ = GetDrawScreen();
SetDrawScreen(screen);
DrawExtendGraph(0, 0, width, height, handle, true);
SetDrawScreen(_);
DeleteGraph(handle);
handle = screen;
}
};
#include <map>
namespace NS_TextureAsset
{
std::map<String, Texture> list;
}
class TextureAsset
{
String name;
public:
/// <summary> 全てのテクスチャ情報を表示します </summary>
static void PrintStatus()
{
for (auto &texture : NS_TextureAsset::list)
{
/*
console::log(
texture.first + L" -> size[" + ToString(texture.second.getSize().width) + L", " + ToString(texture.second.getSize().height) + L"]\n"
);
*/
}
}
void operator=(const String &file)
{
}
void set(const String &file, const bool use3D = false)
{
if (NS_TextureAsset::list.find(name) == NS_TextureAsset::list.end()) {
NS_TextureAsset::list[name] = Texture(file);
if (use3D)
{
NS_TextureAsset::list[name].use3D();
}
}
}
void set(const String &file, const Size<uint32> size)
{
if (NS_TextureAsset::list.find(name) == NS_TextureAsset::list.end()) {
NS_TextureAsset::list[name] = Texture(file, size.width, size.height);
}
}
Texture get()
{
if (NS_TextureAsset::list.find(name) != NS_TextureAsset::list.end()) {
return NS_TextureAsset::list[name];
}
return Texture();
}
TextureAsset(const String &name) : name(name)
{
if (name.empty())
{
throw "aaa";
}
}
};
using TextureAsset2 = Asset<Texture>;