fixed url-generator
[sdlgit/SDL-Site.git] / pages / SDL-Tutorial-Animation.html-inc
1 <div class="pod">
2 <!-- INDEX START -->
3 <h3 id="TOP">Index</h3>
4
5 <ul><li><a href="#NAME">NAME</a>
6 <ul><li><a href="#CATEGORY">CATEGORY</a></li>
7 </ul>
8 </li>
9 <li><a href="#SYNOPSIS">SYNOPSIS</a></li>
10 <li><a href="#ANIMATING_A_RECTANGLE">ANIMATING A RECTANGLE</a>
11 <ul><li><a href="#Redrawing_the_Screen">Redrawing the Screen</a></li>
12 <li><a href="#Undrawing_the_Updated_Rectangle">Undrawing the Updated Rectangle</a></li>
13 </ul>
14 </li>
15 <li><a href="#SEE_ALSO">SEE ALSO</a></li>
16 <li><a href="#AUTHOR">AUTHOR</a></li>
17 <li><a href="#BUGS">BUGS</a></li>
18 <li><a href="#COPYRIGHT">COPYRIGHT</a>
19 </li>
20 </ul><hr />
21 <!-- INDEX END --><a href="assets/Animation.jpg" target="_blank"><img src="assets/Animation.jpg" style="height: 160px" alt="Animation.jpg"/></a><hr />
22
23 <h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
24 <div id="NAME_CONTENT">
25 <p>SDL::Tutorial::Animation</p>
26
27 </div>
28 <h2 id="CATEGORY">CATEGORY</h2>
29 <div id="CATEGORY_CONTENT">
30 <p>Tutorials</p>
31
32 </div>
33 <h1 id="SYNOPSIS">SYNOPSIS</h1><p><a href="#TOP" class="toplink">Top</a></p>
34 <div id="SYNOPSIS_CONTENT">
35 <pre>   # to read this tutorial
36         $ perldoc SDL::Tutorial::Animation
37
38         # to create a demo animation program based on this tutorial
39         $ perl -MSDL::Tutorial::Animation=sdl_anim.pl -e 1
40
41 </pre>
42
43 </div>
44 <h1 id="ANIMATING_A_RECTANGLE">ANIMATING A RECTANGLE</h1><p><a href="#TOP" class="toplink">Top</a></p>
45 <div id="ANIMATING_A_RECTANGLE_CONTENT">
46 <p>Now that you can display a rectangle on the screen, the next step is to animate
47 that rectangle.  As with movies, there's no actual 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).</p>
48 <p>Okay, it's not that difficult.</p>
49 <p>There is one small difficulty to address, however.  Once you blit one surface
50 onto another, the destination is changed permanently.  There's no concept of
51 layers here unless you write it yourself.  If you fail to take this into
52 account (and just about everyone does at first), you'll end up with blurry
53 graphics moving around on the screen.</p>
54 <p>There are two approaches to solve this problem, redrawing the screen on every
55 frame and saving and restoring the background for every object drawn.</p>
56
57 </div>
58 <h2 id="Redrawing_the_Screen">Redrawing the Screen</h2>
59 <div id="Redrawing_the_Screen_CONTENT">
60 <p>Since you have to draw the screen in the right order once to start with it's
61 pretty easy to make this into a loop and redraw things in the right order for
62 every frame.  Given a <a href="SDL-App.html">SDL::App</a> object <code>$app</code>, a <a href="SDL-Rect.html">SDL::Rect</a> <code>$rect</code>, and
63 a <a href="SDL-Color.html">SDL::Color</a> <code>$color</code>, you only have to create a new SDL::Rect <code>$bg</code>,
64 representing the whole of the background surface and a new SDL::Color
65 <code>$bg_color</code>, representing the background color.  You can write a
66 <code>draw_frame()</code> function as follows:</p>
67 <p>&nbsp;</p>
68 <pre>   sub draw_frame
69         {
70                 my ($app, %args) = @_;
71
72                 $app-&gt;fill( $args{ bg }, $args{ bg_color } );
73                 $app-&gt;fill( $args{rect}, $args{rect_color} );
74                 $app-&gt;update( $args{bg} );
75         }
76
77 </pre>
78 <p>&nbsp;</p>
79 <p>Since you can change the <code>x</code> and <code>y</code> coordinates of a rect with the <code>x()</code>
80 and <code>y()</code> methods, you can move a rectangle across the screen with a loop like
81 this:</p>
82 <p>&nbsp;</p>
83 <pre>   for my $x (0 .. 640)
84         {
85                 $rect-&gt;x( $x );
86                 draw_frame( $app,
87                         bg   =&gt; $bg,   bg_color   =&gt; $bg_color,
88                         rect =&gt; $rect, rect_color =&gt; $color,
89                 );
90         }
91
92 </pre>
93 <p>&nbsp;</p>
94 <p>If <code>$rect</code>'s starting y position is 190 and its height and width are 100, the
95 rectangle (er, square) will move across the middle of the screen.</p>
96 <p>Provided you can keep track of the proper order in which to redraw rectangles
97 and provided you don't need the optimal speed necessary (since blitting every
98 object takes more work than just blitting the portions you need), this works
99 quite well.</p>
100
101 </div>
102 <h2 id="Undrawing_the_Updated_Rectangle">Undrawing the Updated Rectangle</h2>
103 <div id="Undrawing_the_Updated_Rectangle_CONT">
104 <p>If you need more speed or want to make a different complexity tradeoff, you can
105 take a snapshot of the destination rectangle <i>before</i> you blit onto it.  That
106 way, when you need to redraw, you can blit the old snapshot back before
107 blitting to the new position.</p>
108 <p><strong>Note:</strong>  I have no idea how this will work in the face of alpha blending,
109 which, admittedly, I haven't even mentioned yet.  If you don't know what this
110 means, forget it.  If you do know what this means and know why I'm waving my
111 hands here, feel free to explain what should and what does happen and why.  :)</p>
112 <p>With this technique, the frame-drawing subroutine has to be a little more
113 complicated.  Instead of the background rect, it needs a rect for the previous
114 position.  It also needs to do two updates (or must perform some scary math to
115 figure out the rectangle of the correct size to <code>update()</code>.  No thanks!).</p>
116 <p>&nbsp;</p>
117 <pre>   sub undraw_redraw_rect
118         {
119                 my ($app, %args) = @_;
120
121                 $app-&gt;fill(   $args{old_rect}, $args{bg_color}   );
122                 $app-&gt;fill(   $args{rect],     $args{rect_color} );
123                 $app-&gt;update( $args{old_rect}, $args{rect}       );
124         }
125
126 </pre>
127 <p>&nbsp;</p>
128 <p>We'll need to create a new SDL::Rect, <code>$old_rect</code>, that is a duplicate of
129 <code>$rect</code>, at the same position at first.  You should already know how to do
130 this.</p>
131 <p>As before, the loop to call <code>undraw_redraw_rect()</code> would look something like:</p>
132 <p>&nbsp;</p>
133 <pre>   for my $x (0 .. 640)
134         {
135                 $rect-&gt;x( $x );
136
137                 undraw_redraw_rect( $app,
138                         rect       =&gt; $rect,  old_rect =&gt; $old_rect,
139                         rect_color =&gt; $color, bg_color =&gt; $bgcolor,
140                 );
141
142                 $old_rect-&gt;x( $x );
143         }
144
145 </pre>
146 <p>&nbsp;</p>
147 <p>If you run this code, you'll probably notice that it's tremendously faster than
148 the previous version.  It may be too fast, where the alternate technique was
149 just fast enough.  There are a couple of good ways to set a fixed animation
150 speed regardless of the speed of the processor and graphics hardware (provided
151 they're good enough, which is increasingly often the case), and we'll get to
152 them soon.</p>
153
154 </div>
155 <h1 id="SEE_ALSO">SEE ALSO</h1><p><a href="#TOP" class="toplink">Top</a></p>
156 <div id="SEE_ALSO_CONTENT">
157 <dl>
158         <dt><a href="SDL-Tutorial-Drawing.html">SDL::Tutorial::Drawing</a></dt>
159         <dd>
160                 <p>basic drawing with SDL Perl</p>
161         </dd>
162         <dt><a href="SDL-Tutorial-Images.html">SDL::Tutorial::Images</a></dt>
163         <dd>
164                 <p>animating images</p>
165         </dd>
166 </dl>
167
168 </div>
169 <h1 id="AUTHOR">AUTHOR</h1><p><a href="#TOP" class="toplink">Top</a></p>
170 <div id="AUTHOR_CONTENT">
171 <p>chromatic, &lt;chromatic@wgz.org&gt;</p>
172 <p>Written for and maintained by the Perl SDL project, <a href="http://sdl.perl.org/">http://sdl.perl.org/</a>.</p>
173
174 </div>
175 <h1 id="BUGS">BUGS</h1><p><a href="#TOP" class="toplink">Top</a></p>
176 <div id="BUGS_CONTENT">
177 <p>No known bugs.</p>
178
179 </div>
180 <h1 id="COPYRIGHT">COPYRIGHT</h1><p><a href="#TOP" class="toplink">Top</a></p>
181 <div id="COPYRIGHT_CONTENT">
182 <p>Copyright (c) 2003 - 2004, chromatic.  All rights reserved.  This module is
183 distributed under the same terms as Perl itself, in the hope that it is useful
184 but certainly under no guarantee.
185 </p>
186
187 </div>
188 </div>