]> git.lizzy.rs Git - minetest.git/blob - src/client/gameui.cpp
d6d52823a5bc52a2697b8c0f838f8574a31b6857
[minetest.git] / src / client / gameui.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "gameui.h"
22 #include <irrlicht_changes/static_text.h>
23 #include "gui/mainmenumanager.h"
24 #include "util/pointedthing.h"
25 #include "client.h"
26 #include "clientmap.h"
27 #include "fontengine.h"
28 #include "nodedef.h"
29 #include "renderingengine.h"
30 #include "version.h"
31
32 inline static const char *yawToDirectionString(int yaw)
33 {
34         static const char *direction[4] = {"N +Z", "W -X", "S -Z", "E +X"};
35
36         yaw = wrapDegrees_0_360(yaw);
37         yaw = (yaw + 45) % 360 / 90;
38
39         return direction[yaw];
40 }
41
42 void GameUI::init()
43 {
44         // First line of debug text
45         m_guitext = gui::StaticText::add(guienv, utf8_to_wide(PROJECT_NAME_C).c_str(),
46                 core::rect<s32>(0, 0, 0, 0), false, false, guiroot);
47
48         // Second line of debug text
49         m_guitext2 = gui::StaticText::add(guienv, L"", core::rect<s32>(0, 0, 0, 0), false,
50                 false, guiroot);
51
52         // At the middle of the screen
53         // Object infos are shown in this
54         m_guitext_info = gui::StaticText::add(guienv, L"",
55                 core::rect<s32>(0, 0, 400, g_fontengine->getTextHeight() * 5 + 5)
56                         + v2s32(100, 200), false, true, guiroot);
57 }
58
59 void GameUI::update(const RunStats &stats, Client *client, MapDrawControl *draw_control,
60         const CameraOrientation &cam, const PointedThing &pointed_old)
61 {
62         v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
63
64         if (m_flags.show_debug) {
65                 static float drawtime_avg = 0;
66                 drawtime_avg = drawtime_avg * 0.95 + stats.drawtime * 0.05;
67                 u16 fps = 1.0 / stats.dtime_jitter.avg;
68
69                 std::ostringstream os(std::ios_base::binary);
70                 os << std::fixed
71                         << PROJECT_NAME_C " " << g_version_hash
72                         << ", FPS: " << fps
73                         << std::setprecision(0)
74                         << ", drawtime: " << drawtime_avg << "ms"
75                         << std::setprecision(1)
76                         << ", dtime jitter: "
77                         << (stats.dtime_jitter.max_fraction * 100.0) << "%"
78                         << std::setprecision(1)
79                         << ", view range: "
80                         << (draw_control->range_all ? "All" : itos(draw_control->wanted_range))
81                         << std::setprecision(3)
82                         << ", RTT: " << client->getRTT() << "s";
83                 setStaticText(m_guitext, utf8_to_wide(os.str()).c_str());
84
85                 m_guitext->setRelativePosition(core::rect<s32>(5, 5, screensize.X,
86                         5 + g_fontengine->getTextHeight()));
87         }
88
89         // Finally set the guitext visible depending on the flag
90         m_guitext->setVisible(m_flags.show_debug);
91
92         if (m_flags.show_debug) {
93                 LocalPlayer *player = client->getEnv().getLocalPlayer();
94                 v3f player_position = player->getPosition();
95
96                 std::ostringstream os(std::ios_base::binary);
97                 os << std::setprecision(1) << std::fixed
98                         << "pos: (" << (player_position.X / BS)
99                         << ", " << (player_position.Y / BS)
100                         << ", " << (player_position.Z / BS)
101                         << "), yaw: " << (wrapDegrees_0_360(cam.camera_yaw)) << "° "
102                         << yawToDirectionString(cam.camera_yaw)
103                         << ", seed: " << ((u64)client->getMapSeed());
104
105                 if (pointed_old.type == POINTEDTHING_NODE) {
106                         ClientMap &map = client->getEnv().getClientMap();
107                         const INodeDefManager *nodedef = client->getNodeDefManager();
108                         MapNode n = map.getNodeNoEx(pointed_old.node_undersurface);
109
110                         if (n.getContent() != CONTENT_IGNORE && nodedef->get(n).name != "unknown") {
111                                 os << ", pointed: " << nodedef->get(n).name
112                                         << ", param2: " << (u64) n.getParam2();
113                         }
114                 }
115
116                 setStaticText(m_guitext2, utf8_to_wide(os.str()).c_str());
117
118                 m_guitext2->setRelativePosition(core::rect<s32>(5,
119                         5 + g_fontengine->getTextHeight(), screensize.X,
120                         5 + g_fontengine->getTextHeight() * 2
121                 ));
122         }
123
124         m_guitext2->setVisible(m_flags.show_debug);
125
126         setStaticText(m_guitext_info, translate_string(m_infotext).c_str());
127         m_guitext_info->setVisible(m_flags.show_hud && g_menumgr.menuCount() == 0);
128 }
129
130 void GameUI::initFlags()
131 {
132         memset(&m_flags, 0, sizeof(GameUI::Flags));
133         m_flags.show_chat = true;
134         m_flags.show_hud = true;
135         m_flags.show_debug = g_settings->getBool("show_debug");
136 }
137
138 void GameUI::showMinimap(bool show)
139 {
140         m_flags.show_minimap = show;
141 }