Now that you can display a rectangle on the screen, the nextstep is to animate
that rectangle. As withmovies, there's noactual motion. Computer animations are just very very fast slideshows. The hard work is creating nearly identical images in every slide (or frame, in graphics terms).
Okay, it's not that difficult.
There is one small difficulty to address, however. Once you blit one surface
onto another, the destination is changed permanently. There's noconcept of
layers here unlessyou writeit yourself. If you fail to take this into
account (and just about everyone does at first), you'll end up withblurry
graphics moving aroundon the screen.
There are two approaches to solve this problem, redrawing the screen on every
frame and saving and restoring the background forevery object drawn.
=head2 Redrawing the Screen
Since you have to draw the screen in the right order once to start withit's
pretty easy to make this into a loop and redraw things in the right order for
every frame. Given a L<SDLx::App> object C<$app>, a L<SDL::Rect> C<$rect>, and
a L<SDL::Color> C<$color>, you only have to create a new SDL::Rect C<$bg>,
representing the whole of the background surface and a new mapped color
C<$bg_color>, representing the background color. The colors need to be mapped
to the formatof the current display. This is done by L<SDL::Video::map_RGB>.
S< >
my$color= SDL::Video::map_RGB (
$app->format,
$rect_r,
$rect_g,
$rect_b,
);
my$bg_color= SDL::Video::map_RGB (
$app->format,
$bg_r,
$bg_g,
$bg_b,
);
S< >
You can writea C<draw_frame()> function as follows: