Skip to content

Commit bb2ee7c

Browse files
committed
average net value across the top
1 parent 92c4dd2 commit bb2ee7c

File tree

6 files changed

+113
-34
lines changed

6 files changed

+113
-34
lines changed

pandatool/src/pstatserver/pStatStripChart.cxx

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,8 @@ set_auto_vertical_scale() {
197197
thread_data->get_frame_number_at_time(time, frame_number);
198198

199199
if (thread_data->has_frame(frame_number)) {
200-
const FrameData &frame = get_frame_data(frame_number);
201-
202-
float overall_value = 0.0;
203-
FrameData::const_iterator fi;
204-
for (fi = frame.begin(); fi != frame.end(); ++fi) {
205-
const ColorData &cd = (*fi);
206-
overall_value += cd._net_value;
207-
}
208-
max_value = max(max_value, overall_value);
200+
float net_value = get_net_value(frame_number);
201+
max_value = max(max_value, net_value);
209202
}
210203
}
211204

@@ -352,6 +345,49 @@ get_frame_data(int frame_number) {
352345
return data;
353346
}
354347

348+
////////////////////////////////////////////////////////////////////
349+
// Function: PStatStripChart::get_net_value
350+
// Access: Protected
351+
// Description: Returns the net value of the chart's collector for
352+
// the indicated fraem number.
353+
////////////////////////////////////////////////////////////////////
354+
float PStatStripChart::
355+
get_net_value(int frame_number) const {
356+
const FrameData &frame =
357+
((PStatStripChart *)this)->get_frame_data(frame_number);
358+
359+
float net_value = 0.0;
360+
FrameData::const_iterator fi;
361+
for (fi = frame.begin(); fi != frame.end(); ++fi) {
362+
const ColorData &cd = (*fi);
363+
net_value += cd._net_value;
364+
}
365+
366+
return net_value;
367+
}
368+
369+
////////////////////////////////////////////////////////////////////
370+
// Function: PStatStripChart::get_average_net_value
371+
// Access: Protected
372+
// Description: Computes the average value of the chart's collector
373+
// over the past indicated number of seconds.
374+
////////////////////////////////////////////////////////////////////
375+
float PStatStripChart::
376+
get_average_net_value(float time) const {
377+
const PStatThreadData *thread_data = _view.get_thread_data();
378+
int now_i, then_i;
379+
if (!thread_data->get_elapsed_frames(then_i, now_i, time)) {
380+
return 0.0f;
381+
}
382+
383+
float net_value = 0.0f;
384+
for (int frame_number = then_i; frame_number <= now_i; frame_number++) {
385+
net_value += get_net_value(frame_number);
386+
}
387+
int num_frames = now_i - then_i + 1;
388+
return net_value / (float)num_frames;
389+
}
390+
355391
////////////////////////////////////////////////////////////////////
356392
// Function: PStatStripChart::changed_size
357393
// Access: Protected

pandatool/src/pstatserver/pStatStripChart.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ class PStatStripChart : public PStatGraph {
8686
typedef pmap<int, FrameData> Data;
8787

8888
const FrameData &get_frame_data(int frame_number);
89+
float get_net_value(int frame_number) const;
90+
float get_average_net_value(float time = 3.0) const;
8991

9092
void changed_size(int xsize, int ysize);
9193
void force_redraw();

pandatool/src/pstatserver/pStatThreadData.cxx

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -230,53 +230,72 @@ get_latest_frame() const {
230230
}
231231

232232
////////////////////////////////////////////////////////////////////
233-
// Function: PStatThreadData::get_frame_rate
233+
// Function: PStatThreadData::get_elapsed_frames
234234
// Access: Public
235-
// Description: Computes the average frame rate over the past number
236-
// of seconds, by counting up the number of frames
237-
// elapsed in that time interval.
235+
// Description: Computes the oldest frame number not older than time
236+
// seconds, and the newest frame number. Handy for
237+
// computing average frame rate over a time. Returns
238+
// true if there is any data in that range, false
239+
// otherwise.
238240
////////////////////////////////////////////////////////////////////
239-
float PStatThreadData::
240-
get_frame_rate(float time) const {
241+
bool PStatThreadData::
242+
get_elapsed_frames(int &then_i, int &now_i, float time) const {
241243
if (_frames.empty()) {
242-
// No frames in the data at all; nothing to base the frame rate
243-
// on.
244-
return 0.0;
244+
// No frames in the data at all.
245+
return false;
245246
}
246247

247-
int now_i = _frames.size() - 1;
248+
now_i = _frames.size() - 1;
248249
while (now_i > 0 && _frames[now_i] == (PStatFrameData *)NULL) {
249250
now_i--;
250251
}
251252
if (now_i < 0) {
252253
// No frames have any real data.
253-
return 0.0;
254+
return false;
254255
}
255-
nassertr(_frames[now_i] != (PStatFrameData *)NULL, 0.0);
256+
nassertr(_frames[now_i] != (PStatFrameData *)NULL, false);
256257

257258
float now = _frames[now_i]->get_end();
258259
float then = now - time;
259260

260-
int then_i = now_i;
261-
int last_good_i = now_i;
261+
int old_i = now_i;
262+
then_i = now_i;
262263

263-
while (then_i >= 0) {
264-
const PStatFrameData *frame = _frames[then_i];
264+
while (old_i >= 0) {
265+
const PStatFrameData *frame = _frames[old_i];
265266
if (frame != (PStatFrameData *)NULL) {
266267
if (frame->get_start() > then) {
267-
last_good_i = then_i;
268+
then_i = old_i;
268269
} else {
269270
break;
270271
}
271272
}
272-
then_i--;
273+
old_i--;
273274
}
274275

275-
nassertr(last_good_i >= 0, 0.0);
276-
nassertr(_frames[last_good_i] != (PStatFrameData *)NULL, 0.0);
276+
nassertr(then_i >= 0, false);
277+
nassertr(_frames[then_i] != (PStatFrameData *)NULL, false);
278+
279+
return true;
280+
}
277281

278-
int num_frames = now_i - last_good_i + 1;
279-
return (float)num_frames / (now - _frames[last_good_i]->get_start());
282+
////////////////////////////////////////////////////////////////////
283+
// Function: PStatThreadData::get_frame_rate
284+
// Access: Public
285+
// Description: Computes the average frame rate over the past number
286+
// of seconds, by counting up the number of frames
287+
// elapsed in that time interval.
288+
////////////////////////////////////////////////////////////////////
289+
float PStatThreadData::
290+
get_frame_rate(float time) const {
291+
int then_i, now_i;
292+
if (!get_elapsed_frames(then_i, now_i, time)) {
293+
return 0.0f;
294+
}
295+
296+
int num_frames = now_i - then_i + 1;
297+
float now = _frames[now_i]->get_end();
298+
return (float)num_frames / (now - _frames[then_i]->get_start());
280299
}
281300

282301

pandatool/src/pstatserver/pStatThreadData.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class PStatThreadData : public ReferenceCount {
6060

6161
const PStatFrameData &get_latest_frame() const;
6262

63+
bool get_elapsed_frames(int &then_i, int &now_i, float time) const;
6364
float get_frame_rate(float time = 3.0) const;
6465

6566

pandatool/src/win-stats/winStatsStripChart.cxx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ WinStatsStripChart(WinStatsMonitor *monitor, int thread_index,
4343

4444
_left_margin = 96;
4545
_right_margin = 32;
46-
_top_margin = 8;
46+
_top_margin = 16;
4747
_bottom_margin = 8;
4848

4949
// Let's show the units on the guide bar labels. There's room.
@@ -93,6 +93,15 @@ new_data(int thread_index, int frame_number) {
9393

9494
if (!_pause) {
9595
update();
96+
97+
string text = format_number(get_average_net_value(), get_guide_bar_units(), get_guide_bar_unit_name());
98+
if (_net_value_text != text) {
99+
_net_value_text = text;
100+
RECT rect;
101+
GetClientRect(_window, &rect);
102+
rect.bottom = _top_margin;
103+
InvalidateRect(_window, &rect, TRUE);
104+
}
96105
}
97106
}
98107

@@ -137,6 +146,10 @@ set_time_units(int unit_mask) {
137146
GetClientRect(_window, &rect);
138147
rect.left = _right_margin;
139148
InvalidateRect(_window, &rect, TRUE);
149+
150+
GetClientRect(_window, &rect);
151+
rect.bottom = _top_margin;
152+
InvalidateRect(_window, &rect, TRUE);
140153
}
141154
}
142155

@@ -503,14 +516,21 @@ additional_window_paint(HDC hdc) {
503516
last_y = draw_guide_label(hdc, x, get_guide_bar(i), last_y);
504517
}
505518

519+
GuideBar top_value = make_guide_bar(get_vertical_scale());
520+
draw_guide_label(hdc, x, top_value, last_y);
521+
506522
last_y = -100;
507523
int num_user_guide_bars = get_num_user_guide_bars();
508524
for (i = 0; i < num_user_guide_bars; i++) {
509525
last_y = draw_guide_label(hdc, x, get_user_guide_bar(i), last_y);
510526
}
511527

512-
GuideBar top_value = make_guide_bar(get_vertical_scale());
513-
draw_guide_label(hdc, x, top_value, last_y);
528+
// Now draw the "net value" label at the top.
529+
SetTextAlign(hdc, TA_RIGHT | TA_BOTTOM);
530+
SetTextColor(hdc, RGB(0, 0, 0));
531+
TextOut(hdc, rect.right - _right_margin, _top_margin,
532+
_net_value_text.data(), _net_value_text.length());
533+
514534
}
515535

516536
////////////////////////////////////////////////////////////////////

pandatool/src/win-stats/winStatsStripChart.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ class WinStatsStripChart : public PStatStripChart, public WinStatsGraph {
7575
static LONG WINAPI static_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
7676

7777
int _brush_origin;
78+
string _net_value_text;
7879

7980
static bool _window_class_registered;
8081
static const char * const _window_class_name;

0 commit comments

Comments
 (0)