Sprites in my game are Shaking and Sprite Animati on is Choppy!

Hi. i’m making Vertical Shoot’em-up game by using SDL.

Framerate is regulated by my created code. (16~17 ms per frame, 60 frames per second)
However, Game animation is not smooth. (Shaking and choppy.)
for example, when Player character move right to left,
the character goes to left, sometimes the character seems to go right. (the character is shaking.)
also the Bullet Animation is not smooth, sometimes choppy.

(are the problems ‘Tearing’??)

so, i checked the my game logic code about X, Y Position. is not the problem.
when i Set framerate to 1FPS (1 frame per second), the animation is not choppy.

please help me…

the codes are the part of my Game Project.

below is screen making code.

if (bFullScreen == true)
{
pSurface = SDL_SetVideoMode(width, height, bpp, SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
}
else
{
pSurface = SDL_SetVideoMode(width, height, bpp, SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF);
}

below is the main logic of Draw Sprites and Regulating Framerate.

static int iFrameCount = 0;
static Uint32 iFrameOldTime;
static Uint32 iAccumulatedTime = 0;

while (bRunning)
{

iFrameOldTime = SDL_GetTicks();

if (bRunning == false)
{
return;
}
SDL_FillRect(pSurface, pSurface->clip_rect, SDL_MapRGB(pSurface->format, 0x00, 0x00, 0x00);

//draw the sprites. (player, enemy, bullets, …) below is example.
SDL_BlitSurface(ExampleSurface, NULL, pSurface, ExampleRect);

SDL_Flip(pSurface);

iFrameCount++;

double fTempFrameRate = 999.0;
Uint32 iTick;

while (fTempFrameRate > 60.0)
{
iTick = SDL_GetTicks();
iFrameTime = iTick - iFrameOldTime;
fTempFrameRate = (double)(iFrameCount) / (double)(iAccumulatedTime + iFrameTime) / 1000.0);
}

iAccumulatedTime = iAccumulatedTime + iFrameTime;
fFrameRate = fTempFrameRate;

if (iAccumulatedTime >= 500)
{
iAccumulatedTime = 0;
iFrameCount = 0;
}

}

You should probably use SDL_Delay() in there to give time back to the
system instead of burning it all in a while loop. It might not fix your
jitter issue, but it’s worth doing anyhow.

Jonny DOn Sat, Sep 8, 2012 at 3:38 AM, axt32 wrote:

Hi. i’m making Vertical Shoot’em-up game by using SDL.

Framerate is regulated by my created code. (16~17 ms per frame, 60 frames
per second)

However, Game animation is not smooth. (Shaking and choppy.)

for example, when Player character move right to left,

the character goes to left, sometimes the character seems to go right.
(the character is shaking.)

also the Bullet Animation is not smooth, sometimes choppy.

(are the problems ‘Tearing’??)

so, i checked the my game logic code about X, Y Position. is not the
problem.

when i Set framerate to 1FPS (1 frame per second), the animation is not
choppy.

please help me…

the codes are the part of my Game Project.

below is screen making code.

if (bFullScreen == true)

{

 pSurface = SDL_SetVideoMode(width, height, bpp, SDL_ANYFORMAT |

SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);

}

else

{

pSurface = SDL_SetVideoMode(width, height, bpp, SDL_ANYFORMAT |
SDL_HWSURFACE | SDL_DOUBLEBUF);

}

below is the main logic of Draw Sprites and Regulating Framerate.

static int iFrameCount = 0;

static Uint32 iFrameOldTime;

static Uint32 iAccumulatedTime = 0;

while (bRunning)

{

iFrameOldTime = SDL_GetTicks();

if (bRunning == false)

{

 return;

}

SDL_FillRect(pSurface, pSurface->clip_rect, SDL_MapRGB(pSurface->format,
0x00, 0x00, 0x00);

//draw the sprites. (player, enemy, bullets, …) below is example.

SDL_BlitSurface(ExampleSurface, NULL, pSurface, ExampleRect);

SDL_Flip(pSurface);

iFrameCount++;

double fTempFrameRate = 999.0;

Uint32 iTick;

while (fTempFrameRate > 60.0)

{

iTick = SDL_GetTicks();

iFrameTime = iTick - iFrameOldTime;

fTempFrameRate = (double)(iFrameCount) / (double)(iAccumulatedTime +
iFrameTime) / 1000.0);

}

iAccumulatedTime = iAccumulatedTime + iFrameTime;

fFrameRate = fTempFrameRate;

if (iAccumulatedTime >= 500)

{

iAccumulatedTime = 0;

iFrameCount = 0;

}

}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Also, there’s a lot missing from your example code. One thing in
particular is the integration of your simulation (i.e. what happens each
time step to move your sprites). I can’t tell if you’re doing that right
or if you’re even using the right data types for position data (use
floating points, not integers to hold x and y).

Jonny D

Try the SDL_gfx framerate manager which is designed to attempt to
maintain a constant framerate.
http://www.ferzkopp.net/Software/SDL_gfx-2.0/Docs/html/_s_d_l__framerate_8h.html

#include "SDL/SDL_framerate.h"
FPSmanager fpsm;
Uint32 rate = 30;
SDL_initFramerate(&fpsm);
SDL_setFramerate(&fpsm,rate);
while (drawing) {

/* Delay to fix rate */
Uint32 time_passed = SDL_framerateDelay(&fpsm);
if (time_passed > 0) {
printf(“Desired rate %i / Delay was %i ms / Actual
framerate %i Hz …”, rate, time_passed, 1000 / time_passed);
}
}On 9/8/2012 12:38 AM, axt32 wrote:

Hi. i’m making Vertical Shoot’em-up game by using SDL.

Framerate is regulated by my created code. (16~17 ms per frame, 60
frames per second)

However, Game animation is not smooth. (Shaking and choppy.)

for example, when Player character move right to left,

the character goes to left, sometimes the character seems to go right.
(the character is shaking.)

also the Bullet Animation is not smooth, sometimes choppy.

(are the problems ‘Tearing’??)

so, i checked the my game logic code about X, Y Position. is not the
problem.

when i Set framerate to 1FPS (1 frame per second), the animation is
not choppy.

please help me…

the codes are the part of my Game Project.

below is screen making code.

if (bFullScreen == true)

{

 pSurface = SDL_SetVideoMode(width, height, bpp, SDL_ANYFORMAT | 

SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);

}

else

{

pSurface = SDL_SetVideoMode(width, height, bpp, SDL_ANYFORMAT |
SDL_HWSURFACE | SDL_DOUBLEBUF);

}

below is the main logic of Draw Sprites and Regulating Framerate.

static int iFrameCount = 0;

static Uint32 iFrameOldTime;

static Uint32 iAccumulatedTime = 0;

while (bRunning)

{

iFrameOldTime = SDL_GetTicks();

if (bRunning == false)

{

 return;

}

SDL_FillRect(pSurface, pSurface->clip_rect,
SDL_MapRGB(pSurface->format, 0x00, 0x00, 0x00);

//draw the sprites. (player, enemy, bullets, …) below is example.

SDL_BlitSurface(ExampleSurface, NULL, pSurface, ExampleRect);

SDL_Flip(pSurface);

iFrameCount++;

double fTempFrameRate = 999.0;

Uint32 iTick;

while (fTempFrameRate > 60.0)

{

iTick = SDL_GetTicks();

iFrameTime = iTick - iFrameOldTime;

fTempFrameRate = (double)(iFrameCount) / (double)(iAccumulatedTime +
iFrameTime) / 1000.0);

}

iAccumulatedTime = iAccumulatedTime + iFrameTime;

fFrameRate = fTempFrameRate;

if (iAccumulatedTime >= 500)

{

iAccumulatedTime = 0;

iFrameCount = 0;

}

}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hi. i’m making Vertical Shoot’em-up game by using SDL.

Framerate is regulated by my created code.
[…]

Short answer: Don’t do that! It’s a last resort “solution” that you use to
save power on battery powered devices and the like.

What you should preferably do is synchronize rendering to the display refresh
rate, which is typically - but not always - 60 Hz. You do this by enabling
retrace (or vblank) synchronization. In SDL 1.2, IIRC, a double buffered
hardware surfare will enable this, if available. The actual synchronization
happens in SDL_Flip().

You should probably still use a framerate regulator, but only as a cap, so you
don’t get hundreds or thousands of fps when retrace sync isn’t working, as
that (apart from wasting power) can cause all sorts of other issues.

Note that you can’t (reliably) chose a suitable display refresh rate on most
platforms, so you have to adapt your code to deal with varying delta times, or
run the game logic at a fixed “virtual” frame rate and then interpolate (or
extrapolate) graphical coordinates as needed.

(are the problems ‘Tearing’??)

Yes, sort of. Unless you actually synchronize with the display refresh, you’ll
sometimes move an object right before it’s draw on the screen - and sometimes
right after it was already drawn, in which case what you’ll actually see is
the last frame repeated.

[…]

while (fTempFrameRate > 60.0)
{
iTick = SDL_GetTicks();
iFrameTime = iTick - iFrameOldTime;
fTempFrameRate = (double)(iFrameCount) / (double)(iAccumulatedTime +
iFrameTime) / 1000.0);
}

