-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
loading_screen.cpp
224 lines (190 loc) · 6.84 KB
/
loading_screen.cpp
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
/*
Copyright (C) 2016 - 2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Screen with logo and loading status info during program-startup.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/dialogs/loading_screen.hpp"
#include "cursor.hpp"
#include "gettext.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/core/timer.hpp"
#include "gui/widgets/label.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "log.hpp"
#include "preferences/general.hpp"
#include "utils/functional.hpp"
#include "video.hpp"
#include <chrono>
#include <cstdlib>
static lg::log_domain log_loadscreen("loadscreen");
#define ERR_LS LOG_STREAM(err, log_loadscreen)
#define WRN_LS LOG_STREAM(warn, log_loadscreen)
static const std::map<loading_stage, std::string> stage_names {
{ loading_stage::build_terrain, N_("Building terrain rules") },
{ loading_stage::create_cache, N_("Reading files and creating cache") },
{ loading_stage::init_display, N_("Initializing display") },
{ loading_stage::init_fonts, N_("Reinitialize fonts for the current language") },
{ loading_stage::init_teams, N_("Initializing teams") },
{ loading_stage::init_theme, N_("Initializing display") },
{ loading_stage::load_config, N_("Loading game configuration") },
{ loading_stage::load_data, N_("Loading data files") },
{ loading_stage::load_level, N_("Loading level") },
{ loading_stage::init_lua, N_("Initializing scripting engine") },
{ loading_stage::init_whiteboard, N_("Initializing planning mode") },
{ loading_stage::load_unit_types, N_("Reading unit files") },
{ loading_stage::load_units, N_("Loading units") },
{ loading_stage::refresh_addons, N_("Searching for installed add-ons") },
{ loading_stage::start_game, N_("Starting game") },
{ loading_stage::verify_cache, N_("Verifying cache") },
{ loading_stage::connect_to_server, N_("Connecting to server") },
{ loading_stage::login_response, N_("Logging in") },
{ loading_stage::waiting, N_("Waiting for server") },
{ loading_stage::redirect, N_("Connecting to redirected server") },
{ loading_stage::next_scenario, N_("Waiting for next scenario") },
{ loading_stage::download_level_data, N_("Getting game data") },
{ loading_stage::download_lobby_data, N_("Downloading lobby data") },
};
namespace gui2
{
namespace dialogs
{
REGISTER_DIALOG(loading_screen)
loading_screen* loading_screen::singleton_ = nullptr;
loading_screen::loading_screen(std::function<void()> f)
: animation_counter_(0)
, load_func_(f)
, worker_result_()
, cursor_setter_()
, progress_stage_label_(nullptr)
, animation_label_(nullptr)
, current_stage_(loading_stage::none)
, visible_stages_()
, animation_stages_()
, current_visible_stage_()
{
for(const auto& pair : stage_names) {
visible_stages_[pair.first] = t_string(pair.second, "wesnoth-lib") + "...";
}
animation_stages_.reserve(20);
for(int i = 0; i != 20; ++i) {
std::string s(20, ' ');
s[i] = '.';
animation_stages_.push_back(std::move(s));
}
current_visible_stage_ = visible_stages_.end();
singleton_ = this;
}
void loading_screen::pre_show(window& window)
{
window.set_enter_disabled(true);
window.set_escape_disabled(true);
cursor_setter_.reset(new cursor::setter(cursor::WAIT));
if(load_func_) {
// Run the load function in its own thread.
try {
worker_result_ = std::async(std::launch::async, [this]() {
try {
load_func_();
} catch(...) {
// TODO: guard this with a mutex.
exception_ = std::current_exception();
}
});
} catch(const std::system_error& e) {
ERR_LS << "Failed to create worker thread: " << e.what() << "\n";
throw;
}
}
progress_stage_label_ = find_widget<label>(&window, "status", false, true);
animation_label_ = find_widget<label>(&window, "test_animation", false, true);
// Add a draw callback to handle the animation, et al.
window.connect_signal<event::DRAW>(
std::bind(&loading_screen::draw_callback, this), event::dispatcher::front_child);
}
void loading_screen::post_show(window& /*window*/)
{
cursor_setter_.reset();
}
void loading_screen::progress(loading_stage stage)
{
if(singleton_ && stage != loading_stage::none) {
singleton_->current_stage_.store(stage, std::memory_order_release);
}
}
void loading_screen::process(events::pump_info&)
{
if(!load_func_ || loading_complete()) {
if(exception_) {
std::rethrow_exception(exception_);
}
get_window()->close();
}
}
void loading_screen::draw_callback()
{
loading_stage stage = current_stage_.load(std::memory_order_acquire);
if(stage != loading_stage::none && (current_visible_stage_ == visible_stages_.end() || stage != current_visible_stage_->first)) {
auto iter = visible_stages_.find(stage);
if(iter == visible_stages_.end()) {
WRN_LS << "Stage missing description." << std::endl;
return;
}
current_visible_stage_ = iter;
progress_stage_label_->set_label(iter->second);
}
++animation_counter_;
if(animation_counter_ % 2 == 0) {
animation_label_->set_label(animation_stages_[(animation_counter_ / 2) % animation_stages_.size()]);
}
}
loading_screen::~loading_screen()
{
/* If the worker thread is running, exit the application to prevent memory corruption.
* TODO: this is still not optimal. The main problem is that this code assumes that this
* happened because the window was closed, which is not necessarily the case (other
* possibilities might be a 'dialog doesn't fit on screen' exception caused by resizing
* the window).
*
* Another approach might be to add exit points (boost::this_thread::interruption_point())
* to the worker functions (filesystem.cpp, config parsing code, etc.) and then use that
* to end the thread faster.
*/
if(!loading_complete()) {
#if defined(_LIBCPP_VERSION) || defined(__MINGW32__)
std::_Exit(0);
#else
std::quick_exit(0);
#endif
}
singleton_ = nullptr;
}
void loading_screen::display(std::function<void()> f)
{
const bool use_loadingscreen_animation = !preferences::disable_loadingscreen_animation();
if(singleton_ || CVideo::get_singleton().faked()) {
f();
} else if(use_loadingscreen_animation) {
loading_screen(f).show();
} else {
loading_screen(std::function<void()>()).show();
f();
}
}
bool loading_screen::loading_complete() const
{
using namespace std::chrono_literals;
return worker_result_.wait_for(0ms) == std::future_status::ready;
}
} // namespace dialogs
} // namespace gui2