From 613e0e628b669ae9aaf6fda4be56cf3b2a899893 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Thu, 31 Jul 2008 14:30:50 +0000 Subject: better fps counter --- src/client/view.cc | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'src/client') diff --git a/src/client/view.cc b/src/client/view.cc index 41cdd80..f72a149 100644 --- a/src/client/view.cc +++ b/src/client/view.cc @@ -38,7 +38,9 @@ core::Cvar *ui_pointerhovercolor =0; namespace view { -float fps = 0; +const size_t fps_counter_size = 25; // fps is the average of 20 frames +float fps_counter_time[fps_counter_size]; +size_t fps_counter_index = 0; void init() { @@ -58,6 +60,9 @@ void init() ui_pointerhovercolor->set_info("[r g b] mouse pointer hover color"); targets::init(); + + for (size_t i =0; i < fps_counter_size; i++) + fps_counter_time[i] = 0.0f; } void shutdown() @@ -93,16 +98,36 @@ void draw_status() using namespace render; std::stringstream status; + + int minutes = (int) floorf(core::application()->time() / 60.0f); + int seconds = (int) floorf(core::application()->time() - (float) minutes* 60.0f); + + status << "^Ntime ^B" << std::setfill(' ') << std::setw(3) << minutes << ":" << std::setfill('0') << std::setw(2) << seconds; + Text::draw(video::width-Text::fontwidth()*11-4, 4, status); + if (core::game()) { int minutes = (int) floorf(core::game()->clientframetime() / 60.0f); int seconds = (int) floorf( core::game()->clientframetime() - (float) minutes* 60.0f); - status << "^Ntime ^B" << std::setfill(' ') << std::setw(3) << minutes << ":" << std::setfill('0') << std::setw(2) << seconds; - Text::draw(video::width-Text::fontwidth()*11-4, 4, status); + status << "^Ngame ^B" << std::setfill(' ') << std::setw(3) << minutes << ":" << std::setfill('0') << std::setw(2) << seconds; + Text::draw(video::width-Text::fontwidth()*11-4, Text::fontheight()+ 4, status); } // print stats if desired if (draw_stats && draw_stats->value()) { + // average fps + fps_counter_time[fps_counter_index] = core::application()->time(); + fps_counter_index = (fps_counter_index + 1 ) % fps_counter_size; + float min_time = core::application()->time(); + for (size_t i=0; i < fps_counter_size; i++) + if (fps_counter_time[i] < min_time) + min_time = fps_counter_time[i]; + float fps = 0.0f; + float t = (core::application()->time() - min_time); + if (t > 0) { + fps = roundf(((float) fps_counter_size - 1.0f) / t); + } + std::stringstream stats; stats << "^Nfps ^B" << std::setw(6) << fps << "\n"; if (core::application()->connected()) { @@ -331,12 +356,6 @@ void frame(float seconds) { using namespace render; - // calculate frames per second - if (seconds > 0.0f) - fps = floorf(1.0f / seconds); - else - fps = 9999; - // flush console messages console()->flush(); -- cgit v1.2.3