The Battle for Wesnoth  1.19.5+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 
36 static lg::log_domain log_loadscreen("loadscreen");
37 #define LOG_LS LOG_STREAM(info, log_loadscreen)
38 #define ERR_LS LOG_STREAM(err, log_loadscreen)
39 #define WRN_LS LOG_STREAM(warn, log_loadscreen)
40 
41 static lg::log_domain log_display("display");
42 #define DBG_DP LOG_STREAM(debug, log_display)
43 
44 static const std::map<loading_stage, std::string> stage_names {
45  { loading_stage::build_terrain, N_("Building terrain rules") },
46  { loading_stage::create_cache, N_("Reading files and creating cache") },
47  { loading_stage::init_display, N_("Initializing display") },
48  { loading_stage::init_fonts, N_("Reinitialize fonts for the current language") },
49  { loading_stage::init_teams, N_("Initializing teams") },
50  { loading_stage::init_theme, N_("Initializing display") },
51  { loading_stage::load_config, N_("Loading game configuration") },
52  { loading_stage::load_data, N_("Loading data files") },
53  { loading_stage::load_level, N_("Loading level") },
54  { loading_stage::init_lua, N_("Initializing scripting engine") },
55  { loading_stage::init_whiteboard, N_("Initializing planning mode") },
56  { loading_stage::load_unit_types, N_("Reading unit files") },
57  { loading_stage::load_units, N_("Loading units") },
58  { loading_stage::refresh_addons, N_("Searching for installed add-ons") },
59  { loading_stage::start_game, N_("Starting game") },
60  { loading_stage::verify_cache, N_("Verifying cache") },
61  { loading_stage::connect_to_server, N_("Connecting to server") },
62  { loading_stage::login_response, N_("Logging in") },
63  { loading_stage::waiting, N_("Waiting for server") },
64  { loading_stage::redirect, N_("Connecting to redirected server") },
65  { loading_stage::next_scenario, N_("Waiting for next scenario") },
66  { loading_stage::download_level_data, N_("Getting game data") },
67  { loading_stage::download_lobby_data, N_("Downloading lobby data") },
68 };
69 
70 namespace { std::chrono::steady_clock::time_point last_spin; }
71 
72 namespace gui2::dialogs
73 {
74 REGISTER_DIALOG(loading_screen)
75 
76 loading_screen* loading_screen::singleton_ = nullptr;
77 
78 loading_screen::loading_screen(std::function<void()> f)
79  : modal_dialog(window_id())
80  , load_funcs_{f}
81  , worker_result_()
82  , cursor_setter_()
83  , progress_stage_label_(nullptr)
84  , animation_(nullptr)
85  , animation_start_()
86  , current_stage_(loading_stage::none)
87  , visible_stages_()
88  , current_visible_stage_()
89  , running_(false)
90 {
91  for(const auto& [stage, description] : stage_names) {
92  visible_stages_[stage] = t_string(description, "wesnoth-lib") + "...";
93  }
94 
96  singleton_ = this;
97 }
98 
100 {
101  set_enter_disabled(true);
102  set_escape_disabled(true);
103 
105 
106  progress_stage_label_ = find_widget<label>("status", false, true);
107  animation_ = find_widget<drawing>("animation", false, true);
108 }
109 
111 {
112  cursor_setter_.reset();
113 }
114 
116 {
117  if(singleton_ && stage != loading_stage::none) {
118  singleton_->current_stage_.store(stage, std::memory_order_release);
119  // Allow display to update, close events to be handled, etc.
121  }
122 }
123 
125 {
126  // If we're not showing a loading screen, do nothing.
127  if (!singleton_) {
128  return;
129  }
130 
131  // If we're not the main thread, do nothing.
132  if (!events::is_in_main_thread()) {
133  return;
134  }
135 
136  // Restrict actual update rate.
137  auto now = std::chrono::steady_clock::now();
138  auto elapsed = now - last_spin;
139  if (elapsed > draw_manager::get_frame_length()) {
140  last_spin = now;
142  }
143 }
144 
146 {
147  if (singleton_) {
149  }
150 }
151 
152 // This will be run inside the window::show() loop.
154 {
155  if (load_funcs_.empty()) {
156  return;
157  }
158 
159  // Do not automatically recurse.
160  if (running_) { return; }
161  running_ = true;
162 
163  // Run the loading function.
164  auto func = load_funcs_.back();
165  load_funcs_.pop_back();
166  LOG_LS << "Executing loading screen worker function.";
167  func();
168 
169  running_ = false;
170 
171  // If there's nothing more to do, close.
172  if (load_funcs_.empty()) {
173  queue_redraw();
174  window::close();
175  }
176 }
177 
179 {
181 
182  DBG_DP << "loading_screen::layout";
183 
184  loading_stage stage = current_stage_.load(std::memory_order_acquire);
185 
186  if(stage != loading_stage::none && (current_visible_stage_ == visible_stages_.end() || stage != current_visible_stage_->first)) {
187  auto iter = visible_stages_.find(stage);
188  if(iter == visible_stages_.end()) {
189  WRN_LS << "Stage missing description.";
190  return;
191  }
192 
193  current_visible_stage_ = iter;
194  progress_stage_label_->set_label(iter->second);
195  }
196 
197  using namespace std::chrono;
198  const auto now = steady_clock::now();
199 
200  // We only need to set the start time once;
201  if(!animation_start_.has_value()) {
202  animation_start_ = now;
203  }
204 
205  animation_->get_drawing_canvas().set_variable("time", wfl::variant(duration_cast<milliseconds>(now - *animation_start_).count()));
207 }
208 
210 {
211  LOG_LS << "Loading screen destroyed.";
212  singleton_ = nullptr;
213 }
214 
215 void loading_screen::display(std::function<void()> f)
216 {
217  if(singleton_ || video::headless()) {
218  LOG_LS << "Directly executing loading function.";
219  f();
220  } else {
221  LOG_LS << "Creating new loading screen.";
222  loading_screen(f).show();
223  }
224 }
225 
226 } // 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.
static void display(std::function< void()> f)
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.
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