I am trying to write a simple animation in XPCE/Prolog for a project, but it is not smooth enough; it appears flashing or flickering. I run this on an ASUS N550jv laptop with Intel i7 (quad core, 2.4 Ghz+), so this shouldn't be a problem. The moving object is always the same (just drawing it at different positions), double-buffering is explicitly enabled, I'm using 40 FPS.
What could be done to improve the smoothness?
Here is the code I'm using:
sm:- %run this
sm(600,4,40). %runs animation over 600 pixels, 4 seconds long, with 40 FPS
sm(Length,Duration,FPS):-
(object( @window),!,free(@window);true), %check is window exists; if it does, delete it
(object( @floor), !, free( @floor); true), %check is floor exists; if it does, delete it
(object( @box), !, free( @box); true), %%check if box exists; if it does, delete it
new( @window,picture('Window',size(900,300))), %create window
send( @window,open), %display window
send(@window,buffered_update,true), %make sure to use double-buffering
new( @floor,box(800,10)), %create floor
send( @floor,fill_pattern,colour(green)), %colour floor
send( @window,display,@floor,point(50,70)), %show floor
new( @box,box(50,50)), %creates box
send( @box,fill_pattern,colour(yellow)), %colours box yellow
Frames is Duration*FPS, %how many frames are to be drawn
Step is Length/Frames, %what's the distance between steps
Delay is 1/FPS, %what is the delay between steps
send( @window,display,@box,point(50,20)), %shows Object
send( @window,flush), %flushes window
ani(Step,Delay,Frames,point(50,20)), %actual animation
sleep(1), %wait 1 second, and then
free( @box), %delete box
free( @floor), %delete floor
free( @window). %delete window (and exit)
ani(_,_,0,_).
ani(Step,Delay,Frames,point(X,Y)):-
send( @box,x(X+Step)), %moves box to the right
send( @window,flush), %flushes
repeat,
sleep(Delay),
FramesLeft is Frames - 1,
NewX is X + Step,
ani(Step,Delay,FramesLeft,point(NewX,Y)).
You should use a timer, it avoids sleep :