Fix crash
[ryzomcore.git] / snowballs2 / client / src / graph.cpp
blob31329fd7d63affc2df1370a4c039b01960939f61
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program 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 Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 // Includes
24 #include <nel/misc/types_nl.h>
26 #include <deque>
28 #include <nel/misc/vector.h>
29 #include <nel/misc/matrix.h>
30 #include <nel/misc/command.h>
32 #include <nel/3d/u_material.h>
33 #include <nel/3d/u_camera.h>
34 #include <nel/3d/u_driver.h>
35 #include <nel/3d/u_text_context.h>
36 #include <nel/3d/u_texture.h>
38 #include "graph.h"
39 #include "snowballs_client.h"
42 // Namespaces
45 using namespace NLMISC;
46 using namespace NL3D;
47 using namespace std;
50 namespace SBCLIENT {
53 // Classes
56 void CGraph::render ()
58 // Display the background
59 uint32 w, h;
60 Driver->getWindowSize (w, h);
61 float ScreenWidth = (float) w;
62 float ScreenHeight = (float) h;
63 Driver->setMatrixMode2D (CFrustum (0.0f, ScreenWidth, 0.0f, ScreenHeight, 0.0f, 1.0f, false));
64 Driver->drawQuad (X, Y, X+Width, Y+Height, BackColor);
66 float pos = X+Width-1;
67 for (deque<float>::reverse_iterator it = Values.rbegin(); it != Values.rend(); it++)
69 float value = (*it) * Height / MaxValue;
70 if (value > Height) value = Height;
71 Driver->drawLine (pos, Y, pos, Y+value, CRGBA (255,255,255,BackColor.A));
72 pos--;
75 float value = Peak * Height / MaxValue;
76 if (value > Height) value = Height;
77 CRGBA frontCol (min(BackColor.R*2,255),min(BackColor.G*2,255),min(BackColor.B*2,255),min(BackColor.A*2,255));
78 Driver->drawLine (X, Y+value, X+Width, Y+value, frontCol);
80 TextContext->setHotSpot (UTextContext::MiddleLeft);
81 TextContext->setColor (frontCol);
82 TextContext->setFontSize (10);
83 TextContext->printfAt ((X+Width+2)/ScreenWidth, (Y+value)/ScreenHeight, toString(Peak).c_str());
85 TextContext->setHotSpot (UTextContext::TopLeft);
86 TextContext->printfAt ((X+1)/ScreenWidth, (Y+Height-1)/ScreenHeight, Name.c_str());
89 void CGraph::addOneValue (float value)
91 Values.push_back (value);
92 while (Values.size () > Width)
93 Values.pop_front ();
95 if (Values.back() > Peak)
96 Peak = Values.back();
100 void CGraph::addValue (float value)
102 TTime currentTime = CTime::getLocalTime ();
104 while (currentTime > CurrentQuantumStart + Quantum)
106 CurrentQuantumStart += Quantum;
107 addOneValue ();
110 Values.back() += value;
112 if (Values.back() > Peak)
113 Peak = Values.back();
117 // Variables
120 CGraph FpsGraph ("fps", 10.0f, 10.0f, 100.0f, 100.0f, CRGBA(128,0,0,128), 1000, 40.0f);
121 CGraph SpfGraph ("spf", 10.0f, 110.0f, 100.0f, 100.0f, CRGBA(0,128,0,128), 0, 0.1f);
123 CGraph DownloadGraph ("download", 10.0f, 260.0f, 100.0f, 100.0f, CRGBA(0,0,128,128), 1000, 1000.0f);
124 CGraph UploadGraph ("upload", 10.0f, 360.0f, 100.0f, 100.0f, CRGBA(0,128,128,128), 1000, 1000.0f);
126 bool ShowGraph;
129 // Functions
132 void cbUpdateGraph (CConfigFile::CVar &var)
134 if (var.Name == "ShowGraph") ShowGraph = var.asInt() == 1;
135 else nlwarning ("Unknown variable update %s", var.Name.c_str());
138 void initGraph ()
140 ConfigFile->setCallback ("ShowGraph", cbUpdateGraph);
142 cbUpdateGraph (ConfigFile->getVar ("ShowGraph"));
145 void updateGraph ()
147 if (!ShowGraph) return;
149 FpsGraph.render ();
150 SpfGraph.render ();
152 DownloadGraph.render ();
153 UploadGraph.render ();
156 void releaseGraph ()
158 ConfigFile->setCallback("ShowGraph", NULL);
161 NLMISC_COMMAND(graph,"swith on/off graphs","")
163 // check args, if there s not the right number of parameter, return bad
164 if (args.size() != 0) return false;
166 ShowGraph = !ShowGraph;
167 return true;
170 } /* namespace SBCLIENT */
172 /* end of file */