Hey everyone, how goes it?
So i've been working on programming a game in C++/SDL2.0 ever since SDL 2.0 release candidate came out. I have spent about 2 days straight of doing nothing except trying to find and implement a game loop I thought would be best for me. Now, I don't mean best ever, I just mean deciding between fixed step and variable step, etc. Basically what I have now is a game loop that calls the Update() function about 60 times per second, while not capping the framerate. I have been thinking about also capping the frame rate to 60, but i'm not sure if that's necessary yet, as all I draw on the screen for now is a blue color. The Update() function will update all of the game logic when it starts to be implemented, while Render() draws everything. Ever since I switched back to C++ from XNA, i've been trying to basically emulate a XNA framework into C++, as that's where I found it to be the easiest.
If it matters at all, i'm planning on making a 2D tile based game. So do you guys mind taking a look at my main game loop, and telling me if I should change it over to a different setup? Here is the code to just the main game loop:
//Event structure
SDL_Event e;
const int UPDATES_PER_SECOND = 60;
const int MS_PER_UPDATE = 1000 / UPDATES_PER_SECOND;
const int MAX_FRAMESKIP = 5;
Uint32 elapsedMS = 0;
Uint32 startTime = SDL_GetTicks();
Uint32 nextGameTick = SDL_GetTicks();
int loops;
int numUpdates = 0;
int numFrames = 0;
while(game->IsRunning())
{
while(SDL_PollEvent(&e))
{
//Exit the game if the user hits the 'X' button on the window.
if(e.type == SDL_QUIT)
game->SetIsRunning(false);
}
//Game update, render loop
loops = 0;
while(SDL_GetTicks() > nextGameTick && loops < MAX_FRAMESKIP)
{
game->Update();
nextGameTick += MS_PER_UPDATE;
loops++;
++numUpdates;
}
//Render the current frame
game->Render();
++numFrames;
//Used to display FPS/UPS
elapsedMS = SDL_GetTicks() - startTime;
if(elapsedMS % 1000 == 0) { //Only update once per second
game->FPS = numFrames / (elapsedMS / 1000.0);
game->UPS = numUpdates / (elapsedMS / 1000.0);
}
} // End main loop
So basically, here are my questions:
1) Is this an efficient game loop? By that I mean, will it update at about the same rate on all computers? It's designed to update the logic at 60 times per second no matter what the computer (usually), but the frame rate will change.
2) Is 60 updates per second too much? Too little?
3) How much should I look into capping the frame rate?
Let me know if you guys see anything you think I should change. I've been wracking my brain over this for 48 hours now, i'm ready to move on! :P Thanks guys!