diff --git a/pandatool/src/pstatserver/pStatStripChart.cxx b/pandatool/src/pstatserver/pStatStripChart.cxx index 008362f036..d479d73ba4 100644 --- a/pandatool/src/pstatserver/pStatStripChart.cxx +++ b/pandatool/src/pstatserver/pStatStripChart.cxx @@ -197,15 +197,8 @@ set_auto_vertical_scale() { thread_data->get_frame_number_at_time(time, frame_number); if (thread_data->has_frame(frame_number)) { - const FrameData &frame = get_frame_data(frame_number); - - float overall_value = 0.0; - FrameData::const_iterator fi; - for (fi = frame.begin(); fi != frame.end(); ++fi) { - const ColorData &cd = (*fi); - overall_value += cd._net_value; - } - max_value = max(max_value, overall_value); + float net_value = get_net_value(frame_number); + max_value = max(max_value, net_value); } } @@ -352,6 +345,49 @@ get_frame_data(int frame_number) { return data; } +//////////////////////////////////////////////////////////////////// +// Function: PStatStripChart::get_net_value +// Access: Protected +// Description: Returns the net value of the chart's collector for +// the indicated fraem number. +//////////////////////////////////////////////////////////////////// +float PStatStripChart:: +get_net_value(int frame_number) const { + const FrameData &frame = + ((PStatStripChart *)this)->get_frame_data(frame_number); + + float net_value = 0.0; + FrameData::const_iterator fi; + for (fi = frame.begin(); fi != frame.end(); ++fi) { + const ColorData &cd = (*fi); + net_value += cd._net_value; + } + + return net_value; +} + +//////////////////////////////////////////////////////////////////// +// Function: PStatStripChart::get_average_net_value +// Access: Protected +// Description: Computes the average value of the chart's collector +// over the past indicated number of seconds. +//////////////////////////////////////////////////////////////////// +float PStatStripChart:: +get_average_net_value(float time) const { + const PStatThreadData *thread_data = _view.get_thread_data(); + int now_i, then_i; + if (!thread_data->get_elapsed_frames(then_i, now_i, time)) { + return 0.0f; + } + + float net_value = 0.0f; + for (int frame_number = then_i; frame_number <= now_i; frame_number++) { + net_value += get_net_value(frame_number); + } + int num_frames = now_i - then_i + 1; + return net_value / (float)num_frames; +} + //////////////////////////////////////////////////////////////////// // Function: PStatStripChart::changed_size // Access: Protected diff --git a/pandatool/src/pstatserver/pStatStripChart.h b/pandatool/src/pstatserver/pStatStripChart.h index 27a2965fa7..d6c844c608 100644 --- a/pandatool/src/pstatserver/pStatStripChart.h +++ b/pandatool/src/pstatserver/pStatStripChart.h @@ -86,6 +86,8 @@ protected: typedef pmap Data; const FrameData &get_frame_data(int frame_number); + float get_net_value(int frame_number) const; + float get_average_net_value(float time = 3.0) const; void changed_size(int xsize, int ysize); void force_redraw(); diff --git a/pandatool/src/pstatserver/pStatThreadData.cxx b/pandatool/src/pstatserver/pStatThreadData.cxx index 5fa8034407..22a5ae6e40 100644 --- a/pandatool/src/pstatserver/pStatThreadData.cxx +++ b/pandatool/src/pstatserver/pStatThreadData.cxx @@ -229,6 +229,56 @@ get_latest_frame() const { return *_frames.back(); } +//////////////////////////////////////////////////////////////////// +// Function: PStatThreadData::get_elapsed_frames +// Access: Public +// Description: Computes the oldest frame number not older than time +// seconds, and the newest frame number. Handy for +// computing average frame rate over a time. Returns +// true if there is any data in that range, false +// otherwise. +//////////////////////////////////////////////////////////////////// +bool PStatThreadData:: +get_elapsed_frames(int &then_i, int &now_i, float time) const { + if (_frames.empty()) { + // No frames in the data at all. + return false; + } + + now_i = _frames.size() - 1; + while (now_i > 0 && _frames[now_i] == (PStatFrameData *)NULL) { + now_i--; + } + if (now_i < 0) { + // No frames have any real data. + return false; + } + nassertr(_frames[now_i] != (PStatFrameData *)NULL, false); + + float now = _frames[now_i]->get_end(); + float then = now - time; + + int old_i = now_i; + then_i = now_i; + + while (old_i >= 0) { + const PStatFrameData *frame = _frames[old_i]; + if (frame != (PStatFrameData *)NULL) { + if (frame->get_start() > then) { + then_i = old_i; + } else { + break; + } + } + old_i--; + } + + nassertr(then_i >= 0, false); + nassertr(_frames[then_i] != (PStatFrameData *)NULL, false); + + return true; +} + //////////////////////////////////////////////////////////////////// // Function: PStatThreadData::get_frame_rate // Access: Public @@ -238,45 +288,14 @@ get_latest_frame() const { //////////////////////////////////////////////////////////////////// float PStatThreadData:: get_frame_rate(float time) const { - if (_frames.empty()) { - // No frames in the data at all; nothing to base the frame rate - // on. - return 0.0; + int then_i, now_i; + if (!get_elapsed_frames(then_i, now_i, time)) { + return 0.0f; } - int now_i = _frames.size() - 1; - while (now_i > 0 && _frames[now_i] == (PStatFrameData *)NULL) { - now_i--; - } - if (now_i < 0) { - // No frames have any real data. - return 0.0; - } - nassertr(_frames[now_i] != (PStatFrameData *)NULL, 0.0); - + int num_frames = now_i - then_i + 1; float now = _frames[now_i]->get_end(); - float then = now - time; - - int then_i = now_i; - int last_good_i = now_i; - - while (then_i >= 0) { - const PStatFrameData *frame = _frames[then_i]; - if (frame != (PStatFrameData *)NULL) { - if (frame->get_start() > then) { - last_good_i = then_i; - } else { - break; - } - } - then_i--; - } - - nassertr(last_good_i >= 0, 0.0); - nassertr(_frames[last_good_i] != (PStatFrameData *)NULL, 0.0); - - int num_frames = now_i - last_good_i + 1; - return (float)num_frames / (now - _frames[last_good_i]->get_start()); + return (float)num_frames / (now - _frames[then_i]->get_start()); } diff --git a/pandatool/src/pstatserver/pStatThreadData.h b/pandatool/src/pstatserver/pStatThreadData.h index 591aeaf6cf..756593be91 100644 --- a/pandatool/src/pstatserver/pStatThreadData.h +++ b/pandatool/src/pstatserver/pStatThreadData.h @@ -60,6 +60,7 @@ public: const PStatFrameData &get_latest_frame() const; + bool get_elapsed_frames(int &then_i, int &now_i, float time) const; float get_frame_rate(float time = 3.0) const; diff --git a/pandatool/src/win-stats/winStatsStripChart.cxx b/pandatool/src/win-stats/winStatsStripChart.cxx index def32f6f5d..1fb8837b71 100644 --- a/pandatool/src/win-stats/winStatsStripChart.cxx +++ b/pandatool/src/win-stats/winStatsStripChart.cxx @@ -43,7 +43,7 @@ WinStatsStripChart(WinStatsMonitor *monitor, int thread_index, _left_margin = 96; _right_margin = 32; - _top_margin = 8; + _top_margin = 16; _bottom_margin = 8; // 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) { if (!_pause) { update(); + + string text = format_number(get_average_net_value(), get_guide_bar_units(), get_guide_bar_unit_name()); + if (_net_value_text != text) { + _net_value_text = text; + RECT rect; + GetClientRect(_window, &rect); + rect.bottom = _top_margin; + InvalidateRect(_window, &rect, TRUE); + } } } @@ -137,6 +146,10 @@ set_time_units(int unit_mask) { GetClientRect(_window, &rect); rect.left = _right_margin; InvalidateRect(_window, &rect, TRUE); + + GetClientRect(_window, &rect); + rect.bottom = _top_margin; + InvalidateRect(_window, &rect, TRUE); } } @@ -503,14 +516,21 @@ additional_window_paint(HDC hdc) { last_y = draw_guide_label(hdc, x, get_guide_bar(i), last_y); } + GuideBar top_value = make_guide_bar(get_vertical_scale()); + draw_guide_label(hdc, x, top_value, last_y); + last_y = -100; int num_user_guide_bars = get_num_user_guide_bars(); for (i = 0; i < num_user_guide_bars; i++) { last_y = draw_guide_label(hdc, x, get_user_guide_bar(i), last_y); } - GuideBar top_value = make_guide_bar(get_vertical_scale()); - draw_guide_label(hdc, x, top_value, last_y); + // Now draw the "net value" label at the top. + SetTextAlign(hdc, TA_RIGHT | TA_BOTTOM); + SetTextColor(hdc, RGB(0, 0, 0)); + TextOut(hdc, rect.right - _right_margin, _top_margin, + _net_value_text.data(), _net_value_text.length()); + } //////////////////////////////////////////////////////////////////// diff --git a/pandatool/src/win-stats/winStatsStripChart.h b/pandatool/src/win-stats/winStatsStripChart.h index dc56595445..92a1b96d54 100644 --- a/pandatool/src/win-stats/winStatsStripChart.h +++ b/pandatool/src/win-stats/winStatsStripChart.h @@ -75,6 +75,7 @@ private: static LONG WINAPI static_window_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam); int _brush_origin; + string _net_value_text; static bool _window_class_registered; static const char * const _window_class_name;