-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwiLoadingScreen.cpp
More file actions
139 lines (121 loc) · 3.43 KB
/
Copy pathwiLoadingScreen.cpp
File metadata and controls
139 lines (121 loc) · 3.43 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
#include "wiLoadingScreen.h"
#include "wiApplication.h"
#include "wiEventHandler.h"
#include <thread>
using namespace wi::graphics;
namespace wi
{
bool LoadingScreen::isActive() const
{
return wi::jobsystem::IsBusy(ctx);
}
bool LoadingScreen::isFinished() const
{
return tasks.empty();
}
int LoadingScreen::getProgress() const
{
if (launchedTasks == 0)
return 100;
uint32_t counter = wi::jobsystem::GetRemainingJobCount(ctx);
float percent = 1 - float(counter) / float(launchedTasks);
return (int)std::round(percent * 100);
}
void LoadingScreen::addLoadingFunction(const std::function<void(wi::jobsystem::JobArgs)>& loadingFunction)
{
if (loadingFunction != nullptr)
{
tasks.push_back(loadingFunction);
}
}
void LoadingScreen::addLoadingComponent(RenderPath* component, Application* main, float fadeSeconds, wi::Color fadeColor, wi::FadeManager::FadeType fadetype)
{
addLoadingFunction([=](wi::jobsystem::JobArgs args) {
component->Load();
});
onFinished([=] {
main->ActivatePath(component, fadeSeconds, fadeColor, fadetype);
});
}
void LoadingScreen::onFinished(const std::function<void()>& finishFunction)
{
if (finishFunction != nullptr)
finish = finishFunction;
}
void LoadingScreen::Start()
{
launchedTasks = (uint32_t)tasks.size();
for (auto& x : tasks)
{
wi::jobsystem::Execute(ctx, x);
}
std::thread([this]() {
wi::jobsystem::Wait(ctx);
wi::eventhandler::Subscribe_Once(wi::eventhandler::EVENT_THREAD_SAFE_POINT, [this](uint64_t) {
if (finish != nullptr)
finish();
tasks.clear();
launchedTasks = 0;
finish = nullptr;
});
}).detach();
RenderPath2D::Start();
}
void LoadingScreen::Compose(wi::graphics::CommandList cmd) const
{
if (backgroundTexture.IsValid())
{
wi::image::Params fx;
const Texture& tex = backgroundTexture.GetTexture();
const TextureDesc& desc = tex.GetDesc();
const float canvas_aspect = GetLogicalWidth() / GetLogicalHeight();
const float image_aspect = float(desc.width) / float(desc.height);
switch (background_mode)
{
default:
case wi::LoadingScreen::BackgroundMode::Fill:
if (canvas_aspect > image_aspect)
{
// display aspect is wider than image:
fx.siz.x = GetLogicalWidth();
fx.siz.y = GetLogicalHeight() / image_aspect * canvas_aspect;
}
else
{
// image aspect is wider or equal to display
fx.siz.x = GetLogicalWidth() / canvas_aspect * image_aspect;
fx.siz.y = GetLogicalHeight();
}
fx.pos = XMFLOAT3(GetLogicalWidth() * 0.5f, GetLogicalHeight() * 0.5f, 0);
fx.pivot = XMFLOAT2(0.5f, 0.5f);
break;
case wi::LoadingScreen::BackgroundMode::Fit:
if (canvas_aspect > image_aspect)
{
// display aspect is wider than image:
fx.siz.x = GetLogicalWidth() / canvas_aspect * image_aspect;
fx.siz.y = GetLogicalHeight();
}
else
{
// image aspect is wider or equal to display
fx.siz.x = GetLogicalWidth();
fx.siz.y = GetLogicalHeight() * canvas_aspect / image_aspect;
}
fx.pos = XMFLOAT3(GetLogicalWidth() * 0.5f, GetLogicalHeight() * 0.5f, 0);
fx.pivot = XMFLOAT2(0.5f, 0.5f);
break;
case wi::LoadingScreen::BackgroundMode::Stretch:
fx.enableFullScreen();
break;
}
fx.blendFlag = wi::enums::BLENDMODE_ALPHA;
if (colorspace != ColorSpace::SRGB)
{
fx.enableLinearOutputMapping(hdr_scaling);
}
wi::image::Draw(&tex, fx, cmd);
}
RenderPath2D::Compose(cmd);
}
}