Smooth animation

Hi I’m an italian newbie…
I was trying to have a smooth animation independent of the rendering frame
rate of a bouncing ball. I have read many tutorial about this without decent
result.

http://gaffer.org/articles/Timestep.html
http://www.iguanademos.com/Jare/Articles.php?view=FixLoop
http://www.sacredsoftware.net/tutorials/Animation/TimeBasedAnimation.xhtml

The best demo that I have seen is this http://olofson.net/download/pig-1.0.tar.gz
but is too complex for me beacuse it’s like a small game with 1000 lines
of code…It’s not suitable for a beginner.

Anyone could send me an efficient and easy source of a bouncing ball?

Thanks and SDL Rules :slight_smile:

hi luke,

for animation what i do is use SDL_GetTicks() like so:

//defined globaly or in the main function
Uint32 LastTime, ThisTime, DifferenceTime;

//in my game loop
LastTime=ThisTime;
ThisTime=SDL_GetTicks();
DifferenceTime=ThisTime-LastTime;

what that does is make it so you have the number of miliseconds between the
last frame and this one stored in DifferenceTime. I use this number in all
my equations. For instance if you have an object’s x and y movement stored,
multiply each by DifferenceTime when you actualy move the object. This will
make it so your objects will move the same distance for the same amount of
time, even on slower and faster systems! (:

hope that helps> ----- Original Message -----

From: luke_123@virgilio.it ()
To:
Sent: Sunday, May 29, 2005 2:33 AM
Subject: [SDL] Smooth animation

Hi I’m an italian newbie…
I was trying to have a smooth animation independent of the rendering frame
rate of a bouncing ball. I have read many tutorial about this without decent
result.

http://gaffer.org/articles/Timestep.html
http://www.iguanademos.com/Jare/Articles.php?view=FixLoop
http://www.sacredsoftware.net/tutorials/Animation/TimeBasedAnimation.xhtml

The best demo that I have seen is this
http://olofson.net/download/pig-1.0.tar.gz
but is too complex for me beacuse it’s like a small game with 1000 lines
of code…It’s not suitable for a beginner.

Anyone could send me an efficient and easy source of a bouncing ball?

Thanks and SDL Rules :slight_smile:


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

luke_123 at virgilio.it a ?crit :

Hi I’m an italian newbie…
I was trying to have a smooth animation independent of the rendering frame
rate of a bouncing ball. I have read many tutorial about this without decent
result.

http://gaffer.org/articles/Timestep.html
http://www.iguanademos.com/Jare/Articles.php?view=FixLoop
http://www.sacredsoftware.net/tutorials/Animation/TimeBasedAnimation.xhtml

The best demo that I have seen is this http://olofson.net/download/pig-1.0.tar.gz
but is too complex for me beacuse it’s like a small game with 1000 lines
of code…It’s not suitable for a beginner.

Anyone could send me an efficient and easy source of a bouncing ball?

In the SDL source package, there is a demo called “testsprite” :
http://www.libsdl.org/cgi/cvsweb.cgi/SDL12/test/testsprite.c?rev=1.7&content-type=text/x-cvsweb-markup

Ok, it’s actually a bouncing smiley and not a bouncing ball, but chaging
the bitmap is left as an exercice to the reader :slight_smile:

Stephane

Hi, thanks for your e-mails but the problem remains unsolved
Alan Wolfe: with delta time I have another problem…When I move the mouse
during the animation the animation slow down and I see flickers.

Try this please:

#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>

#define BALL 200

SDL_Surface *screen;

static float v=0.5,s=0;
static int dt,t0,t1;

void GameLogic()
{
s+=(v*(float)dt);
if(s<=0)
{
s=0;
v=-v;
}
if(s>=(float)(screen->w-BALL))
{
s=(float)(screen->w-BALL);
v=-v;
}
}

void Draw()
{
SDL_Rect dest;

dest.x=0;
dest.y=0;
dest.w=screen->w;
dest.h=screen->h;
SDL_FillRect(screen,&dest,SDL_MapRGB(screen->format,0,0,0));
dest.x=(int)s;
dest.y=(screen->h/2)-(BALL/2);
dest.w=BALL;
dest.h=BALL;
SDL_FillRect(screen,&dest,SDL_MapRGB(screen->format,255,255,255));
SDL_Flip(screen);
}

int main(int a, char *b[])
{
SDL_Event event;
char done=0;

if(SDL_Init(SDL_INIT_VIDEO)!=0)
{
exit(EXIT_FAILURE);
}
atexit(SDL_Quit);
screen=SDL_SetVideoMode(640,480,32,SDL_DOUBLEBUF|SDL_FULLSCREEN|SDL_HWSURFACE);
if(screen==NULL)
{
exit(EXIT_FAILURE);
}
t0=SDL_GetTicks();
while(!done)
{
t1=SDL_GetTicks();
dt=t1-t0;
t0=t1;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
done=1;
break;
}
}
GameLogic();
Draw();
}
return 0;
}

