Hey everyone. I'm back again with this damn fixed time step loop. I've been messing with it some more and I want to know where I'm messing up (if I am) and what else am I missing. I think I understand the logic now for the most part and I wrote this code to test out the fixed step loop.
#include <SDL.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
cout << "Error starting SDL" << endl;
bool quit = false;
const uint64_t UPDATES_PER_SECOND = 33;
int positionX = 0;
uint64_t currentTime = 0;
uint64_t lastUpdate = 0;
uint64_t nextUpdate = 0;
int interpolationPercent = 0;
while (!quit)
{
currentTime = SDL_GetTicks();
while (currentTime - nextUpdate > UPDATES_PER_SECOND)
{
//Do update
positionX += 2;
nextUpdate += UPDATES_PER_SECOND;
cout << "CurrentTime: " << currentTime << endl;
cout << "NextUpdate: " << nextUpdate << endl;
cout << "AfterUpdate: " << positionX << endl;
}
interpolationPercent = ((currentTime - lastUpdate) / UPDATES_PER_SECOND) * 100;
cout << "Interpolation Percent: " << interpolationPercent << endl;
lastUpdate = currentTime;
}
SDL_Quit();
return 0;
};
I know I'm missing the cap to avoid the spiral of death but I just want to make sure everything else is correct in my implementation. I did notice that interpolation percent was always 0 so I'm not sure if it's because it's just running really fast or if it's a mistake on my part but it seems fishy to me.
Thanks.