X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fport.cpp;h=1e4219da1025fbff77823a4aebd949178a851bce;hb=9954e8cbf9bae21f5a727f9c92e9c33dc6d8eaf1;hp=11ccfe0331674ebe05a1fc37a332c7216e7e02aa;hpb=c147c9caaf9bc2f6323baf188a438ced9f0f5894;p=netvis.git diff --git a/source/port.cpp b/source/port.cpp index 11ccfe0..1e4219d 100644 --- a/source/port.cpp +++ b/source/port.cpp @@ -5,14 +5,25 @@ Copyright @ 2008 Mikko Rasa, Mikkosoft Productions Distributed unter the GPL */ +#include #include #include +#include +#include +#include +#include #include #include "port.h" +#include "netvis.h" -Port::Port(unsigned n, const Msp::GL::Color &c): +using namespace std; +using namespace Msp; + +Port::Port(NetVis &v, unsigned n): + netvis(v), number(n), - color(c) + registered(false), + mesh((GL::COLOR4_UBYTE, GL::VERTEX2)) { char buf[128]; sockaddr_in addr; @@ -21,7 +32,73 @@ Port::Port(unsigned n, const Msp::GL::Color &c): addr.sin_addr.s_addr = 0; int err = getnameinfo(reinterpret_cast(&addr), sizeof(sockaddr_in), 0, 0, buf, sizeof(buf), 0); if(err==0) + { name = buf; + registered = !isdigit(name[0]); + } else name = Msp::lexical_cast(number); + + const map &ports = netvis.get_ports(); + unsigned tries = 100; + if(ports.size()>100 && !registered) + tries = 10000/ports.size()+1; + float best_score = 0; + for(unsigned i=0; (i::const_iterator j=ports.begin(); j!=ports.end(); ++j) + { + if(registered && !j->second->is_registered()) + break; + const GL::Color &other = j->second->get_color(); + float dr = c.r-other.r; + float dg = c.g-other.g; + float db = c.b-other.b; + score = min(score, dr*dr+dg*dg+db*db); + } + if(score>best_score) + { + best_score = score; + color = c; + } + } + color.a = 0.4f; + + GL::MeshBuilder bld(mesh); + bld.begin(GL::QUADS); + bld.color(color.r, color.g, color.b, color.a); + for(unsigned x=0; x<=4; x+=2) + { + bld.vertex(x+0, 0); + bld.vertex(x+10, 0); + bld.vertex(x+10, 10); + bld.vertex(x+0, 10); + } + bld.end(); +} + +void Port::render() const +{ + GL::PushMatrix push_; + mesh.draw(); + GL::translate(16, 1, 0); + GL::scale_uniform(10); + GL::Immediate imm((GL::COLOR4_UBYTE, GL::TEXCOORD2, GL::VERTEX2)); + netvis.get_font().draw_string(name, imm); + GL::Texture::unbind(); }