updated docs
[sdlgit/SDL-Site.git] / pages / SDL-Cookbook-PDL.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="#Creating_a_SDL_Surface_piddle">Creating a SDL Surface piddle</a>
10 <ul><li><a href="#Creating_a_simple_piddle">Creating a simple piddle</a></li>
11 <li><a href="#Operating_on_the_Surface_safely">Operating on the Surface safely</a></li>
12 <li><a href="#Error_due_to_BPP_at_blitting">Error due to BPP at blitting</a></li>
13 </ul>
14 </li>
15 <li><a href="#AUTHORS">AUTHORS</a>
16 </li>
17 </ul><hr />
18 <!-- INDEX END -->
19
20 <h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
21 <div id="NAME_CONTENT">
22 <p>SDL::CookBook::PDL -- CookBook for SDL + PDL</p>
23 <p>PDL provides great number crunching capabilities to Perl and SDL provides game-developer quality real-time bitmapping and sound.  You can use PDL and SDL ''together'' to create real-time, responsive animations and simulations.  In this section we will go through the pleasures and pitfalls of working with both powerhouse libraries.</p>
24
25
26
27
28
29 </div>
30 <h2 id="CATEGORY">CATEGORY</h2>
31 <div id="CATEGORY_CONTENT">
32 <p>Cookbook</p>
33
34 </div>
35 <h1 id="Creating_a_SDL_Surface_piddle">Creating a SDL Surface piddle</h1><p><a href="#TOP" class="toplink">Top</a></p>
36 <div id="Creating_a_SDL_Surface_piddle_CONTEN">
37 <p>PDL's core type is a piddle. Once a piddle is provided to PDL it can go do a numerous amounts of things. Please see the example in 'examples/cookbook/pdl.pl' that came with this module.</p>
38
39 </div>
40 <h2 id="Creating_a_simple_piddle">Creating a simple piddle</h2>
41 <div id="Creating_a_simple_piddle_CONTENT">
42 <p>First lets get the right modules.</p>
43 <pre>  use PDL;
44   use SDL::Rect;
45   use SDL::Video;
46   use SDL::Surface;
47   use SDL::PixelFormat;
48
49 </pre>
50 <p>Suppose you want a surface of size (200,400) and 32 bit (RGBA).</p>
51 <pre>   my ( $bytes_per_pixel, $width, $height ) = ( 4, 200, 400 );
52
53 </pre>
54 <p>Define the $width, $height and $bytes_per_pixel. Your $bytes_per_pixel is the number of bits (in this case 32) divided by 8 bits per byte. Therefore for our 32 bpp we have 4 Bpp;</p>
55 <pre>   my $piddle  = zeros( byte, $bytes_per_pixel, $width, $height );
56
57 </pre>
58 <p>Create a normal $piddle with zeros, byte format and the Bpp x width x height dimensions.</p>
59 <pre>   my $pointer = $piddle-&gt;get_dataref();
60
61 </pre>
62 <p>Here is where we get the acutal data the piddle is pointing to. We will have SDL create a new surface from this function.</p>
63 <pre>   my $surface = SDL::Surface-&gt;new_from( $pointer, $width, $height, 32,
64         $width * $bytes_per_pixel );
65
66 </pre>
67 <p>Using the same dimensions we create the surface using new_form. The width *  Bpp is the scanline (pitch) of the surface in bytes.</p>
68 <pre>   warn &quot;Made surface of $width, $height and &quot;. $surface-&gt;format-&gt;BytesPerPixel;
69    return ( $piddle, $surface );
70
71 </pre>
72 <p>Finally make sure that the surface acutally has the correct dimensions we gave.</p>
73 <p><strong>NOTE:</strong> $surface-&gt;format-&gt;BytesPerPixel must return 1,2,3,4. !!</p>
74 <p>Now you can blit and use the surface as needed; and do PDL operations as required.</p>
75
76
77
78
79
80 </div>
81 <h2 id="Operating_on_the_Surface_safely">Operating on the Surface safely</h2>
82 <div id="Operating_on_the_Surface_safely_CONT">
83 <p>To make sure SDL is in sync with the data. You must call SDL::Video::lock <strong>before</strong> doing PDL operations on the piddle.</p>
84 <pre>    SDL::Video::lock_surface($surface);
85
86     $piddle-&gt;mslice( 'X', [ rand(400), rand(400), 1 ], [ rand(200), rand(200), 1 ] )
87         .= pdl( rand(225), rand(225), rand(255), 255 );
88
89 </pre>
90 <p>After that you can unlock the surface to blit.</p>
91 <pre>    SDL::Video::unlock_surface($surface);
92
93 </pre>
94
95 </div>
96 <h2 id="Error_due_to_BPP_at_blitting">Error due to BPP at blitting</h2>
97 <div id="Error_due_to_BPP_at_blitting_CONTENT">
98 <p>When blitting the new surface check for the return value to see if there has been a problem.</p>
99 <pre>    my $b = SDL::Video::blit_surface(
100         $surface,  SDL::Rect-&gt;new( 0, 0, $surface-&gt;w, $surface-&gt;h ),
101         $app, SDL::Rect-&gt;new(  ( $app-&gt;w - $surface-&gt;w ) / 2, ( $app-&gt;h - $surface-&gt;h ) / 2, $app-&gt;w, $app-&gt;h )
102        );
103
104     die &quot;Could not blit: &quot; . SDL::get_error() if ( $b == -1 );
105
106 </pre>
107 <p>If the error message is 'Blit combination not supported' that means that the BPP is incorrect or incosistent with the dimensions.
108 After that a simple update_rect will so your new surface on the screen.</p>
109
110 </div>
111 <h1 id="AUTHORS">AUTHORS</h1><p><a href="#TOP" class="toplink">Top</a></p>
112 <div id="AUTHORS_CONTENT">
113 <p>See <b>AUTHORS</b> in <cite>SDL</cite>.</p>
114
115
116
117
118
119 </div>
120 </div>