From b1b486544459217ab975aeef12268687afc26964 Mon Sep 17 00:00:00 2001 From: Toasterbirb Date: Mon, 14 Feb 2022 17:29:17 +0200 Subject: [PATCH] Timestep tweaks and increased text rendering quality --- include/Math.hpp | 3 +++ include/Timestep.hpp | 14 +++++++++----- include/Values.hpp | 2 +- src/math.cpp | 21 +++++++++++++++++++++ src/renderwindow.cpp | 5 +++-- src/timestep.cpp | 25 +++++++++++++++---------- 6 files changed, 52 insertions(+), 18 deletions(-) diff --git a/include/Math.hpp b/include/Math.hpp index f9cb401..b3022ff 100644 --- a/include/Math.hpp +++ b/include/Math.hpp @@ -241,6 +241,9 @@ namespace Birb static float VectorDistance(Vector3f a, Vector3f b); ///< Calculate the distance between two 3D floating point vectors static float VectorDistance(Vector3int a, Vector3int b); ///< Calculate the distance between two 3D integer vectors + static float Clamp(float value, float min, float max); ///< Clamp a float between the given values + static int Clamp(int value, int min, int max); ///< Clamp an integer between the given values + static int Lerp(int a, int b, float t); ///< Interpolate a value between two values given time t static float Lerp(float a, float b, float t); ///< Interpolate a value between two values given time t static Vector2f Lerp(Vector2f a, Vector2f b, float t); ///< Interpolate a point between two 2D floating point vectors given time t diff --git a/include/Timestep.hpp b/include/Timestep.hpp index 6ef97a5..b632f56 100644 --- a/include/Timestep.hpp +++ b/include/Timestep.hpp @@ -1,22 +1,26 @@ #pragma once +#include + namespace Birb { /// Limit the runtime speed and handle delta time struct TimeStep { /* Functions */ - void Init(); ///< Reset the currentTime variable + void Init(Window* p_mainWindow); ///< Reset the currentTime variable void Start(); ///< Start a new step. Call this in the beginning of the gameloop bool Running(); ///< Check if the accumulator is empty yet. While this is true, handle input events etc. void Step(); ///< Deplete the accumulator. Speed determined by the value of timeStep void End(); ///< End the timeStep and delay the rendering to stay within the refreshrate - float deltaTime(); ///< Method to get deltatime + //float deltaTime(); ///< Method to get deltatime /* Variables */ - const float timeStep = 0.01f; - float accumulator = 0.01f; - float currentTime; + Window* mainWindow; + const double timeStep = 0.01; + double accumulator = 0.00; + double currentTime; + float deltaTime; int startTick; }; } diff --git a/include/Values.hpp b/include/Values.hpp index 4f924f6..7e7a823 100644 --- a/include/Values.hpp +++ b/include/Values.hpp @@ -16,7 +16,7 @@ namespace Birb namespace RenderVars { - static int RefreshRate = 60; + static int RefreshRate = 240; static SDL_Window* MainWindow; static SDL_Renderer* Renderer; } diff --git a/src/math.cpp b/src/math.cpp index 5723b0b..1099956 100644 --- a/src/math.cpp +++ b/src/math.cpp @@ -22,14 +22,35 @@ namespace Birb return std::sqrt(std::pow(b.x - a.x, 2) + std::pow(b.y - a.y, 2) + std::pow(b.z - a.z, 2)); } + float Math::Clamp(float value, float min, float max) + { + if (value < min) + value = min; + else if (value > max) + value = max; + + return value; + } + + int Math::Clamp(int value, int min, int max) + { + if (value < min) + value = min; + else if (value > max) + value = max; + + return value; + } int Math::Lerp(int a, int b, float t) { + t = Clamp(t, 0.0, 1.0f); return std::round(a + (b - a) * t); } float Math::Lerp(float a, float b, float t) { + t = Clamp(t, 0.0, 1.0f); return (a + (b - a) * t); } diff --git a/src/renderwindow.cpp b/src/renderwindow.cpp index 1a0cf27..5a6e26e 100644 --- a/src/renderwindow.cpp +++ b/src/renderwindow.cpp @@ -23,7 +23,7 @@ namespace Birb renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED); /* Set some global rendering variables */ - Global::RenderVars::RefreshRate = p_refresh_rate; + Global::RenderVars::RefreshRate = refresh_rate; Global::RenderVars::MainWindow = win; Global::RenderVars::Renderer = renderer; } @@ -233,7 +233,8 @@ namespace Birb return NULL; } - SDL_Surface* surface = TTF_RenderText_Solid(font, text.c_str(), color); + //SDL_Surface* surface = TTF_RenderText_Solid(font, text.c_str(), color); + SDL_Surface* surface = TTF_RenderText_Blended(font, text.c_str(), color); if (surface == nullptr) Debug::Log("Error creating SDL_Surface. Text: " + (std::string)text + ". SDL Error: " + (std::string)SDL_GetError(), Debug::error); diff --git a/src/timestep.cpp b/src/timestep.cpp index 6ebda57..1261fe0 100644 --- a/src/timestep.cpp +++ b/src/timestep.cpp @@ -1,21 +1,26 @@ #include #include "Timestep.hpp" -#include "Utils.hpp" +//#include "Utils.hpp" #include "Values.hpp" namespace Birb { - void TimeStep::Init() + void TimeStep::Init(Window* p_mainWindow) { currentTime = utils::hireTimeInSeconds(); + mainWindow = p_mainWindow; } void TimeStep::Start() { + /* Start deltaTime timer */ + //deltaTimer.Start(); + startTick = SDL_GetTicks(); - float newTime = utils::hireTimeInSeconds(); - float frameTime = newTime - currentTime; + double newTime = utils::hireTimeInSeconds(); + double frameTime = newTime - currentTime; + deltaTime = frameTime; if (frameTime > 0.25f) frameTime = 0.25f; @@ -38,12 +43,12 @@ namespace Birb { int frameTicks = SDL_GetTicks() - startTick; - if (frameTicks < 1000 / Global::RenderVars::RefreshRate) - SDL_Delay(1000 / Global::RenderVars::RefreshRate - frameTicks); + if (frameTicks < 1000 / mainWindow->refresh_rate) + SDL_Delay(1000 / mainWindow->refresh_rate - frameTicks); } - float TimeStep::deltaTime() - { - return accumulator / timeStep; - } + //float TimeStep::deltaTime() + //{ + // return (accumulator / timeStep); + //} }