Updated
[sdlgit/SDL-Site.git] / pages / blog-0008.html-inc
index fe4b8d4..706ea53 100644 (file)
@@ -1,6 +1,77 @@
 <div class="blog">
 <h1 id="NAME">
-The beginnings of modular design for SDL Perl
+SDL RC 2.5 decides to play with PDL
 </h1>
 <div class="CONTENT">
-<div style="text-align: right;"><i>“Do or do not... there is no try.”</i><br /></div><div style="text-align: right;"><i>--yoda </i><br /><br /></div><h1>The design before</h1><br />The bindings before were all in <a href="http://github.com/kthakore/SDL_perl/blob/master/src/SDL.xs">one huge XS file</a>. This was then exported into the SDL module. This means that the XS file has to handle with macros if any component (e.x SDL_Mixer) is not compiled. Moreover having ever binding in one XS file prevents use to treat C structs as object with only one point of free and malloc. This would be BEGIN and DESTROY in Perl. Also the monolithic design introduces a lot of bugs because we have to use free and malloc all over the place. Lastly SDL monolithic design has the constructor for all structs in both Perl and in XS. <br /><br /><h1>The design we are aiming for</h1>Simple <a href="http://github.com/kthakore/SDL_perl/blob/master/src/Rect.xs">one XS</a> per Module. This would also simplify the Build code.<br /><br /><h2>First Step </h2><br />We have began with SDL Rect. It is in github master branch now. We are in the progress of making it back compatible. Originally SDL::Rect took named variables as parameters for new(). Now since the constructor <a href="http://github.com/kthakore/SDL_perl/blob/master/src/Rect.xs#L16">is in XS</a> we have only unnamed parameters. <br /><br /><br /><h3>Before</h3><br />SDL::Rect-&gt;new( -x =&gt; 0, -y =&gt; 0, -width =&gt; 0, -height =&gt; 0);<br /><br /><h3>After</h3><br />SDL::Rect-&gt;new(0, 0, 0, 0);<br /><br />Ideally we would like both ways of constructing Rect.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3102167581424744259-6151839795039915111?l=yapgh.blogspot.com' alt='' /></div></div></div>
\ No newline at end of file
+<i>PDL provides great number crunching capabilities to Perl and SDL provides game-developer quality real-time bitmapping and sound.<br />
+You can use PDL and SDL together to create real-time,<br />
+responsive animations and simulations.<br />
+In this section we will go through the pleasures and pitfalls of working with both powerhouse libraries.</i> <b>-- David Mertnes</b><br />
+<br />
+<br />
+<h1>Creating a SDL Surface piddle</h1><br />
+PDL's core type is a piddle.<br />
+Once a piddle is provided to PDL it can go do a numerous amounts of things.<br />
+Please see the example in '<a href="http://github.com/kthakore/SDL_perl/blob/master/examples/cookbook/pdl.pl">examples/cookbook/pdl.pl</a>' in github.<br />
+<br />
+<h3>Creating a simple piddle</h3><br />
+First lets get the right modules.<br />
+<br />
+<pre style='color:#d1d1d1;background:#000000;'>  <span style='color:#e66170; font-weight:bold; '>use</span> <span style='color:#b060b0; '>PDL;</span>
+  <span style='color:#e66170; font-weight:bold; '>use</span> <span style='color:#904050; '>SDL</span><span style='color:#d2cd86; '>::</span><span style='color:#904050; '>Rect</span><span style='color:#b060b0; '>;</span>
+  <span style='color:#e66170; font-weight:bold; '>use</span> <span style='color:#904050; '>SDL</span><span style='color:#d2cd86; '>::</span><span style='color:#904050; '>Video</span><span style='color:#b060b0; '>;</span>
+  <span style='color:#e66170; font-weight:bold; '>use</span> <span style='color:#904050; '>SDL</span><span style='color:#d2cd86; '>::</span><span style='color:#904050; '>Surface</span><span style='color:#b060b0; '>;</span>
+  <span style='color:#e66170; font-weight:bold; '>use</span> <span style='color:#904050; '>SDL</span><span style='color:#d2cd86; '>::</span><span style='color:#904050; '>PixelFormat</span><span style='color:#b060b0; '>;</span>
+</pre><br />
+Suppose you want a surface of size (200,400) and 32 bit (RGBA).<br />
+<br />
+<pre style='color:#d1d1d1;background:#000000;'><span style='color:#e66170; font-weight:bold; '>my</span> <span style='color:#d2cd86; '>(</span> $bytes_per_pixel<span style='color:#d2cd86; '>,</span> $width<span style='color:#d2cd86; '>,</span> $height <span style='color:#d2cd86; '>)</span> <span style='color:#d2cd86; '>=</span> <span style='color:#d2cd86; '>(</span> <span style='color:#00a800; '>4</span><span style='color:#d2cd86; '>,</span> <span style='color:#00a800; '>200</span><span style='color:#d2cd86; '>,</span> <span style='color:#00a800; '>400</span> <span style='color:#d2cd86; '>)</span><span style='color:#b060b0; '>;</span>
+</pre><br />
+Define the <b>$width</b>, <b>$height</b> and <b>$bytes_per_pixel</b>. Your <b>$bytes_per_pixel</b> is the number of bits (in this case 32) divided by 8 bits per byte. Therefore for our 32 bpp we have 4 Bpp;<br />
+<br />
+<pre style='color:#d1d1d1;background:#000000;'><span style='color:#e66170; font-weight:bold; '>my</span> $piddle  <span style='color:#d2cd86; '>=</span> zeros<span style='color:#d2cd86; '>(</span> byte<span style='color:#d2cd86; '>,</span> $bytes_per_pixel<span style='color:#d2cd86; '>,</span> $width<span style='color:#d2cd86; '>,</span> $height <span style='color:#d2cd86; '>)</span><span style='color:#b060b0; '>;</span>
+</pre><br />
+Create a normal $piddle with zeros, byte format and the Bpp x width x height dimensions.<br />
+<br />
+<pre style='color:#d1d1d1;background:#000000;'><span style='color:#e66170; font-weight:bold; '>my</span> $pointer <span style='color:#d2cd86; '>=</span> $piddle<span style='color:#d2cd86; '>-></span>get_dataref<span style='color:#d2cd86; '>(</span><span style='color:#d2cd86; '>)</span><span style='color:#b060b0; '>;</span>
+</pre><br />
+Here is where we get the acutal data the piddle is pointing to. We will have SDL create a new surface from this function.<br />
+<br />
+<pre style='color:#d1d1d1;background:#000000;'><span style='color:#e66170; font-weight:bold; '>my</span> $surface <span style='color:#d2cd86; '>=</span> <span style='color:#904050; '>SDL</span><span style='color:#d2cd86; '>::</span><span style='color:#904050; '>Surface</span><span style='color:#d2cd86; '>-></span>new_from<span style='color:#d2cd86; '>(</span> $pointer<span style='color:#d2cd86; '>,</span> $width<span style='color:#d2cd86; '>,</span> $height<span style='color:#d2cd86; '>,</span> <span style='color:#00a800; '>32</span><span style='color:#d2cd86; '>,</span>
+        $width <span style='color:#d2cd86; '>*</span> $bytes_per_pixel <span style='color:#d2cd86; '>)</span><span style='color:#b060b0; '>;</span>
+</pre><br />
+Using the same dimensions we create the surface using <b>SDL::Surface->new_form()</b>. The <b>$width * $Bpp</b> is the scanline (pitch) of the surface in bytes.<br />
+<br />
+<pre style='color:#d1d1d1;background:#000000;'><span style='color:#e66170; font-weight:bold; '>warn</span> <span style='color:#00c4c4; '>"Made surface of </span><span style='color:#00c4c4; '>$width</span><span style='color:#00c4c4; '>, </span><span style='color:#00c4c4; '>$height</span><span style='color:#00c4c4; '> and "</span><span style='color:#d2cd86; '>.</span> $surface<span style='color:#d2cd86; '>-></span>format<span style='color:#d2cd86; '>-></span>BytesPerPixel<span style='color:#b060b0; '>;</span>
+   <span style='color:#e66170; font-weight:bold; '>return</span> <span style='color:#d2cd86; '>(</span> $piddle<span style='color:#d2cd86; '>,</span> $surface <span style='color:#d2cd86; '>)</span><span style='color:#b060b0; '>;</span>
+</pre><br />
+Finally make sure that the surface acutally has the correct dimensions we gave.<br />
+<br />
+<b>NOTE:</b> <b>$surface->format->BytesPerPixel</b> must return 1,2,3,4. !!<br />
+<br />
+Now you can blit and use the surface as needed; and do PDL operations as required.<br />
+<br />
+<h3>Operating on the Surface safely</h3><br />
+To make sure SDL is in sync with the data. You must call SDL::Video::lock <b>before</b> doing PDL operations on the piddle.<br />
+<br />
+<pre><pre style='color:#d1d1d1;background:#000000;'><span style='color:#904050; '>SDL</span><span style='color:#d2cd86; '>::</span><span style='color:#904050; '>Video</span><span style='color:#d2cd86; '>:</span><span style='color:#d2cd86; '>:</span>lock_surface<span style='color:#d2cd86; '>(</span>$surface<span style='color:#d2cd86; '>)</span><span style='color:#b060b0; '>;</span>
+    $piddle <span style='color:#d2cd86; '>(</span> <span style='color:#d2cd86; '>:</span><span style='color:#d2cd86; '>,</span> <span style='color:#00a800; '>0</span> <span style='color:#d2cd86; '>:</span> <span style='color:#e66170; font-weight:bold; '>rand</span><span style='color:#d2cd86; '>(</span><span style='color:#00a800; '>400</span><span style='color:#d2cd86; '>)</span><span style='color:#d2cd86; '>,</span> <span style='color:#00a800; '>0</span> <span style='color:#d2cd86; '>:</span> <span style='color:#e66170; font-weight:bold; '>rand</span><span style='color:#d2cd86; '>(</span><span style='color:#00a800; '>200</span><span style='color:#d2cd86; '>)</span> <span style='color:#d2cd86; '>)</span> <span style='color:#d2cd86; '>.</span><span style='color:#d2cd86; '>=</span>   pdl<span style='color:#d2cd86; '>(</span> <span style='color:#e66170; font-weight:bold; '>rand</span><span style='color:#d2cd86; '>(</span><span style='color:#00a800; '>225</span><span style='color:#d2cd86; '>)</span><span style='color:#d2cd86; '>,</span> <span style='color:#e66170; font-weight:bold; '>rand</span><span style='color:#d2cd86; '>(</span><span style='color:#00a800; '>225</span><span style='color:#d2cd86; '>)</span><span style='color:#d2cd86; '>,</span> <span style='color:#e66170; font-weight:bold; '>rand</span><span style='color:#d2cd86; '>(</span><span style='color:#00a800; '>255</span><span style='color:#d2cd86; '>)</span><span style='color:#d2cd86; '>,</span> <span style='color:#00a800; '>255</span> <span style='color:#d2cd86; '>)</span><span style='color:#b060b0; '>;</span>
+</pre></pre><br />
+After that you can unlock the surface to blit.<br />
+<br />
+<pre style='color:#d1d1d1;background:#000000;'><span style='color:#904050; '>SDL</span><span style='color:#d2cd86; '>::</span><span style='color:#904050; '>Video</span><span style='color:#d2cd86; '>:</span><span style='color:#d2cd86; '>:</span>unlock_surface<span style='color:#d2cd86; '>(</span>$surface<span style='color:#d2cd86; '>)</span><span style='color:#b060b0; '>;</span>
+</pre><br />
+<h3>Errors due to BPP at blitting</h3><br />
+When blitting the new surface check for the return value to see if there has been a problem.<br />
+<br />
+<pre style='color:#d1d1d1;background:#000000;'><span style='color:#e66170; font-weight:bold; '>my</span> $b <span style='color:#d2cd86; '>=</span> <span style='color:#904050; '>SDL</span><span style='color:#d2cd86; '>::</span><span style='color:#904050; '>Video</span><span style='color:#d2cd86; '>:</span><span style='color:#d2cd86; '>:</span>blit_surface<span style='color:#d2cd86; '>(</span>
+        $surface<span style='color:#d2cd86; '>,</span>  <span style='color:#904050; '>SDL</span><span style='color:#d2cd86; '>::</span><span style='color:#904050; '>Rect</span><span style='color:#d2cd86; '>-></span>new<span style='color:#d2cd86; '>(</span> <span style='color:#00a800; '>0</span><span style='color:#d2cd86; '>,</span> <span style='color:#00a800; '>0</span><span style='color:#d2cd86; '>,</span> $surface<span style='color:#d2cd86; '>-></span>w<span style='color:#d2cd86; '>,</span> $surface<span style='color:#d2cd86; '>-></span>h <span style='color:#d2cd86; '>)</span><span style='color:#d2cd86; '>,</span>
+        $app<span style='color:#d2cd86; '>,</span> <span style='color:#904050; '>SDL</span><span style='color:#d2cd86; '>::</span><span style='color:#904050; '>Rect</span><span style='color:#d2cd86; '>-></span>new<span style='color:#d2cd86; '>(</span>  <span style='color:#d2cd86; '>(</span> $app<span style='color:#d2cd86; '>-></span>w <span style='color:#d2cd86; '>-</span> $surface<span style='color:#d2cd86; '>-></span>w <span style='color:#d2cd86; '>)</span> <span style='color:#d2cd86; '>/</span> <span style='color:#00a800; '>2</span><span style='color:#d2cd86; '>,</span> <span style='color:#d2cd86; '>(</span> $app<span style='color:#d2cd86; '>-></span>h <span style='color:#d2cd86; '>-</span> $surface<span style='color:#d2cd86; '>-></span>h <span style='color:#d2cd86; '>)</span> <span style='color:#d2cd86; '>/</span> <span style='color:#00a800; '>2</span><span style='color:#d2cd86; '>,</span> $app<span style='color:#d2cd86; '>-></span>w<span style='color:#d2cd86; '>,</span> $app<span style='color:#d2cd86; '>-></span>h <span style='color:#d2cd86; '>)</span>
+       <span style='color:#d2cd86; '>)</span><span style='color:#b060b0; '>;</span>
+    <span style='color:#e66170; font-weight:bold; '>die</span> <span style='color:#00c4c4; '>"Could not blit: "</span> <span style='color:#d2cd86; '>.</span> <span style='color:#904050; '>SDL</span><span style='color:#d2cd86; '>::</span><span style='color:#904050; '>get_error</span><span style='color:#d2cd86; '>(</span><span style='color:#d2cd86; '>)</span> <span style='color:#e66170; font-weight:bold; '>if</span> <span style='color:#d2cd86; '>(</span> $b <span style='color:#d2cd86; '>=</span><span style='color:#d2cd86; '>=</span> <span style='color:#d2cd86; '>-</span><span style='color:#00a800; '>1</span> <span style='color:#d2cd86; '>)</span><span style='color:#b060b0; '>;</span>
+</pre><br />
+If the error message is 'Blit combination not supported' that means that the BPP is incorrect or incosistent with the dimensions. After that a simple update_rect will so your new surface on the screen.<div class="blogger-post-footer"><img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3102167581424744259-3580429408870545769?l=yapgh.blogspot.com' alt='' /></div>
+<p><a href="http://feedads.g.doubleclick.net/~a/HRpCokqdJ-wzrP1ZXUvgwcUNL_s/0/da"><img src="http://feedads.g.doubleclick.net/~a/HRpCokqdJ-wzrP1ZXUvgwcUNL_s/0/di" border="0" ismap="true"></img></a><br/>
+<a href="http://feedads.g.doubleclick.net/~a/HRpCokqdJ-wzrP1ZXUvgwcUNL_s/1/da"><img src="http://feedads.g.doubleclick.net/~a/HRpCokqdJ-wzrP1ZXUvgwcUNL_s/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/YetAnotherPerlGameHackeryapgh/~4/KBG0kvzZCFo" height="1" width="1"/></div></div>
\ No newline at end of file