Regards

Luke

in my code moving the mouse doesnt affect anything…

this part of your code is a bit off too:

t1=SDL_GetTicks();
dt=t1-t0;
t0=t1;

that should be:

t0=t1;
t1=SDL_GetTicks();
dt=t1-t0;

give it a try and see if it doesnt clear anything up> ----- Original Message -----

From: luke_123@virgilio.it ()
To:
Sent: Monday, May 30, 2005 7:50 AM
Subject: [SDL] Smooth animation

Hi, thanks for your e-mails but the problem remains unsolved
Alan Wolfe: with delta time I have another problem…When I move the mouse
during the animation the animation slow down and I see flickers.

Try this please:

#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>

#define BALL 200

SDL_Surface *screen;

static float v=0.5,s=0;
static int dt,t0,t1;

void GameLogic()
{
s+=(v*(float)dt);
if(s<=0)
{
s=0;
v=-v;
}
if(s>=(float)(screen->w-BALL))
{
s=(float)(screen->w-BALL);
v=-v;
}
}

void Draw()
{
SDL_Rect dest;

dest.x=0;
dest.y=0;
dest.w=screen->w;
dest.h=screen->h;
SDL_FillRect(screen,&dest,SDL_MapRGB(screen->format,0,0,0));
dest.x=(int)s;
dest.y=(screen->h/2)-(BALL/2);
dest.w=BALL;
dest.h=BALL;
SDL_FillRect(screen,&dest,SDL_MapRGB(screen->format,255,255,255));
SDL_Flip(screen);
}

int main(int a, char *b[])
{
SDL_Event event;
char done=0;

if(SDL_Init(SDL_INIT_VIDEO)!=0)
{
exit(EXIT_FAILURE);
}
atexit(SDL_Quit);

screen=SDL_SetVideoMode(640,480,32,SDL_DOUBLEBUF|SDL_FULLSCREEN|SDL_HWSURFAC
E);
if(screen==NULL)
{
exit(EXIT_FAILURE);
}
t0=SDL_GetTicks();
while(!done)
{
t1=SDL_GetTicks();
dt=t1-t0;
t0=t1;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
done=1;
break;
}
}
GameLogic();
Draw();
}
return 0;
}

Regards

Luke


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hi, I’ve changed the code with your suggestion but when I move the mouse
I still see flickers…
Could you send me or paste here your code?
Do you use windows xp?

Thanks

i don’t have a simple enough example to be of any value to you
unfortunately ):

you mean your mouse cursor flickers right? or do you mean that the entire
screen flickers?

if just your mouse cursor, ive encountered this before in many different
kinds of programs and im not sure what exactly causes it, what you might do
is live with it for now and get your example working, and after that, handle
your own mouse cursor by disabling the cursor at startup with
"SDL_ShowCursor(SDL_DISABLE);" and then draw the art of a mouse cursor at
the mouse’s location.

That’s what i do and it works great> ----- Original Message -----

From: luke_123@virgilio.it ()
To:
Sent: Monday, May 30, 2005 1:42 PM
Subject: [SDL] Smooth animation

Hi, I’ve changed the code with your suggestion but when I move the mouse
I still see flickers…
Could you send me or paste here your code?
Do you use windows xp?

Thanks


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hey PS if you want send me your entire code and i can look at it here on my
comp and try and figure out what the problem might be> ----- Original Message -----

From: luke_123@virgilio.it ()
To:
Sent: Monday, May 30, 2005 1:42 PM
Subject: [SDL] Smooth animation

Hi, I’ve changed the code with your suggestion but when I move the mouse
I still see flickers…
Could you send me or paste here your code?
Do you use windows xp?

Thanks


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Actually, there’s no difference between these two pieces of code. The
second is just clearer because you don’t need to initialize t0.

c.

http://www.cesaremarilungo.com

Alan Wolfe wrote:>in my code moving the mouse doesnt affect anything…

this part of your code is a bit off too:

t1=SDL_GetTicks();
dt=t1-t0;
t0=t1;

that should be:

t0=t1;
t1=SDL_GetTicks();
dt=t1-t0;

give it a try and see if it doesnt clear anything up

----- Original Message -----
From: <luke_123 at virgilio.it>
To:
Sent: Monday, May 30, 2005 7:50 AM
Subject: [SDL] Smooth animation

Hi, thanks for your e-mails but the problem remains unsolved
Alan Wolfe: with delta time I have another problem…When I move the mouse
during the animation the animation slow down and I see flickers.

