0:38 - or you could set a tickrate for the game. Then it will only update after a interval of time. That way you can have a high or low fps without changing gameplay speed.
In the section where you give code for regulating fps, it should be SDL_Delay((int)((1000/FPS)-SDL_GetTicks()+currentTick); , not -currentTick. Took me ages to catch that one - it would freeze the game because SDL_Delay's parameter was negative.
Another important feature of a timer is that it regulates the amount of CPU utilisation your program causes.
If your program runs without a timer, it calculates as many frames as possible, resulting in a 100% utilisation of the CPU, slowing every other program down (may also cause problems on certain systems if the OS doesn't get enough calculation time to handle it's message queue).
As long as your games don't get too complex, they shouldn't use 100% of the CPU and therefore use a timer.
If for example you have a networked program, where both computers need to keep in sync. If one cannot keep up with a regulated framerate, it will go out of sync with the other pretty fast and cause difficulty.
Basically you want to use frame rate regulation. Or you and up using 100% CPU. And doing frame rate independent movement is very easy. You just calculate how much is the time passed since the last frame, and use it in the movement calculations. From then it's just physics and math.
problem with my game is that the fps slowly lower
Earthium 3 months ago
Your a life saver I didn't know why my player was moving so fast i had it set to sprite.x +=1; and it was still fast but this should work
0ChrisMackle0 4 months ago
Why dont write the game framerate indepent?
Damatelon 7 months ago
cool
adkarb 1 year ago
0:38 - or you could set a tickrate for the game. Then it will only update after a interval of time. That way you can have a high or low fps without changing gameplay speed.
DevKire 1 year ago
In the section where you give code for regulating fps, it should be SDL_Delay((int)((1000/FPS)-SDL_GetTicks()+currentTick); , not -currentTick. Took me ages to catch that one - it would freeze the game because SDL_Delay's parameter was negative.
AllOneYou 1 year ago
Holy Grail of beginner game dev
right here.
keithgarry 1 year ago
you should make more of these with with openGL
TreyRust 2 years ago
Your random numbers tutorial seems to be private, leading me here!
ryancfcsas 2 years ago
Comment removed
Xteaphn 2 years ago
Hi I´m a beginner to programming so I could be wrong. But is the code in SDL_Delay right? Shouldnt it be:
SDL_Delay( ((1000/MAX_FPS) - (SDL_GetTicks() - currentTick) ) ); or am I totally wrong? :)
vicmanbeer 2 years ago
oh you might be right, I'll have to look at it again.
LusikkaMage 2 years ago
@LusikkaMage
Your website is screwed up and wont work
0ChrisMackle0 4 months ago
awesome tutorials lusikka
aldex123 2 years ago
Another important feature of a timer is that it regulates the amount of CPU utilisation your program causes.
If your program runs without a timer, it calculates as many frames as possible, resulting in a 100% utilisation of the CPU, slowing every other program down (may also cause problems on certain systems if the OS doesn't get enough calculation time to handle it's message queue).
As long as your games don't get too complex, they shouldn't use 100% of the CPU and therefore use a timer.
Thunabrain 2 years ago
seriously just use the turbo key...
MadPumpkinGames 2 years ago
For what? Sonic runs _too fast_ on my newer computers.
LusikkaMage 2 years ago
just press the turbo button! :P
( if you get that joke.. )
ADeadRock 2 years ago
...leik, in zsnes? o_o
LusikkaMage 2 years ago
on old computers they use to have a "turbo" key, google it
ADeadRock 2 years ago
Hey, I made a best guess, not a "durr, wut iz turb0 kee?"
LusikkaMage 2 years ago
lol, sorry if I seemed mean, just hoping to offer a bit of humor :D
( tbh, I don't entirely know if the turbo button speeds up/slows the pc or whatever, but I think it does )
ADeadRock 2 years ago
I remember the turbo button that used to be on PCs, lol. With it turned off, your PC's CPU would only run at about 75%.
Deddinsyde 2 years ago
Haha, yeah, I had a pc with one of those. Back when cpus made absurd amount of heat we didn't quite know what to do with yet.
Chaotisu 2 years ago
Its titled Part 7 B Regulating FPS is Allegro not SDL. Besides that, good tutorial! Took me a long time to find anything on this.
IonDuel 2 years ago
Oops x_X
LusikkaMage 2 years ago
Why don't you show how to make framerate independent movement ?
Meja69 2 years ago
I don't understand what this would achieve, and how it'd be different from not having the timer at all.
LusikkaMage 2 years ago
Well the game would run at the same speed whatever the framerate is. It would allow people to have a higher framerate. (or lower)
Meja69 2 years ago
I'll look into it for the future, but for now, this is Pickin' Sticks. Let's keep it simple. :)
LusikkaMage 2 years ago
If for example you have a networked program, where both computers need to keep in sync. If one cannot keep up with a regulated framerate, it will go out of sync with the other pretty fast and cause difficulty.
RyanPridgeon 2 years ago
Basically you want to use frame rate regulation. Or you and up using 100% CPU. And doing frame rate independent movement is very easy. You just calculate how much is the time passed since the last frame, and use it in the movement calculations. From then it's just physics and math.
TomCatFort 2 years ago