The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
loadscreen.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 by the Battle for Wesnoth Project http://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 
21 #include "cursor.hpp"
23 #include "gui/widgets/window.hpp"
24 #include "gui/widgets/settings.hpp"
25 #include "gui/core/timer.hpp"
27 
28 #include "video.hpp"
29 #include "cursor.hpp"
30 #include "gettext.hpp"
31 #include "log.hpp"
32 #include "preferences.hpp"
33 #include "utils/functional.hpp"
34 #include <boost/thread.hpp>
35 
36 static lg::log_domain log_loadscreen("loadscreen");
37 #define ERR_LS LOG_STREAM(err, log_loadscreen)
38 #define WRN_LS LOG_STREAM(warn, log_loadscreen)
39 #define LOG_LS LOG_STREAM(info, log_loadscreen)
40 #define DBG_LS LOG_STREAM(debug, log_loadscreen)
41 
42 static const std::map<std::string, std::string> stages =
43 {
44  { "build terrain", N_("Building terrain rules") },
45  { "create cache", N_("Reading files and creating cache") },
46  { "init display", N_("Initializing display") },
47  { "init fonts", N_("Reinitialize fonts for the current language") },
48  { "init teams", N_("Initializing teams") },
49  { "init theme", N_("Initializing display") },
50  { "load config", N_("Loading game configuration") },
51  { "load data", N_("Loading data files") },
52  { "load level", N_("Loading level") },
53  { "init lua", N_("Initializing scripting engine") },
54  { "init whiteboard", N_("Initializing planning mode") },
55  { "load unit types", N_("Reading unit files") },
56  { "load units", N_("Loading units") },
57  { "refresh addons", N_("Searching for installed add-ons") },
58  { "start game", N_("Starting game") },
59  { "verify cache", N_("Verifying cache") },
60 };
61 
62 namespace gui2
63 {
64 
65 REGISTER_DIALOG(loadscreen)
66 
67 tloadscreen::tloadscreen(std::function<void()> f)
68  : window_(nullptr)
69  , timer_id_(0)
70  , animation_counter_(0)
71  , work_(f)
72  , worker_()
73  , cursor_setter_()
74  , progress_stage_label_(nullptr)
75  , animation_label_(nullptr)
76  , current_stage_(nullptr)
77  , visible_stages_()
78  , animation_stages_()
79  , current_visible_stage_()
80 {
81  for (const auto& pair : stages) {
82  visible_stages_[pair.first] = t_string(pair.second, "wesnoth-lib") + "...";
83  }
84  for (int i = 0; i != 20; ++i) {
85  std::string s(20, ' ');
86  s[i] = '.';
87  animation_stages_.push_back(s);
88  }
89  current_visible_stage_ = visible_stages_.end();
90  current_load = this;
91 }
92 
94 {
95  if(window_) {
96  window_->undraw();
97  delete window_;
98  window_ = nullptr;
99  }
100 }
101 
103 {
104  return build(video, window_id());
105 }
106 
108 {
109  if (work_) {
110  worker_.reset(new boost::thread(work_));
111  }
112  timer_id_ = add_timer(100, std::bind(&tloadscreen::timer_callback, this, std::ref(window)), true);
114  progress_stage_label_ = &find_widget<tlabel>(&window, "status", false);
115  animation_label_ = &find_widget<tlabel>(&window, "test_animation", false);
116 
117  window.set_enter_disabled(true);
118  window.set_escape_disabled(true);
119 }
120 
122 {
123  worker_.reset();
125  cursor_setter_.reset();
126 }
127 
128 void tloadscreen::progress(const char* stage)
129 {
130  if(!current_load) {
131  return;
132  }
133  if(stage) {
135 #if defined(_MSC_VER) && _MSC_VER < 1900
136  = stage;
137 #else
138  .store(stage, std::memory_order_release);
139 #endif
140  }
141 }
142 
144 
146 {
147  if (!work_ || !worker_ || worker_->timed_join(boost::posix_time::milliseconds(0))) {
148  window.close();
149  }
150  if (!work_) {
151  return;
152  }
153  const char* stage = current_stage_
154 #if defined(_MSC_VER) && _MSC_VER < 1900
155  ;
156 #else
157  .load(std::memory_order_acquire);
158 #endif
159  if (stage && (current_visible_stage_ == visible_stages_.end() || stage != current_visible_stage_->first))
160  {
161  auto iter = visible_stages_.find(stage);
162  if(iter == visible_stages_.end()) {
163  WRN_LS << "Stage ID '" << stage << "' missing description." << std::endl;
164  return;
165  }
166  current_visible_stage_ = iter;
167  progress_stage_label_->set_label(iter->second);
168  }
170  if (animation_counter_ % 2 == 0) {
172  }
173 }
174 
176 {
177  close();
178  current_load = nullptr;
179 }
180 
181 void tloadscreen::display(CVideo& video, std::function<void()> f)
182 {
183  const bool use_loadingscreen_animation = !preferences::disable_loadingscreen_animation();
184  if (current_load || video.faked()) {
185  f();
186  }
187  else if(use_loadingscreen_animation) {
188  tloadscreen(f).show(video);
189  }
190  else {
191  tloadscreen(std::function<void()>()).show(video);
192  f();
193  }
194 }
195 
196 } // namespace gui2
tloadscreen(std::function< void()> f)
Definition: loadscreen.cpp:67
twindow * build_window(CVideo &video) const
Definition: loadscreen.cpp:102
void pre_show(twindow &window)
Inherited from tdialog.
Definition: loadscreen.cpp:107
twindow * build(CVideo &video, const twindow_builder::tresolution *definition)
Builds a window.
tlabel * animation_label_
Definition: loadscreen.hpp:81
Definition: video.hpp:58
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG(label_settings)
virtual void set_label(const t_string &label)
Definition: control.cpp:330
bool remove_timer(const size_t id)
Removes a timer.
Definition: timer.cpp:144
STL namespace.
void undraw()
Undraws the window.
Definition: window.cpp:917
std::map< std::string, t_string > visible_stages_
Definition: loadscreen.hpp:90
-file util.hpp
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
tlabel * progress_stage_label_
Definition: loadscreen.hpp:80
This file contains the settings handling of the widget library.
void timer_callback(twindow &window)
Definition: loadscreen.cpp:145
twindow * window_
Definition: loadscreen.hpp:61
static tloadscreen * current_load
Definition: loadscreen.hpp:82
boost::scoped_ptr< cursor::setter > cursor_setter_
Definition: loadscreen.hpp:66
void close()
Requests to close the window.
Definition: window.hpp:235
static void display(CVideo &video, std::function< void()> f)
Definition: loadscreen.cpp:181
static void progress(const char *stage_name=nullptr)
Definition: loadscreen.cpp:128
std::atomic< const char * > current_stage_
Definition: loadscreen.hpp:88
size_t i
Definition: function.cpp:1057
Contains the gui2 timer routines.
std::map< std::string, t_string >::const_iterator current_visible_stage_
Definition: loadscreen.hpp:92
#define N_(String)
Definition: gettext.hpp:90
void post_show(twindow &window)
Inherited from tdialog.
Definition: loadscreen.cpp:121
GLbitfield stages
Definition: glew.h:4366
bool disable_loadingscreen_animation()
bool faked() const
Definition: video.hpp:166
GLenum GLint ref
Definition: glew.h:1813
Standard logging facilities (interface).
boost::scoped_ptr< boost::thread > worker_
Definition: loadscreen.hpp:65
GLdouble s
Definition: glew.h:1358
virtual const std::string & window_id() const
The id of the window to build.
size_t add_timer(const Uint32 interval, const std::function< void(size_t id)> &callback, const bool repeat)
Adds a new timer.
Definition: timer.cpp:112
std::function< void()> work_
Definition: loadscreen.hpp:64
void close()
Hides the window.
Definition: loadscreen.cpp:93
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::vector< t_string > animation_stages_
Definition: loadscreen.hpp:91
#define WRN_LS
Definition: loadscreen.cpp:38
GLclampf f
Definition: glew.h:3024
static lg::log_domain log_loadscreen("loadscreen")