The Battle for Wesnoth  1.19.0-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"
28 #include "gui/widgets/drawing.hpp"
29 #include "gui/widgets/label.hpp"
30 #include "gui/widgets/window.hpp"
31 #include "log.hpp"
32 #include "video.hpp"
33 
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 { int last_spin_ = 0; }
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 {
103 
105 
106  progress_stage_label_ = find_widget<label>(&window, "status", false, true);
107  animation_ = find_widget<drawing>(&window, "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  int elapsed = SDL_GetTicks() - last_spin_;
138  if (elapsed > draw_manager::get_frame_length() || elapsed < 0) {
139  last_spin_ = SDL_GetTicks();
141  }
142 }
143 
145 {
146  if (singleton_) {
148  }
149 }
150 
151 // This will be run inside the window::show() loop.
153 {
154  if (load_funcs_.empty()) {
155  return;
156  }
157 
158  // Do not automatically recurse.
159  if (running_) { return; }
160  running_ = true;
161 
162  // Run the loading function.
163  auto func = load_funcs_.back();
164  load_funcs_.pop_back();
165  LOG_LS << "Executing loading screen worker function.";
166  func();
167 
168  running_ = false;
169 
170  // If there's nothing more to do, close.
171  if (load_funcs_.empty()) {
172  queue_redraw();
173  window::close();
174  }
175 }
176 
178 {
180 
181  DBG_DP << "loading_screen::layout";
182 
183  loading_stage stage = current_stage_.load(std::memory_order_acquire);
184 
185  if(stage != loading_stage::none && (current_visible_stage_ == visible_stages_.end() || stage != current_visible_stage_->first)) {
186  auto iter = visible_stages_.find(stage);
187  if(iter == visible_stages_.end()) {
188  WRN_LS << "Stage missing description.";
189  return;
190  }
191 
192  current_visible_stage_ = iter;
193  progress_stage_label_->set_label(iter->second);
194  }
195 
196  using namespace std::chrono;
197  const auto now = steady_clock::now();
198 
199  // We only need to set the start time once;
200  if(!animation_start_.has_value()) {
201  animation_start_ = now;
202  }
203 
204  animation_->get_drawing_canvas().set_variable("time", wfl::variant(duration_cast<milliseconds>(now - *animation_start_).count()));
206 }
207 
209 {
210  LOG_LS << "Loading screen destroyed.";
211  singleton_ = nullptr;
212 }
213 
214 void loading_screen::display(std::function<void()> f)
215 {
216  if(singleton_ || video::headless()) {
217  LOG_LS << "Directly executing loading function.";
218  f();
219  } else {
220  LOG_LS << "Creating new loading screen.";
221  loading_screen(f).show();
222  }
223 }
224 
225 } // namespace dialogs
void set_variable(const std::string &key, wfl::variant &&value)
Definition: canvas.hpp:153
static void progress(loading_stage stage=loading_stage::none)
Report what is being loaded to the loading screen.
std::optional< decltype(std::chrono::steady_clock::now())> animation_start_
virtual void layout() override
Called by draw_manager to assign concrete layout.
std::atomic< loading_stage > current_stage_
static loading_screen * singleton_
virtual void process(events::pump_info &) override
Inherited from events::pump_monitor.
static void raise()
Raise the loading screen to the top of the draw stack.
static void display(std::function< void()> f)
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
std::vector< std::function< void()> > load_funcs_
std::unique_ptr< cursor::setter > cursor_setter_
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
stage_map::const_iterator current_visible_stage_
loading_screen(std::function< void()> f)
static void spin()
Indicate to the player that loading is progressing.
Abstract base class for all modal dialogs.
canvas & get_drawing_canvas()
Definition: drawing.hpp:53
virtual void set_label(const t_string &text)
void queue_redraw()
Indicates that this widget should be redrawn.
Definition: widget.cpp:455
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
void set_enter_disabled(const bool enter_disabled)
Disable the enter key.
Definition: window.hpp:327
void close()
Requests to close the window.
Definition: window.hpp:223
virtual void layout() override
Lays out the window.
Definition: window.cpp:850
void set_escape_disabled(const bool escape_disabled)
Disable the escape key.
Definition: window.hpp:340
#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
int 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:473
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:141
#define f