# Unity SDL Framerate Capping Issue [Solved]

This topic is 2896 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,
This should be my first post on the forums but as many of you may have noticed, there were like 8 threads with this same subject. I'm sorry about that, what happened was that when I pressed the button to submit my post nothing happened, I tried again a few times and still, but I tried again now a few times and finally my post was submitted, only to found out that I had accidentally submitted the post a few times, but I've deleted the others. I apologize for that. It was the source tags that caused it, I'm now using code tags instead.

I hope someone can help me with this. I'm having some trouble making my animations look smooth while capping the frame rate, my weapon of choice is Cplusplus with the SDL API. I'm following Lazy Foo's tutorial about Regulating Frame rate.

The logic is this:

#include "SDL.h"const int FRAMES_PER_SECOND = 60;int main(int argc, char* args[]){    //Here I initialize everything    bool running = true;    int startTime;    while(running)    {        startTime = SDL_GetTicks();           //Here I handle events        //*Events*        //Here's the game logic for my character movement on the screen        //*Logic stuff*        //Here I handle the rendering        //*Blits*, *Flip the screen*...        //============== Here I cap the frame rate ======================        static int elapsedTime = 0;        elapsedTime = (SDL_GetTicks() - startTime);        if( elapsedTime < (1000 / FRAMES_PER_SECOND) )        {            SDL_Delay( (1000 / FRAMES_PER_SECOND) - elapsedTime);        }        //===============================================================    }//End of game loop    //Free resources    //*Free SDL surfaces and close SDL stuff*    return 0;}//End main

This is only the logic, I didn't do exactly this on my program.

OK, in details the problems are:

