Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
GameUI refactor (part 3/X): Move Game::guitext2, Game::guitext_info, …
…Game::infotext to GameUI class

Other enhancements:
* Drop unused GameRunData::time_of_day
* Little GameUI::update code path optimizations
  • Loading branch information
nerzhul committed Jan 5, 2018
1 parent 3a772e7 commit aab3b18
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 93 deletions.
78 changes: 68 additions & 10 deletions src/client/gameui.cpp
Expand Up @@ -21,22 +21,46 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "gameui.h"
#include <irrlicht_changes/static_text.h>
#include "gui/mainmenumanager.h"
#include "util/pointedthing.h"
#include "client.h"
#include "fontengine.h"
#include "clientmap.h"
#include "version.h"
#include "fontengine.h"
#include "nodedef.h"
#include "renderingengine.h"
#include "version.h"

inline static const char *yawToDirectionString(int yaw)
{
static const char *direction[4] = {"N +Z", "W -X", "S -Z", "E +X"};

yaw = wrapDegrees_0_360(yaw);
yaw = (yaw + 45) % 360 / 90;

return direction[yaw];
}

void GameUI::init()
{
// First line of debug text
m_guitext = gui::StaticText::add(guienv, utf8_to_wide(PROJECT_NAME_C).c_str(),
core::rect<s32>(0, 0, 0, 0), false, false, guiroot);

// Second line of debug text
m_guitext2 = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0), false,
false, guiroot);

// At the middle of the screen
// Object infos are shown in this
m_guitext_info = gui::StaticText::add(guienv, L"",
core::rect<s32>(0, 0, 400, g_fontengine->getTextHeight() * 5 + 5)
+ v2s32(100, 200), false, true, guiroot);
}

void GameUI::update(const RunStats &stats, Client *client,
const MapDrawControl *draw_control)
void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_control,
const CameraOrientation &cam, const PointedThing &pointed_old)
{
v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();

if (m_flags.show_debug) {
static float drawtime_avg = 0;
drawtime_avg = drawtime_avg * 0.95 + stats.drawtime * 0.05;
Expand All @@ -57,16 +81,50 @@ void GameUI::update(const RunStats &stats, Client *client,
<< std::setprecision(3)
<< ", RTT: " << client->getRTT() << "s";
setStaticText(m_guitext, utf8_to_wide(os.str()).c_str());
m_guitext->setVisible(true);
} else {
m_guitext->setVisible(false);
}

if (m_guitext->isVisible()) {
v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
m_guitext->setRelativePosition(core::rect<s32>(5, 5, screensize.X,
5 + g_fontengine->getTextHeight()));
}

// Finally set the guitext visible depending on the flag
m_guitext->setVisible(m_flags.show_debug);

if (m_flags.show_debug) {
LocalPlayer *player = client->getEnv().getLocalPlayer();
v3f player_position = player->getPosition();

std::ostringstream os(std::ios_base::binary);
os << std::setprecision(1) << std::fixed
<< "pos: (" << (player_position.X / BS)
<< ", " << (player_position.Y / BS)
<< ", " << (player_position.Z / BS)
<< "), yaw: " << (wrapDegrees_0_360(cam.camera_yaw)) << "° "
<< yawToDirectionString(cam.camera_yaw)
<< ", seed: " << ((u64)client->getMapSeed());

if (pointed_old.type == POINTEDTHING_NODE) {
ClientMap &map = client->getEnv().getClientMap();
const INodeDefManager *nodedef = client->getNodeDefManager();
MapNode n = map.getNodeNoEx(pointed_old.node_undersurface);

if (n.getContent() != CONTENT_IGNORE && nodedef->get(n).name != "unknown") {
os << ", pointed: " << nodedef->get(n).name
<< ", param2: " << (u64) n.getParam2();
}
}

setStaticText(m_guitext2, utf8_to_wide(os.str()).c_str());

m_guitext2->setRelativePosition(core::rect<s32>(5,
5 + g_fontengine->getTextHeight(), screensize.X,
5 + g_fontengine->getTextHeight() * 2
));
}

m_guitext2->setVisible(m_flags.show_debug);

setStaticText(m_guitext_info, translate_string(m_infotext).c_str());
m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0);
}