This will potentially cause another problem: When your main loop is constantly
burning CPU cycles, never sleeping or blocking on anything, the operating
system (pretty much regardless of which on it is) will respond by lowering
your dynamic priority - and as a result, you’re going to lose the CPU,
sometimes for extended periods of time, to just about any random background
task that wants it.

You probably want to put an SDL_Delay(10) in that loop. A smaller value would
be preferable, but that might not work on some platforms. The scheduling
granularity of older Linux systems and some other platforms is 10 ms, so if
you go below that, you’re effectively still busy-waiting, burning CPU cycles.

One option is to use sched_yield() (Un*x) or Sleep(0) (Windows; SDL_Delay(0)
also works on Win32 in the SDL versions I’ve checked), which essentially just
calls the OS, saying “Hey, now would be a good time to grab the CPU for a
moment if you have work queued!” It’s not particularly nice (still burns CPU
cycles doing nothing useful), but it tends to work if you’re not certain that
you can sleep with ms granularity…On Saturday 08 September 2012, at 09.38.35, axt32 wrote:


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://consulting.olofson.net http://olofsonarcade.com |
’---------------------------------------------------------------------’

Yes, this is important.? Any worthwhile game will have a “game timer” available that
workssomething like this:

Declare a timestamp variable that gets updated at the start of each frame.
Each frame, check the timestamp and see how many milliseconds have passed, then
update the timestamp.
Calculate movement distances based on how many ms it’s been since the last frame,
and update all screen objects.
Render.

