forked from scarsty/kys-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.cpp
More file actions
582 lines (529 loc) · 15.4 KB
/
Copy pathEngine.cpp
File metadata and controls
582 lines (529 loc) · 15.4 KB
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#include "Engine.h"
#ifdef _MSC_VER
#define NOMINMAX
#include <windows.h>
#pragma comment(lib, "user32.lib")
#endif
Engine::Engine()
{
}
Engine::~Engine()
{
destroy();
}
int Engine::init(void* handle)
{
if (SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC))
{
return -1;
}
window_ = SDL_CreateWindow(title_.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
start_w_, start_h_, SDL_WINDOW_RESIZABLE);
SDL_ShowWindow(window_);
SDL_RaiseWindow(window_);
renderer_ = SDL_CreateRenderer(window_, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE /*| SDL_RENDERER_PRESENTVSYNC*/);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
//屏蔽触摸板
SDL_EventState(SDL_FINGERUP, SDL_DISABLE);
SDL_EventState(SDL_FINGERDOWN, SDL_DISABLE);
SDL_EventState(SDL_FINGERMOTION, SDL_DISABLE);
//手柄
if (SDL_NumJoysticks() < 1)
{
fmt1::print("Warning: No joysticks connected!\n");
}
else
{
//按照游戏控制器打开
game_controller_ = SDL_GameControllerOpen(0);
if (game_controller_)
{
fmt1::print("Found {} game controller(s)\n", SDL_NumJoysticks());
std::string name = SDL_GameControllerName(game_controller_);
fmt1::print("{}\n", name);
if (name.find("Switch") != std::string::npos) { switch_ = 1; }
haptic_ = SDL_HapticOpenFromJoystick(SDL_GameControllerGetJoystick(game_controller_));
if (haptic_ == nullptr) { fmt1::print("{}\n", SDL_GetError()); }
}
else
{
fmt1::print("Warning: Unable to open game controller! SDL Error: {}\n", SDL_GetError());
}
}
rect_ = { 0, 0, start_w_, start_h_ };
logo_ = loadImage("logo.png");
showLogo();
renderPresent();
TTF_Init();
#ifdef _MSC_VER
RECT r;
SystemParametersInfo(SPI_GETWORKAREA, 0, (PVOID)&r, 0);
int w = GetSystemMetrics(SM_CXEDGE);
int h = GetSystemMetrics(SM_CYEDGE);
min_x_ = r.left + w;
min_y_ = r.top + h + GetSystemMetrics(SM_CYCAPTION);
max_x_ = r.right - w;
max_y_ = r.bottom - h;
#else
BP_Rect r;
SDL_GetDisplayBounds(0, &r);
min_x_ = r.x;
min_y_ = r.y;
max_x_ = r.w + r.x;
max_y_ = r.h + r.y;
#endif
square_ = createRectTexture(100, 100, 0);
fmt1::print("maximum width and height are: {}, {}\n", max_x_, max_y_);
#if defined(_WIN32) && defined(WITH_SMALLPOT) && !defined(_DEBUG)
tinypot_ = PotCreateFromWindow(window_);
#endif
return 0;
}
void Engine::destroy()
{
//SDL_DestroyTexture(tex_);
destroyAssistTexture();
SDL_DestroyRenderer(renderer_);
SDL_DestroyWindow(window_);
#if defined(_WIN32) && defined(WITH_SMALLPOT) && !defined(_DEBUG)
PotDestory(tinypot_);
#endif
SDL_Quit();
}
BP_Texture* Engine::createYUVTexture(int w, int h)
{
return SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, w, h);
}
void Engine::updateYUVTexture(BP_Texture* t, uint8_t* data0, int size0, uint8_t* data1, int size1, uint8_t* data2, int size2)
{
SDL_UpdateYUVTexture(t, nullptr, data0, size0, data1, size1, data2, size2);
}
BP_Texture* Engine::createARGBTexture(int w, int h)
{
return SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, w, h);
}
BP_Texture* Engine::createARGBRenderedTexture(int w, int h)
{
return SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, w, h);
}
void Engine::updateARGBTexture(BP_Texture* t, uint8_t* buffer, int pitch)
{
SDL_UpdateTexture(t, nullptr, buffer, pitch);
}
void Engine::renderCopy(BP_Texture* t, int x, int y, int w, int h, double angle, int inPresent)
{
if (inPresent == 1)
{
x += rect_.x;
y += rect_.y;
}
BP_Rect r = { x, y, w, h };
renderCopy(t, nullptr, &r, angle);
}
void Engine::renderCopy(BP_Texture* t /*= nullptr*/, double angle)
{
SDL_RenderCopyEx(renderer_, t, nullptr, &rect_, angle, nullptr, SDL_FLIP_NONE);
render_times_++;
}
void Engine::renderPresent()
{
SDL_RenderPresent(renderer_);
}
void Engine::renderCopy(BP_Texture* t, BP_Rect* rect0, BP_Rect* rect1, double angle, int inPresent /*= 0*/)
{
SDL_RenderCopyEx(renderer_, t, rect0, rect1, angle, nullptr, SDL_FLIP_NONE);
render_times_++;
}
void Engine::getMouseState(int& x, int& y)
{
SDL_GetMouseState(&x, &y);
}
void Engine::setMouseState(int x, int y)
{
SDL_WarpMouseInWindow(window_, x, y);
}
int Engine::pollEvent(BP_Event& e)
{
int r = SDL_PollEvent(&e);
if (switch_)
{
if (e.type == BP_CONTROLLERBUTTONDOWN || e.type == BP_CONTROLLERBUTTONUP)
{
auto& key = e.cbutton.button;
if (key == BP_CONTROLLER_BUTTON_A) { key = BP_CONTROLLER_BUTTON_B; }
else if (key == BP_CONTROLLER_BUTTON_B) { key = BP_CONTROLLER_BUTTON_A; }
else if (key == BP_CONTROLLER_BUTTON_X) { key = BP_CONTROLLER_BUTTON_Y; }
else if (key == BP_CONTROLLER_BUTTON_Y) { key = BP_CONTROLLER_BUTTON_X; }
}
}
return r;
}
bool Engine::checkKeyPress(BP_Keycode key)
{
return SDL_GetKeyboardState(NULL)[SDL_GetScancodeFromKey(key)];
}
bool Engine::gameControllerGetButton(int key)
{
if (game_controller_)
{
if (switch_)
{
if (key == BP_CONTROLLER_BUTTON_A) { key = BP_CONTROLLER_BUTTON_B; }
else if (key == BP_CONTROLLER_BUTTON_B) { key = BP_CONTROLLER_BUTTON_A; }
else if (key == BP_CONTROLLER_BUTTON_X) { key = BP_CONTROLLER_BUTTON_Y; }
else if (key == BP_CONTROLLER_BUTTON_Y) { key = BP_CONTROLLER_BUTTON_X; }
}
return SDL_GameControllerGetButton(game_controller_, SDL_GameControllerButton(key));
}
return false;
}
int16_t Engine::gameControllerGetAxis(int axis)
{
if (game_controller_)
{
return SDL_GameControllerGetAxis(game_controller_, SDL_GameControllerAxis(axis));
}
return 0;
}
BP_Texture* Engine::createRectTexture(int w, int h, int style)
{
auto square_s = SDL_CreateRGBSurface(0, w, h, 32, RMASK, GMASK, BMASK, AMASK);
//SDL_FillRect(square_s, nullptr, 0xffffffff);
BP_Rect r = { 0, 0, 1, 1 };
auto& x = r.x;
auto& y = r.y;
uint8_t a = 0;
for (x = 0; x < w; x++)
{
for (y = 0; y < h; y++)
{
int c;
if (style == 0)
{
a = 100 + 150 * cos(M_PI * (1.0 * y / w - 0.5));
c = 0x00ffffff | (a << 24);
}
else if (style == 1)
{
c = 0xffffffff;
}
SDL_FillRect(square_s, &r, c);
/*if ((x - d / 2)*(x - d / 2) + (y - d / 2)*(y - d / 2) < (d / 2) * (d / 2))
{
SDL_FillRect(square_s, &r, 0x00ffffff | (a<<24));
}*/
}
}
auto square = SDL_CreateTextureFromSurface(renderer_, square_s);
setTextureBlendMode(square);
//setTextureAlphaMod(square, 128);
SDL_FreeSurface(square_s);
return square;
}
BP_Texture* Engine::createTextTexture(const std::string& fontname, const std::string& text, int size, BP_Color c)
{
auto font = TTF_OpenFont(fontname.c_str(), size);
if (!font)
{
return nullptr;
}
//SDL_Color c = { 255, 255, 255, 128 };
auto text_s = TTF_RenderUTF8_Blended(font, text.c_str(), c);
auto text_t = SDL_CreateTextureFromSurface(renderer_, text_s);
SDL_FreeSurface(text_s);
TTF_CloseFont(font);
return text_t;
}
int Engine::getWindowWidth()
{
int w, h;
getWindowSize(w, h);
return w;
}
int Engine::getWindowHeight()
{
int w, h;
getWindowSize(w, h);
return h;
}
bool Engine::isFullScreen()
{
Uint32 state = SDL_GetWindowFlags(window_);
full_screen_ = (state & SDL_WINDOW_FULLSCREEN) || (state & SDL_WINDOW_FULLSCREEN_DESKTOP);
return full_screen_;
}
void Engine::toggleFullscreen()
{
full_screen_ = !full_screen_;
if (full_screen_)
{
SDL_SetWindowFullscreen(window_, SDL_WINDOW_FULLSCREEN_DESKTOP);
}
else
{
SDL_SetWindowFullscreen(window_, 0);
}
renderClear();
}
BP_Texture* Engine::loadImage(const std::string& filename, int as_white)
{
//fmt1::print("%s", filename.c_str());
auto sur = IMG_Load(filename.c_str());
if (as_white) { toWhite(sur); }
auto tex = SDL_CreateTextureFromSurface(renderer_, sur);
SDL_FreeSurface(sur);
return tex;
}
BP_Texture* Engine::loadImageFromMemory(const std::string& content, int as_white)
{
auto rw = SDL_RWFromConstMem(content.data(), content.size());
auto sur = IMG_LoadTyped_RW(rw, 1, "png");
if (as_white) { toWhite(sur); }
auto tex = SDL_CreateTextureFromSurface(renderer_, sur);
SDL_FreeSurface(sur);
return tex;
}
void Engine::toWhite(BP_Surface* sur)
{
for (int i = 0; i < sur->w * sur->h; i++)
{
auto p = (uint32_t*)sur->pixels + i;
uint8_t r, g, b, a;
SDL_GetRGBA(*p, sur->format, &r, &g, &b, &a);
if (a == 0)
{
*p = SDL_MapRGBA(sur->format, 255, 255, 255, 0);
}
else
{
*p = SDL_MapRGBA(sur->format, 255, 255, 255, 255);
}
}
}
bool Engine::setKeepRatio(bool b)
{
return keep_ratio_ = b;
}
//创建一个专用于画场景的,后期放大
void Engine::createAssistTexture(int w, int h)
{
//tex_ = createYUVTexture(w, h);
tex2_ = createARGBRenderedTexture(w, h);
//tex_ = createARGBRenderedTexture(768, 480);
//SDL_SetTextureBlendMode(tex2_, SDL_BLENDMODE_BLEND);
}
void Engine::setPresentPosition(BP_Texture* tex)
{
if (!tex)
{
return;
}
int w_dst = 0, h_dst = 0;
int w_src = 0, h_src = 0;
getWindowSize(w_dst, h_dst);
queryTexture(tex, &w_src, &h_src);
w_src *= ratio_x_;
h_src *= ratio_y_;
if (keep_ratio_)
{
if (w_src == 0 || h_src == 0)
{
return;
}
double w_ratio = 1.0 * w_dst / w_src;
double h_ratio = 1.0 * h_dst / h_src;
double ratio = std::min(w_ratio, h_ratio);
if (w_ratio > h_ratio)
{
//宽度大,左右留空
rect_.x = (w_dst - w_src * ratio) / 2;
rect_.y = 0;
rect_.w = w_src * ratio;
rect_.h = h_dst;
}
else
{
//高度大,上下留空
rect_.x = 0;
rect_.y = (h_dst - h_src * ratio) / 2;
rect_.w = w_dst;
rect_.h = h_src * ratio;
}
}
else
{
rect_.x = 0;
rect_.y = 0;
rect_.w = w_dst;
rect_.h = h_dst;
}
}
BP_Texture* Engine::transBitmapToTexture(const uint8_t* src, uint32_t color, int w, int h, int stride)
{
auto s = SDL_CreateRGBSurface(0, w, h, 32, 0xff000000, 0xff0000, 0xff00, 0xff);
SDL_FillRect(s, nullptr, color);
auto p = (uint8_t*)s->pixels;
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
p[4 * (y * w + x)] = src[y * stride + x];
}
}
auto t = SDL_CreateTextureFromSurface(renderer_, s);
SDL_FreeSurface(s);
setTextureBlendMode(t);
setTextureAlphaMod(t, 192);
return t;
}
int Engine::showMessage(const std::string& content)
{
const SDL_MessageBoxButtonData buttons[] =
{
{ /* .flags, .buttonid, .text */ 0, 0, "no" },
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "yes" },
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "cancel" },
};
const SDL_MessageBoxColorScheme colorScheme =
{
{ /* .colors (.r, .g, .b) */
/* [SDL_MESSAGEBOX_COLOR_BACKGROUND] */
{ 255, 0, 0 },
/* [SDL_MESSAGEBOX_COLOR_TEXT] */
{ 0, 255, 0 },
/* [SDL_MESSAGEBOX_COLOR_BUTTON_BORDER] */
{ 255, 255, 0 },
/* [SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND] */
{ 0, 0, 255 },
/* [SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED] */
{ 255, 0, 255 }
}
};
const SDL_MessageBoxData messageboxdata =
{
SDL_MESSAGEBOX_INFORMATION, /* .flags */
NULL, /* .window */
title_.c_str(), /* .title */
content.c_str(), /* .message */
SDL_arraysize(buttons), /* .numbuttons */
buttons, /* .buttons */
&colorScheme /* .colorScheme */
};
int buttonid;
SDL_ShowMessageBox(&messageboxdata, &buttonid);
return buttonid;
}
void Engine::setWindowSize(int w, int h)
{
if (w <= 0 || h <= 0)
{
return;
}
win_w_ = std::min(max_x_ - min_x_, w);
win_h_ = std::min(max_y_ - min_y_, h);
SDL_SetWindowSize(window_, win_w_, win_h_);
//setPresentPosition();
getWindowSize(win_w_, win_h_);
//resetWindowsPosition();
//renderPresent();
}
void Engine::resetWindowPosition()
{
int x, y, w, h, x0, y0;
getWindowSize(w, h);
SDL_GetWindowPosition(window_, &x0, &y0);
x = std::max(min_x_, x0);
y = std::max(min_y_, y0);
if (x + w > max_x_)
{
x = std::min(x, max_x_ - w);
}
if (y + h > max_y_)
{
y = std::min(y, max_y_ - h);
}
if (x != x0 || y != y0)
{
setWindowPosition(x, y);
}
}
void Engine::setColor(BP_Texture* tex, BP_Color c)
{
SDL_SetTextureColorMod(tex, c.r, c.g, c.b);
setTextureAlphaMod(tex, c.a);
setTextureBlendMode(tex);
}
void Engine::fillColor(BP_Color color, int x, int y, int w, int h)
{
if (w < 0 || h < 0)
{
getWindowSize(w, h);
}
BP_Rect r{ x, y, w, h };
SDL_SetRenderDrawColor(renderer_, color.r, color.g, color.b, color.a);
SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND);
SDL_RenderFillRect(renderer_, &r);
}
void Engine::renderAssistTextureToWindow()
{
resetRenderTarget();
renderCopy(tex2_, nullptr, nullptr);
}
void Engine::renderSquareTexture(BP_Rect* rect, BP_Color color, uint8_t alpha)
{
color.a = alpha;
setColor(square_, color);
renderCopy(square_, nullptr, rect);
}
int Engine::playVideo(std::string filename)
{
if (filename == "")
{
return 0;
}
#if defined(_WIN32) && defined(WITH_SMALLPOT) && !defined(_DEBUG)
return PotInputVideo(tinypot_, (char*)filename.c_str());
#endif
return 0;
}
int Engine::saveScreen(const char* filename)
{
BP_Rect rect;
rect.x = 0;
rect.y = 0;
getWindowSize(rect.w, rect.h);
BP_Surface* sur = SDL_CreateRGBSurface(0, rect.w, rect.h, 32, RMASK, GMASK, BMASK, AMASK);
SDL_RenderReadPixels(renderer_, &rect, SDL_PIXELFORMAT_ARGB8888, sur->pixels, rect.w * 4);
SDL_SaveBMP(sur, filename);
SDL_FreeSurface(sur);
return 0;
}
int Engine::saveTexture(BP_Texture* tex, const char* filename)
{
BP_Rect rect;
rect.x = 0;
rect.y = 0;
queryTexture(tex, &rect.w, &rect.h);
setRenderTarget(tex);
BP_Surface* sur = SDL_CreateRGBSurface(0, rect.w, rect.h, 32, RMASK, GMASK, BMASK, AMASK);
SDL_RenderReadPixels(renderer_, &rect, SDL_PIXELFORMAT_ARGB8888, sur->pixels, rect.w * 4);
SDL_SaveBMP(sur, filename);
SDL_FreeSurface(sur);
resetRenderTarget();
return 0;
}
void Engine::setWindowPosition(int x, int y)
{
int w, h;
getWindowSize(w, h);
if (x == BP_WINDOWPOS_CENTERED)
{
x = min_x_ + (max_x_ - min_x_ - w) / 2;
}
if (y == BP_WINDOWPOS_CENTERED)
{
y = min_y_ + (max_y_ - min_y_ - h) / 2;
}
SDL_SetWindowPosition(window_, x, y);
}