3 <h3 id="TOP">Index</h3>
7 <ul><li><a href="#Redrawing_the_Screen">Redrawing the Screen</a></li>
8 <li><a href="#Undrawing_the_Updated_Rectangle">Undrawing the Updated Rectangle</a></li>
11 <li><a href="#SEE_ALSO">SEE ALSO</a></li>
12 <li><a href="#AUTHOR">AUTHOR</a></li>
13 <li><a href="#BUGS">BUGS</a></li>
14 <li><a href="#COPYRIGHT">COPYRIGHT</a>
17 <!-- INDEX END --><a href="assets/Animation.jpg" target="_blank"><img src="assets/Animation.jpg" style="height: 160px" alt="Animation.jpg"/></a><hr />
19 <h2 id="Redrawing_the_Screen">Redrawing the Screen</h2>
20 <div id="Redrawing_the_Screen_CONTENT">
21 <p>Since you have to draw the screen in the right order once to start with it's
22 pretty easy to make this into a loop and redraw things in the right order for
23 every frame. Given a <a href="http://search.cpan.org/perldoc?SDLx::App">SDLx::App</a> object <code>$app</code>, a <a href="SDL-Rect.html">SDL::Rect</a> <code>$rect</code>, and
24 a <a href="SDL-Color.html">SDL::Color</a> <code>$color</code>, you only have to create a new SDL::Rect <code>$bg</code>,
25 representing the whole of the background surface and a new mapped color
26 <code>$bg_color</code>, representing the background color. The colors need to be mapped
27 to the format of the current display. This is done by <a href="/SDL-Video.html#map_RGB">SDL::Video::map_RGB</a>.</p>
29 <p>my $color = SDL::Video::map_RGB (
35 <p>my $bg_color = SDL::Video::map_RGB (
49 <p>You can write a <code>draw_frame()</code> function as follows:</p>
53 my ($app, %args) = @_;
55 SDL::Video::fill_rect($app, $args{bg}, $args{bg_color} );
56 SDL::Video::fill_rect($app, $args{rect}, $args{rect_color} );
57 SDL::Video::update_rects($app, $args{bg} );
62 <p>Since you can change the <code>x</code> and <code>y</code> coordinates of a rect with the <code>x()</code>
63 and <code>y()</code> methods, you can move a rectangle across the screen with a loop like
66 <pre> for my $x (0 .. 640)
70 bg => $bg, bg_color => $bg_color,
71 rect => $rect, rect_color => $color,
77 <p>If <code>$rect</code>'s starting y position is 190 and its height and width are 100, the
78 rectangle (er, square) will move across the middle of the screen.</p>
79 <p>Provided you can keep track of the proper order in which to redraw rectangles
80 and provided you don't need the optimal speed necessary (since blitting every
81 object takes more work than just blitting the portions you need), this works
85 <h2 id="Undrawing_the_Updated_Rectangle">Undrawing the Updated Rectangle</h2>
86 <div id="Undrawing_the_Updated_Rectangle_CONT">
87 <p>If you need more speed or want to make a different complexity tradeoff, you can
88 take a snapshot of the destination rectangle <i>before</i> you blit onto it. That
89 way, when you need to redraw, you can blit the old snapshot back before
90 blitting to the new position.</p>
91 <p><strong>Note:</strong> I have no idea how this will work in the face of alpha blending,
92 which, admittedly, I haven't even mentioned yet. If you don't know what this
93 means, forget it. If you do know what this means and know why I'm waving my
94 hands here, feel free to explain what should and what does happen and why. :)</p>
95 <p>With this technique, the frame-drawing subroutine has to be a little more
96 complicated. Instead of the background rect, it needs a rect for the previous
97 position. It also needs to do two updates (or must perform some scary math to
98 figure out the rectangle of the correct size to <code>update()</code>. No thanks!).</p>
100 <pre> sub undraw_redraw_rect
102 my ($app, %args) = @_;
104 SDL::Video::fill_rect($app, $args{old_rect}, $args{bg_color} );
105 SDL::Video::fill_rect($app, $args{rect}, $args{rect_color} );
106 SDL::Video::update_rects($app, $args{old_rect} );
107 SDL::Video::update_rects($app, $args{rect} );
112 <p>We'll need to create a new SDL::Rect, <code>$old_rect</code>, that is a duplicate of
113 <code>$rect</code>, at the same position at first. You should already know how to do
115 <p>As before, the loop to call <code>undraw_redraw_rect()</code> would look something like:</p>
117 <pre> for my $x (0 .. 640)
121 undraw_redraw_rect( $app,
122 rect => $rect, old_rect => $old_rect,
123 rect_color => $color, bg_color => $bgcolor,
126 $old_rect->x( $x );
131 <p>If you run this code, you'll probably notice that it's tremendously faster than
132 the previous version. It may be too fast, where the alternate technique was
133 just fast enough. There are a couple of good ways to set a fixed animation
134 speed regardless of the speed of the processor and graphics hardware (provided
135 they're good enough, which is increasingly often the case), and we'll get to
139 <h1 id="SEE_ALSO">SEE ALSO</h1><p><a href="#TOP" class="toplink">Top</a></p>
140 <div id="SEE_ALSO_CONTENT">
142 <dt><a href="SDL-Tutorial-Drawing.html">SDL::Tutorial::Drawing</a></dt>
144 <p>basic drawing with SDL Perl</p>
146 <dt><a href="SDL-Tutorial-Images.html">SDL::Tutorial::Images</a></dt>
148 <p>animating images</p>
153 <h1 id="AUTHOR">AUTHOR</h1><p><a href="#TOP" class="toplink">Top</a></p>
154 <div id="AUTHOR_CONTENT">
155 <p>chromatic, <chromatic@wgz.org></p>
156 <p>updated by kthakore, <kthakore@cpan.org></p>
157 <p>Written for and maintained by the Perl SDL project, <a href="http://sdl.perl.org/">http://sdl.perl.org/</a>.</p>
160 <h1 id="BUGS">BUGS</h1><p><a href="#TOP" class="toplink">Top</a></p>
161 <div id="BUGS_CONTENT">
162 <p>No known bugs.</p>
165 <h1 id="COPYRIGHT">COPYRIGHT</h1><p><a href="#TOP" class="toplink">Top</a></p>
166 <div id="COPYRIGHT_CONTENT">
167 <p>Copyright (c) 2003 - 2004, chromatic. All rights reserved. This module is
168 distributed under the same terms as Perl itself, in the hope that it is useful
169 but certainly under no guarantee.