Note that on-screen movement is tied to the elapsed time and NOT TO THE FRAME

RATE.? This means that your frame rate can go up or down and things will still move
at a consistent speed.________________________________
From: David Olofson
Subject: Re: [SDL] Sprites in my game are Shaking and Sprite Animation is Choppy!

Note that you can’t (reliably) chose a suitable display refresh rate on most
platforms, so you have to adapt your code to deal with varying delta times, or
run the game logic at a fixed “virtual” frame rate and then interpolate (or
extrapolate) graphical coordinates as needed.

The problem could be that your game is just switching between 2 frame rates, like say jumping between 30 and 60 fps. This can give a sense of choppiness, as an unstable frame rate can look worse than a consistent frame rate ( depending on implementation, a constant 30 fps can look better than one switching between 30 and 60 fps).

I would use a solution like the following, where you use a fixed rate:

const double desiredMillisecondsPrFrame = 1000.0 / 60.0;
double accumulatedTime = 0.0;

void gameloop(double deltaTimeSinceLastFrame)
{
accumulatedTime += deltaTimeSinceLastFrame;
while( accumulatedTime >= desiredMillisecondsPrFrame )
{
accumulatedTime -= desiredMillisecondsPrFrame;
DoTheActualUpdateOfGameLogic( desiredMillisecondsPrFrame );
}
}On Saturday, September 8, 2012 at 11:46 PM, Mason Wheeler wrote:

Yes, this is important. Any worthwhile game will have a “game timer” available that
works something like this:

Declare a timestamp variable that gets updated at the start of each frame.
Each frame, check the timestamp and see how many milliseconds have passed, then
update the timestamp.
Calculate movement distances based on how many ms it’s been since the last frame,
and update all screen objects.
Render.

Note that on-screen movement is tied to the elapsed time and NOT TO THE FRAME
RATE. This means that your frame rate can go up or down and things will still move
at a consistent speed.

From: David Olofson <david at olofson.net (mailto:david at olofson.net)>
Subject: Re: [SDL] Sprites in my game are Shaking and Sprite Animation is Choppy!

Note that you can’t (reliably) chose a suitable display refresh rate on most
platforms, so you have to adapt your code to deal with varying delta times, or
run the game logic at a fixed “virtual” frame rate and then interpolate (or
extrapolate) graphical coordinates as needed.