1. On my PC when capping the frame rate at 60FPS I get frame rates around 62.5 and 62.486 and my game animation (the character movement is not smooth, it's kind of jumpy, with or without using frame independent movement together with this approach of frame capping. However when using frame independent movement explained here without frame capping the animation runs super smooth (at around 303 fps), but I want to cap the frame rate to keep the CPU more relaxed and to avoid rendering unnecessary frames to the screen. Maybe the problem is because my FPS are not exactly 60 all of the time, but I've seen games run on this PC at exactly 60 FPS, how do I achieve this?

This problem occurs on a Intel Core i7 CPU @ 2.80GHz, 4 GB of RAM, Using Windows 7 Professional (64 bit).

2. On my bro's PC the same frame rate capping approach works smoothly but it runs around 58 and 59 FPS and I noticed some tearing on the character sprites while moving around the screen.

That's on a AMD Athlon 64 X2 Dual Core CPU @ 2.71GHx, 3 GB of RAM, Using Win XP SP3.

Before posting, I searched everywhere and the closest thing I found about my problem was this unresolved post.
The poster of that thread seems to have the exact same problem I'm having.

[disturbed] Am I just doing something wrong?

[looksaround] Is frame rate capping really needed, or should I let the game be selfish and run the loop as fast as possible unnecessarily without taking into account that the CPU could be doing something else in the background?

[wow] What is the standard way of handling frames?

Any pointers would be greatly appreciated as I've been searching info about the subject for two days without success;

[Edited by - DragonicZero on July 14, 2010 3:06:55 PM]

##### Share on other sites
Quote:
 Original post by DragonicZero[disturbed] Am I just doing something wrong?

I would guess that relying on SDL_Delay to regulate framerate is not a good idea, since suspending a thread is not guaranteed to return exactly at the requested duration. From SDL documentation: This function [SDL_Delay] waits a specified number of milliseconds before returning. It waits at least the specified time, but possible longer due to OS scheduling. The delay granularity is at least 10 ms. Some platforms have shorter clock ticks but this is the most common.

Quote:
 [looksaround] Is frame rate capping really needed, or should I let the game be selfish and run the loop as fast as possible unnecessarily without taking into account that the CPU could be doing something else in the background?

Frame rate capping is not really needed, but letting the OS know that your game doesn't need all of the cpu (aka SDL_Delay(0) or SDL_Delay(1)) is all you need at the end of rendering a frame.

Quote:
 [wow] What is the standard way of handling frames?Any pointers would be greatly appreciated as I've been searching info about the subject for two days without success;

See the Fix Your Timestep article for ideas:
// Although this code renders as fast as possible, it helps// keep the logic regulated at a constant rateunsigned currentTicks = SDL_GetTicks();		// Current timeunsigned previousTicks = currentTicks;		// Time at last frameunsigned elapsedTime = 0;			// Elapsed ticks between the current and previous frameunsigned bukkit = 0;						unsigned fps = 60;unsigned ticksPerSecond = 1000;unsigned ticksPerFrame = ticksPerSecond / fps;unsigned updateThreshold = ticksPerFrame * 5;	float deltaTime = static_cast<float>(ticksPerFrame) / ticksPerSecond;while ( running ) {	previousTicks =	currentTicks;	currentTicks = SDL_GetTicks();	elapsedTicks = currentTicks - previousTicks;		if ( elapsedTicks >= updateThreshold )		elapsedTicks %= ticksPerFrame;		for ( bukkit += elapsedTicks; bukkit >= ticksPerFrame; bukkit -= ticksPerFrame ) {		// Event Handling				// Logic		theGame.update( deltaTime );	}		// Rendering	// Additionally, you can use interpolation here	// float interp = static_cast<float>( bukkit ) / ticksPerFrame; 	// theGame.render( interp );  // A value between [0, 1), 0 being the previous state, 1 being the current state	theGame.render();		SDL_Sleep( 0 );}

##### Share on other sites
Quote:
Original post by _fastcall
Quote:
 Original post by DragonicZero[disturbed] Am I just doing something wrong?

I would guess that relying on SDL_Delay to regulate framerate is not a good idea, since suspending a thread is not guaranteed to return exactly at the requested duration. From SDL documentation: This function [SDL_Delay] waits a specified number of milliseconds before returning. It waits at least the specified time, but possible longer due to OS scheduling. The delay granularity is at least 10 ms. Some platforms have shorter clock ticks but this is the most common.

Quote:
 [looksaround] Is frame rate capping really needed, or should I let the game be selfish and run the loop as fast as possible unnecessarily without taking into account that the CPU could be doing something else in the background?

Frame rate capping is not really needed, but letting the OS know that your game doesn't need all of the cpu (aka SDL_Delay(0) or SDL_Delay(1)) is all you need at the end of rendering a frame.

Quote:
 [wow] What is the standard way of handling frames?Any pointers would be greatly appreciated as I've been searching info about the subject for two days without success;

See the Fix Your Timestep article for ideas:
*** Source Snippet Removed ***

Well, it seems like I wont be capping the framerate then. However I found out a way to make the Delay more consistent, instead of skipping the remaining frames, it is better to check if a frame took the time it should, if it was faster, wait for the difference between the time it should have taken and the time it took.

Quote:
Original post by rip-off
Quote:
 Original post by TheBuzzSawI use SDL_Delay(1) in my program. What is the fundamental difference between that at SDL_Delay(0)?

I believe that on some implementations of SDL, SDL_Delay(0) may return immediately, whereas SDL_Delay(1) will always sleep for some period (generally a timeslice, which could have a granularity of 10 milliseconds for example on Windows).

Can someone confirm this?
Which one is preffered?

By the way, very nice article and thanks for the response.
I'm trying to get better at this so that I can help others too.

##### Share on other sites
Hi, I don't want to hijack the thread but I tried this too and was also wondering whether I had implemented the code correctly too. It is exactly the same code that DragonicZero has posted up.

DragonicZero - When you start the game does the framerate start at zero and then gradually builds up to the framerate you set?

I've set the framerate to 30fps and the framerate builds up slowly from zero to about 28- 29 fps I think.

Game here

Mines is an AMD opteron 180 @ 2.4Ghz, 2GB of RAM with a Geforce 6800GT GPU, WinXP SP3.

##### Share on other sites
Quote:
 Original post by popcornHi, I don't want to hijack the thread but I tried this too and was also wondering whether I had implemented the code correctly too. It is exactly the same code that DragonicZero has posted up.DragonicZero - When you start the game does the framerate start at zero and then gradually builds up to the framerate you set?I've set the framerate to 30fps and the framerate builds up slowly from zero to about 28- 29 fps I think.Download the game and have a look, is it the same behaviour as your game?Game hereMines is an AMD opteron 180 @ 2.4Ghz, 2GB of RAM with a Geforce 6800GT GPU, WinXP SP3.

I usually don't open programs without trusting the source, but I made an exception this time.

Now, I checked your game and it seems to be working nicely around the 29 - 30 fps from the beginning. The title of your game when running says Average Frames per Second, so I'm guessing you're using Lazy Foo's method for calculating framerate.
Well, what happens is this, you're calculating the average frames per second since the beginning of the program, before the actual game there's the title screen that seems to be running at a very low framerate, the way you implemented your code that framerate is taken into account together with the framerate of the actual game which is 30 fps. Check it out yourself, when you run your game hit s immediately to start, and look at the framerate, it will say 30 because it didn't got to store the framerate of the title screen. To calculate the average frames per second of your game you have to start counting frames after the title screen.

Edit: it could be either what I said about the title screen having low framerate or maybe you start counting frames before the title screen and start the timer for calculating framerate after the title screen or the other way around.

##### Share on other sites
Thanks DragonicZero, it was your second option that was correct, I started the timer before entering the game loop, I've changed the code so that the timer starts when 's' is pressed and it calculates at 29 - 30fps straight away.

I can understand about not wanting to open programs from unknown sources so thanks for explaining and for checking the program.

About your problem, not that I'm much help but maybe you could try running at a lower fps like 30/40fps instead of 60fps and see if it runs better?

##### Share on other sites

Thanks for the suggestion, actually I am now using the technique I talked about on my earlier post for regulating my framerate (the one where I check if a frame took the time it should, if it was faster, wait for the difference between the time it should have taken and the time it took, instead of skipping the remaining frames, which is what was causing the jumpiness on my animation in the first place, so now I get a smooth animation while at 30 or 60 fps. I noticed your game a little jumpy too, you could get your fps more constant by doing what I did.

It was basically:

const float FRAMERATE = 30;const float FPS = 1000.f / FRAMERATE;int main(int argc, char* args[]){    int frameNumber = 0    Uint32 startTime = SDL_GetTicks();    //Game loop    while(static bool running = true)    {        frameNumber++                //Events, Logic, Rendering...        //=================== Framerate capping =========================                Uint32 timeTheFrameShouldHaveTaken = startTime + frameNumber * FPS;        Uint32 timeItTookToRenderAllPreviousFrames = SDL_GetTicks();         if(timeItTookToRenderAllPreviousFrames <= timeTheFrameShouldHaveTaken)        {            SDL_Delay(timeItShoulHaveTaken - timeItTook);        }        else        {            //if it took longer than it should            //reset the frames and the startTime            frame = 0;            startTime = SDL_GetTicks;            //if you dont do this the frames will try            //to catch up with a higher framerate            //that happens for example if you move             //your window while ingame for some time        }       //===============================================================    }//end game loop    //free resources    return 0;}//end main

But I finally decided not to cap my framerate and just use "SDL_Delay(1);" because with that the program uses around 7% CPU, I tried "SDL_Delay(0); but it used around 13% CPU, that's why I stayed with 1.

EDIT: I fixed something on the code above because it gave almost the same effect as the one on the first code because there wasn't enough precision, so I changed the const int on the first lines to const float, sorry if that caused any confusion.

[Edited by - DragonicZero on July 13, 2010 12:14:07 AM]

##### Share on other sites
Quote:
 Original post by DragonicZeroI'm glad I could help.Thanks for the suggestion, actually I am now using the technique I talked about on my earlier post for regulating my framerate (the one where I check if a frame took the time it should, if it was faster, wait for the difference between the time it should have taken and the time it took, instead of skipping the remaining frames, which is what was causing the jumpiness on my animation in the first place, so now I get a smooth animation while at 30 or 60 fps. I noticed your game a little jumpy too, you could get your fps more constant by doing what I did.But I finally decided not to cap my framerate and just use "SDL_Delay(1);" because with that the program uses around 7% CPU, I tried "SDL_Delay(0); but it used around 13% CPU, that's why I stayed with 1. EDIT: I fixed something on the code above because it gave almost the same effect as the one on the first code because there wasn't enough precision, so I changed the const int on the first lines to const float, sorry if that caused any confusion.

OK DragonicZero I'll bear your advice in mind but at the moment I feel the way the fps are calculated is fine, I personally don't feel the game is a little jumpy. The method in lazy foos tutorials does seem to take more CPU time than your suggestion though since I notice that it takes about 24 - 26% of CPU time.

##### Share on other sites
Your game is fine Popcorn, the jumpiness is barely noticeable, just giving a different approach to framerate capping. As for me, I think my problem is solved. For simple games that don't use physics for now I'll do them framerate based until I get better at this. Then I'll try to implement a timestep based games with physics, I hope my brain survives.

• ### Similar Content

• By D34DPOOL
Edit Your Profile D34DPOOL 0 Threads 0 Updates 0 Messages Network Mod DB GameFront Sign Out Add jobEdit jobDeleteC# Programmer for a Unity FPS at Anywhere   Programmers located Anywhere.
Posted by D34DPOOL on May 20th, 2018
Hello, my name is Mason, and I've been working on a Quake style arena shooter about destroying boxes on and off for about a year now. I have a proof of concept with all of the basic features, but as an artist with little programming skill I've reached the end of my abilities as a programmer haha. I need someone to help fix bugs, optomize code, and to implent new features into the game. As a programmer you will have creative freedom to suggest new features and modes to add into the game if you choose to, I'm usually very open to suggestions :).
What is required:
Skill using C#
Experience with Unity
Experience using UNET (since it is a multiplayer game), or the effort and ability to learn it
Compensation:
Since the game currently has no funding, we can split whatever revenue the game makes in the future. However if you would perfer I can create 2D and/or 3D assets for whatever you need in return for your time and work.
It's a very open and chill enviornment, where you'll have relative creative freedom. I hope you are interested in joining the team, and have a good day!

To apply email me at mangemason@yahoo.com

• Is there a way to automatically change the start position of an animation? I have a bunch of animations set up on 3D models in unity. The issue is that I need to move the 3D models, however when I do so the animation start positions are not updated and I have to do it manually.

Changing the transform of key frames is time consuming with the amount of animations I have, so I was wondering if there was a way to do it automatically?
• By MoreLion
hey all! We are looking for members for our Unity horror game!
Here’s the story:
After a deadly virus plunges the world into chaos killing 85% of the human population there are now what they call “zones” these zones are watched very closely by the surviving government, people are checked every day for the virus, even if you touch the spit or any human waste or fluids of the victim who is infected, you will die. But one day, people in the west zone start to go missing, 1 woman goes outside the walls to uncover the mystery, is there more to the virus than meets the eye?, That is where your story starts.
This game is not a long development game, I have loads other game ideas,
I will also allow you to have a bit of creative freedom if you wish to add or share a idea!
And no, it’s not a zombie game lol I feel like zombie games are too generic, in this game you will encounter terrifying beasts!
There is some concept art one of our concept artists have made
If interested email liondude12@gmail.com

• GOVERNOR is a modernized version of the highly popular series of “Caesar” games. Our small team has already developed maps, written specifications, acquired music and performed the historical research needed to create a good base for the programming part of the project.

Our ultimate goal is to create a world class multi-level strategic city building game, but to start with we would like to create some of the simpler modules to demonstrate proof of concept and graphical elegance.

We would like programmers and graphical artists to come onboard to (initially) create:

A module where Province wide infrastructure can be built on an interactive 3D map of one of the ancient Roman Provinces.
A module where city infrastructure can be built on a real 3D interactive landscape.
For both parts, geographically and historically accurate base maps will be prepared by our team cartographer. Graphics development will be using Blender. The game engine will be Unity.

More information, and examples of the work carried out so far can be found at http://playgovernor.com/ (most of the interesting content is under the Encyclopedia tab).

This project represents a good opportunity for upcoming programmers and 3D modeling artists to develop something for their portfolios in a relatively short time span, working closely with one of Canada’s leading cartographers. There is also the possibility of being involved in this project to the point of a finished game and commercial success! Above all, this is a fun project to work on.

Best regards,

• So I have hundreds of moving objects that need to check there speed. One of the reasons they need to check there speed is so they don't accelerate into oblivion, as more and more force is added to each object.
At first I was just using the Unity vector3.magnitude. However this is actually very slow; when used hundreds of times.
Next I tried the dot-product check:  vector3.dot(this.transform.foward, ShipBody.velocity) The performance boost was fantastic. However this only measures speed in the forward direction. Resulting in bouncing objects accelerating way past the allowed limit.

I am hoping someone else knows a good way for me to check the speed with accuracy, that is fast on the CPU. Or just any magnitude calculations that I can test when I get home later.

What if I used  vector3.dot(ShipBody.velocity.normalized, ShipBody.velocity)?
How slow is it to normalize a vector, compared to asking it's magnitude?
• By Ds ds
Hi, my name is Andres, I'm a programmer with a technician degree and a Diploma in C#, looking for a project in Unity to start my career in game development. I don't do it for a paid but a recognition and start a portfolio, preferably a 2D game. Thanks for read, have a nice day.

• Hi there! Is the first time that I'm posting here so I'm sorry if I'm doing it wrong ha.
So here it comes, my doubt is, I'm doing a game with different levels, each of these levels in one different scene. Each scene contains to cameras that you can change pressing a button. Everything works fine.
The only problem is that I would like it to look a bit more professional, and I would like that if you finish the level with camera2, the next level start the same way. I've been thinking about using dontdestroyonloadon both cameras, but obviously this cameras need to be attached to the player to make the movement work, what do you recommend? Sorry If I've explained it in a messy way, and feel free to dm me for anything. Thanks in advance!

• Hello everyone
I am a programmer from Baku.
I need a 3D Modeller for my shooter project in unity.I have 2 years Unity exp.
Project will paid when we finish the work
If you interested write me on email:
mr.danilo911@gmail.com
• By markoal
Hi,
I'm Unity developer from Croatia and I'm looking to work on the paid project in my spare time.
I have 5+ years of experience in Unity and I'm familiar with almost anything, including all platforms (also Switch, PS4 and Xbox).
Feel free to contact me.

• Hello
Lvl 28   Programmer (day job: non-gamedev-programmer, making games as a hobby for about 2 years) Some vector art experience - tried to make some assets on my own using vector software and scripts   Some design experience (designing my own games ) About game
Turn-based fantasy rpg inspired by games like Heroes 3 (also WoG mod), NEO Scavenger, Battle Brothers I would like to create easy to use editor for creating custom scenarios (similar to the one from Heroes 3) World and story are clean slate, I did some drafts but I'm not good at it so it's possibly subject to change I decided to create graphics using vector software + scripts to make it faster (rpg's tend to have lots of assets), also it's more precise and easier to create tileable graphics (for example: rivers, paths) No sound/music work has been done yet Who do I look for?
Definitely someone with 2d art skills  I would like to focus more on programming 2D animator (skletal animations are preferred) Additional programmer could make development faster Someone for creating sounds/music/both It's a hobby project, I work on it in my free time. In case the project make it to the finish line and get shipped  - I can offer rev-share
Below should be few screens of what I already did (about 2 months of work) - some graphics, editor prototype screenshot and game prototype screenshot

• 35
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631357
• Total Posts
2999527
×