From 553f8dbfa522388ac48a776895b3b3a3245aadc1 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 6 Apr 2014 20:53:51 +1000 Subject: [PATCH] Fixed some framerate issues --- client/client_application.cpp | 11 +++----- client/scenes/in_world.cpp | 10 ++++++++ client/scenes/in_world.hpp | 3 +++ common/frame_rate.hpp | 48 +++++++++++++++++++++++++++++++++++ 4 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 common/frame_rate.hpp diff --git a/client/client_application.cpp b/client/client_application.cpp index 2bf9203..c8603b0 100644 --- a/client/client_application.cpp +++ b/client/client_application.cpp @@ -62,7 +62,7 @@ void ClientApplication::Init() { if (SDL_Init(SDL_INIT_VIDEO)) { throw(std::runtime_error("Failed to initialize SDL")); } - BaseScene::SetScreen(config.Int("screen.w"), config.Int("screen.h"), 0, (config.Bool("screen.f")) ? SDL_HWSURFACE|SDL_DOUBLEBUF : SDL_HWSURFACE); + BaseScene::SetScreen(config.Int("screen.w"), config.Int("screen.h"), 0, (config.Bool("screen.f")) ? SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN : SDL_HWSURFACE|SDL_DOUBLEBUF); //initialize SDL_net if (SDLNet_Init()) { @@ -77,7 +77,7 @@ void ClientApplication::Proc() { //prepare the time system typedef std::chrono::steady_clock Clock; - Clock::duration delta(16 * Clock::duration::period::den / std::milli::den); + std::chrono::duration delta(16); Clock::time_point simTime = Clock::now(); Clock::time_point realTime; @@ -95,15 +95,12 @@ void ClientApplication::Proc() { //simulate game time while (simTime < realTime) { //call each user defined function - activeScene->RunFrame(double(delta.count()) / Clock::duration::period::den); + activeScene->RunFrame(double(delta.count()) / std::chrono::duration::period::den); simTime += delta; } //draw the game to the screen activeScene->RenderFrame(); - - //give the computer a break - SDL_Delay(10); } UnloadScene(); @@ -121,7 +118,6 @@ void ClientApplication::Quit() { void ClientApplication::LoadScene(SceneList sceneIndex) { UnloadScene(); - switch(sceneIndex) { //add scene creation calls here case SceneList::FIRST: @@ -143,7 +139,6 @@ void ClientApplication::LoadScene(SceneList sceneIndex) { case SceneList::INCOMBAT: activeScene = new InCombat(); break; - default: throw(std::logic_error("Failed to recognize the scene index")); } diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 92eacc1..2f9cf60 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -133,6 +133,12 @@ void InWorld::FrameEnd() { // } +void InWorld::RenderFrame() { +// SDL_FillRect(GetScreen(), 0, 0); + Render(GetScreen()); + SDL_Flip(GetScreen()); +} + void InWorld::Render(SDL_Surface* const screen) { //draw the map ForNearbyRegions([&](Region* const region) { @@ -147,6 +153,10 @@ void InWorld::Render(SDL_Surface* const screen) { //draw UI disconnectButton.DrawTo(screen); shutDownButton.DrawTo(screen); + + font.DrawStringTo(to_string_custom(fps.GetFrameRate()), screen, 0, 0); + + fps.Calculate(); } //------------------------- diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index b7aa50d..41c9082 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -40,6 +40,7 @@ //common #include "config_utility.hpp" +#include "frame_rate.hpp" //client #include "base_scene.hpp" @@ -60,6 +61,7 @@ protected: void FrameStart(); void Update(double delta); void FrameEnd(); + void RenderFrame(); void Render(SDL_Surface* const); //Event handlers @@ -90,6 +92,7 @@ protected: //globals ConfigUtility& config; + FrameRate fps; UDPNetworkUtility& network; int& clientIndex; diff --git a/common/frame_rate.hpp b/common/frame_rate.hpp new file mode 100644 index 0000000..f9b0a6d --- /dev/null +++ b/common/frame_rate.hpp @@ -0,0 +1,48 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef FRAMERATE_HPP_ +#define FRAMERATE_HPP_ + +#include + +class FrameRate { +public: + typedef std::chrono::high_resolution_clock Clock; + + FrameRate() = default; + int Calculate() { + frameCount++; + if (Clock::now() - tick >= std::chrono::duration(1)) { + lastFrameRate = frameCount; + frameCount = 0; + tick = Clock::now(); + } + return lastFrameRate; + } + int GetFrameRate() { return lastFrameRate; } +private: + int frameCount = 0; + int lastFrameRate = 0; + Clock::time_point tick = Clock::now(); +}; + +#endif