void GameUI::initFlags()
Expand Down
12 changes: 9 additions & 3 deletions src/client/gameui.h
Expand Up @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once

#include <IGUIEnvironment.h>
#include <util/pointedthing.h>
#include "game.h"

using namespace irr;
Expand Down Expand Up @@ -49,20 +50,25 @@ class GameUI
};

void init();
void update(const RunStats &stats, Client *client, const MapDrawControl *draw_control);
void update(const RunStats &stats, Client *client, MapDrawControl *draw_control,
const CameraOrientation &cam, const PointedThing &pointed_old);

void initFlags();
const Flags &getFlags() const { return m_flags; }

void showMinimap(bool show);

void setInfoText(const std::wstring &str) { m_infotext = str; }
void clearInfoText() { m_infotext.clear(); }

private:
Flags m_flags;

gui::IGUIStaticText *m_guitext; // First line of debug text
gui::IGUIStaticText *m_guitext2; // Second line of debug text
gui::IGUIStaticText *m_guitext_info; // At the middle of the screen
std::wstring m_infotext;
// @TODO future move
// gui::IGUIStaticText *m_guitext2; // Second line of debug text
// gui::IGUIStaticText *m_guitext_info; // At the middle of the screen
// gui::IGUIStaticText *m_guitext_status;
// gui::IGUIStaticText *m_guitext_chat; // Chat text
// gui::IGUIStaticText *m_guitext_profiler; // Profiler text
Expand Down
88 changes: 9 additions & 79 deletions src/game.cpp
Expand Up @@ -1134,10 +1134,6 @@ struct FpsControl {
* many functions that do require objects of thse types do not modify them
* (so they can be passed as a const qualified parameter)
*/
struct CameraOrientation {
f32 camera_yaw; // "right/left"
f32 camera_pitch; // "up/down"
};

struct GameRunData {
u16 dig_index;
Expand Down Expand Up @@ -1170,7 +1166,6 @@ struct GameRunData {
u32 profiler_current_page;
u32 profiler_max_page; // Number of pages

float time_of_day;
float time_of_day_smooth;
};

Expand Down Expand Up @@ -1442,13 +1437,10 @@ class Game {

/* GUI stuff
*/
gui::IGUIStaticText *guitext2; // Second line of debug text
gui::IGUIStaticText *guitext_info; // At the middle of the screen
gui::IGUIStaticText *guitext_status;
gui::IGUIStaticText *guitext_chat; // Chat text
gui::IGUIStaticText *guitext_profiler; // Profiler text

std::wstring infotext;
std::wstring m_statustext;

KeyCache keycache;
Expand Down Expand Up @@ -1690,7 +1682,7 @@ void Game::run()

processQueues();

infotext = L"";
m_game_ui->clearInfoText();
hud->resizeHotbar();

updateProfilers(stats, draw_times, dtime);
Expand Down Expand Up @@ -1989,19 +1981,6 @@ bool Game::initGui()
{
m_game_ui->init();

// Second line of debug text
guitext2 = gui::StaticText::add(guienv,
L"",
core::rect<s32>(0, 0, 0, 0),
false, false, guiroot);

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

// Status text (displays info when showing and hiding GUI stuff, etc.)
guitext_status = gui::StaticText::add(guienv,
L"<Status>",
Expand Down Expand Up @@ -3840,13 +3819,14 @@ void Game::handlePointingAtNode(const PointedThing &pointed,
NodeMetadata *meta = map.getNodeMetadata(nodepos);

if (meta) {
infotext = unescape_translate(utf8_to_wide(meta->getString("infotext")));
m_game_ui->setInfoText(unescape_translate(utf8_to_wide(
meta->getString("infotext"))));
} else {
MapNode n = map.getNodeNoEx(nodepos);

if (nodedef_manager->get(n).tiledef[0].name == "unknown_node.png") {
infotext = L"Unknown node: ";
infotext += utf8_to_wide(nodedef_manager->get(n).name);
m_game_ui->setInfoText(L"Unknown node: " +
utf8_to_wide(nodedef_manager->get(n).name));
}
}

Expand Down Expand Up @@ -3917,7 +3897,7 @@ void Game::handlePointingAtNode(const PointedThing &pointed,
void Game::handlePointingAtObject(const PointedThing &pointed, const ItemStack &playeritem,
const v3f &player_position, bool show_debug)
{
infotext = unescape_translate(
std::wstring infotext = unescape_translate(
utf8_to_wide(runData.selected_object->infoText()));

if (show_debug) {
Expand All @@ -3927,6 +3907,8 @@ void Game::handlePointingAtObject(const PointedThing &pointed, const ItemStack &
infotext += utf8_to_wide(runData.selected_object->debugInfoText());
}

m_game_ui->setInfoText(infotext);

if (isLeftPressed()) {
bool do_punch = false;
bool do_punch_damage = false;
Expand Down Expand Up @@ -4167,7 +4149,6 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
time_of_day_smooth = time_of_day_smooth * (1.0 - todsm)
+ time_of_day * todsm;

runData.time_of_day = time_of_day;
runData.time_of_day_smooth = time_of_day_smooth;

sky->update(time_of_day_smooth, time_brightness, direct_brightness,
Expand Down Expand Up @@ -4378,62 +4359,11 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats, f32 dtime,
}


inline static const char *yawToDirectionString(int yaw)
{
static const char *direction[4] = {"N +Z", "W -X", "S -Z", "E +X"};

yaw = wrapDegrees_0_360(yaw);
yaw = (yaw + 45) % 360 / 90;

return direction[yaw];
}


void Game::updateGui(const RunStats &stats, f32 dtime, const CameraOrientation &cam)
{
v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
LocalPlayer *player = client->getEnv().getLocalPlayer();
v3f player_position = player->getPosition();

m_game_ui->update(stats, client, draw_control);

if (m_game_ui->m_flags.show_debug) {
std::ostringstream os(std::ios_base::binary);
os << std::setprecision(1) << std::fixed
<< "pos: (" << (player_position.X / BS)
<< ", " << (player_position.Y / BS)
<< ", " << (player_position.Z / BS)
<< "), yaw: " << (wrapDegrees_0_360(cam.camera_yaw)) << "° "
<< yawToDirectionString(cam.camera_yaw)
<< ", seed: " << ((u64)client->getMapSeed());

if (runData.pointed_old.type == POINTEDTHING_NODE) {
ClientMap &map = client->getEnv().getClientMap();
const INodeDefManager *nodedef = client->getNodeDefManager();
MapNode n = map.getNodeNoEx(runData.pointed_old.node_undersurface);

if (n.getContent() != CONTENT_IGNORE && nodedef->get(n).name != "unknown") {
os << ", pointed: " << nodedef->get(n).name
<< ", param2: " << (u64) n.getParam2();
}
}

setStaticText(guitext2, utf8_to_wide(os.str()).c_str());
guitext2->setVisible(true);
} else {
guitext2->setVisible(false);
}

if (guitext2->isVisible()) {
core::rect<s32> rect(
5, 5 + g_fontengine->getTextHeight(),
screensize.X, 5 + g_fontengine->getTextHeight() * 2
);
guitext2->setRelativePosition(rect);
}

setStaticText(guitext_info, translate_string(infotext).c_str());
guitext_info->setVisible(m_game_ui->m_flags.show_hud && g_menumgr.menuCount() == 0);
m_game_ui->update(stats, client, draw_control, cam, runData.pointed_old);

float statustext_time_max = 1.5;

Expand Down
5 changes: 5 additions & 0 deletions src/game.h
Expand Up @@ -36,6 +36,11 @@ struct RunStats {
Jitter dtime_jitter, busy_time_jitter;
};

struct CameraOrientation {
f32 camera_yaw; // "right/left"
f32 camera_pitch; // "up/down"
};

void the_game(bool *kill,
bool random_input,
InputHandler *input,
Expand Down
2 changes: 1 addition & 1 deletion src/unittest/test_gameui.cpp
Expand Up @@ -49,7 +49,7 @@ void TestGameUI::testInit()
UASSERT(gui.getFlags().show_hud)

// @TODO verify if we can create non UI nulldevice to test this function
gui.init();
// gui.init();
}

void TestGameUI::testFlagSetters()
Expand Down

0 comments on commit aab3b18

Please sign in to comment.