The Battle for Wesnoth  1.19.6+dev
loading_screen.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2024
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Screen with logo and loading status info during program-startup.
18  */
19 
20 #define GETTEXT_DOMAIN "wesnoth-lib"
21 
23 
24 #include "cursor.hpp"
25 #include "draw_manager.hpp"
26 #include "gettext.hpp"
27 #include "gui/widgets/drawing.hpp"
28 #include "gui/widgets/label.hpp"
29 #include "gui/widgets/window.hpp"
30 #include "log.hpp"
31 #include "video.hpp"
32 
33 #include <chrono>
34 #include <functional>
35 #include <utility>
36 
37 static lg::log_domain log_loadscreen("loadscreen");
38 #define LOG_LS LOG_STREAM(info, log_loadscreen)
39 #define ERR_LS LOG_STREAM(err, log_loadscreen)
40 #define WRN_LS LOG_STREAM(warn, log_loadscreen)
41 
42 static lg::log_domain log_display("display");
43 #define DBG_DP LOG_STREAM(debug, log_display)
44 
45 static const std::map<loading_stage, std::string> stage_names {
46  { loading_stage::build_terrain, N_("Building terrain rules") },
47  { loading_stage::create_cache, N_("Reading files and creating cache") },
48  { loading_stage::init_display, N_("Initializing display") },
49  { loading_stage::init_fonts, N_("Reinitialize fonts for the current language") },
50  { loading_stage::init_teams, N_("Initializing teams") },
51  { loading_stage::init_theme, N_("Initializing display") },
52  { loading_stage::load_config, N_("Loading game configuration") },
53  { loading_stage::load_data, N_("Loading data files") },
54  { loading_stage::load_level, N_("Loading level") },
55  { loading_stage::init_lua, N_("Initializing scripting engine") },
56  { loading_stage::init_whiteboard, N_("Initializing planning mode") },
57  { loading_stage::load_unit_types, N_("Reading unit files") },
58  { loading_stage::load_units, N_("Loading units") },
59  { loading_stage::refresh_addons, N_("Searching for installed add-ons") },
60  { loading_stage::start_game, N_("Starting game") },
61  { loading_stage::verify_cache, N_("Verifying cache") },
62  { loading_stage::connect_to_server, N_("Connecting to server") },
63  { loading_stage::login_response, N_("Logging in") },
64  { loading_stage::waiting, N_("Waiting for server") },
65  { loading_stage::redirect, N_("Connecting to redirected server") },
66  { loading_stage::next_scenario, N_("Waiting for next scenario") },
67  { loading_stage::download_level_data, N_("Getting game data") },
68  { loading_stage::download_lobby_data, N_("Downloading lobby data") },
69 };
70 
71 namespace { std::chrono::steady_clock::time_point last_spin; }
72 
73 namespace gui2::dialogs
74 {
75 REGISTER_DIALOG(loading_screen)
76 
77 loading_screen* loading_screen::singleton_ = nullptr;
78 
79 loading_screen::loading_screen(std::function<void()> f)
80  : modal_dialog(window_id())
81  , load_funcs_{std::move(f)}
82  , worker_result_()
83  , cursor_setter_()
84  , progress_stage_label_(nullptr)
85  , animation_(nullptr)
86  , animation_start_()
87  , current_stage_(loading_stage::none)
88  , visible_stages_()
89  , current_visible_stage_()
90  , running_(false)
91 {
92  for(const auto& [stage, description] : stage_names) {
93  visible_stages_[stage] = t_string(description, "wesnoth-lib") + "...";
94  }
95 
97  singleton_ = this;
98 }
99 
101 {
102  set_enter_disabled(true);
103  set_escape_disabled(true);
104 
106 
107  progress_stage_label_ = find_widget<label>("status", false, true);
108  animation_ = find_widget<drawing>("animation", false, true);
109 }
110 
112 {
113  cursor_setter_.reset();
114 }
115 
117 {
118  if(singleton_ && stage != loading_stage::none) {
119  singleton_->current_stage_.store(stage, std::memory_order_release);
120  // Allow display to update, close events to be handled, etc.
122  }
123 }
124 
126 {
127  // If we're not showing a loading screen, do nothing.
128  if (!singleton_) {
129  return;
130  }
131 
132  // If we're not the main thread, do nothing.
133  if (!events::is_in_main_thread()) {
134  return;
135  }
136 
137  // Restrict actual update rate.
138  auto now = std::chrono::steady_clock::now();
139  auto elapsed = now - last_spin;
140  if (elapsed > draw_manager::get_frame_length()) {
141  last_spin = now;
143  }
144 }
145 
147 {
148  if (singleton_) {
150  }
151 }
152 
153 // This will be run inside the window::show() loop.
155 {
156  if (load_funcs_.empty()) {
157  return;
158  }
159 
160  // Do not automatically recurse.
161  if (running_) { return; }
162  running_ = true;
163 
164  // Run the loading function.
165  auto func = load_funcs_.back();
166  load_funcs_.pop_back();
167  LOG_LS << "Executing loading screen worker function.";
168  func();
169 
170  running_ = false;
171 
172  // If there's nothing more to do, close.
173  if (load_funcs_.empty()) {
174  queue_redraw();
175  window::close();
176  }
177 }
178 
180 {
182 
183  DBG_DP << "loading_screen::layout";
184 
185  loading_stage stage = current_stage_.load(std::memory_order_acquire);
186 
187  if(stage != loading_stage::none && (current_visible_stage_ == visible_stages_.end() || stage != current_visible_stage_->first)) {
188  auto iter = visible_stages_.find(stage);
189  if(iter == visible_stages_.end()) {
190  WRN_LS << "Stage missing description.";
191  return;
192  }
193 
194  current_visible_stage_ = iter;
195  progress_stage_label_->set_label(iter->second);
196  }
197 
198  using namespace std::chrono;
199  const auto now = steady_clock::now();
200 
201  // We only need to set the start time once;
202  if(!animation_start_.has_value()) {
203  animation_start_ = now;
204  }
205 
206  animation_->get_drawing_canvas().set_variable("time", wfl::variant(duration_cast<milliseconds>(now - *animation_start_).count()));
208 }
209 
211 {
212  LOG_LS << "Loading screen destroyed.";
213  singleton_ = nullptr;
214 }
215 
216 void loading_screen::display(const std::function<void()>& f)
217 {
218  if(singleton_ || video::headless()) {
219  LOG_LS << "Directly executing loading function.";
220  f();
221  } else {
222  LOG_LS << "Creating new loading screen.";
223  loading_screen(f).show();
224  }
225 }
226 
227 } // namespace dialogs
void set_variable(const std::string &key, wfl::variant &&value)
Definition: canvas.hpp:154
static void progress(loading_stage stage=loading_stage::none)
Report what is being loaded to the loading screen.
virtual void layout() override
Called by draw_manager to assign concrete layout.
std::atomic< loading_stage > current_stage_
static loading_screen * singleton_
static void raise()
Raise the loading screen to the top of the draw stack.
std::vector< std::function< void()> > load_funcs_
std::unique_ptr< cursor::setter > cursor_setter_
stage_map::const_iterator current_visible_stage_
virtual void process() override
Inherited from events::pump_monitor.
static void display(const std::function< void()> &f)
utils::optional< decltype(std::chrono::steady_clock::now())> animation_start_
loading_screen(std::function< void()> f)
static void spin()
Indicate to the player that loading is progressing.
virtual void post_show() override
Actions to be taken after the window has been shown.
virtual void pre_show() override
Actions to be taken before showing the window.
Abstract base class for all modal dialogs.
canvas & get_drawing_canvas()
Definition: drawing.hpp:34
virtual void set_label(const t_string &text)
void queue_redraw()
Indicates that this widget should be redrawn.
Definition: widget.cpp:464
void set_enter_disabled(const bool enter_disabled)
Disable the enter key.
Definition: window.hpp:323
void close()
Requests to close the window.
Definition: window.hpp:219
virtual void layout() override
Lays out the window.
Definition: window.cpp:838
void set_escape_disabled(const bool escape_disabled)
Disable the escape key.
Definition: window.hpp:336
#define N_(String)
Definition: gettext.hpp:101
This file contains the window object, this object is a top level container which has the event manage...
static lg::log_domain log_loadscreen("loadscreen")
#define WRN_LS
#define LOG_LS
static const std::map< loading_stage, std::string > stage_names
static lg::log_domain log_display("display")
#define DBG_DP
loading_stage
Loading screen stage IDs.
Standard logging facilities (interface).
@ WAIT
Definition: cursor.hpp:28
std::chrono::milliseconds get_frame_length()
Returns the length of one display frame, in milliseconds.
void raise_drawable(top_level_drawable *tld)
Raise a TLD to the top of the drawing stack.
bool is_in_main_thread()
Definition: events.cpp:474
void pump_and_draw()
pump() then immediately draw()
Definition: events.hpp:150
REGISTER_DIALOG(editor_edit_unit)
bool headless()
The game is running headless.
Definition: video.cpp:139
#define f