Skip to content

Commit 3a772e7

Browse files
committedJan 5, 2018
GameUI refactor (part 2/X): Move Game::guitext to GameUI + enhancements on StaticText
Other enhancements: * C++ friendlyness for addStaticText() -> move to static StaticText::add()
1 parent 0ebaed4 commit 3a772e7

File tree

8 files changed

+159
-134
lines changed

8 files changed

+159
-134
lines changed
 

Diff for: ‎src/client/gameui.cpp

+50-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,55 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1919
*/
2020

2121
#include "gameui.h"
22-
#include "settings.h"
22+
#include <irrlicht_changes/static_text.h>
23+
#include "gui/mainmenumanager.h"
24+
#include "client.h"
25+
#include "fontengine.h"
26+
#include "clientmap.h"
27+
#include "version.h"
28+
#include "renderingengine.h"
29+
30+
void GameUI::init()
31+
{
32+
// First line of debug text
33+
m_guitext = gui::StaticText::add(guienv, utf8_to_wide(PROJECT_NAME_C).c_str(),
34+
core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
35+
}
36+
37+
void GameUI::update(const RunStats &stats, Client *client,
38+
const MapDrawControl *draw_control)
39+
{
40+
if (m_flags.show_debug) {
41+
static float drawtime_avg = 0;
42+
drawtime_avg = drawtime_avg * 0.95 + stats.drawtime * 0.05;
43+
u16 fps = 1.0 / stats.dtime_jitter.avg;
44+
45+
std::ostringstream os(std::ios_base::binary);
46+
os << std::fixed
47+
<< PROJECT_NAME_C " " << g_version_hash
48+
<< ", FPS: " << fps
49+
<< std::setprecision(0)
50+
<< ", drawtime: " << drawtime_avg << "ms"
51+
<< std::setprecision(1)
52+
<< ", dtime jitter: "
53+
<< (stats.dtime_jitter.max_fraction * 100.0) << "%"
54+
<< std::setprecision(1)
55+
<< ", view range: "
56+
<< (draw_control->range_all ? "All" : itos(draw_control->wanted_range))
57+
<< std::setprecision(3)
58+
<< ", RTT: " << client->getRTT() << "s";
59+
setStaticText(m_guitext, utf8_to_wide(os.str()).c_str());
60+
m_guitext->setVisible(true);
61+
} else {
62+
m_guitext->setVisible(false);
63+
}
64+
65+
if (m_guitext->isVisible()) {
66+
v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
67+
m_guitext->setRelativePosition(core::rect<s32>(5, 5, screensize.X,
68+
5 + g_fontengine->getTextHeight()));
69+
}
70+
}
2371

2472
void GameUI::initFlags()
2573
{
@@ -29,7 +77,7 @@ void GameUI::initFlags()
2977
m_flags.show_debug = g_settings->getBool("show_debug");
3078
}
3179

32-
void GameUI::showMinimap(const bool show)
80+
void GameUI::showMinimap(bool show)
3381
{
3482
m_flags.show_minimap = show;
3583
}

Diff for: ‎src/client/gameui.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,22 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020

2121
#pragma once
2222

23-
#include "IGUIEnvironment.h"
23+
#include <IGUIEnvironment.h>
24+
#include "game.h"
2425

2526
using namespace irr;
27+
class Client;
28+
struct MapDrawControl;
2629

2730
class GameUI
2831
{
2932
// Temporary between coding time to move things here
3033
friend class Game;
3134

3235
public:
36+
GameUI() = default;
37+
~GameUI() = default;
38+
3339
// Flags that can, or may, change during main game loop
3440
struct Flags
3541
{
@@ -42,16 +48,19 @@ class GameUI
4248
bool disable_camera_update;
4349
};
4450

51+
void init();
52+
void update(const RunStats &stats, Client *client, const MapDrawControl *draw_control);
53+
4554
void initFlags();
4655
const Flags &getFlags() const { return m_flags; }
4756

48-
void showMinimap(const bool show);
57+
void showMinimap(bool show);
4958

5059
private:
5160
Flags m_flags;
5261

62+
gui::IGUIStaticText *m_guitext; // First line of debug text
5363
// @TODO future move
54-
// gui::IGUIStaticText *m_guitext; // First line of debug text
5564
// gui::IGUIStaticText *m_guitext2; // Second line of debug text
5665
// gui::IGUIStaticText *m_guitext_info; // At the middle of the screen
5766
// gui::IGUIStaticText *m_guitext_status;

Diff for: ‎src/game.cpp

+7-53
Original file line numberDiff line numberDiff line change
@@ -1174,16 +1174,6 @@ struct GameRunData {
11741174
float time_of_day_smooth;
11751175
};
11761176

1177-
struct Jitter {
1178-
f32 max, min, avg, counter, max_sample, min_sample, max_fraction;
1179-
};
1180-
1181-
struct RunStats {
1182-
u32 drawtime;
1183-
1184-
Jitter dtime_jitter, busy_time_jitter;
1185-
};
1186-
11871177
class Game;
11881178

11891179
struct ClientEventHandler
@@ -1452,7 +1442,6 @@ class Game {
14521442

14531443
/* GUI stuff
14541444
*/
1455-
gui::IGUIStaticText *guitext; // First line of debug text
14561445
gui::IGUIStaticText *guitext2; // Second line of debug text
14571446
gui::IGUIStaticText *guitext_info; // At the middle of the screen
14581447
gui::IGUIStaticText *guitext_status;
@@ -1998,34 +1987,30 @@ bool Game::createClient(const std::string &playername,
19981987

19991988
bool Game::initGui()
20001989
{
2001-
// First line of debug text
2002-
guitext = addStaticText(guienv,
2003-
utf8_to_wide(PROJECT_NAME_C).c_str(),
2004-
core::rect<s32>(0, 0, 0, 0),
2005-
false, false, guiroot);
1990+
m_game_ui->init();
20061991

20071992
// Second line of debug text
2008-
guitext2 = addStaticText(guienv,
1993+
guitext2 = gui::StaticText::add(guienv,
20091994
L"",
20101995
core::rect<s32>(0, 0, 0, 0),
20111996
false, false, guiroot);
20121997

20131998
// At the middle of the screen
20141999
// Object infos are shown in this
2015-
guitext_info = addStaticText(guienv,
2000+
guitext_info = gui::StaticText::add(guienv,
20162001
L"",
20172002
core::rect<s32>(0, 0, 400, g_fontengine->getTextHeight() * 5 + 5) + v2s32(100, 200),
20182003
false, true, guiroot);
20192004

20202005
// Status text (displays info when showing and hiding GUI stuff, etc.)
2021-
guitext_status = addStaticText(guienv,
2006+
guitext_status = gui::StaticText::add(guienv,
20222007
L"<Status>",
20232008
core::rect<s32>(0, 0, 0, 0),
20242009
false, false, guiroot);
20252010
guitext_status->setVisible(false);
20262011

20272012
// Chat text
2028-
guitext_chat = addStaticText(
2013+
guitext_chat = gui::StaticText::add(
20292014
guienv,
20302015
L"",
20312016
core::rect<s32>(0, 0, 0, 0),
@@ -2048,7 +2033,7 @@ bool Game::initGui()
20482033
}
20492034

20502035
// Profiler text (size is updated when text is updated)
2051-
guitext_profiler = addStaticText(guienv,
2036+
guitext_profiler = gui::StaticText::add(guienv,
20522037
L"<Profiler>",
20532038
core::rect<s32>(0, 0, 0, 0),
20542039
false, false, guiroot);
@@ -4410,38 +4395,7 @@ void Game::updateGui(const RunStats &stats, f32 dtime, const CameraOrientation &
44104395
LocalPlayer *player = client->getEnv().getLocalPlayer();
44114396
v3f player_position = player->getPosition();
44124397

4413-
if (m_game_ui->m_flags.show_debug) {
4414-
static float drawtime_avg = 0;
4415-
drawtime_avg = drawtime_avg * 0.95 + stats.drawtime * 0.05;
4416-
u16 fps = 1.0 / stats.dtime_jitter.avg;
4417-
4418-
std::ostringstream os(std::ios_base::binary);
4419-
os << std::fixed
4420-
<< PROJECT_NAME_C " " << g_version_hash
4421-
<< ", FPS: " << fps
4422-
<< std::setprecision(0)
4423-
<< ", drawtime: " << drawtime_avg << "ms"
4424-
<< std::setprecision(1)
4425-
<< ", dtime jitter: "
4426-
<< (stats.dtime_jitter.max_fraction * 100.0) << "%"
4427-
<< std::setprecision(1)
4428-
<< ", view range: "
4429-
<< (draw_control->range_all ? "All" : itos(draw_control->wanted_range))
4430-
<< std::setprecision(3)
4431-
<< ", RTT: " << client->getRTT() << "s";
4432-
setStaticText(guitext, utf8_to_wide(os.str()).c_str());
4433-
guitext->setVisible(true);
4434-
} else {
4435-
guitext->setVisible(false);
4436-
}
4437-
4438-
if (guitext->isVisible()) {
4439-
core::rect<s32> rect(
4440-
5, 5,
4441-
screensize.X, 5 + g_fontengine->getTextHeight()
4442-
);
4443-
guitext->setRelativePosition(rect);
4444-
}
4398+
m_game_ui->update(stats, client, draw_control);
44454399

44464400
if (m_game_ui->m_flags.show_debug) {
44474401
std::ostringstream os(std::ios_base::binary);

Diff for: ‎src/game.h

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ class InputHandler;
2626
class ChatBackend; /* to avoid having to include chat.h */
2727
struct SubgameSpec;
2828

29+
struct Jitter {
30+
f32 max, min, avg, counter, max_sample, min_sample, max_fraction;
31+
};
32+
33+
struct RunStats {
34+
u32 drawtime;
35+
36+
Jitter dtime_jitter, busy_time_jitter;
37+
};
38+
2939
void the_game(bool *kill,
3040
bool random_input,
3141
InputHandler *input,

Diff for: ‎src/gui/guiEngine.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ GUIEngine::GUIEngine(JoystickController *joystick,
149149
g_fontengine->getTextHeight());
150150
rect += v2s32(4, 0);
151151

152-
m_irr_toplefttext =
153-
addStaticText(RenderingEngine::get_gui_env(), m_toplefttext,
154-
rect, false, true, 0, -1);
152+
m_irr_toplefttext = gui::StaticText::add(RenderingEngine::get_gui_env(),
153+
m_toplefttext, rect, false, true, 0, -1);
155154

156155
//create formspecsource
157156
m_formspecgui = new FormspecFormSource("");
@@ -560,9 +559,8 @@ void GUIEngine::updateTopLeftTextSize()
560559
rect += v2s32(4, 0);
561560

562561
m_irr_toplefttext->remove();
563-
m_irr_toplefttext =
564-
addStaticText(RenderingEngine::get_gui_env(), m_toplefttext,
565-
rect, false, true, 0, -1);
562+
m_irr_toplefttext = gui::StaticText::add(RenderingEngine::get_gui_env(),
563+
m_toplefttext, rect, false, true, 0, -1);
566564
}
567565

568566
/******************************************************************************/

Diff for: ‎src/gui/guiFormSpecMenu.cpp

+19-17
Original file line numberDiff line numberDiff line change
@@ -950,12 +950,12 @@ void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element
950950
Environment->setFocus(e);
951951
}
952952

953-
if (label.length() >= 1)
954-
{
953+
if (label.length() >= 1) {
955954
int font_height = g_fontengine->getTextHeight();
956955
rect.UpperLeftCorner.Y -= font_height;
957956
rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
958-
addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
957+
gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
958+
this, 0);
959959
}
960960

961961
e->setPasswordBox(true,L'*');
@@ -1017,7 +1017,8 @@ void GUIFormSpecMenu::parseSimpleField(parserData* data,
10171017

10181018
if (name.empty()) {
10191019
// spec field id to 0, this stops submit searching for a value that isn't there
1020-
addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
1020+
gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true, this,
1021+
spec.fid);
10211022
} else {
10221023
spec.send = true;
10231024
gui::IGUIElement *e;
@@ -1050,7 +1051,8 @@ void GUIFormSpecMenu::parseSimpleField(parserData* data,
10501051
int font_height = g_fontengine->getTextHeight();
10511052
rect.UpperLeftCorner.Y -= font_height;
10521053
rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1053-
addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
1054+
gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1055+
this, 0);
10541056
}
10551057
}
10561058

@@ -1162,7 +1164,8 @@ void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>&
11621164
int font_height = g_fontengine->getTextHeight();
11631165
rect.UpperLeftCorner.Y -= font_height;
11641166
rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
1165-
addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
1167+
gui::StaticText::add(Environment, spec.flabel.c_str(), rect, false, true,
1168+
this, 0);
11661169
}
11671170

11681171
if (parts.size() >= 6) {
@@ -1237,11 +1240,9 @@ void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
12371240
L"",
12381241
258+m_fields.size()
12391242
);
1240-
gui::IGUIStaticText *e =
1241-
addStaticText(Environment, spec.flabel.c_str(),
1242-
rect, false, false, this, spec.fid);
1243-
e->setTextAlignment(gui::EGUIA_UPPERLEFT,
1244-
gui::EGUIA_CENTER);
1243+
gui::IGUIStaticText *e = gui::StaticText::add(Environment,
1244+
spec.flabel.c_str(), rect, false, false, this, spec.fid);
1245+
e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_CENTER);
12451246
m_fields.push_back(spec);
12461247
}
12471248

@@ -1291,8 +1292,8 @@ void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &elemen
12911292
L"",
12921293
258+m_fields.size()
12931294
);
1294-
gui::IGUIStaticText *t =
1295-
addStaticText(Environment, spec.flabel.c_str(), rect, false, false, this, spec.fid);
1295+
gui::IGUIStaticText *t = gui::StaticText::add(Environment, spec.flabel.c_str(),
1296+
rect, false, false, this, spec.fid);
12961297
t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
12971298
m_fields.push_back(spec);
12981299
return;
@@ -2024,7 +2025,8 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
20242025
{
20252026
assert(!m_tooltip_element);
20262027
// Note: parent != this so that the tooltip isn't clipped by the menu rectangle
2027-
m_tooltip_element = addStaticText(Environment, L"",core::rect<s32>(0,0,110,18));
2028+
m_tooltip_element = gui::StaticText::add(Environment, L"",
2029+
core::rect<s32>(0,0,110,18));
20282030
m_tooltip_element->enableOverrideColor(true);
20292031
m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
20302032
m_tooltip_element->setDrawBackground(true);
@@ -3669,14 +3671,14 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
36693671
m_invmgr->inventoryAction(a);
36703672
} else if (craft_amount > 0) {
36713673
assert(s.isValid());
3672-
3674+
36733675
// if there are no items selected or the selected item
36743676
// belongs to craftresult list, proceed with crafting
36753677
if (m_selected_item == NULL ||
36763678
!m_selected_item->isValid() || m_selected_item->listname == "craftresult") {
3677-
3679+
36783680
m_selected_content_guess = ItemStack(); // Clear
3679-
3681+
36803682
assert(inv_s);
36813683

36823684
// Send IACTION_CRAFT

Diff for: ‎src/irrlicht_changes/static_text.h

+53-52
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,59 @@ namespace gui
4242
//! destructor
4343
virtual ~StaticText();
4444

45+
static irr::gui::IGUIStaticText *add(
46+
irr::gui::IGUIEnvironment *guienv,
47+
const EnrichedString &text,
48+
const core::rect< s32 > &rectangle,
49+
bool border = false,
50+
bool wordWrap = true,
51+
irr::gui::IGUIElement *parent = NULL,
52+
s32 id = -1,
53+
bool fillBackground = false)
54+
{
55+
if (parent == NULL) {
56+
// parent is NULL, so we must find one, or we need not to drop
57+
// result, but then there will be a memory leak.
58+
//
59+
// What Irrlicht does is to use guienv as a parent, but the problem
60+
// is that guienv is here only an IGUIEnvironment, while it is a
61+
// CGUIEnvironment in Irrlicht, which inherits from both IGUIElement
62+
// and IGUIEnvironment.
63+
//
64+
// A solution would be to dynamic_cast guienv to a
65+
// IGUIElement*, but Irrlicht is shipped without rtti support
66+
// in some distributions, causing the dymanic_cast to segfault.
67+
//
68+
// Thus, to find the parent, we create a dummy StaticText and ask
69+
// for its parent, and then remove it.
70+
irr::gui::IGUIStaticText *dummy_text =
71+
guienv->addStaticText(L"", rectangle, border, wordWrap,
72+
parent, id, fillBackground);
73+
parent = dummy_text->getParent();
74+
dummy_text->remove();
75+
}
76+
irr::gui::IGUIStaticText *result = new irr::gui::StaticText(
77+
text, border, guienv, parent,
78+
id, rectangle, fillBackground);
79+
80+
result->setWordWrap(wordWrap);
81+
result->drop();
82+
return result;
83+
}
84+
85+
static irr::gui::IGUIStaticText *add(
86+
irr::gui::IGUIEnvironment *guienv,
87+
const wchar_t *text,
88+
const core::rect< s32 > &rectangle,
89+
bool border = false,
90+
bool wordWrap = true,
91+
irr::gui::IGUIElement *parent = NULL,
92+
s32 id = -1,
93+
bool fillBackground = false)
94+
{
95+
return add(guienv, EnrichedString(text), rectangle, border, wordWrap, parent, id, fillBackground);
96+
}
97+
4598
//! draws the element and its children
4699
virtual void draw();
47100

@@ -171,46 +224,6 @@ namespace gui
171224

172225
} // end namespace irr
173226

174-
inline irr::gui::IGUIStaticText *addStaticText(
175-
irr::gui::IGUIEnvironment *guienv,
176-
const EnrichedString &text,
177-
const core::rect< s32 > &rectangle,
178-
bool border = false,
179-
bool wordWrap = true,
180-
irr::gui::IGUIElement *parent = NULL,
181-
s32 id = -1,
182-
bool fillBackground = false)
183-
{
184-
if (parent == NULL) {
185-
// parent is NULL, so we must find one, or we need not to drop
186-
// result, but then there will be a memory leak.
187-
//
188-
// What Irrlicht does is to use guienv as a parent, but the problem
189-
// is that guienv is here only an IGUIEnvironment, while it is a
190-
// CGUIEnvironment in Irrlicht, which inherits from both IGUIElement
191-
// and IGUIEnvironment.
192-
//
193-
// A solution would be to dynamic_cast guienv to a
194-
// IGUIElement*, but Irrlicht is shipped without rtti support
195-
// in some distributions, causing the dymanic_cast to segfault.
196-
//
197-
// Thus, to find the parent, we create a dummy StaticText and ask
198-
// for its parent, and then remove it.
199-
irr::gui::IGUIStaticText *dummy_text =
200-
guienv->addStaticText(L"", rectangle, border, wordWrap,
201-
parent, id, fillBackground);
202-
parent = dummy_text->getParent();
203-
dummy_text->remove();
204-
}
205-
irr::gui::IGUIStaticText *result = new irr::gui::StaticText(
206-
text, border, guienv, parent,
207-
id, rectangle, fillBackground);
208-
209-
result->setWordWrap(wordWrap);
210-
result->drop();
211-
return result;
212-
}
213-
214227
inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedString &text)
215228
{
216229
// dynamic_cast not possible due to some distributions shipped
@@ -245,18 +258,6 @@ inline void setStaticText(irr::gui::IGUIStaticText *static_text, const EnrichedS
245258

246259
#endif
247260

248-
inline irr::gui::IGUIStaticText *addStaticText(
249-
irr::gui::IGUIEnvironment *guienv,
250-
const wchar_t *text,
251-
const core::rect< s32 > &rectangle,
252-
bool border = false,
253-
bool wordWrap = true,
254-
irr::gui::IGUIElement *parent = NULL,
255-
s32 id = -1,
256-
bool fillBackground = false) {
257-
return addStaticText(guienv, EnrichedString(text), rectangle, border, wordWrap, parent, id, fillBackground);
258-
}
259-
260261
inline void setStaticText(irr::gui::IGUIStaticText *static_text, const wchar_t *text)
261262
{
262263
setStaticText(static_text, EnrichedString(text));

Diff for: ‎src/unittest/test_gameui.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ void TestGameUI::testInit()
4747
gui.initFlags();
4848
UASSERT(gui.getFlags().show_chat)
4949
UASSERT(gui.getFlags().show_hud)
50+
51+
// @TODO verify if we can create non UI nulldevice to test this function
52+
gui.init();
5053
}
5154

5255
void TestGameUI::testFlagSetters()
5356
{
54-
GameUI gui;
57+
GameUI gui{};
5558
gui.showMinimap(true);
5659
UASSERT(gui.getFlags().show_minimap);
5760

0 commit comments

Comments
 (0)
Please sign in to comment.