The Battle for Wesnoth  1.15.12+dev
loading_screen.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 /**
15  * @file
16  * Screen with logo and loading status info during program-startup.
17  */
18 
19 #define GETTEXT_DOMAIN "wesnoth-lib"
20 
22 
23 #include "cursor.hpp"
24 #include "gettext.hpp"
26 #include "gui/core/timer.hpp"
27 #include "gui/widgets/drawing.hpp"
28 #include "gui/widgets/label.hpp"
29 #include "gui/widgets/settings.hpp"
30 #include "gui/widgets/window.hpp"
31 #include "log.hpp"
32 #include "preferences/general.hpp"
33 #include "video.hpp"
34 
35 #include <cstdlib>
36 #include <functional>
37 
38 static lg::log_domain log_loadscreen("loadscreen");
39 #define ERR_LS LOG_STREAM(err, log_loadscreen)
40 #define WRN_LS LOG_STREAM(warn, log_loadscreen)
41 
42 static const std::map<loading_stage, std::string> stage_names {
43  { loading_stage::build_terrain, N_("Building terrain rules") },
44  { loading_stage::create_cache, N_("Reading files and creating cache") },
45  { loading_stage::init_display, N_("Initializing display") },
46  { loading_stage::init_fonts, N_("Reinitialize fonts for the current language") },
47  { loading_stage::init_teams, N_("Initializing teams") },
48  { loading_stage::init_theme, N_("Initializing display") },
49  { loading_stage::load_config, N_("Loading game configuration") },
50  { loading_stage::load_data, N_("Loading data files") },
51  { loading_stage::load_level, N_("Loading level") },
52  { loading_stage::init_lua, N_("Initializing scripting engine") },
53  { loading_stage::init_whiteboard, N_("Initializing planning mode") },
54  { loading_stage::load_unit_types, N_("Reading unit files") },
55  { loading_stage::load_units, N_("Loading units") },
56  { loading_stage::refresh_addons, N_("Searching for installed add-ons") },
57  { loading_stage::start_game, N_("Starting game") },
58  { loading_stage::verify_cache, N_("Verifying cache") },
59  { loading_stage::connect_to_server, N_("Connecting to server") },
60  { loading_stage::login_response, N_("Logging in") },
61  { loading_stage::waiting, N_("Waiting for server") },
62  { loading_stage::redirect, N_("Connecting to redirected server") },
63  { loading_stage::next_scenario, N_("Waiting for next scenario") },
64  { loading_stage::download_level_data, N_("Getting game data") },
65  { loading_stage::download_lobby_data, N_("Downloading lobby data") },
66 };
67 
68 namespace gui2::dialogs
69 {
70 REGISTER_DIALOG(loading_screen)
71 
72 loading_screen* loading_screen::singleton_ = nullptr;
73 
74 loading_screen::loading_screen(std::function<void()> f)
75  : load_func_(f)
76  , worker_result_()
77  , cursor_setter_()
78  , progress_stage_label_(nullptr)
79  , animation_(nullptr)
80  , animation_start_()
81  , current_stage_(loading_stage::none)
82  , visible_stages_()
83  , current_visible_stage_()
84 {
85  for(const auto& [stage, description] : stage_names) {
86  visible_stages_[stage] = t_string(description, "wesnoth-lib") + "...";
87  }
88 
90  singleton_ = this;
91 }
92 
94 {
95  window.set_enter_disabled(true);
96  window.set_escape_disabled(true);
97 
99 
100  if(load_func_) {
101  // Run the load function in its own thread.
102  try {
103  worker_result_ = std::async(std::launch::async, load_func_);
104  } catch(const std::system_error& e) {
105  ERR_LS << "Failed to create worker thread: " << e.what() << "\n";
106  throw;
107  }
108  }
109 
110  progress_stage_label_ = find_widget<label>(&window, "status", false, true);
111  animation_ = find_widget<drawing>(&window, "animation", false, true);
112 
113  // Add a draw callback to handle the animation, et al.
114  window.connect_signal<event::DRAW>(
116 }
117 
119 {
120  cursor_setter_.reset();
121 }
122 
124 {
125  if(singleton_ && stage != loading_stage::none) {
126  singleton_->current_stage_.store(stage, std::memory_order_release);
127  }
128 }
129 
131 {
132  using namespace std::chrono_literals;
133 
134  if(!load_func_ || worker_result_.wait_for(0ms) == std::future_status::ready) {
135  // The worker returns void, so this is only to handle any exceptions thrown from the worker.
136  // worker_result_.valid() will return false after.
137  if(worker_result_.valid()) {
138  worker_result_.get();
139  }
140 
141  get_window()->close();
142  }
143 }
144 
146 {
147  loading_stage stage = current_stage_.load(std::memory_order_acquire);
148 
149  if(stage != loading_stage::none && (current_visible_stage_ == visible_stages_.end() || stage != current_visible_stage_->first)) {
150  auto iter = visible_stages_.find(stage);
151  if(iter == visible_stages_.end()) {
152  WRN_LS << "Stage missing description." << std::endl;
153  return;
154  }
155 
156  current_visible_stage_ = iter;
157  progress_stage_label_->set_label(iter->second);
158  }
159 
160  using namespace std::chrono;
161  const auto now = steady_clock::now();
162 
163  // We only need to set the start time once;
164  if(!animation_start_.has_value()) {
165  animation_start_ = now;
166  }
167 
168  animation_->get_drawing_canvas().set_variable("time", wfl::variant(duration_cast<milliseconds>(now - *animation_start_).count()));
169  animation_->set_is_dirty(true);
170 }
171 
173 {
174  /* If the worker thread is running, exit the application to prevent memory corruption.
175  * TODO: this is still not optimal. The main problem is that this code assumes that this
176  * happened because the window was closed, which is not necessarily the case (other
177  * possibilities might be a 'dialog doesn't fit on screen' exception caused by resizing
178  * the window).
179  */
180  if(worker_result_.valid()) {
181 #if defined(_LIBCPP_VERSION) || defined(__MINGW32__)
182  std::_Exit(0);
183 #else
184  std::quick_exit(0);
185 #endif
186  }
187 
188  singleton_ = nullptr;
189 }
190 
191 void loading_screen::display(std::function<void()> f)
192 {
193  if(singleton_ || CVideo::get_singleton().faked()) {
194  f();
195  } else {
196  loading_screen(f).show();
197  }
198 }
199 
200 } // namespace dialogs
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
static lg::log_domain log_loadscreen("loadscreen")
#define WRN_LS
void close()
Requests to close the window.
Definition: window.hpp:181
stage_map::const_iterator current_visible_stage_
std::atomic< loading_stage > current_stage_
void set_variable(const std::string &key, const wfl::variant &value)
Definition: canvas.hpp:167
This file contains the window object, this object is a top level container which has the event manage...
static void progress(loading_stage stage=loading_stage::none)
static CVideo & get_singleton()
Definition: video.hpp:48
void set_escape_disabled(const bool escape_disabled)
Disable the escape key.
Definition: window.hpp:298
window * get_window() const
Returns a pointer to the dialog&#39;s window.
#define ERR_LS
loading_stage
Loading screen stage IDs.
virtual void set_label(const t_string &label)
static const std::map< loading_stage, std::string > stage_names
This file contains the settings handling of the widget library.
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:465
Periodic redraw request.
Definition: handler.hpp:49
canvas & get_drawing_canvas()
Definition: drawing.hpp:56
std::future< void > worker_result_
virtual void process(events::pump_info &) override
Inherited from events::pump_monitor.
std::optional< decltype(std::chrono::steady_clock::now())> animation_start_
std::enable_if_t< is_general_event(E)> connect_signal(const signal_function &signal, const queue_position position=back_child)
Connect a signal for callback in set_event.
Definition: dispatcher.hpp:505
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
Contains the gui2 timer routines.
void draw_callback()
Callback to handle drawing the progress animation.
#define N_(String)
Definition: gettext.hpp:100
std::unique_ptr< cursor::setter > cursor_setter_
static loading_screen * singleton_
static void display(std::function< void()> f)
#define f
std::function< void()> load_func_
Standard logging facilities (interface).
#define e
loading_screen(std::function< void()> f)
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
void set_enter_disabled(const bool enter_disabled)
Disable the enter key.
Definition: window.hpp:285