Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / samples / net / udp / graph.cpp
blob5000641e30d4f25dd8294bcc89c821d72bee208a
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 // Includes
21 #include "graph.h"
23 #ifdef USE_3D
25 #include <deque>
27 #include <nel/misc/types_nl.h>
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>
37 #include <nel/3d/driver.h>
38 #include <nel/3d/vertex_buffer.h>
39 #include <nel/3d/material.h>
40 #include <nel/3d/driver_user.h>
43 // Namespaces
46 using namespace NLMISC;
47 using namespace NL3D;
48 using namespace std;
51 // Variables
54 vector<CGraph*> *CGraph::_Graphs = NULL;
56 bool CGraph::Display = true;
57 bool CGraph::DisplayAverageValue = true;
60 // Classes
63 void CGraph::render (NL3D::UDriver *Driver, NL3D::UTextContext *TextContext)
65 // Display the background
66 uint32 w, h;
67 Driver->getWindowSize (w, h);
68 float ScreenWidth = (float) w;
69 float ScreenHeight = (float) h;
70 Driver->setMatrixMode2D (CFrustum (0.0f, ScreenWidth, 0.0f, ScreenHeight, 0.0f, 1.0f, false));
71 Driver->drawQuad (X, Y, X+Width, Y+Height, BackColor);
73 Peak = 0.0f;
74 float sum = 0.0f;
76 CMaterial material;
77 material.initUnlit ();
78 material.setColor (CRGBA (255,255,255,BackColor.A));
79 material.setBlend (true);
82 CVertexBuffer vbuffer;
83 vbuffer.setVertexFormat (CVertexBuffer::PositionFlag);
84 vbuffer.setNumVertices ((uint32)Values.size() * 2);
86 float pos = X+Width-1;
87 uint i = 0;
88 for (deque<float>::reverse_iterator it = Values.rbegin(); it != Values.rend(); it++)
90 // get a read accessor to the VB
91 CVertexBufferRead vba;
92 vbuffer.lock (vba);
94 float value = (*it) * Height / MaxValue;
95 if (value > Height) value = Height;
97 CVector *vect1 = (CVector*)vba.getVertexCoordPointer(i*2+0);
98 vect1->x = pos;
99 vect1->y = Y;
100 CVector *vect2 = (CVector*)vba.getVertexCoordPointer(i*2+1);
101 vect2->x = pos;
102 vect2->y = Y+value;
104 // Driver->drawLine (pos, Y, pos, Y+value, CRGBA (255,255,255,BackColor.A));
105 pos--;
106 if ((*it) > Peak) Peak = *it;
107 sum += *it;
108 i++;
111 // Render
112 IDriver *drv = ((CDriverUser*)Driver)->getDriver();
113 drv->activeVertexBuffer(vbuffer);
115 // Display max
116 float value = Peak * Height / MaxValue;
117 if (value > Height) value = Height;
118 CRGBA frontCol (min(BackColor.R*2,255),min(BackColor.G*2,255),min(BackColor.B*2,255),min(BackColor.A*2,255));
119 float peakval = Y+value;
120 Driver->drawLine (X, peakval, X+Width, peakval, frontCol);
122 TextContext->setHotSpot (UTextContext::MiddleLeft);
123 TextContext->setColor (frontCol);
124 TextContext->setFontSize (10);
125 TextContext->printfAt ((X+Width+2)/ScreenWidth, (Y+value)/ScreenHeight, "%.2f", Peak);
127 // Display average
128 float average = sum / (float)Values.size();
129 value = average * Height / MaxValue;
130 if (value > Height) value = Height;
131 float avrval = Y+value;
132 Driver->drawLine (X, avrval, X+Width, avrval, frontCol);
134 if (DisplayAverageValue)
136 if (avrval+10<peakval-10 || avrval-10>peakval+10)
138 TextContext->setHotSpot (UTextContext::MiddleLeft);
139 TextContext->setColor (frontCol);
140 TextContext->setFontSize (10);
141 TextContext->printfAt ((X+Width+2)/ScreenWidth, (Y+value)/ScreenHeight, "%.2f", average);
145 // Display name
146 TextContext->setHotSpot (UTextContext::TopLeft);
147 TextContext->printfAt ((X+1)/ScreenWidth, (Y+Height-1)/ScreenHeight, Name.c_str());
150 void CGraph::addOneValue (float value)
152 if (value < 0.0f) value = 0.0f;
154 Values.push_back (value);
155 while (Values.size () > Width)
156 Values.pop_front ();
158 // if (Values.back() > Peak)
159 // Peak = Values.back();
163 void CGraph::addValue (float value)
165 TTime currentTime = CTime::getLocalTime ();
167 while (Values.size () == 0 || currentTime > CurrentQuantumStart + Quantum)
169 CurrentQuantumStart += Quantum;
170 addOneValue ();
173 Values.back() += value;
175 // if (Values.back() > Peak)
176 // Peak = Values.back();
180 // Variables
183 CGraph FpsGraph ("fps", 10.0f, 110.0f, 100.0f, 100.0f, CRGBA(128,0,0,128), 1000, 150.0f);
184 CGraph SpfGraph ("spf", 10.0f, 10.0f, 100.0f, 100.0f, CRGBA(0,128,0,128), 0, 100.0f);
186 CGraph DownloadGraph ("download", 10.0f, 260.0f, 100.0f, 100.0f, CRGBA(0,0,128,128), 1000, 2000.0f);
187 CGraph UploadGraph ("upload", 10.0f, 360.0f, 100.0f, 100.0f, CRGBA(0,128,128,128), 1000, 2000.0f);
189 CGraph DpfGraph ("dpf", 150.0f, 260.0f, 100.0f, 100.0f, CRGBA(128,0,128,128), 100000, 180.0f);
190 CGraph UpfGraph ("upf", 150.0f, 360.0f, 100.0f, 100.0f, CRGBA(128,128,0,128), 100000, 180.0f);
193 CGraph UserLWatchGraph ("ul", 10.0f, 490.0f, 100.0f, 100.0f, CRGBA(0,64,128,128), 100000, 1000.0f);
194 CGraph CycleWatchGraph ("cl", 150.0f, 490.0f, 100.0f, 100.0f, CRGBA(0,128,64,128), 100000, 1000.0f);
195 CGraph ReceiveWatchGraph ("rl", 300.0f, 490.0f, 100.0f, 100.0f, CRGBA(128,64,0,128), 100000, 100.0f);
196 CGraph SendWatchGraph ("sl", 450.0f, 490.0f, 100.0f, 100.0f, CRGBA(128,0,64,128), 100000, 100.0f);
197 CGraph PriorityAmountGraph ("prio amount", 150.0f, 110.0f, 100.0f, 100.0f, CRGBA(128,64,64,128), 100000, 16.0f);
198 CGraph SeenEntitiesGraph( "seen entities", 150.0f, 10.0f, 100.0f, 100.0f, CRGBA(64,128,64,128), 100000, 256);
202 // Functions
205 void CGraph::render (NL3D::UDriver &driver, NL3D::UTextContext &tc)
207 if (!Display) return;
209 if (_Graphs == NULL) return;
211 for (uint i = 0; i < _Graphs->size(); i++)
213 (*_Graphs)[i]->render (&driver, &tc);
218 NLMISC_VARIABLE(bool, GraphShow, "Display or not graphs");
219 NLMISC_VARIABLE(bool, GraphDisplayAverageValue, "Display or not average values");
222 #endif