Original Post
I have recently decided to use Qt for making my 2D game. All was going well until i started thinking about the game loop. Previously (when i used SDL), it was something like this:
I was also intending on using a different rate for graphics and physics updates, as this guy says.
But now with Qt, i don't have access to the main event loop, so i am not sure how to do this.
Some have suggested using a QTimer, but i don't like that approach for a number of reasons. Firstly, the resolution may not be enough for high frame rates. And if i separate logic and graphics updates, i would need two separate timers. Maybe that would not be a problem, but it doesn't sound like a nice way to do it.
There is also the question of how often to update. Most people just write a loop that updates as fast as it can - i.e. with no sleeping in between. I don't like this idea because it uses an unnecessary amount of CPU for rendering a fairly simple 2D game. And regardless of your opinion on this, i have my reasons, and so i would like to be able to set a maximum frame rate for my graphics and/or physics updates.
So, does anyone have any ideas? There must be some way to do this in Qt. Using Qt (for anything, let alone games) is a joy, and i really do not want to go back from it now.
int main(int argc, char* argv[]) {
// Setup stuff
// ...
int lastTime, dt, timeLeft;
while(!appQuit) {
while (SDL_PollEvent(&sdlEvent)) { // <-- Process any events in the queue
if (sdlEvent.type == SDL_QUIT) {
appQuit = true;
break;
}
Event::eventProc(sdlEvent); // <-- Do my own event processing, for GUI and stuff
}
lastTime = SDL_GetTicks();
clearScreen();
if (Game::isStarted && !Game::isPaused)
Game::update(); // <-- Update game state (physics)
GUI::drawScreen(); // <-- Draw everything
glFlush();
SDL_GL_SwapBuffers();
dt = SDL_GetTicks() - lastTime;
timeLeft = (1000/Settings::frameRate) - dt;
if (timeLeft > 0)
Sleep(timeLeft); // <-- Sleep until next update
}
// Shutdown stuff
// ...
}I was also intending on using a different rate for graphics and physics updates, as this guy says.
But now with Qt, i don't have access to the main event loop, so i am not sure how to do this.
Some have suggested using a QTimer, but i don't like that approach for a number of reasons. Firstly, the resolution may not be enough for high frame rates. And if i separate logic and graphics updates, i would need two separate timers. Maybe that would not be a problem, but it doesn't sound like a nice way to do it.
There is also the question of how often to update. Most people just write a loop that updates as fast as it can - i.e. with no sleeping in between. I don't like this idea because it uses an unnecessary amount of CPU for rendering a fairly simple 2D game. And regardless of your opinion on this, i have my reasons, and so i would like to be able to set a maximum frame rate for my graphics and/or physics updates.
So, does anyone have any ideas? There must be some way to do this in Qt. Using Qt (for anything, let alone games) is a joy, and i really do not want to go back from it now.