Here is the main loop that I currently have (and it does work perfectly):
// in another header:
...
using i64u = unsigned long long;
...
// mainloop:
i64u lastTime = static_cast<const i64u>(SDL_GetPerformanceCounter());
double accumulator = 0.0;
double t = 0.0;
double dt = 1.0 / 30.0;
SDL_Event e;
while (IsRunning())
{
bool bRet;
bRet = SDL_PollEvent(&e) == SDL_TRUE;
if (bRet)
{
if (SDL::ProcessMessages(e))
{
}
else
{
IsRunning() = false;
break;
}
}
else
{
}
bool render = false;
const i64u now = static_cast<const i64u>(SDL_GetPerformanceCounter());
const double frameTime = static_cast<const double>((now - lastTime) * 1000) / SDL_GetPerformanceFrequency();
accumulator += (frameTime / 1000);
while (accumulator >= dt)
{
if (bRet)
{
if (this->Game().Get()->InputDevice().Get()->ProcessMessages(e))
{
}
else
{
}
}
else
{
}
// side-note: should OnUpdate go here?
// or remain outside of this loop and keep passing "frameTime"?
t += dt;
accumulator -= dt;
render = true;
}
this->Game().Get()->OnUpdate(frameTime);
lastTime = now;
if (render == true)
{
}
else
{
}
this->Game().Get()->OnDraw();
SDL::SwapWindow();
}
...
bool& <App>::IsRunning()
{
return m_Running;
}
const bool <App>::IsRunning() const
{
return m_Running;
}
My question is: How should (or rather, how does it normally tend to be implemented) a frame be determined?
For example, should lastTime be set after the OnDraw(), or should it be set before OnDraw() but AFTER the the logic and input has been determined (after OnUpdate())?
What truly constitutes "a frame"? In my mind, a frame would constitute as "everything being prepared in order to be drawn", which I think of as "get the input, calculate physics and other game logic data, and then once that "frame" is finished, draw the "frame" - though I'm not really sure how this is (or might) done in the real world.
Is there even "a right or wrong way" when implementing this kind of loop? I would imagine so.
What would y'all recommend?
(Also, ignore the trailing if statements; that's some stuff to help me keep track of what's going on.)
lastTime = now;) right afterframeTimeis calculated, while I did so after the game finished updating, and was curious whether there are any reasons why one way may or may not be any better than the other. I guess it doesn't matter too much since yeah I'm not having any issues with the code. \$\endgroup\$