Timestep tweaks and increased text rendering quality
This commit is contained in:
parent
b20622e4ce
commit
b1b4865444
|
@ -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
|
||||
|
|
|
@ -1,22 +1,26 @@
|
|||
#pragma once
|
||||
|
||||
#include <birb2d/Renderwindow.hpp>
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ namespace Birb
|
|||
|
||||
namespace RenderVars
|
||||
{
|
||||
static int RefreshRate = 60;
|
||||
static int RefreshRate = 240;
|
||||
static SDL_Window* MainWindow;
|
||||
static SDL_Renderer* Renderer;
|
||||
}
|
||||
|
|
21
src/math.cpp
21
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -1,21 +1,26 @@
|
|||
#include <SDL2/SDL.h>
|
||||
#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);
|
||||
//}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue