Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/Background.cpp
4776 views
1
#include <cmath>
2
#include "Common/GPU/thin3d.h"
3
#include "Common/Data/Text/I18n.h"
4
#include "Common/Math/geom2d.h"
5
#include "Common/Math/curves.h"
6
#include "Common/Data/Random/Rng.h"
7
#include "Common/Data/Color/RGBAUtil.h"
8
#include "Common/UI/Context.h"
9
#include "Common/UI/UI.h"
10
#include "Common/File/Path.h"
11
#include "Common/File/FileUtil.h"
12
#include "Common/TimeUtil.h"
13
#include "Common/Render/ManagedTexture.h"
14
#include "Common/System/System.h"
15
#include "Common/System/Display.h"
16
#include "Common/System/Request.h"
17
#include "Core/ConfigValues.h"
18
#include "Core/Config.h"
19
#include "Core/System.h"
20
#include "Core/Util/RecentFiles.h"
21
#include "UI/Background.h"
22
#include "UI/GameInfoCache.h"
23
24
25
#ifdef _MSC_VER
26
#pragma execution_character_set("utf-8")
27
#endif
28
29
static Draw::Texture *bgTexture;
30
31
class Animation {
32
public:
33
virtual ~Animation() = default;
34
virtual void Draw(UIContext &dc, double t, float alpha, float x, float y, float z) = 0;
35
};
36
37
class MovingBackground : public Animation {
38
public:
39
void Draw(UIContext &dc, double t, float alpha, float x, float y, float z) override {
40
if (!bgTexture)
41
return;
42
43
dc.Flush();
44
dc.GetDrawContext()->BindTexture(0, bgTexture);
45
Bounds bounds = dc.GetBounds();
46
47
x = std::min(std::max(x / bounds.w, 0.0f), 1.0f) * XFAC;
48
y = std::min(std::max(y / bounds.h, 0.0f), 1.0f) * YFAC;
49
z = 1.0f + std::max(XFAC, YFAC) + (z - 1.0f) * ZFAC;
50
51
lastX_ = abs(x - lastX_) > 0.001f ? x * XSPEED + lastX_ * (1.0f - XSPEED) : x;
52
lastY_ = abs(y - lastY_) > 0.001f ? y * YSPEED + lastY_ * (1.0f - YSPEED) : y;
53
lastZ_ = abs(z - lastZ_) > 0.001f ? z * ZSPEED + lastZ_ * (1.0f - ZSPEED) : z;
54
55
float u1 = lastX_ / lastZ_;
56
float v1 = lastY_ / lastZ_;
57
float u2 = (1.0f + lastX_) / lastZ_;
58
float v2 = (1.0f + lastY_) / lastZ_;
59
60
dc.Draw()->DrawTexRect(bounds, u1, v1, u2, v2, whiteAlpha(alpha));
61
62
dc.Flush();
63
dc.RebindTexture();
64
}
65
66
private:
67
static constexpr float XFAC = 0.3f;
68
static constexpr float YFAC = 0.3f;
69
static constexpr float ZFAC = 0.12f;
70
static constexpr float XSPEED = 0.05f;
71
static constexpr float YSPEED = 0.05f;
72
static constexpr float ZSPEED = 0.1f;
73
74
float lastX_ = 0.0f;
75
float lastY_ = 0.0f;
76
float lastZ_ = 1.0f + std::max(XFAC, YFAC);
77
};
78
79
class WaveAnimation : public Animation {
80
public:
81
void Draw(UIContext &dc, double t, float alpha, float x, float y, float z) override {
82
const uint32_t color = colorAlpha(0xFFFFFFFF, alpha * 0.2f);
83
const float speed = 1.0;
84
85
Bounds bounds = dc.GetBounds();
86
dc.Flush();
87
dc.BeginNoTex();
88
89
// 500 is enough for any resolution really. 24 * 500 = 12000 which fits handily in our UI vertex buffer (max 65536 per flush).
90
const int steps = std::max(20, std::min((int)g_display.dp_xres, 500));
91
float step = (float)g_display.dp_xres / (float)steps;
92
t *= speed;
93
94
for (int n = 0; n < steps; n++) {
95
float x = (float)n * step;
96
float nextX = (float)(n + 1) * step;
97
float i = x * 1280 / bounds.w;
98
99
float wave0 = sin(i * 0.005 + t * 0.8) * 0.05 + sin(i * 0.002 + t * 0.25) * 0.02 + sin(i * 0.001 + t * 0.3) * 0.03 + 0.625;
100
float wave1 = sin(i * 0.0044 + t * 0.4) * 0.07 + sin(i * 0.003 + t * 0.1) * 0.02 + sin(i * 0.001 + t * 0.3) * 0.01 + 0.625;
101
dc.Draw()->RectVGradient(x, wave0 * bounds.h, nextX, bounds.h, color, 0x00000000);
102
dc.Draw()->RectVGradient(x, wave1 * bounds.h, nextX, bounds.h, color, 0x00000000);
103
104
// Add some "antialiasing"
105
dc.Draw()->RectVGradient(x, wave0 * bounds.h - 3.0f * g_display.pixel_in_dps_y, nextX, wave0 * bounds.h, 0x00000000, color);
106
dc.Draw()->RectVGradient(x, wave1 * bounds.h - 3.0f * g_display.pixel_in_dps_y, nextX, wave1 * bounds.h, 0x00000000, color);
107
}
108
109
dc.Flush();
110
dc.Begin();
111
}
112
};
113
114
class FloatingSymbolsAnimation : public Animation {
115
public:
116
FloatingSymbolsAnimation(bool is_colored) {
117
this->is_colored = is_colored;
118
}
119
120
void Draw(UIContext &dc, double t, float alpha, float x, float y, float z) override {
121
dc.Flush();
122
dc.Begin();
123
float xres = dc.GetBounds().w;
124
float yres = dc.GetBounds().h;
125
if (last_xres != xres || last_yres != yres) {
126
Regenerate(xres, yres);
127
}
128
129
for (int i = 0; i < COUNT; i++) {
130
float x = xbase[i] + dc.GetBounds().x;
131
float y = ybase[i] + dc.GetBounds().y + 40 * cosf(i * 7.2f + t * 1.3f);
132
float angle = (float)sin(i + t);
133
int n = i & 3;
134
Color color = is_colored ? colorAlpha(COLORS[n], alpha * 0.25f) : colorAlpha(DEFAULT_COLOR, alpha * 0.1f);
135
ui_draw2d.DrawImageRotated(SYMBOLS[n], x, y, 1.0f, angle, color);
136
}
137
dc.Flush();
138
}
139
140
private:
141
static constexpr int COUNT = 100;
142
static constexpr Color DEFAULT_COLOR = 0xC0FFFFFF;
143
static constexpr Color COLORS[4] = {0xFFE3B56F, 0xFF615BFF, 0xFFAA88F3, 0xFFC2CC7A,}; // X O D A
144
static const ImageID SYMBOLS[4];
145
146
bool is_colored = false;
147
float xbase[COUNT]{};
148
float ybase[COUNT]{};
149
float last_xres = 0;
150
float last_yres = 0;
151
152
void Regenerate(int xres, int yres) {
153
GMRng rng;
154
for (int i = 0; i < COUNT; i++) {
155
xbase[i] = rng.F() * xres;
156
ybase[i] = rng.F() * yres;
157
}
158
159
last_xres = xres;
160
last_yres = yres;
161
}
162
};
163
164
const ImageID FloatingSymbolsAnimation::SYMBOLS[4] = {
165
ImageID("I_CROSS"),
166
ImageID("I_CIRCLE"),
167
ImageID("I_SQUARE"),
168
ImageID("I_TRIANGLE"),
169
};
170
171
class RecentGamesAnimation : public Animation {
172
public:
173
void Draw(UIContext &dc, double t, float alpha, float x, float y, float z) override {
174
if (lastIndex_ == nextIndex_) {
175
CheckNext(dc, t);
176
} else if (t > nextT_) {
177
lastIndex_ = nextIndex_;
178
}
179
180
if (g_recentFiles.HasAny()) {
181
std::shared_ptr<GameInfo> lastInfo = GetInfo(dc, lastIndex_);
182
std::shared_ptr<GameInfo> nextInfo = GetInfo(dc, nextIndex_);
183
dc.Flush();
184
185
float lastAmount = Clamp((float)(nextT_ - t) * 1.0f / TRANSITION, 0.0f, 1.0f);
186
DrawTex(dc, lastInfo, lastAmount * alpha * 0.2f);
187
188
float nextAmount = lastAmount <= 0.0f ? 1.0f : 1.0f - lastAmount;
189
DrawTex(dc, nextInfo, nextAmount * alpha * 0.2f);
190
191
dc.RebindTexture();
192
}
193
}
194
195
private:
196
void CheckNext(UIContext &dc, double t) {
197
if (!g_recentFiles.HasAny()) {
198
return;
199
}
200
201
std::vector<std::string> recents = g_recentFiles.GetRecentFiles();
202
203
for (int index = lastIndex_ + 1; index != lastIndex_; ++index) {
204
if (index < 0 || index >= (int)recents.size()) {
205
if (lastIndex_ == -1)
206
break;
207
index = 0;
208
}
209
210
std::shared_ptr<GameInfo> ginfo = GetInfo(dc, index);
211
if (ginfo && !ginfo->Ready(GameInfoFlags::PIC1)) {
212
// Wait for it to load. It might be the next one.
213
break;
214
}
215
if (ginfo && ginfo->pic1.texture) {
216
nextIndex_ = index;
217
nextT_ = t + INTERVAL;
218
break;
219
}
220
221
// Otherwise, keep going. This skips games with no BG.
222
}
223
}
224
225
static std::shared_ptr<GameInfo> GetInfo(UIContext &dc, int index) {
226
if (index < 0) {
227
return nullptr;
228
}
229
const auto recentIsos = g_recentFiles.GetRecentFiles();
230
if (index >= (int)recentIsos.size())
231
return std::shared_ptr<GameInfo>();
232
return g_gameInfoCache->GetInfo(dc.GetDrawContext(), Path(recentIsos[index]), GameInfoFlags::PIC1);
233
}
234
235
static void DrawTex(UIContext &dc, std::shared_ptr<GameInfo> &ginfo, float amount) {
236
if (!ginfo || amount <= 0.0f)
237
return;
238
GameInfoTex *pic = ginfo->GetPIC1();
239
if (!pic)
240
return;
241
242
dc.GetDrawContext()->BindTexture(0, pic->texture);
243
uint32_t color = whiteAlpha(amount) & 0xFFc0c0c0;
244
dc.Draw()->DrawTexRect(dc.GetBounds(), 0, 0, 1, 1, color);
245
dc.Flush();
246
}
247
248
static constexpr double INTERVAL = 8.0;
249
static constexpr float TRANSITION = 3.0f;
250
251
int lastIndex_ = -1;
252
int nextIndex_ = -1;
253
double nextT_ = -INTERVAL;
254
};
255
256
class BouncingIconAnimation : public Animation {
257
public:
258
void Draw(UIContext &dc, double t, float alpha, float x, float y, float z) override {
259
dc.Flush();
260
dc.Begin();
261
262
// Handle change in resolution.
263
float xres = dc.GetBounds().w;
264
float yres = dc.GetBounds().h;
265
if (last_xres != xres || last_yres != yres) {
266
Recalculate(xres, yres);
267
}
268
269
// Draw the image.
270
float xpos = xbase + dc.GetBounds().x;
271
float ypos = ybase + dc.GetBounds().y;
272
ImageID icon = !color_ix && System_GetPropertyBool(SYSPROP_APP_GOLD) ? ImageID("I_ICON_GOLD") : ImageID("I_ICON");
273
ui_draw2d.DrawImage(icon, xpos, ypos, scale, COLORS[color_ix], ALIGN_CENTER);
274
dc.Flush();
275
276
// Switch direction if within border.
277
bool should_recolor = true;
278
if (xbase > xres - border || xbase < border) {
279
xspeed *= -1.0f;
280
RandomizeColor();
281
should_recolor = false;
282
}
283
284
if (ybase > yres - border || ybase < border) {
285
yspeed *= -1.0f;
286
287
if (should_recolor) {
288
RandomizeColor();
289
}
290
}
291
292
// Place to border if out of bounds.
293
if (xbase > xres - border) xbase = xres - border;
294
else if (xbase < border) xbase = border;
295
if (ybase > yres - border) ybase = yres - border;
296
else if (ybase < border) ybase = border;
297
298
// Update location.
299
xbase += xspeed;
300
ybase += yspeed;
301
}
302
303
private:
304
static constexpr int COLOR_COUNT = 11;
305
static constexpr Color COLORS[COLOR_COUNT] = {0xFFFFFFFF, 0xFFFFFF00, 0xFFFF0000, 0xFF00FF00, 0xFF0000FF,
306
0xFF00FFFF, 0xFFFF00FF, 0xFF4111D1, 0xFF3577F3, 0xFFAA77FF, 0xFF623B84};
307
308
float xbase = 0.0f;
309
float ybase = 0.0f;
310
float last_xres = 0.0f;
311
float last_yres = 0.0f;
312
float xspeed = 1.0f;
313
float yspeed = 1.0f;
314
float scale = 1.0f;
315
float border = 35.0f;
316
int color_ix = 0;
317
int last_color_ix = -1;
318
GMRng rng;
319
320
void Recalculate(int xres, int yres) {
321
// First calculation.
322
if (last_color_ix == -1) {
323
xbase = xres / 2.0f;
324
ybase = yres / 2.0f;
325
last_color_ix = 0;
326
327
// Determine initial direction.
328
if ((int)(rng.F() * xres) % 2) xspeed *= -1.0f;
329
if ((int)(rng.F() * yres) % 2) yspeed *= -1.0f;
330
}
331
332
// Scale certain attributes to resolution.
333
scale = std::min(xres, yres) / 400.0f;
334
float speed = scale < 2.5f ? scale * 0.58f : scale * 0.46f;
335
xspeed = std::signbit(xspeed) ? speed * -1.0f : speed;
336
yspeed = std::signbit(yspeed) ? speed * -1.0f : speed;
337
border = 35.0f * scale;
338
339
last_xres = xres;
340
last_yres = yres;
341
}
342
343
void RandomizeColor() {
344
do {
345
color_ix = (int)(rng.F() * xbase) % COLOR_COUNT;
346
} while (color_ix == last_color_ix);
347
348
last_color_ix = color_ix;
349
}
350
};
351
352
// TODO: Add more styles. Remember to add to the enum in ConfigValues.h and the selector in GameSettings too.
353
354
static BackgroundAnimation g_CurBackgroundAnimation = BackgroundAnimation::OFF;
355
static std::unique_ptr<Animation> g_Animation;
356
static bool bgTextureInited = false; // Separate variable because init could fail.
357
358
void UIBackgroundInit(UIContext &dc) {
359
const Path bgPng = GetSysDirectory(DIRECTORY_SYSTEM) / "background.png";
360
const Path bgJpg = GetSysDirectory(DIRECTORY_SYSTEM) / "background.jpg";
361
if (File::Exists(bgPng) || File::Exists(bgJpg)) {
362
const Path &bgFile = File::Exists(bgPng) ? bgPng : bgJpg;
363
bgTexture = CreateTextureFromFile(dc.GetDrawContext(), bgFile.c_str(), ImageFileType::DETECT, true);
364
}
365
}
366
367
void UIBackgroundShutdown() {
368
if (bgTexture) {
369
bgTexture->Release();
370
bgTexture = nullptr;
371
}
372
bgTextureInited = false;
373
g_Animation.reset(nullptr);
374
g_CurBackgroundAnimation = BackgroundAnimation::OFF;
375
}
376
377
void DrawBackground(UIContext &dc, float alpha, float x, float y, float z) {
378
if (!bgTextureInited) {
379
UIBackgroundInit(dc);
380
bgTextureInited = true;
381
}
382
if (g_CurBackgroundAnimation != (BackgroundAnimation)g_Config.iBackgroundAnimation) {
383
g_CurBackgroundAnimation = (BackgroundAnimation)g_Config.iBackgroundAnimation;
384
385
switch (g_CurBackgroundAnimation) {
386
case BackgroundAnimation::FLOATING_SYMBOLS:
387
g_Animation.reset(new FloatingSymbolsAnimation(false));
388
break;
389
case BackgroundAnimation::RECENT_GAMES:
390
g_Animation.reset(new RecentGamesAnimation());
391
break;
392
case BackgroundAnimation::WAVE:
393
g_Animation.reset(new WaveAnimation());
394
break;
395
case BackgroundAnimation::MOVING_BACKGROUND:
396
g_Animation.reset(new MovingBackground());
397
break;
398
case BackgroundAnimation::BOUNCING_ICON:
399
g_Animation.reset(new BouncingIconAnimation());
400
break;
401
case BackgroundAnimation::FLOATING_SYMBOLS_COLORED:
402
g_Animation.reset(new FloatingSymbolsAnimation(true));
403
break;
404
default:
405
g_Animation.reset(nullptr);
406
}
407
}
408
409
uint32_t bgColor = whiteAlpha(alpha);
410
411
if (bgTexture != nullptr) {
412
dc.Flush();
413
dc.Begin();
414
dc.GetDrawContext()->BindTexture(0, bgTexture);
415
dc.Draw()->DrawTexRect(dc.GetBounds(), 0, 0, 1, 1, bgColor);
416
417
dc.Flush();
418
dc.RebindTexture();
419
} else {
420
// I_BG original color: 0xFF754D24
421
ImageID img = ImageID("I_BG");
422
dc.Begin();
423
dc.Draw()->DrawImageStretch(img, dc.GetBounds(), bgColor & dc.GetTheme().backgroundColor);
424
dc.Flush();
425
}
426
427
#if PPSSPP_PLATFORM(IOS)
428
// iOS uses an old screenshot when restoring the task, so to avoid an ugly
429
// jitter we accumulate time instead.
430
static int frameCount = 0.0;
431
frameCount++;
432
double t = (double)frameCount / System_GetPropertyFloat(SYSPROP_DISPLAY_REFRESH_RATE);
433
#else
434
double t = time_now_d();
435
#endif
436
437
if (g_Animation) {
438
g_Animation->Draw(dc, t, alpha, x, y, z);
439
}
440
}
441
442
uint32_t GetBackgroundColorWithAlpha(const UIContext &dc) {
443
return colorAlpha(colorBlend(dc.GetTheme().backgroundColor, 0, 0.5f), 0.65f); // 0.65 = 166 = A6
444
}
445
446
enum class BackgroundFillMode {
447
Stretch = 0,
448
CropToScreen = 1,
449
FitToScreen = 2,
450
};
451
452
void DrawGameBackground(UIContext &dc, const Path &gamePath, float x, float y, float z) {
453
using namespace Draw;
454
using namespace UI;
455
dc.Flush();
456
457
std::shared_ptr<GameInfo> ginfo;
458
if (!gamePath.empty()) {
459
ginfo = g_gameInfoCache->GetInfo(dc.GetDrawContext(), gamePath, GameInfoFlags::PIC1);
460
}
461
462
GameInfoTex *pic = (ginfo && ginfo->Ready(GameInfoFlags::PIC1)) ? ginfo->GetPIC1() : nullptr;
463
if (pic && pic->texture) {
464
dc.GetDrawContext()->BindTexture(0, pic->texture);
465
uint32_t color = whiteAlpha(ease((time_now_d() - pic->timeLoaded) * 3)) & 0xFFc0c0c0;
466
467
// TODO: Make this configurable?
468
const BackgroundFillMode mode = BackgroundFillMode::CropToScreen;
469
470
const Bounds screenBounds = dc.GetBounds();
471
float imageW = screenBounds.w;
472
float imageH = screenBounds.h;
473
float imageAspect = (float)pic->texture->Width() / (float)pic->texture->Height();
474
475
// Fit the image into the screen bounds according to the fill mode.
476
if (imageAspect > screenBounds.AspectRatio()) {
477
// Image is wider than screen.
478
if (mode == BackgroundFillMode::CropToScreen) {
479
// Crop width.
480
imageW = screenBounds.h * imageAspect;
481
} else if (mode == BackgroundFillMode::FitToScreen) {
482
// Fit height.
483
imageH = screenBounds.w / imageAspect;
484
}
485
} else {
486
// Image is taller than screen.
487
if (mode == BackgroundFillMode::CropToScreen) {
488
// Crop height.
489
imageH = screenBounds.w / imageAspect;
490
} else if (mode == BackgroundFillMode::FitToScreen) {
491
// Fit width.
492
imageW = screenBounds.h / imageAspect;
493
}
494
}
495
496
Bounds finalImageBounds = Bounds::FromCenterWH(screenBounds.centerX(), screenBounds.centerY(), imageW, imageH);
497
498
dc.Draw()->DrawTexRect(finalImageBounds, 0, 0, 1, 1, color);
499
dc.Flush();
500
dc.RebindTexture();
501
} else {
502
::DrawBackground(dc, 1.0f, x, y, z);
503
dc.RebindTexture();
504
dc.Flush();
505
}
506
}
507
508