The Battle for Wesnoth  1.15.0-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/label.hpp"
28 #include "gui/widgets/window.hpp"
29 #include "log.hpp"
30 #include "preferences/general.hpp"
31 #include "utils/functional.hpp"
32 #include "video.hpp"
33 
34 #include <chrono>
35 #include <cstdlib>
36 #include <limits>
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
69 {
70 namespace dialogs
71 {
72 REGISTER_DIALOG(loading_screen)
73 
74 loading_screen* loading_screen::current_load = nullptr;
75 
76 loading_screen::loading_screen(std::function<void()> f)
77  : animation_counter_(0)
78  , next_animation_time_(std::numeric_limits<uint32_t>::max())
79  , load_func_(f)
80  , worker_result_()
81  , cursor_setter_()
82  , progress_stage_label_(nullptr)
83  , animation_label_(nullptr)
84  , current_stage_(loading_stage::none)
85  , visible_stages_()
86  , animation_stages_()
87  , current_visible_stage_()
88 {
89  for(const auto& pair : stage_names) {
90  visible_stages_[pair.first] = t_string(pair.second, "wesnoth-lib") + "...";
91  }
92 
93  animation_stages_.reserve(20);
94 
95  for(int i = 0; i != 20; ++i) {
96  std::string s(20, ' ');
97  s[i] = '.';
98  animation_stages_.push_back(std::move(s));
99  }
100 
102  current_load = this;
103 }
104 
106 {
107  window.set_enter_disabled(true);
108  window.set_escape_disabled(true);
109 
111 
112  if(load_func_) {
113  // Run the load function in its own thread.
114  try {
115  worker_result_ = std::async(std::launch::async, [this]() {
116  try {
117  load_func_();
118  } catch(...) {
119  // TODO: guard this with a mutex.
120  exception_ = std::current_exception();
121  }
122  });
123  } catch(const std::system_error& e) {
124  ERR_LS << "Failed to create worker thread: " << e.what() << "\n";
125  throw;
126  }
127  }
128 
129  progress_stage_label_ = find_widget<label>(&window, "status", false, true);
130  animation_label_ = find_widget<label>(&window, "test_animation", false, true);
131 
132  // Add a draw callback to handle the animation, et al.
133  connect_signal_on_draw(window, std::bind(&loading_screen::draw_callback, this));
134 
136 }
137 
139 {
140  cursor_setter_.reset();
141 }
142 
144 {
145  if(current_load && stage != loading_stage::none) {
146  current_load->current_stage_.store(stage, std::memory_order_release);
147  }
148 }
149 
151 {
152  if(!load_func_ || loading_complete()) {
153  if(exception_) {
154  std::rethrow_exception(exception_);
155  }
156 
157  get_window()->close();
158  }
159 }
160 
162 {
163  loading_stage stage = current_stage_.load(std::memory_order_acquire);
164 
165  if(stage != loading_stage::none && (current_visible_stage_ == visible_stages_.end() || stage != current_visible_stage_->first)) {
166  auto iter = visible_stages_.find(stage);
167  if(iter == visible_stages_.end()) {
168  WRN_LS << "Stage missing description." << std::endl;
169  return;
170  }
171 
172  current_visible_stage_ = iter;
173  progress_stage_label_->set_label(iter->second);
174  }
175 
176  //if(SDL_GetTicks() < next_animation_time_) {
177  // return;
178  //}
179 
181  if(animation_counter_ % 2 == 0) {
183  }
184 
185  //set_next_animation_time();
186 }
187 
189 {
190  /* If the worker thread is running, exit the application to prevent memory corruption.
191  * TODO: this is still not optimal. The main problem is that this code assumes that this
192  * happened because the window was closed, which is not necessarily the case (other
193  * possibilities might be a 'dialog doesn't fit on screen' exception caused by resizing
194  * the window).
195  *
196  * Another approach might be to add exit points (boost::this_thread::interruption_point())
197  * to the worker functions (filesystem.cpp, config parsing code, etc.) and then use that
198  * to end the thread faster.
199  */
200  if(!loading_complete()) {
201 #if defined(_LIBCPP_VERSION) || defined(__MINGW32__)
202  std::_Exit(0);
203 #else
204  std::quick_exit(0);
205 #endif
206  }
207 
208  current_load = nullptr;
209 }
210 
211 void loading_screen::display(std::function<void()> f)
212 {
213  const bool use_loadingscreen_animation = !preferences::disable_loadingscreen_animation();
214 
215  if(current_load || CVideo::get_singleton().faked()) {
216  f();
217  } else if(use_loadingscreen_animation) {
218  loading_screen(f).show();
219  } else {
220  loading_screen(std::function<void()>()).show();
221  f();
222  }
223 }
224 
226 {
227  using namespace std::chrono_literals;
228  return worker_result_.wait_for(0ms) == std::future_status::ready;
229 }
230 
232 {
233  next_animation_time_ = SDL_GetTicks() + 100;
234 }
235 
236 } // namespace dialogs
237 } // namespace gui2
virtual void pre_show(window &window) override
Inherited from modal_dialog.
static lg::log_domain log_loadscreen("loadscreen")
#define WRN_LS
void close()
Requests to close the window.
Definition: window.hpp:164
stage_map::const_iterator current_visible_stage_
std::atomic< loading_stage > current_stage_
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:281
STL namespace.
window * get_window() const
Returns a pointer to the dialog&#39;s window.
#define ERR_LS
-file util.hpp
Generic file dialog.
Definition: field-fwd.hpp:22
loading_stage
Loading screen stage IDs.
virtual void set_label(const t_string &label)
std::vector< t_string > animation_stages_
static const std::map< loading_stage, std::string > stage_names
std::future< void > worker_result_
bool loading_complete() const
Checks whether the worker thread has returned and loading is complete.
Various uncategorised dialogs.
virtual void process(events::pump_info &) override
Inherited from events::pump_monitor.
std::size_t i
Definition: function.cpp:933
static map_location::DIRECTION s
virtual void post_show(window &window) override
Inherited from modal_dialog.
Contains the gui2 timer routines.
void draw_callback()
Callback to handle drawing the progress animation.
#define N_(String)
Definition: gettext.hpp:97
std::unique_ptr< cursor::setter > cursor_setter_
bool disable_loadingscreen_animation()
Definition: general.cpp:921
static void display(std::function< void()> f)
static loading_screen * current_load
#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:62
void connect_signal_on_draw(dispatcher &dispatcher, const signal_function &signal)
Connects a signal handler for a callback when the widget is drawn.
Definition: dispatcher.cpp:183
void set_enter_disabled(const bool enter_disabled)
Disable the enter key.
Definition: window.hpp:268