Hi!
I'm on a project of a 2D platform action game (it's my first "ambitious" project, I've just been able to finish mini game) code in C++ with SDL2.
I need some advice about how to lock the frame rate (by lock I understand that it's lock for frame rate to go higher but not lower).
On my game the movement of the player is frame based, 6 pixel per frame (or 8 pixel when running), I've not been able to understand correctly the time based movement (At least to understand enough to write my own code), I thought to lock frame rate is my best solution. The game will not be graphically heavy so I guess it will not be a big deal since most of the computer could run it.
I've been able to write the following code:
- My game loop:
#define FRAMERATE 60
void CMain::GameLoop()
{
while (windowEvent->GetMainEvent()->type != SDL_QUIT)
{
timerFps = SDL_GetTicks();
windowEvent->Begin();
SDL_GetMouseState(&MouseX, &MouseY);
Player->Update();
environment->Update();
environment->DrawBack();
environment->DrawFront();
Player->Draw();
windowEvent->End();
timerFps = SDL_GetTicks() - timerFps;
if(timerFps < 1000/FRAMERATE)
{
SDL_Delay((1000/FRAMERATE) - timerFps);
}
}
} Detail of functions used:
void CWindow_Event::Begin()
{
SDL_PollEvent(mainEvent);
SDL_RenderClear(renderer);
}
void CWindow_Event::End()
{
SDL_RenderPresent(renderer);
} When I check my frame rate with other tools, it indicates a fps around 65 (not steady), I notice on other 2D game (e.g Rogue Legacy or Spelunky) the frame rate is really stable 60.
So I guess there is something wrong in my code, and maybe it will look horrible for some of you. What should I change?
(ps: I have many question on other topic related to my project, should I open a topic for each or put everything in here?
pps: Sorry for my english, it's not my mother tongue.)