Fix mod channels crash (#7481)
[oweals/minetest.git] / src / gui / profilergraph.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 "profilergraph.h"
22 #include "util/string.h"
23
24 void ProfilerGraph::put(const Profiler::GraphValues &values)
25 {
26         m_log.emplace_back(values);
27
28         while (m_log.size() > m_log_max_size)
29                 m_log.erase(m_log.begin());
30 }
31
32 void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
33                 gui::IGUIFont *font) const
34 {
35         // Do *not* use UNORDERED_MAP here as the order needs
36         // to be the same for each call to prevent flickering
37         std::map<std::string, Meta> m_meta;
38
39         for (const Piece &piece : m_log) {
40                 for (const auto &i : piece.values) {
41                         const std::string &id = i.first;
42                         const float &value = i.second;
43                         std::map<std::string, Meta>::iterator j = m_meta.find(id);
44
45                         if (j == m_meta.end()) {
46                                 m_meta[id] = Meta(value);
47                                 continue;
48                         }
49
50                         if (value < j->second.min)
51                                 j->second.min = value;
52
53                         if (value > j->second.max)
54                                 j->second.max = value;
55                 }
56         }
57
58         // Assign colors
59         static const video::SColor usable_colors[] = {video::SColor(255, 255, 100, 100),
60                         video::SColor(255, 90, 225, 90),
61                         video::SColor(255, 100, 100, 255),
62                         video::SColor(255, 255, 150, 50),
63                         video::SColor(255, 220, 220, 100)};
64         static const u32 usable_colors_count =
65                         sizeof(usable_colors) / sizeof(*usable_colors);
66         u32 next_color_i = 0;
67
68         for (auto &i : m_meta) {
69                 Meta &meta = i.second;
70                 video::SColor color(255, 200, 200, 200);
71
72                 if (next_color_i < usable_colors_count)
73                         color = usable_colors[next_color_i++];
74
75                 meta.color = color;
76         }
77
78         s32 graphh = 50;
79         s32 textx = x_left + m_log_max_size + 15;
80         s32 textx2 = textx + 200 - 15;
81         s32 meta_i = 0;
82
83         for (const auto &p : m_meta) {
84                 const std::string &id = p.first;
85                 const Meta &meta = p.second;
86                 s32 x = x_left;
87                 s32 y = y_bottom - meta_i * 50;
88                 float show_min = meta.min;
89                 float show_max = meta.max;
90
91                 if (show_min >= -0.0001 && show_max >= -0.0001) {
92                         if (show_min <= show_max * 0.5)
93                                 show_min = 0;
94                 }
95
96                 s32 texth = 15;
97                 char buf[10];
98                 snprintf(buf, 10, "%.3g", show_max);
99                 font->draw(utf8_to_wide(buf).c_str(),
100                                 core::rect<s32>(textx, y - graphh, textx2,
101                                                 y - graphh + texth),
102                                 meta.color);
103                 snprintf(buf, 10, "%.3g", show_min);
104                 font->draw(utf8_to_wide(buf).c_str(),
105                                 core::rect<s32>(textx, y - texth, textx2, y), meta.color);
106                 font->draw(utf8_to_wide(id).c_str(),
107                                 core::rect<s32>(textx, y - graphh / 2 - texth / 2, textx2,
108                                                 y - graphh / 2 + texth / 2),
109                                 meta.color);
110                 s32 graph1y = y;
111                 s32 graph1h = graphh;
112                 bool relativegraph = (show_min != 0 && show_min != show_max);
113                 float lastscaledvalue = 0.0;
114                 bool lastscaledvalue_exists = false;
115
116                 for (const Piece &piece : m_log) {
117                         float value = 0;
118                         bool value_exists = false;
119                         Profiler::GraphValues::const_iterator k = piece.values.find(id);
120
121                         if (k != piece.values.end()) {
122                                 value = k->second;
123                                 value_exists = true;
124                         }
125
126                         if (!value_exists) {
127                                 x++;
128                                 lastscaledvalue_exists = false;
129                                 continue;
130                         }
131
132                         float scaledvalue = 1.0;
133
134                         if (show_max != show_min)
135                                 scaledvalue = (value - show_min) / (show_max - show_min);
136
137                         if (scaledvalue == 1.0 && value == 0) {
138                                 x++;
139                                 lastscaledvalue_exists = false;
140                                 continue;
141                         }
142
143                         if (relativegraph) {
144                                 if (lastscaledvalue_exists) {
145                                         s32 ivalue1 = lastscaledvalue * graph1h;
146                                         s32 ivalue2 = scaledvalue * graph1h;
147                                         driver->draw2DLine(
148                                                         v2s32(x - 1, graph1y - ivalue1),
149                                                         v2s32(x, graph1y - ivalue2),
150                                                         meta.color);
151                                 }
152
153                                 lastscaledvalue = scaledvalue;
154                                 lastscaledvalue_exists = true;
155                         } else {
156                                 s32 ivalue = scaledvalue * graph1h;
157                                 driver->draw2DLine(v2s32(x, graph1y),
158                                                 v2s32(x, graph1y - ivalue), meta.color);
159                         }
160
161                         x++;
162                 }
163
164                 meta_i++;
165         }
166 }