/
loading_screen.cpp
237 lines (214 loc) · 6.48 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
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
Copyright (C) 2016 - 2017 by the Battle for Wesnoth Project http://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 "cursor.hpp"
#include "gui/dialogs/loading_screen.hpp"
#include "gui/widgets/label.hpp"
#include "gui/widgets/window.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/core/timer.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "video.hpp"
#include "cursor.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "preferences/general.hpp"
#include "utils/functional.hpp"
#include <boost/thread.hpp>
static lg::log_domain log_loadscreen("loadscreen");
#define ERR_LS LOG_STREAM(err, log_loadscreen)
#define WRN_LS LOG_STREAM(warn, log_loadscreen)
#define LOG_LS LOG_STREAM(info, log_loadscreen)
#define DBG_LS LOG_STREAM(debug, log_loadscreen)
static const std::map<std::string, std::string> stages =
{
{ "build terrain", N_("Building terrain rules") },
{ "create cache", N_("Reading files and creating cache") },
{ "init display", N_("Initializing display") },
{ "init fonts", N_("Reinitialize fonts for the current language") },
{ "init teams", N_("Initializing teams") },
{ "init theme", N_("Initializing display") },
{ "load config", N_("Loading game configuration") },
{ "load data", N_("Loading data files") },
{ "load level", N_("Loading level") },
{ "init lua", N_("Initializing scripting engine") },
{ "init whiteboard", N_("Initializing planning mode") },
{ "load unit types", N_("Reading unit files") },
{ "load units", N_("Loading units") },
{ "refresh addons", N_("Searching for installed add-ons") },
{ "start game", N_("Starting game") },
{ "verify cache", N_("Verifying cache") },
{ "connect to server", N_("Connecting to server") },
{ "login response", N_("Logging in") },
{ "waiting", N_("Waiting for server") },
{ "redirect", N_("Connecting to redirected server") },
{ "next scenario", N_("Waiting for next scenario") },
{ "download level data", N_("Getting game data") },
};
namespace gui2
{
namespace dialogs
{
REGISTER_DIALOG(loading_screen)
loading_screen::loading_screen(std::function<void()> f)
: window_(nullptr)
, timer_id_(0)
, animation_counter_(0)
, work_(f)
, worker_()
, cursor_setter_()
, progress_stage_label_(nullptr)
, animation_label_(nullptr)
, current_stage_(nullptr)
, visible_stages_()
, animation_stages_()
, current_visible_stage_()
, is_worker_running_(false)
{
for (const auto& pair : stages) {
visible_stages_[pair.first] = t_string(pair.second, "wesnoth-lib") + "...";
}
for (int i = 0; i != 20; ++i) {
std::string s(20, ' ');
s[i] = '.';
animation_stages_.push_back(s);
}
current_visible_stage_ = visible_stages_.end();
current_load = this;
}
void loading_screen::close()
{
if(window_) {
delete window_;
window_ = nullptr;
}
}
window* loading_screen::build_window(CVideo& video) const
{
return build(video, window_id());
}
void loading_screen::pre_show(window& window)
{
if (work_) {
worker_.reset(new boost::thread([this]() {
is_worker_running_ = true;
try {
work_();
} catch(...) {
//TODO: guard this with a mutex.
exception_ = std::current_exception();
}
is_worker_running_ = false;
}));
}
timer_id_ = add_timer(100, std::bind(&loading_screen::timer_callback, this, std::ref(window)), true);
cursor_setter_.reset(new cursor::setter(cursor::WAIT));
progress_stage_label_ = &find_widget<label>(&window, "status", false);
animation_label_ = &find_widget<label>(&window, "test_animation", false);
window.set_enter_disabled(true);
window.set_escape_disabled(true);
}
void loading_screen::post_show(window& /*window*/)
{
worker_.reset();
clear_timer();
cursor_setter_.reset();
}
void loading_screen::progress(const char* stage)
{
if(!current_load) {
return;
}
if(stage) {
current_load->current_stage_
#if defined(_MSC_VER) && _MSC_VER < 1900
= stage;
#else
.store(stage, std::memory_order_release);
#endif
}
}
loading_screen* loading_screen::current_load = nullptr;
void loading_screen::timer_callback(window& window)
{
if (!work_ || !worker_ || worker_->timed_join(boost::posix_time::milliseconds(0))) {
if (exception_) {
clear_timer();
std::rethrow_exception(exception_);
}
window.close();
}
if (!work_) {
return;
}
const char* stage = current_stage_
#if defined(_MSC_VER) && _MSC_VER < 1900
;
#else
.load(std::memory_order_acquire);
#endif
if (stage && (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 ID '" << 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 (is_worker_running_) {
// 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 here assumes
// that this happened becasue the window was closed which is not necessarily the case
// (other possibilities migth be a 'dialog doesn't fit on screen' exception casued by resizing the window)
// Another approach migth 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.
std::exit(0);
}
clear_timer();
close();
current_load = nullptr;
}
void loading_screen::display(CVideo& video, std::function<void()> f)
{
const bool use_loadingscreen_animation = !preferences::disable_loadingscreen_animation();
if (current_load || video.faked()) {
f();
}
else if(use_loadingscreen_animation) {
loading_screen(f).show(video);
}
else {
loading_screen(std::function<void()>()).show(video);
f();
}
}
void loading_screen::clear_timer()
{
if (timer_id_ != 0) {
remove_timer(timer_id_);
timer_id_ = 0;
}
}
} // namespace dialogs
} // namespace gui2