0
\$\begingroup\$

The game I am making runs at 60 fps, but occasionally it will drop lower than that. I want to limit the frame-rate to 30 fps so that those drops will be less noticeable.

I don't understand everything about this, so bear with me. Here is what I have in the Initialize method:

IsFixedTimeStep = true;
TargetElapsedTime = TimeSpan.FromSeconds(1 / 30.0f);

All this is doing is making the game run in slow-motion. What am I doing wrong?

Edit: I would like Update to be called the same number of times per second, but for Draw to be called only 30 times per second, instead of 60. How could this be accomplished?

\$\endgroup\$
  • \$\begingroup\$ Do you mean your objects are moving in slower speeds ? this us probably because in your Update() function you had something like Position += Speed*Position and because you went from 60 to 30 fps , this Update is called 50% less times , so the movement is 50% slower, so just double your Speed to make up for it \$\endgroup\$ – dimitris93 Jan 9 '15 at 23:03
  • \$\begingroup\$ but do you know why you are dropping below 60fps ? 30fps is fine but i would definitely try to make my code more effecient \$\endgroup\$ – dimitris93 Jan 9 '15 at 23:12
  • \$\begingroup\$ are you sure the reason you drop frames is GPU based ? \$\endgroup\$ – dimitris93 Jan 9 '15 at 23:37
1
\$\begingroup\$

AFTER READING EDIT: You cannot change how fast the game will do it's draw calls. What happens is that on a fixed timestep your game will attempt to do one update and one draw call at a steady rate (usually 60fps). If it cannot keep that up for whatever reason then it will lag.

On a non-fixed timestep your game will run as fast as it possibly can and you would NEED to take into account the GameTime in order to have a stable game.

If you are experiencing frame stutter then you probably need to take some time and figure out where the bottleneck is. Make sure you aren't looping unnecessarily and that you aren't drawing things that are off the screen and don't need to be drawn.

It sounds like you weren't considering the fps when doing movement calculations.

What you are doing is indeed slowing down the frame rate. The consequence of doing so is that now your game will have half as many updates, and most likely you are not taking the frame rate into account when considering movement, so this means everything moves at half speed.

For example if you were moving a sprite around like the following:

int Speed = 2;
int XPos;
public void Update(GameTime GT)
{
     XPos += Speed;
}

Then every time your game updates it will add a value of 2 to the XPos variable. Now since you used to be at 60 frames-per-second this meant that your XPos variable was being increased by 120 every second (2 * 60fps). But now, Since you changed the frames-per-second to 30, instead of XPos adding 120 to it's value every second it is now adding 60 (2 * 30fps). The result is that everything is moving at half the speed it was moving when you were at 60fps.

The solution to this can be very simple if you keep good coding practices. XNA provides your game with a GameTime variable. Inside this variable are two very useful values, ElapsedGameTime and TotalGameTime. ElapsedGameTime gives you the amount of time that has passed since your game last updated (your frame time). TotalGameTime gives you the amount of time that has passed since your game started.

Using ElapsedGameTime you can make your game run independent of the framerate. Borrowing from the earlier example:

int Speed = 120;
int XPos;
public void Update(GameTime GT)
{
     XPos += Speed * GT.ElapsedGameTime.TotalSeconds;
}

There are two important modifications I made to the example. The first is that the Speed value is no longer 2. When using the frame rate as a part of your calculations you need to think of movement values as how far the object should move per second. Since in the previous example we were moving 120 units every second (2 * 60fps), to get the same movement the Speed value had to be changed to 120 to match it.

The reason the Speed value was changed to 120 is because of the second modification.

XPos += Speed * GT.ElapsedGameTime.TotalSeconds;
//If TotalSeconds = 0.5 then Speed = 60.
//If TotalSeconds = 0.25 then Speed = 30.
//If TotalSeconds = 1 then Speed = 120.

By multiplying the Speed variable by how many seconds have passed since the last update you end up moving the XPos at a consistent rate across varying frame rates.

TL;DR - Change your movement variables to take into account the ElapsedGameTime so they run at a steady rate.

NOTE: When using a FixedTimeStep you often do not have to take into account the elapsed time as it is usually steady, but it's still a good idea.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.