I calculate, how many milliseconds needed for 1 frame, there are 1000 miliseconds (ms) in a second, and we need to render FPS (for example 100) frames in this time, that's mean, that we need to render a frame every 1000/100=10ms. then I check, how much time already spend, while rendering the frame, for example, it needed 6ms, so we need to wait 4 more ms, to keep up the 100FPS (so every frame will take 10ms, so 1000/10=100FPS)
Works really well, before the fps regulating code this program would run a thread on my processor to the max(25%), and with it keeps at 0% cuz of no rendering being done. and I don't have a weak poc: AMD phenom II x4 955 BE
Thank you for making these videos! I'm trying to get into graphics api's and I'm having a hard time. Your programs actually compile, unlike a lot of other online tutorials! Keep up the great work! Subscribed
Maybe it seems easier, but it will run your CPU at 100%, but the way i showed in this video (so the SDL_Delay) will actually wait without use the CPU (so it will stop the program for the amount of time, we gave to the SDL_Delay, so the OS can care other programs for that time). So yes, it's work, but it's not the best way, if every program would do that your CPU usage would be 100% all time.
The other thing, you should use constants, it's not really matter in this program, but later..
No it wouldn't work, because the SDL_GetTicks() gets the time in millisecond, but not the time elapsed from the last frame, for example, in the first frame it's 30 and then 60,90... it will increasing, so the (1000/FPS>SDL_GetTicks() would only true for some cases, becase the 1000/FPS is always 33.3, but the SDL_GetTicks will always gets bigger.
To get the time of this frame I subtract the starttime from the endtime. (+2 syntactic error: ) in the 1. line, _ in the 2 line)
I understand everything, but I don't know whu you do: 1000/FPS ??
I think I got it but...
linuxandunix4ever 1 month ago
@linuxandunix4ever
I calculate, how many milliseconds needed for 1 frame, there are 1000 miliseconds (ms) in a second, and we need to render FPS (for example 100) frames in this time, that's mean, that we need to render a frame every 1000/100=10ms. then I check, how much time already spend, while rendering the frame, for example, it needed 6ms, so we need to wait 4 more ms, to keep up the 100FPS (so every frame will take 10ms, so 1000/10=100FPS)
thecplusplusguy 1 month ago
trying it now
0ChrisMackle0 1 month ago
Arigatou - works pretty well. My game now uses 15% CPU
SystemError51 2 months ago
Works really well, before the fps regulating code this program would run a thread on my processor to the max(25%), and with it keeps at 0% cuz of no rendering being done. and I don't have a weak poc: AMD phenom II x4 955 BE
mmt322 3 months ago
I'm with MrKonnorsniper, these are great
alanxoc3 6 months ago
Thanks
JonAndCallum 7 months ago
Thank you for making these videos! I'm trying to get into graphics api's and I'm having a hard time. Your programs actually compile, unlike a lot of other online tutorials! Keep up the great work! Subscribed
MrKonnorsniper 7 months ago
while((SDL_GetTicks() - start) < 33);
this is more easier
Hyrppa95 8 months ago
@Hyrppa95
Maybe it seems easier, but it will run your CPU at 100%, but the way i showed in this video (so the SDL_Delay) will actually wait without use the CPU (so it will stop the program for the amount of time, we gave to the SDL_Delay, so the OS can care other programs for that time). So yes, it's work, but it's not the best way, if every program would do that your CPU usage would be 100% all time.
The other thing, you should use constants, it's not really matter in this program, but later..
thecplusplusguy 8 months ago 3
@thecplusplusguy
Hey man great tutorials, just out of interest would this work:
if(1000/FPS>SDL_GetTicks()
{
SDL_Delay(1000/FPS - SDL-GetTicks());
start = 0;
}
If it doesnt, why wouldnt it?
Great Tutorials!
viral262 4 months ago
@viral262
Hey, Thanks.
No it wouldn't work, because the SDL_GetTicks() gets the time in millisecond, but not the time elapsed from the last frame, for example, in the first frame it's 30 and then 60,90... it will increasing, so the (1000/FPS>SDL_GetTicks() would only true for some cases, becase the 1000/FPS is always 33.3, but the SDL_GetTicks will always gets bigger.
To get the time of this frame I subtract the starttime from the endtime. (+2 syntactic error: ) in the 1. line, _ in the 2 line)
thecplusplusguy 4 months ago