From 7ad855348fe3b89da298c49b958bf54123dcd911 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 23 Jun 2013 15:05:52 +1000 Subject: [PATCH] Implemented a simpler frame rate system --- client/in_world.cpp | 7 ++++++- client/in_world.hpp | 3 ++- libs/Codebase/frame_rate.cpp | 24 +++++++++++++++++++++--- libs/Codebase/frame_rate.hpp | 24 ++---------------------- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/client/in_world.cpp b/client/in_world.cpp index 39a0d3d..8da6c36 100644 --- a/client/in_world.cpp +++ b/client/in_world.cpp @@ -34,6 +34,7 @@ InWorld::InWorld() { cout << "entering InWorld" << endl; #endif cout << "Client Index: " << infoMgr->GetClientIndex() << endl; + font.SetSurface(surfaceMgr->Get("font")); } InWorld::~InWorld() { @@ -59,7 +60,11 @@ void InWorld::FrameEnd() { } void InWorld::Render(SDL_Surface* const screen) { - // + ClockFrameRate(); + + //since we're using this twice, make a tmp var + string fps = itos(GetFrameRate()); + font.DrawStringTo(fps, screen, screen->w - fps.size() * font.GetCharW(), 0); } //------------------------- diff --git a/client/in_world.hpp b/client/in_world.hpp index d363cff..1c90f41 100644 --- a/client/in_world.hpp +++ b/client/in_world.hpp @@ -35,6 +35,7 @@ #include "udp_network_utility.hpp" #include "button.hpp" #include "raster_font.hpp" +#include "frame_rate.hpp" class InWorld : public BaseScene { public: @@ -70,7 +71,7 @@ protected: InformationManager* infoMgr = ServiceLocator::Get(); //members - //... + RasterFont font; }; #endif diff --git a/libs/Codebase/frame_rate.cpp b/libs/Codebase/frame_rate.cpp index e0be7a7..0b855d3 100644 --- a/libs/Codebase/frame_rate.cpp +++ b/libs/Codebase/frame_rate.cpp @@ -21,6 +21,24 @@ */ #include "frame_rate.hpp" -int FrameRate::frameCount = 0; -int FrameRate::lastFrameRate = 0; -FrameRate::Clock::time_point FrameRate::tick = FrameRate::Clock::now(); +#include + +typedef std::chrono::high_resolution_clock Clock; + +static int frameCount = 0; +static int lastFrameRate = 0; +static Clock::time_point tick = Clock::now(); + +int ClockFrameRate() { + frameCount++; + if (Clock::now() - tick >= std::chrono::duration(1)) { + lastFrameRate = frameCount; + frameCount = 0; + tick = Clock::now(); + } + return lastFrameRate; +} + +int GetFrameRate() { + return lastFrameRate; +} diff --git a/libs/Codebase/frame_rate.hpp b/libs/Codebase/frame_rate.hpp index 078139a..8f0a920 100644 --- a/libs/Codebase/frame_rate.hpp +++ b/libs/Codebase/frame_rate.hpp @@ -22,27 +22,7 @@ #ifndef FRAMERATE_HPP_ #define FRAMERATE_HPP_ -#include - -class FrameRate { -public: - typedef std::chrono::high_resolution_clock Clock; - - FrameRate() = delete; - static int Calculate() { - frameCount++; - if (Clock::now() - tick >= std::chrono::duration(1)) { - lastFrameRate = frameCount; - frameCount = 0; - tick = Clock::now(); - } - return lastFrameRate; - } - static int GetFrameRate() { return lastFrameRate; } -private: - static int frameCount; - static int lastFrameRate; - static Clock::time_point tick; -}; +int ClockFrameRate(); +int GetFrameRate(); #endif