1SDL::Cookbook::PDL(3) User Contributed Perl DocumentationSDL::Cookbook::PDL(3)
2
3
4
6 SDL::CookBook::PDL -- CookBook for SDL + PDL
7
8 PDL provides great number crunching capabilities to Perl and SDL
9 provides game-developer quality real-time bitmapping and sound. You
10 can use PDL and SDL ''together'' to create real-time, responsive
11 animations and simulations. In this section we will go through the
12 pleasures and pitfalls of working with both powerhouse libraries.
13
14 CATEGORY
15 Cookbook
16
18 PDL's core type is a piddle. Once a piddle is provided to PDL it can go
19 do a numerous amounts of things. Please see the example in
20 'examples/cookbook/pdl.pl' that came with this module.
21
22 Creating a simple piddle
23 First lets get the right modules.
24
25 use PDL;
26 use SDL::Rect;
27 use SDL::Video;
28 use SDL::Surface;
29 use SDL::PixelFormat;
30
31 Suppose you want a surface of size (200,400) and 32 bit (RGBA).
32
33 my ( $bytes_per_pixel, $width, $height ) = ( 4, 200, 400 );
34
35 Define the $width, $height and $bytes_per_pixel. Your $bytes_per_pixel
36 is the number of bits (in this case 32) divided by 8 bits per byte.
37 Therefore for our 32 bpp we have 4 Bpp;
38
39 my $piddle = zeros( byte, $bytes_per_pixel, $width, $height );
40
41 Create a normal $piddle with zeros, byte format and the Bpp x width x
42 height dimensions.
43
44 my $pointer = $piddle->get_dataref();
45
46 Here is where we get the actual data the piddle is pointing to. We will
47 have SDL create a new surface from this function.
48
49 my $surface = SDL::Surface->new_from( $pointer, $width, $height, 32,
50 $width * $bytes_per_pixel );
51
52 Using the same dimensions we create the surface using new_form. The
53 width * Bpp is the scanline (pitch) of the surface in bytes.
54
55 warn "Made surface of $width, $height and ". $surface->format->BytesPerPixel;
56 return ( $piddle, $surface );
57
58 Finally make sure that the surface actually has the correct dimensions
59 we gave.
60
61 NOTE: $surface->format->BytesPerPixel must return 1,2,3,4. !!
62
63 Now you can blit and use the surface as needed; and do PDL operations
64 as required.
65
66 Operating on the Surface safely
67 To make sure SDL is in sync with the data. You must call
68 SDL::Video::lock before doing PDL operations on the piddle.
69
70 SDL::Video::lock_surface($surface);
71
72 $piddle->mslice( 'X', [ rand(400), rand(400), 1 ], [ rand(200), rand(200), 1 ] )
73 .= pdl( rand(225), rand(225), rand(255), 255 );
74
75 After that you can unlock the surface to blit.
76
77 SDL::Video::unlock_surface($surface);
78
79 Error due to BPP at blitting
80 When blitting the new surface check for the return value to see if
81 there has been a problem.
82
83 my $b = SDL::Video::blit_surface(
84 $surface, SDL::Rect->new( 0, 0, $surface->w, $surface->h ),
85 $app, SDL::Rect->new( ( $app->w - $surface->w ) / 2, ( $app->h - $surface->h ) / 2, $app->w, $app->h )
86 );
87
88 die "Could not blit: " . SDL::get_error() if ( $b == -1 );
89
90 If the error message is 'Blit combination not supported' that means
91 that the BPP is incorrect or inconsistent with the dimensions. After
92 that a simple update_rect will so your new surface on the screen.
93
95 See "AUTHORS" in SDL.
96
97
98
99perl v5.32.1 2021-01-27 SDL::Cookbook::PDL(3)