SDL mailing list
SDL at lists.libsdl.org (mailto:SDL at lists.libsdl.org)
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Yeah, Listen to Mason on this one, just be sure to make sure you compensate
for game pause-states. :)On Sun, Sep 9, 2012 at 2:06 PM, Gorm Lai wrote:

The problem could be that your game is just switching between 2 frame
rates, like say jumping between 30 and 60 fps. This can give a sense of
choppiness, as an unstable frame rate can look worse than a consistent
frame rate ( depending on implementation, a constant 30 fps can look better
than one switching between 30 and 60 fps).

I would use a solution like the following, where you use a fixed rate:

const double desiredMillisecondsPrFrame = 1000.0 / 60.0;
double accumulatedTime = 0.0;

void gameloop(double deltaTimeSinceLastFrame)
{
accumulatedTime += deltaTimeSinceLastFrame;
while( accumulatedTime >= desiredMillisecondsPrFrame )
{
accumulatedTime -= desiredMillisecondsPrFrame;
DoTheActualUpdateOfGameLogic( desiredMillisecondsPrFrame );
}
}

On Saturday, September 8, 2012 at 11:46 PM, Mason Wheeler wrote:

Yes, this is important. Any worthwhile game will have a "game timer"
available that
works something like this:

Declare a timestamp variable that gets updated at the start of each frame.
Each frame, check the timestamp and see how many milliseconds have passed,
then
update the timestamp.
Calculate movement distances based on how many ms it’s been since the last
frame,
and update all screen objects.
Render.

Note that on-screen movement is tied to the elapsed time and NOT TO THE
FRAME
RATE. This means that your frame rate can go up or down and things will
still move
at a consistent speed.


From: David Olofson
Subject: Re: [SDL] Sprites in my game are Shaking and Sprite Animation
is Choppy!

Note that you can’t (reliably) chose a suitable display refresh rate on
most
platforms, so you have to adapt your code to deal with varying delta
times, or
run the game logic at a fixed “virtual” frame rate and then interpolate
(or
extrapolate) graphical coordinates as needed.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I seem to remember that there is a frame rate manager in SDL_gfx. You
should use that rather than rolling your own. Remember, not just rendering
needs this, your position calculation needs to be smooth over time too,

IanOn 9 Sep 2012 21:30, “Alex Barry” <alex.barry at gmail.com> wrote:

Yeah, Listen to Mason on this one, just be sure to make sure you
compensate for game pause-states. :slight_smile:

On Sun, Sep 9, 2012 at 2:06 PM, Gorm Lai wrote:

The problem could be that your game is just switching between 2 frame
rates, like say jumping between 30 and 60 fps. This can give a sense of
choppiness, as an unstable frame rate can look worse than a consistent
frame rate ( depending on implementation, a constant 30 fps can look better
than one switching between 30 and 60 fps).

I would use a solution like the following, where you use a fixed rate:

const double desiredMillisecondsPrFrame = 1000.0 / 60.0;
double accumulatedTime = 0.0;

void gameloop(double deltaTimeSinceLastFrame)
{
accumulatedTime += deltaTimeSinceLastFrame;
while( accumulatedTime >= desiredMillisecondsPrFrame )
{
accumulatedTime -= desiredMillisecondsPrFrame;
DoTheActualUpdateOfGameLogic( desiredMillisecondsPrFrame );
}
}

On Saturday, September 8, 2012 at 11:46 PM, Mason Wheeler wrote:

Yes, this is important. Any worthwhile game will have a "game timer"
available that
works something like this:

Declare a timestamp variable that gets updated at the start of each frame.
Each frame, check the timestamp and see how many milliseconds have
passed, then
update the timestamp.
Calculate movement distances based on how many ms it’s been since the
last frame,
and update all screen objects.
Render.

Note that on-screen movement is tied to the elapsed time and NOT TO THE
FRAME
RATE. This means that your frame rate can go up or down and things will
still move
at a consistent speed.


From: David Olofson
Subject: Re: [SDL] Sprites in my game are Shaking and Sprite Animation
is Choppy!

Note that you can’t (reliably) chose a suitable display refresh rate on
most
platforms, so you have to adapt your code to deal with varying delta
times, or
run the game logic at a fixed “virtual” frame rate and then interpolate
(or
extrapolate) graphical coordinates as needed.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org