Original Post
So I have a game that I wrote in C++ with SDL. I originally wrote the main game loop with a form of busy waiting, since that was the easiest thing to do. However, now I'm wondering whether I should switch to some other form.
Basically, the way it goes is that it loops repeatedly, checking for input and processing it, until it detects that enough time has elapsed for the next frame. If it's behind schedule, it then attempts to catch up with a second frame, before going back to the input processing part. The game is intended to run at 62.5FPS (16ms per frame)
The problem is that this uses a lot of computer resources, even when the game is paused. The only alternative I can think of is to add threading, and make the thread sleep periodically. However, multithreading is relatively difficult in C++. Also, another worry I have is that the sleep function has large inaccuracies. So if I rely on it to time my game loop, then there might be large variations in timing between frames.
What should I do? What do you recommend?
Basically, the way it goes is that it loops repeatedly, checking for input and processing it, until it detects that enough time has elapsed for the next frame. If it's behind schedule, it then attempts to catch up with a second frame, before going back to the input processing part. The game is intended to run at 62.5FPS (16ms per frame)
The problem is that this uses a lot of computer resources, even when the game is paused. The only alternative I can think of is to add threading, and make the thread sleep periodically. However, multithreading is relatively difficult in C++. Also, another worry I have is that the sleep function has large inaccuracies. So if I rely on it to time my game loop, then there might be large variations in timing between frames.
What should I do? What do you recommend?
void GameLoop()
{
Debug("Game loop begun");
Uint32 lastphysicstime = SDL_GetTicks();
Uint32 newtime = 0;
while(!hasquit)
{
ProcessInput();
newtime = SDL_GetTicks();
//The physics happens at a set framerate
if ((newtime - lastphysicstime)>=MSPERPHYSICFRAME)
{
PhysicsTick(Paused);
RenderEverything(screen);
lastphysicstime += MSPERPHYSICFRAME;
}
if ((newtime - lastphysicstime)>=MSPERPHYSICFRAME)
{
PhysicsTick(Paused);
RenderEverything(screen);
lastphysicstime = SDL_GetTicks();
}
}
Debug("Gameloop returned");
}