Try this please:

#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>

#define BALL 200

SDL_Surface *screen;

static float v=0.5,s=0;
static int dt,t0,t1;

void GameLogic()
{
s+=(v*(float)dt);
if(s<=0)
{
s=0;
v=-v;
}
if(s>=(float)(screen->w-BALL))
{
s=(float)(screen->w-BALL);
v=-v;
}
}

void Draw()
{
SDL_Rect dest;

dest.x=0;
dest.y=0;
dest.w=screen->w;
dest.h=screen->h;
SDL_FillRect(screen,&dest,SDL_MapRGB(screen->format,0,0,0));
dest.x=(int)s;
dest.y=(screen->h/2)-(BALL/2);
dest.w=BALL;
dest.h=BALL;
SDL_FillRect(screen,&dest,SDL_MapRGB(screen->format,255,255,255));
SDL_Flip(screen);
}

int main(int a, char *b[])
{
SDL_Event event;
char done=0;

if(SDL_Init(SDL_INIT_VIDEO)!=0)
{
exit(EXIT_FAILURE);
}
atexit(SDL_Quit);

screen=SDL_SetVideoMode(640,480,32,SDL_DOUBLEBUF|SDL_FULLSCREEN|SDL_HWSURFAC
E);
if(screen==NULL)
{
exit(EXIT_FAILURE);
}
t0=SDL_GetTicks();
while(!done)
{
t1=SDL_GetTicks();
dt=t1-t0;
t0=t1;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
done=1;
break;
}
}
GameLogic();
Draw();
}
return 0;
}

Regards

Luke


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

good point, at first glance i thought he was measuring the time period of
those 3 statements but i read it wrong (;

it sounds to me like his mouse cursor is just flashing (on redraw or
whatever mouse cursor isnt being properly handled), which ive experienced a
bunch before.> ----- Original Message -----

From: cesare@poeticstudios.com (Cesare Marilungo)
To: "A list for developers using the SDL library. (includes SDL-announce)"

Sent: Monday, May 30, 2005 4:35 PM
Subject: Re: [SDL] Smooth animation

Actually, there’s no difference between these two pieces of code. The
second is just clearer because you don’t need to initialize t0.

c.

http://www.cesaremarilungo.com

Alan Wolfe wrote:

in my code moving the mouse doesnt affect anything…

this part of your code is a bit off too:

t1=SDL_GetTicks();
dt=t1-t0;
t0=t1;

that should be:

t0=t1;
t1=SDL_GetTicks();
dt=t1-t0;

give it a try and see if it doesnt clear anything up

----- Original Message -----
From: <luke_123 at virgilio.it>
To:
Sent: Monday, May 30, 2005 7:50 AM
Subject: [SDL] Smooth animation

Hi, thanks for your e-mails but the problem remains unsolved
Alan Wolfe: with delta time I have another problem…When I move the
mouse

during the animation the animation slow down and I see flickers.

Try this please:

#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>

#define BALL 200

SDL_Surface *screen;

static float v=0.5,s=0;
static int dt,t0,t1;

void GameLogic()
{
s+=(v*(float)dt);
if(s<=0)
{
s=0;
v=-v;
}
if(s>=(float)(screen->w-BALL))
{
s=(float)(screen->w-BALL);
v=-v;
}
}

void Draw()
{
SDL_Rect dest;

dest.x=0;
dest.y=0;
dest.w=screen->w;
dest.h=screen->h;
SDL_FillRect(screen,&dest,SDL_MapRGB(screen->format,0,0,0));
dest.x=(int)s;
dest.y=(screen->h/2)-(BALL/2);
dest.w=BALL;
dest.h=BALL;
SDL_FillRect(screen,&dest,SDL_MapRGB(screen->format,255,255,255));
SDL_Flip(screen);
}

int main(int a, char *b[])
{
SDL_Event event;
char done=0;

if(SDL_Init(SDL_INIT_VIDEO)!=0)
{
exit(EXIT_FAILURE);
}
atexit(SDL_Quit);

screen=SDL_SetVideoMode(640,480,32,SDL_DOUBLEBUF|SDL_FULLSCREEN|SDL_HWSURFA
C

E);
if(screen==NULL)
{
exit(EXIT_FAILURE);
}
t0=SDL_GetTicks();
while(!done)
{
t1=SDL_GetTicks();
dt=t1-t0;
t0=t1;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
done=1;
break;
}
}
GameLogic();
Draw();
}
return 0;
}

Regards

Luke


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hi Wolfe I mean that the white square on the screen flikers not the mouse.
Please run my code and see the effect when you are moving the mouse. It’s
like SDL_PollEvent takes to much time and so the delta time becomes suddenly
big.

Thanks