Commit | Line | Data |
162a0989 |
1 | <div class="pod"> |
2 | <!-- INDEX START --> |
3 | <h3 id="TOP">Index</h3> |
4 | |
879e3e79 |
5 | <ul><li><a href="#NAME">NAME</a> |
60f74f6f |
6 | <ul><li><a href="#CATEGORY">CATEGORY</a></li> |
7 | </ul> |
8 | </li> |
ca0a3441 |
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> |
162a0989 |
13 | </li> |
162a0989 |
14 | </ul> |
15 | </li> |
162a0989 |
16 | </ul><hr /> |
17 | <!-- INDEX END --> |
18 | |
879e3e79 |
19 | <h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p> |
20 | <div id="NAME_CONTENT"> |
21 | <p>SDL::CookBook::PDL -- CookBook for SDL + PDL</p> |
162a0989 |
22 | <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> |
879e3e79 |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | </div> |
60f74f6f |
29 | <h2 id="CATEGORY">CATEGORY</h2> |
30 | <div id="CATEGORY_CONTENT"> |
31 | <p>Cookbook</p> |
32 | |
33 | </div> |
ca0a3441 |
34 | <h1 id="Creating_a_SDL_Surface_piddle">Creating a SDL Surface piddle</h1><p><a href="#TOP" class="toplink">Top</a></p> |
35 | <div id="Creating_a_SDL_Surface_piddle_CONTEN"> |
36 | <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> |
162a0989 |
37 | |
38 | </div> |
ca0a3441 |
39 | <h2 id="Creating_a_simple_piddle">Creating a simple piddle</h2> |
40 | <div id="Creating_a_simple_piddle_CONTENT"> |
41 | <p>First lets get the right modules.</p> |
42 | <pre> use PDL; |
43 | use SDL::Rect; |
44 | use SDL::Video; |
45 | use SDL::Surface; |
46 | use SDL::PixelFormat; |
162a0989 |
47 | |
48 | </pre> |
ca0a3441 |
49 | <p>Suppose you want a surface of size (200,400) and 32 bit (RGBA).</p> |
50 | <pre> my ( $bytes_per_pixel, $width, $height ) = ( 4, 200, 400 ); |
162a0989 |
51 | |
52 | </pre> |
ca0a3441 |
53 | <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> |
54 | <pre> my $piddle = zeros( byte, $bytes_per_pixel, $width, $height ); |
162a0989 |
55 | |
56 | </pre> |
ca0a3441 |
57 | <p>Create a normal $piddle with zeros, byte format and the Bpp x width x height dimensions.</p> |
58 | <pre> my $pointer = $piddle->get_dataref(); |
162a0989 |
59 | |
60 | </pre> |
ca0a3441 |
61 | <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> |
62 | <pre> my $surface = SDL::Surface->new_from( $pointer, $width, $height, 32, |
63 | $width * $bytes_per_pixel ); |
162a0989 |
64 | |
65 | </pre> |
ca0a3441 |
66 | <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> |
67 | <pre> warn "Made surface of $width, $height and ". $surface->format->BytesPerPixel; |
68 | return ( $piddle, $surface ); |
162a0989 |
69 | |
70 | </pre> |
ca0a3441 |
71 | <p>Finally make sure that the surface acutally has the correct dimensions we gave.</p> |
72 | <p><strong>NOTE:</strong> $surface->format->BytesPerPixel must return 1,2,3,4. !!</p> |
73 | <p>Now you can blit and use the surface as needed; and do PDL operations as required.</p> |
162a0989 |
74 | |
162a0989 |
75 | |
162a0989 |
76 | |
162a0989 |
77 | |
162a0989 |
78 | |
79 | </div> |
ca0a3441 |
80 | <h2 id="Operating_on_the_Surface_safely">Operating on the Surface safely</h2> |
81 | <div id="Operating_on_the_Surface_safely_CONT"> |
82 | <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> |
83 | <pre> SDL::Video::lock_surface($surface); |
162a0989 |
84 | |
ca0a3441 |
85 | $piddle ( :, 0 : rand(400), 0 : rand(200) ) .= pdl( rand(225), rand(225), rand(255), 255 ); |
162a0989 |
86 | |
87 | </pre> |
ca0a3441 |
88 | <p>After that you can unlock the surface to blit.</p> |
89 | <pre> SDL::Video::unlock_surface($surface); |
162a0989 |
90 | |
91 | </pre> |
162a0989 |
92 | |
93 | </div> |
ca0a3441 |
94 | <h2 id="Error_due_to_BPP_at_blitting">Error due to BPP at blitting</h2> |
95 | <div id="Error_due_to_BPP_at_blitting_CONTENT"> |
96 | <p>When blitting the new surface check for the return value to see if there has been a problem.</p> |
97 | <pre> my $b = SDL::Video::blit_surface( |
98 | $surface, SDL::Rect->new( 0, 0, $surface->w, $surface->h ), |
99 | $app, SDL::Rect->new( ( $app->w - $surface->w ) / 2, ( $app->h - $surface->h ) / 2, $app->w, $app->h ) |
100 | ); |
162a0989 |
101 | |
ca0a3441 |
102 | die "Could not blit: " . SDL::get_error() if ( $b == -1 ); |
162a0989 |
103 | |
104 | </pre> |
ca0a3441 |
105 | <p>If the error message is 'Blit combination not supported' that means that the BPP is incorrect or incosistent with the dimensions. |
106 | After that a simple update_rect will so your new surface on the screen.</p> |
162a0989 |
107 | |
108 | </div> |
109 | </div> |