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