xoreos  0.0.5
loadprogress.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cassert>
26 
27 #include "src/common/util.h"
28 
29 #include "src/graphics/graphics.h"
30 #include "src/graphics/font.h"
31 
34 
35 #include "src/events/events.h"
36 
38 
39 namespace Engines {
40 
41 LoadProgress::LoadProgress(size_t steps) : _steps(steps), _currentStep(0),
42  _currentAmount(0.0f), _startTime(0) {
43 
44  assert(_steps >= 2);
45 
46  _stepAmount = 1.0f / (_steps - 1);
47 
49 
53 
54  float left = -(WindowMan.getWindowWidth() / 2.0f);
55  float width = WindowMan.getWindowWidth();
56  float height = font.getFont().getHeight();
57 
59  _barUpper.reset (new Graphics::Aurora::Text(font, width, height, barUpperStr, 1.0f, 1.0f, 1.0f, 1.0f, Graphics::Aurora::kHAlignCenter, Graphics::Aurora::kVAlignBottom));
60  _barLower.reset (new Graphics::Aurora::Text(font, width, height, barLowerStr, 1.0f, 1.0f, 1.0f, 1.0f, Graphics::Aurora::kHAlignCenter, Graphics::Aurora::kVAlignTop));
61  _progressbar.reset(new Graphics::Aurora::Text(font, width, height, barStr, 1.0f, 1.0f, 1.0f, 1.0f, Graphics::Aurora::kHAlignCenter, Graphics::Aurora::kVAlignMiddle));
62  _percent.reset (new Graphics::Aurora::Text(font, width, height, "", 1.0f, 1.0f, 1.0f, 1.0f, Graphics::Aurora::kHAlignCenter, Graphics::Aurora::kVAlignTop));
63 
64  _description->setPosition(left, height);
65  _percent ->setPosition(left, -height);
66 
67  _barUpper ->setPosition(left, 0.0f);
68  _barLower ->setPosition(left, 0.0f);
69  _progressbar->setPosition(left, 0.0f);
70 }
71 
73 }
74 
75 void LoadProgress::step(const Common::UString &description) {
76  const uint32 timeNow = EventMan.getTimestamp();
77 
78  if (_currentStep == 0)
79  _startTime = timeNow;
80 
81  // The first step is the 0% mark, so don't add to the amount yet
82  if (_currentStep > 0)
84 
85  // Take the next step and make sure we get nice, round 100% at the end
86  if (++_currentStep > (_steps - 1)) {
87  _currentStep = _steps - 1;
88  _currentAmount = 1.0f;
89  }
90 
91  const int percentage = (int) (_currentAmount * 100.0f);
92  const uint32 timeElapsed = timeNow - _startTime;
93 
94  const Common::UString timeStr = Common::UString::format("(%.2fs)", timeElapsed / 1000.0);
95 
96  // Update the text
97  {
98  // Create string representing the percentage of done-ness and progress bar
99  const Common::UString percentStr = Common::UString::format("%d%%", percentage);
101 
102  GfxMan.lockFrame();
103 
104  _description->setText(description + " " + timeStr);
105  _percent->setText(percentStr);
106  _progressbar->setText(barStr);
107 
108  _description->show();
109  _barUpper ->show();
110  _barLower ->show();
111  _progressbar->show();
112  _percent ->show();
113 
114  GfxMan.unlockFrame();
115  }
116 
117  // And also print the status
118  status("[%3d%%] %s %s", percentage, description.c_str(), timeStr.c_str());
119 }
120 
121 Common::UString LoadProgress::createProgressbar(size_t length, double filled) {
122  const size_t amount = length * filled;
123 
124  Common::UString str;
125 
126  // RIGHT ONE EIGHTH BLOCK
127  str += (uint32) 0x2595;
128 
129  str += Common::UString((uint32) 0x2588, amount); // FULL BLOCK
130  str += Common::UString((uint32) 0x0020, length - amount); // Space
131 
132  // LEFT ONE EIGHTH BLOCK
133  str += (uint32) 0x258F;
134 
135  return str;
136 }
137 
139  // UPPER ONE EIGHTH BLOCK
140  return Common::UString((uint32) 0x2594, length);
141 }
142 
144  // LOWER ONE EIGHTH BLOCK
145  return Common::UString((uint32) 0x2581, length);
146 }
147 
148 } // End of namespace Engines
The global graphics manager.
A class holding an UTF-8 string.
Definition: ustring.h:48
LoadProgress(size_t steps)
Create a load progress display.
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
const float kVAlignTop
Definition: types.h:46
A handle to a font.
Definition: fonthandle.h:52
double _currentAmount
The accumulated amount.
Definition: loadprogress.h:71
A text object.
static Common::UString createProgressbarUpper(size_t length)
size_t _steps
The number of total steps.
Definition: loadprogress.h:67
size_t _currentStep
The current step we&#39;re on.
Definition: loadprogress.h:68
The Aurora font manager.
static Common::UString createProgressbarLower(size_t length)
Common::ScopedPtr< Graphics::Aurora::Text > _progressbar
The actual progress bar.
Definition: loadprogress.h:81
A text object.
Definition: text.h:42
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
Definition: ustring.cpp:718
Utility templates and functions.
uint32 _startTime
The timestamp the first step happened.
Definition: loadprogress.h:73
The global events manager.
const char * kSystemFontMono
Identifier used for the monospaced system font.
Definition: fontman.cpp:41
Common::ScopedPtr< Graphics::Aurora::Text > _barUpper
The upper border of the progress bar.
Definition: loadprogress.h:79
const float kVAlignBottom
Definition: types.h:48
const float kHAlignCenter
Definition: types.h:43
#define EventMan
Shortcut for accessing the events manager.
Definition: events.h:210
Displaying the progress in loading a game.
const float kVAlignMiddle
Definition: types.h:47
#define WindowMan
Shortcut for accessing the window manager.
Definition: windowman.h:137
static Common::UString createProgressbar(size_t length, double filled)
Common::ScopedPtr< Graphics::Aurora::Text > _percent
The text containing the current percentage of done-ness.
Definition: loadprogress.h:84
uint32_t uint32
Definition: types.h:204
void step(const Common::UString &description)
Take a step in advancing the progress.
Common::ScopedPtr< Graphics::Aurora::Text > _barLower
The lower border of the progress bar.
Definition: loadprogress.h:80
void status(const char *s,...)
Definition: util.cpp:52
Common::ScopedPtr< Graphics::Aurora::Text > _description
The text containing the description of the current step.
Definition: loadprogress.h:76
static const int kBarLength
The length of the progress bar in characters.
Definition: loadprogress.h:65
double _stepAmount
The amount to step each time.
Definition: loadprogress.h:70
virtual float getHeight() const =0
Return the height of a character.
A font.
#define GfxMan
Shortcut for accessing the graphics manager.
Definition: graphics.h:299
#define FontMan
Shortcut for accessing the font manager.
Definition: fontman.h:105