-
Notifications
You must be signed in to change notification settings - Fork 18
/
chart.cpp
157 lines (141 loc) · 3.62 KB
/
chart.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright 2008 Utah State University
This file is part of OIP.
OIP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OIP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OIP. If not, see <http://www.gnu.org/licenses/>.
*/
#include "chart.h"
#include "text.h"
#include "gui/draw.h"
#include <sstream>
using std::stringstream;
chart::chart(const char* u, Uint32 c)
{
unit = u;
color = c;
//some reasonable defaults
lastupdate = SDL_GetTicks();
minmax = 100 * 1024;
maxval = minmax;
tickwidth = 2;
setTickInterval(FRAMERATE * 4);
datapos = MAXWIDTH-1;
for(;datapos>=0; datapos--)
data[datapos]=0;
datapos++;
datalen = 0;
bg = 0;
resize(512, 128);
}
void chart::resize(int width, int height)
{
if (width > MAXWIDTH)
width = MAXWIDTH;
w = width;
h = height;
if (bg)
SDL_FreeSurface(bg);
bg = SDL_CreateRGBSurface(SDL_HWSURFACE, width, height, 32, 0xff000000,0x00ff0000,0x0000ff00,0x000000ff);
datalen = width / tickwidth;
redraw();
}
void chart::redraw()
{
int i = (datapos) + 1 % datalen;
int x = 0;
SDL_FillRect(bg, NULL, 0x000000ff);
int newmaxval=data[datapos];
do
{
SDL_Rect r = {x, h - data[i] / maxval * h, tickwidth, data[i] / maxval * h};
SDL_FillRect(bg, &r, color);
if (data[i] > newmaxval)
newmaxval = data[i];
i = (i + 1) % datalen;
x += tickwidth;
}
while (i != datapos);
maxval = newmaxval;
drawscale();
}
void chart::scroll()
{
SDL_Rect dst;
/* this doesnt seem to work.
dst.x = -tickwidth;
dst.y = 0;
SDL_BlitSurface(bg, NULL, bg, &dst);
// */
datapos = (datapos + 1) % datalen;
data[datapos] = 0;
redraw();
dst.x = w - tickwidth;
dst.y = 0;
dst.w = tickwidth;
dst.h = h;
SDL_FillRect(bg, &dst, 0);
}
void chart::copypacket(unsigned int src, unsigned int dst, unsigned int color, unsigned int size)
{
data[datapos] += size;
}
void chart::draw(int x, int y, SDL_Surface* s)
{
Uint32 now = SDL_GetTicks();
if (data[datapos] > maxval)
{
maxval = data[datapos];
redraw();
}
SDL_Rect r = {w-tickwidth, h - data[datapos] / maxval * h, tickwidth, data[datapos] / maxval * h};
SDL_FillRect(bg, &r, color);
if (now > lastupdate + tickinterval)
{
scroll();
lastupdate = now;
}
r.x = x;
r.y = y;
SDL_BlitSurface(bg, NULL, s, &r);
}
chart::~chart()
{
if (bg)
SDL_FreeSurface(bg);
}
void chart::drawscale()
{
//*
//conver to a per second value
float maxrate = maxval * spti;
int i;
for (i = 1; i < PREFIXCOUNT-1; i++)
if (maxrate < prefixes[i].value)
break;
i--;
float scalemax = maxrate / prefixes[i].value;
//draw 2 lines
float line1 = (int)scalemax;
if (h - h * (line1/scalemax) < text.getH()) //is there enough room?
line1 = (int)((float)(h-text.getH()) / h * scalemax);
float line2 = line1/2;
gui::horizLine(0, w, h - h * ((line1/scalemax)), 0x404040ff, bg);
gui::horizLine(0, w, h - h * ((line2/scalemax)), 0x404040ff, bg);
int tw, th;
stringstream s, ss;
s << line1 << " " << prefixes[i].prefix << unit;
text.getSize(s.str().c_str(), tw, th);
text.render(bg, s.str().c_str(), w - tw, h - h * ((line1/scalemax))-th, 10);
ss << line2 << " " << prefixes[i].prefix << unit;
text.getSize(ss.str().c_str(), tw, th);
text.render(bg, ss.str().c_str(), w - tw, h - h * ((line2/scalemax))-th,10);
// */
}