added tuts
[sdlgit/SDL-Site.git] / pages / SDL-Tutorial-Animation.html-inc
diff --git a/pages/SDL-Tutorial-Animation.html-inc b/pages/SDL-Tutorial-Animation.html-inc
new file mode 100644 (file)
index 0000000..51fcdcf
--- /dev/null
@@ -0,0 +1,180 @@
+<div class="pod">
+<!-- INDEX START -->
+<h3 id="TOP">Index</h3>
+
+<ul><li><a href="#NAME">NAME</a></li>
+<li><a href="#SYNOPSIS">SYNOPSIS</a></li>
+<li><a href="#ANIMATING_A_RECTANGLE">ANIMATING A RECTANGLE</a>
+<ul><li><a href="#Redrawing_the_Screen">Redrawing the Screen</a></li>
+<li><a href="#Undrawing_the_Updated_Rectangle">Undrawing the Updated Rectangle</a></li>
+</ul>
+</li>
+<li><a href="#SEE_ALSO">SEE ALSO</a></li>
+<li><a href="#AUTHOR">AUTHOR</a></li>
+<li><a href="#BUGS">BUGS</a></li>
+<li><a href="#COPYRIGHT">COPYRIGHT</a>
+</li>
+</ul><hr />
+<!-- INDEX END -->
+
+<h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
+<div id="NAME_CONTENT">
+<p>SDL::Tutorial::Animation</p>
+
+</div>
+<h1 id="SYNOPSIS">SYNOPSIS</h1><p><a href="#TOP" class="toplink">Top</a></p>
+<div id="SYNOPSIS_CONTENT">
+<pre>  # to read this tutorial
+       $ perldoc SDL::Tutorial::Animation
+
+       # to create a demo animation program based on this tutorial
+       $ perl -MSDL::Tutorial::Animation=sdl_anim.pl -e 1
+
+</pre>
+
+</div>
+<h1 id="ANIMATING_A_RECTANGLE">ANIMATING A RECTANGLE</h1><p><a href="#TOP" class="toplink">Top</a></p>
+<div id="ANIMATING_A_RECTANGLE_CONTENT">
+<p>Now that you can display a rectangle on the screen, the next step is to animate
+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>
+<p>Okay, it's not that difficult.</p>
+<p>There is one small difficulty to address, however.  Once you blit one surface
+onto another, the destination is changed permanently.  There's no concept of
+layers here unless you write it yourself.  If you fail to take this into
+account (and just about everyone does at first), you'll end up with blurry
+graphics moving around on the screen.</p>
+<p>There are two approaches to solve this problem, redrawing the screen on every
+frame and saving and restoring the background for every object drawn.</p>
+
+</div>
+<h2 id="Redrawing_the_Screen">Redrawing the Screen</h2>
+<div id="Redrawing_the_Screen_CONTENT">
+<p>Since you have to draw the screen in the right order once to start with it's
+pretty easy to make this into a loop and redraw things in the right order for
+every frame.  Given a <cite>SDL::App</cite> object <code>$app</code>, a <cite>SDL::Rect</cite> <code>$rect</code>, and
+a <cite>SDL::Color</cite> <code>$color</code>, you only have to create a new SDL::Rect <code>$bg</code>,
+representing the whole of the background surface and a new SDL::Color
+<code>$bg_color</code>, representing the background color.  You can write a
+<code>draw_frame()</code> function as follows:</p>
+<p>&nbsp;</p>
+<pre>  sub draw_frame
+       {
+               my ($app, %args) = @_;
+
+               $app-&gt;fill( $args{ bg }, $args{ bg_color } );
+               $app-&gt;fill( $args{rect}, $args{rect_color} );
+               $app-&gt;update( $args{bg} );
+       }
+
+</pre>
+<p>&nbsp;</p>
+<p>Since you can change the <code>x</code> and <code>y</code> coordinates of a rect with the <code>x()</code>
+and <code>y()</code> methods, you can move a rectangle across the screen with a loop like
+this:</p>
+<p>&nbsp;</p>
+<pre>  for my $x (0 .. 640)
+       {
+               $rect-&gt;x( $x );
+               draw_frame( $app,
+                       bg   =&gt; $bg,   bg_color   =&gt; $bg_color,
+                       rect =&gt; $rect, rect_color =&gt; $color,
+               );
+       }
+
+</pre>
+<p>&nbsp;</p>
+<p>If <code>$rect</code>'s starting y position is 190 and its height and width are 100, the
+rectangle (er, square) will move across the middle of the screen.</p>
+<p>Provided you can keep track of the proper order in which to redraw rectangles
+and provided you don't need the optimal speed necessary (since blitting every
+object takes more work than just blitting the portions you need), this works
+quite well.</p>
+
+</div>
+<h2 id="Undrawing_the_Updated_Rectangle">Undrawing the Updated Rectangle</h2>
+<div id="Undrawing_the_Updated_Rectangle_CONT">
+<p>If you need more speed or want to make a different complexity tradeoff, you can
+take a snapshot of the destination rectangle <i>before</i> you blit onto it.  That
+way, when you need to redraw, you can blit the old snapshot back before
+blitting to the new position.</p>
+<p><strong>Note:</strong>  I have no idea how this will work in the face of alpha blending,
+which, admittedly, I haven't even mentioned yet.  If you don't know what this
+means, forget it.  If you do know what this means and know why I'm waving my
+hands here, feel free to explain what should and what does happen and why.  :)</p>
+<p>With this technique, the frame-drawing subroutine has to be a little more
+complicated.  Instead of the background rect, it needs a rect for the previous
+position.  It also needs to do two updates (or must perform some scary math to
+figure out the rectangle of the correct size to <code>update()</code>.  No thanks!).</p>
+<p>&nbsp;</p>
+<pre>  sub undraw_redraw_rect
+       {
+               my ($app, %args) = @_;
+
+               $app-&gt;fill(   $args{old_rect}, $args{bg_color}   );
+               $app-&gt;fill(   $args{rect],     $args{rect_color} );
+               $app-&gt;update( $args{old_rect}, $args{rect}       );
+       }
+
+</pre>
+<p>&nbsp;</p>
+<p>We'll need to create a new SDL::Rect, <code>$old_rect</code>, that is a duplicate of
+<code>$rect</code>, at the same position at first.  You should already know how to do
+this.</p>
+<p>As before, the loop to call <code>undraw_redraw_rect()</code> would look something like:</p>
+<p>&nbsp;</p>
+<pre>  for my $x (0 .. 640)
+       {
+               $rect-&gt;x( $x );
+
+               undraw_redraw_rect( $app,
+                       rect       =&gt; $rect,  old_rect =&gt; $old_rect,
+                       rect_color =&gt; $color, bg_color =&gt; $bgcolor,
+               );
+
+               $old_rect-&gt;x( $x );
+       }
+
+</pre>
+<p>&nbsp;</p>
+<p>If you run this code, you'll probably notice that it's tremendously faster than
+the previous version.  It may be too fast, where the alternate technique was
+just fast enough.  There are a couple of good ways to set a fixed animation
+speed regardless of the speed of the processor and graphics hardware (provided
+they're good enough, which is increasingly often the case), and we'll get to
+them soon.</p>
+
+</div>
+<h1 id="SEE_ALSO">SEE ALSO</h1><p><a href="#TOP" class="toplink">Top</a></p>
+<div id="SEE_ALSO_CONTENT">
+<dl>
+       <dt><cite>SDL::Tutorial::Drawing</cite></dt>
+       <dd>
+               <p>basic drawing with SDL Perl</p>
+       </dd>
+       <dt><cite>SDL::Tutorial::Images</cite></dt>
+       <dd>
+               <p>animating images</p>
+       </dd>
+</dl>
+
+</div>
+<h1 id="AUTHOR">AUTHOR</h1><p><a href="#TOP" class="toplink">Top</a></p>
+<div id="AUTHOR_CONTENT">
+<p>chromatic, &lt;chromatic@wgz.org&gt;</p>
+<p>Written for and maintained by the Perl SDL project, <a href="http://sdl.perl.org/">http://sdl.perl.org/</a>.</p>
+
+</div>
+<h1 id="BUGS">BUGS</h1><p><a href="#TOP" class="toplink">Top</a></p>
+<div id="BUGS_CONTENT">
+<p>No known bugs.</p>
+
+</div>
+<h1 id="COPYRIGHT">COPYRIGHT</h1><p><a href="#TOP" class="toplink">Top</a></p>
+<div id="COPYRIGHT_CONTENT">
+<p>Copyright (c) 2003 - 2004, chromatic.  All rights reserved.  This module is
+distributed under the same terms as Perl itself, in the hope that it is useful
+but certainly under no guarantee.
+</p>
+
+</div>
+</div>
\ No newline at end of file