updated old docs
[sdlgit/SDL-Site.git] / pages / SDL-Tutorial-Animation.html-inc
CommitLineData
b3ef54ec 1<div class="pod">
2<!-- INDEX START -->
3<h3 id="TOP">Index</h3>
4
60f74f6f 5<ul><li><a href="#NAME">NAME</a>
6<ul><li><a href="#CATEGORY">CATEGORY</a></li>
7</ul>
8</li>
b3ef54ec 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 />
cbc85b7f 21<!-- INDEX END --><a href="assets/Animation.jpg" target="_blank"><img src="assets/Animation.jpg" style="height: 160px" alt="Animation.jpg"/></a><hr />
b3ef54ec 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>
60f74f6f 28<h2 id="CATEGORY">CATEGORY</h2>
29<div id="CATEGORY_CONTENT">
30<p>Tutorials</p>
31
32</div>
b3ef54ec 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
47that 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
50onto another, the destination is changed permanently. There's no concept of
51layers here unless you write it yourself. If you fail to take this into
52account (and just about everyone does at first), you'll end up with blurry
53graphics moving around on the screen.</p>
54<p>There are two approaches to solve this problem, redrawing the screen on every
55frame 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
61pretty easy to make this into a loop and redraw things in the right order for
55bbf7a2 62every 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
63a <a href="SDL-Color.html">SDL::Color</a> <code>$color</code>, you only have to create a new SDL::Rect <code>$bg</code>,
b3ef54ec 64representing 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
1f5d8082 72 SDL::Video::fill_rect($app, $args{bg}, $args{bg_color} );
73 SDL::Video::fill_rect($app, $args{rect}, $args{rect_color} );
74 SDL::Video::update_rects($app, $args{bg} );
b3ef54ec 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>
80and <code>y()</code> methods, you can move a rectangle across the screen with a loop like
81this:</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
95rectangle (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
97and provided you don't need the optimal speed necessary (since blitting every
98object takes more work than just blitting the portions you need), this works
99quite 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
105take a snapshot of the destination rectangle <i>before</i> you blit onto it. That
106way, when you need to redraw, you can blit the old snapshot back before
107blitting to the new position.</p>
108<p><strong>Note:</strong> I have no idea how this will work in the face of alpha blending,
109which, admittedly, I haven't even mentioned yet. If you don't know what this
110means, forget it. If you do know what this means and know why I'm waving my
111hands 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
113complicated. Instead of the background rect, it needs a rect for the previous
114position. It also needs to do two updates (or must perform some scary math to
115figure 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
1f5d8082 121 SDL::Video::fill_rect($app, $args{old_rect}, $args{bg_color} );
122 SDL::Video::fill_rect($app, $args{rect}, $args{rect_color} );
123 SDL::Video::update_rects($app, $args{old_rect} );
124 SDL::Video::update_rects($app, $args{rect} );
b3ef54ec 125 }
126
127</pre>
128<p>&nbsp;</p>
129<p>We'll need to create a new SDL::Rect, <code>$old_rect</code>, that is a duplicate of
130<code>$rect</code>, at the same position at first. You should already know how to do
131this.</p>
132<p>As before, the loop to call <code>undraw_redraw_rect()</code> would look something like:</p>
133<p>&nbsp;</p>
134<pre> for my $x (0 .. 640)
135 {
136 $rect-&gt;x( $x );
137
138 undraw_redraw_rect( $app,
139 rect =&gt; $rect, old_rect =&gt; $old_rect,
140 rect_color =&gt; $color, bg_color =&gt; $bgcolor,
141 );
142
143 $old_rect-&gt;x( $x );
144 }
145
146</pre>
147<p>&nbsp;</p>
148<p>If you run this code, you'll probably notice that it's tremendously faster than
149the previous version. It may be too fast, where the alternate technique was
150just fast enough. There are a couple of good ways to set a fixed animation
151speed regardless of the speed of the processor and graphics hardware (provided
152they're good enough, which is increasingly often the case), and we'll get to
153them soon.</p>
154
155</div>
156<h1 id="SEE_ALSO">SEE ALSO</h1><p><a href="#TOP" class="toplink">Top</a></p>
157<div id="SEE_ALSO_CONTENT">
158<dl>
55bbf7a2 159 <dt><a href="SDL-Tutorial-Drawing.html">SDL::Tutorial::Drawing</a></dt>
b3ef54ec 160 <dd>
161 <p>basic drawing with SDL Perl</p>
162 </dd>
55bbf7a2 163 <dt><a href="SDL-Tutorial-Images.html">SDL::Tutorial::Images</a></dt>
b3ef54ec 164 <dd>
165 <p>animating images</p>
166 </dd>
167</dl>
168
169</div>
170<h1 id="AUTHOR">AUTHOR</h1><p><a href="#TOP" class="toplink">Top</a></p>
171<div id="AUTHOR_CONTENT">
172<p>chromatic, &lt;chromatic@wgz.org&gt;</p>
1f5d8082 173<p>updated by kthakore, &lt;kthakore@cpan.org&gt;</p>
b3ef54ec 174<p>Written for and maintained by the Perl SDL project, <a href="http://sdl.perl.org/">http://sdl.perl.org/</a>.</p>
175
176</div>
177<h1 id="BUGS">BUGS</h1><p><a href="#TOP" class="toplink">Top</a></p>
178<div id="BUGS_CONTENT">
179<p>No known bugs.</p>
180
181</div>
182<h1 id="COPYRIGHT">COPYRIGHT</h1><p><a href="#TOP" class="toplink">Top</a></p>
183<div id="COPYRIGHT_CONTENT">
184<p>Copyright (c) 2003 - 2004, chromatic. All rights reserved. This module is
185distributed under the same terms as Perl itself, in the hope that it is useful
186but certainly under no guarantee.
187</p>
188
189</div>
190</div>