--- /dev/null
+/* $Id$
+
+This file is part of NetVis
+Copyright @ 2008 Mikko Rasa, Mikkosoft Productions
+Distributed unter the GPL
+*/
+
+#include <msp/gl/immediate.h>
+#include <msp/gl/matrix.h>
+#include <msp/gl/meshbuilder.h>
+#include <msp/gl/misc.h>
+#include <msp/strings/formatter.h>
+#include <msp/time/units.h>
+#include "history.h"
+#include "netvis.h"
+
+using namespace std;
+using namespace Msp;
+
+History::History(NetVis &n, unsigned w, unsigned h):
+ netvis(n),
+ width(w),
+ height(h),
+ buffer(w),
+ scale(1024),
+ mesh((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2))
+{
+ texture.storage(GL::RGBA, height, width, 0);
+ texture.set_min_filter(GL::LINEAR);
+ GL::MeshBuilder bld(mesh);
+ bld.color(1.0f, 1.0f, 1.0f);
+ bld.begin(GL::QUADS);
+ bld.texcoord(0, 1);
+ bld.vertex(0, 0);
+ bld.texcoord(0, 0);
+ bld.vertex(width, 0);
+ bld.texcoord(1, 0);
+ bld.vertex(width, height);
+ bld.texcoord(1, 1);
+ bld.vertex(0, height);
+ bld.end();
+}
+
+void History::activity(unsigned down, unsigned up)
+{
+ accum.down += down;
+ accum.up += up;
+}
+
+void History::tick(const Time::TimeStamp &t)
+{
+ if(t>next_push)
+ {
+ buffer.push(accum);
+ accum = Bandwidth();
+ next_push += Time::sec;
+ if(next_push<t)
+ next_push = t+Time::sec;
+
+ unsigned m = 0;
+ for(unsigned i=0; i<width; ++i)
+ {
+ const Bandwidth &bw = buffer.get(i);
+ m = max(m, max(bw.down, bw.up));
+ }
+ scale = 1;
+ while(m>=scale*1000)
+ scale *= 1024;
+ while(m>=scale*5)
+ scale *= 10;
+ if(m>scale*2)
+ scale *= 5;
+ else if(m>scale)
+ scale *= 2;
+
+ create_texture();
+ }
+}
+
+void History::render() const
+{
+ texture.bind();
+ mesh.draw();
+
+ char suffix = 'B';
+ unsigned value = scale;
+ if(value>=1024)
+ {
+ value /= 1024;
+ suffix = 'k';
+ }
+ if(value>=1024)
+ {
+ value /= 1024;
+ suffix = 'M';
+ }
+ GL::PushMatrix push_;
+ GL::translate(width+5, height-10, 0);
+ GL::scale_uniform(10);
+ GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2));
+ imm.color(1.0f, 1.0f, 1.0f);
+ netvis.get_font().draw_string(format("%d%c", value, suffix), imm);
+ GL::translate(0, (height-12)*-0.05, 0);
+ netvis.get_font().draw_string(format("%g%c", value*0.5, suffix), imm);
+ GL::translate(0, (height-12)*-0.05, 0);
+ netvis.get_font().draw_string(format("0%c", suffix), imm);
+ GL::Texture::unbind();
+}
+
+void History::create_texture()
+{
+ vector<unsigned> data(width*height);
+ for(unsigned y=0; y<width; ++y)
+ {
+ const Bandwidth &bw = buffer.get(y);
+ unsigned down = bw.down*height/scale;
+ unsigned up = bw.up*height/scale;
+ unsigned up_min = up;
+ unsigned up_max = up;
+ if(y>0)
+ {
+ unsigned value = (up+buffer.get(y-1).up*height/scale)/2;
+ up_min = min(value+1, up_min);
+ up_max = max(value, up_max);
+ }
+ if(y<width-1)
+ {
+ unsigned value = (up+buffer.get(y+1).up*height/scale)/2;
+ up_min = min(value+1, up_min);
+ up_max = max(value, up_max);
+ }
+ unsigned *row = &data[y*100];
+ for(unsigned x=0; x<100; ++x)
+ {
+ if(x>=up_min && x<=up_max)
+ row[x] = 0xFF8000FF;
+ else if(x<=down)
+ row[x] = 0xFF80FF80;
+ else if(x%25==24 || y%60==0)
+ row[x] = 0x40FFFFFF;
+ else
+ row[x] = 0x00000000;
+ }
+ }
+ texture.image(0, GL::RGBA, GL::UNSIGNED_BYTE, data.data());
+}
+
+
+History::Bandwidth::Bandwidth():
+ down(0),
+ up(0)
+{ }