Commit | Line | Data |
8fde61e3 |
1 | # |
2 | # Surface.pm |
3 | # |
4 | # A package for manipulating SDL_Surface * |
5 | # |
6 | # Copyright (C) 2003 David J. Goehrig |
7 | |
8 | package SDL::Surface; |
9 | |
10 | use strict; |
084b921f |
11 | use warnings; |
12 | use Carp; |
8fde61e3 |
13 | use SDL; |
14 | use SDL::SFont; |
15 | use SDL::Color; |
16 | use SDL::Rect; |
17 | |
18 | sub new { |
19 | my $proto = shift; |
20 | my $class = ref($proto) || $proto; |
21 | my %options = @_; |
22 | my $self; |
23 | |
24 | verify (%options, qw/ -name -n -flags -fl -width -w -height -h -depth -d |
25 | -pitch -p -Rmask -r -Gmask -g -Bmask -b -Amask -a |
26 | -from -f /) if $SDL::DEBUG; |
27 | |
28 | if ( defined($options{-name}) && $options{-name} ne "" && exists $SDL::{IMGLoad} ) { |
29 | $self = \SDL::IMGLoad($options{-name}); |
30 | } else { |
31 | my $f = $options{-flags} || $options{-fl} || SDL::SDL_ANYFORMAT(); |
32 | my $w = $options{-width} || $options{-w} || 1; |
33 | my $h = $options{-height} || $options{-h} || 1; |
34 | my $d = $options{-depth} || $options{-d} || 8; |
35 | my $p = $options{-pitch} || $options{-p} || $w*$d; |
36 | my $r = $options{-Rmask} || $options{-r} |
37 | || ( SDL::BigEndian() ? 0xff000000 : 0x000000ff ); |
38 | my $g = $options{-Gmask} || $options{-g} |
39 | || ( SDL::BigEndian() ? 0x00ff0000 : 0x0000ff00 ); |
40 | my $b = $options{-Bmask} || $options{-b} |
41 | || ( SDL::BigEndian() ? 0x0000ff00 : 0x00ff0000 ); |
42 | my $a = $options{-Amask} || $options{-a} |
43 | || ( SDL::BigEndian() ? 0x000000ff : 0xff000000 ); |
44 | |
45 | if ( $options{-from}|| $options{-f} ) { |
46 | my $src = $options{-from}|| $options{-f}; |
47 | $self = \SDL::CreateRGBSurfaceFrom($src,$w,$h,$d,$p,$r,$g,$b,$a); |
48 | } else { |
49 | $self = \SDL::CreateRGBSurface($f,$w,$h,$d,$r,$g,$b,$a); |
50 | } |
51 | } |
084b921f |
52 | croak "SDL::Surface::new failed. ", SDL::GetError() |
8fde61e3 |
53 | unless ( $$self); |
54 | bless $self,$class; |
55 | return $self; |
56 | } |
57 | |
58 | sub DESTROY { |
59 | SDL::FreeSurface(${$_[0]}); |
60 | } |
61 | |
62 | sub flags { |
63 | SDL::SurfaceFlags(${$_[0]}); |
64 | } |
65 | |
66 | sub palette { |
67 | SDL::SurfacePalette(${$_[0]}); |
68 | } |
69 | |
70 | sub bpp { |
71 | SDL::SurfaceBitsPerPixel(${$_[0]}); |
72 | } |
73 | |
74 | sub bytes_per_pixel { |
75 | SDL::SurfaceBytesPerPixel(${$_[0]}); |
76 | } |
77 | |
78 | sub Rshift { |
79 | SDL::SurfaceRshift(${$_[0]}); |
80 | } |
81 | |
82 | sub Gshift { |
83 | SDL::SurfaceGshift(${$_[0]}); |
84 | } |
85 | |
86 | sub Bshift { |
87 | SDL::SurfaceBshift(${$_[0]}); |
88 | } |
89 | |
90 | sub Ashift { |
91 | SDL::SurfaceAshift(${$_[0]}); |
92 | } |
93 | |
94 | sub Rmask { |
95 | SDL::SurfaceRmask(${$_[0]}); |
96 | } |
97 | |
98 | sub Gmask { |
99 | SDL::SurfaceGmask(${$_[0]}); |
100 | } |
101 | |
102 | sub Bmask { |
103 | SDL::SurfaceBmask(${$_[0]}); |
104 | } |
105 | |
106 | sub Amask { |
107 | SDL::SurfaceAmask(${$_[0]}); |
108 | } |
109 | |
110 | sub color_key { |
111 | SDL::SurfaceColorKey(${$_[0]}); |
112 | } |
113 | |
114 | sub alpha { |
115 | SDL::SurfaceAlpha(${$_[0]}); |
116 | } |
117 | |
118 | sub width { |
119 | SDL::SurfaceW(${$_[0]}); |
120 | } |
121 | |
122 | sub height { |
123 | SDL::SurfaceH(${$_[0]}); |
124 | } |
125 | |
126 | sub pitch { |
127 | SDL::SurfacePitch(${$_[0]}); |
128 | } |
129 | |
130 | sub pixels { |
131 | SDL::SurfacePixels(${$_[0]}); |
132 | } |
133 | |
134 | sub pixel { |
084b921f |
135 | croak "SDL::Surface::pixel requires a SDL::Color" |
8fde61e3 |
136 | if $_[3] && $SDL::DEBUG && !$_[3]->isa("SDL::Color"); |
137 | $_[3] ? |
138 | new SDL::Color -color => SDL::SurfacePixel(${$_[0]},$_[1],$_[2],${$_[3]}) : |
139 | new SDL::Color -color => SDL::SurfacePixel(${$_[0]},$_[1],$_[2]); |
140 | } |
141 | |
142 | sub fill { |
084b921f |
143 | croak "SDL::Surface::fill requires a SDL::Rect object" |
8fde61e3 |
144 | unless !$SDL::DEBUG || $_[1] == 0 || $_[1]->isa('SDL::Rect'); |
084b921f |
145 | croak "SDL::Surface::fill requires a SDL::Color object" |
8fde61e3 |
146 | unless !$SDL::DEBUG || $_[2]->isa('SDL::Color'); |
147 | if ($_[1] == 0 ) { |
148 | SDL::FillRect(${$_[0]},0,${$_[2]}); |
149 | } else { |
150 | SDL::FillRect(${$_[0]},${$_[1]},${$_[2]}); |
151 | } |
152 | } |
153 | |
154 | sub lockp { |
155 | SDL::MUSTLOCK(${$_[0]}); |
156 | } |
157 | |
158 | sub lock { |
159 | SDL::SurfaceLock(${$_[0]}); |
160 | } |
161 | |
162 | sub unlock { |
163 | SDL::SurfaceUnlock(${$_[0]}); |
164 | } |
165 | |
166 | sub update { |
167 | my $self = shift;; |
168 | if ($SDL::DEBUG) { |
169 | for (@_) { |
084b921f |
170 | croak "SDL::Surface::update requires SDL::Rect objects" |
8fde61e3 |
171 | unless $_->isa('SDL::Rect'); |
172 | } |
173 | } |
174 | SDL::UpdateRects($$self, map { ${$_} } @_ ); |
175 | } |
176 | |
177 | sub flip { |
178 | SDL::Flip(${$_[0]}); |
179 | } |
180 | |
181 | sub blit { |
182 | if ($SDL::DEBUG) { |
084b921f |
183 | croak "SDL::Surface::blit requires SDL::Rect objects" |
8fde61e3 |
184 | unless ($_[1] == 0 || $_[1]->isa('SDL::Rect')) |
185 | && ($_[3] == 0 || $_[3]->isa('SDL::Rect')); |
084b921f |
186 | croak "SDL::Surface::blit requires SDL::Surface objects" |
8fde61e3 |
187 | unless $_[2]->isa('SDL::Surface'); |
188 | } |
189 | SDL::BlitSurface(map { $_ != 0 ? ${$_} : $_ } @_); |
190 | } |
191 | |
192 | sub set_colors { |
193 | my $self = shift; |
194 | my $start = shift; |
195 | for (@_) { |
084b921f |
196 | croak "SDL::Surface::set_colors requires SDL::Color objects" |
8fde61e3 |
197 | unless !$SDL::DEBUG || $_->isa('SDL::Color'); |
198 | } |
199 | return SDL::SetColors($$self, $start, map { ${$_} } @_); |
200 | } |
201 | |
202 | sub set_color_key { |
084b921f |
203 | croak "SDL::Surface::set_color_key requires a SDL::Color object" |
8fde61e3 |
204 | unless !$SDL::DEBUG || (ref($_[2]) && $_[2]->isa('SDL::Color')); |
205 | SDL::SetColorKey(${$_[0]},$_[1],${$_[2]}); |
206 | } |
207 | |
208 | sub set_alpha { |
209 | SDL::SetAlpha(${$_[0]},$_[1],$_[2]); |
210 | } |
211 | |
212 | sub display_format { |
213 | my $self = shift; |
214 | my $tmp = SDL::DisplayFormat($$self); |
215 | SDL::FreeSurface ($$self); |
216 | $$self = $tmp; |
217 | $self; |
218 | } |
219 | |
220 | sub rgb { |
221 | my $self = shift; |
222 | my $tmp = SDL::ConvertRGB($$self); |
223 | SDL::FreeSurface($$self); |
224 | $$self = $tmp; |
225 | $self; |
226 | } |
227 | |
228 | sub rgba { |
229 | my $self = shift; |
230 | my $tmp = SDL::ConvertRGBA($$self); |
231 | SDL::FreeSurface($$self); |
232 | $$self = $tmp; |
233 | $self; |
234 | } |
235 | |
236 | sub rect { |
237 | my $self = shift; |
238 | new SDL::Rect -width => $self->width(), -height => $self->height(), |
239 | -x => $_[0] || 0, -y => $_[1] || 0; |
240 | } |
241 | |
242 | sub print { |
243 | my ($self,$x,$y,@text) = @_; |
244 | SDL::SFont::PutString( $$self, $x, $y, join('',@text)); |
245 | } |
246 | |
247 | sub save_bmp { |
248 | SDL::SaveBMP( ${$_[0]},$_[1]); |
249 | } |
250 | |
251 | sub video_info { |
252 | shift; |
253 | SDL::VideoInfo(); |
254 | } |
255 | |
256 | 1; |
257 | |
258 | __END__; |
259 | |
260 | =pod |
261 | |
262 | =head1 NAME |
263 | |
264 | SDL::Surface - a SDL perl extension |
265 | |
266 | =head1 SYNOPSIS |
267 | |
268 | use SDL::Surface; |
269 | $image = new SDL::Surface(-name=>"yomama.jpg"); |
270 | |
271 | =head1 DESCRIPTION |
272 | |
273 | The C<SDL::Surface> module encapsulates the SDL_Surface* structure, and |
274 | many of its ancillatory functions. Not only is it a workhorse of the |
275 | OO Layer, it is the base class for the C<SDL::App> class. |
276 | |
277 | =head1 EXPORTS |
278 | |
279 | SDL_SWSURFACE SDL_HWSURFACE |
280 | SDL_ASYNCBLIT SDL_ANYFORMAT |
281 | SDL_HWPALETTE SDL_DOUBLEBUF |
282 | SDL_FULLSCREEN SDL_OPENGL |
283 | SDL_OPENGLBLIT SDL_RESIZEABLE |
284 | SDL_NOFRAME SDL_SRCCOLORKEY |
285 | SDL_RLEACCEL SDL_SRCALPHA |
286 | SDL_PREALLOC |
287 | |
288 | =head1 METHODS |
289 | |
290 | =head2 new (-name => 'foo.png') |
291 | |
292 | The C<SDL::Surface> class can be instantiated in a number of different ways. |
293 | If support for the SDL_image library was included when SDL_perl was compiled, |
294 | the easiest way to create a new surface is to use the C<SDL::Surface::new> |
295 | method with the C<-name> option. This will load the image from the file |
296 | and return an object encapsulating the SDL_Surface*. |
297 | |
298 | =head2 new (-from => $buffer, ... ) |
299 | |
300 | If the contents of the new Surface is already in memory, C<SDL::Surface::new> |
301 | may be called with the C<-from> option to create an image from that section |
302 | of memory. This method takes the following additional parameters: |
303 | |
304 | =over 4 |
305 | |
306 | =item * |
307 | |
308 | -width the width of the image in pixels |
309 | |
310 | =item * |
311 | |
312 | -height the height of the image in pixels |
313 | |
314 | =item * |
315 | |
316 | -depth the number of bits per pixel |
317 | |
318 | =item * |
319 | |
320 | -pitch the number of bytes per line |
321 | |
322 | =item * |
323 | |
324 | -Rmask an optional bitmask for red |
325 | |
326 | =item * |
327 | |
328 | -Gmask an optional bitmask for green |
329 | |
330 | =item * |
331 | |
332 | -Bmask an optional bitmask for green |
333 | |
334 | =item * |
335 | |
336 | -Amask an optional bitmask for alpha |
337 | |
338 | =back |
339 | |
340 | =head2 new ( -flags => SDL_SWSURFACE, ... ) |
341 | |
342 | Finally, C<SDL::Suface::new> may be invoked with the C<-flags> option, in a |
343 | similar fashion to the C<-from> directive. This invocation takes the same |
344 | additional options as C<-from> with the exception of C<-pitch> which is ignored. |
345 | This method returns a new, blank, SDL::Surface option with any of the following |
346 | flags turned on: |
347 | |
348 | =over 4 |
349 | |
350 | =item * |
351 | |
352 | SWSURFACE() a non-accelerated surface |
353 | |
354 | =item * |
355 | |
356 | HWSURFACE() a hardware accelerated surface |
357 | |
358 | =item * |
359 | |
360 | SRCCOLORKEY() a surface with a transperant color |
361 | |
362 | =item * |
363 | |
364 | SRCALPHA() an alpha blended, translucent surface |
365 | |
366 | =back |
367 | |
368 | =head2 flags () |
369 | |
370 | C<SDL::Surface::flags> returns the flags with which the surface was initialized. |
371 | |
372 | =head2 palette () |
373 | |
374 | C<SDL::Surface::palette> currently returns a SDL_Palette*, this may change in |
375 | future revisions. |
376 | |
377 | =head2 bpp () |
378 | |
379 | C<SDL::Surface::bpp> returns the bits per pixel of the surface |
380 | |
381 | =head2 bytes_per_pixel () |
382 | |
383 | C<SDL::Surface::bytes_per_pixel> returns the bytes per pixel of the surface |
384 | |
385 | =head2 Rshift () |
386 | |
387 | C<SDL::Surface::Rshift> returns the bit index of the red field for the surface's pixel format |
388 | |
389 | =head2 Gshift () |
390 | |
391 | C<SDL::Surface::Gshift> returns the bit index of the green field for the surface's pixel format |
392 | |
393 | =head2 Bshift () |
394 | |
395 | C<SDL::Surface::Bshift> returns the bit index of the blue field for the surface's pixel format |
396 | |
397 | =head2 Ashift () |
398 | |
399 | C<SDL::Surface::Ashift> returns the bit index of the alpha field for the surface's pixel format |
400 | |
401 | =head2 Rmask () |
402 | |
403 | C<SDL::Surface::Rmask> returns the bit mask for the red field for teh surface's pixel format |
404 | |
405 | =head2 Gmask () |
406 | |
407 | C<SDL::Surface::Gmask> returns the bit mask for the green field for teh surface's pixel format |
408 | |
409 | =head2 Bmask () |
410 | |
411 | C<SDL::Surface::Bmask> returns the bit mask for the blue field for teh surface's pixel format |
412 | |
413 | =head2 Amask () |
414 | |
415 | C<SDL::Surface::Amask> returns the bit mask for the alpha field for teh surface's pixel format |
416 | |
417 | =head2 color_key () |
418 | |
419 | C<SDL::Surface::color_key> returns the current color key for the image, which can be set with |
420 | the C<SDL::Surface::set_color_key> method. Before calling C<SDL::Surface::color_key> on |
421 | a image, you should fist call C<SDL::Surface::display_format> to convert it to the same |
422 | format as the display. Failure to do so will result in failure to apply the correct color_key. |
423 | |
424 | =head2 alpha () |
425 | |
426 | C<SDL::Surface::alpha> returns the current alpha value for the image, which can be set with |
427 | the C<SDL::Surface::set_alpha> method. |
428 | |
429 | =head2 width () |
430 | |
431 | C<SDL::Surface::width> returns the width in pixels of the surface |
432 | |
433 | =head2 height () |
434 | |
435 | C<SDL::Surface::height> returns the height in pixels of the surface |
436 | |
437 | =head2 pitch () |
438 | |
439 | C<SDL::Surface::pitch> returns the width of a surface's scanline in bytes |
440 | |
441 | =head2 pixels () |
442 | |
443 | C<SDL::Surface::pixels> returns a Uint8* to the image's pixel data. This is not |
444 | inherently useful within perl, though may be used to pass image data to user provided |
445 | C functions. |
446 | |
447 | =head2 pixel (x,y,[color]) |
448 | |
449 | C<SDL::Surface::pixel> will set the color value of the pixel at (x,y) to the given |
450 | color if provided. C<SDL::Surface::pixel> returns a SDL::Color object for the |
451 | color value of the pixel at (x,y) after any possible modifications. |
452 | |
453 | =head2 fill (rect,color) |
454 | |
455 | C<SDL::Surface::fill> will fill the given SDL::Rect rectangle with the specified SDL::Color |
456 | This function optionally takes a SDL_Rect* and a SDL_Color* |
457 | |
458 | =head2 lockp () |
459 | |
460 | C<SDL::Surface::lockp> returns true if the surface must be locked |
461 | |
462 | =head2 lock () |
463 | |
464 | C<SDL::Surface::lock> places a hardware lock if necessary, preventing access to |
465 | the surface's memory |
466 | |
467 | =head2 unlock () |
468 | |
469 | C<SDL::Surface::unlock> removes any hardware locks, enabling blits |
470 | |
471 | =head2 update ( rects...) |
472 | |
473 | C<SDL::Surface::update> takes one or more SDL::Rect's which determine which sections |
474 | of the image are to be updated. This option is only useful on the appliaction surface. |
475 | |
476 | =head2 flip () |
477 | |
478 | C<SDL::Surface::flip> updates the full surface, using a double buffer if available |
479 | |
480 | =head2 blit (srect,dest,drect) |
481 | |
482 | C<SDL::Surface::blit> blits the current surface onto the destination surface, |
483 | according to the provided rectangles. If a rectangle is 0, then the full surface is used. |
484 | |
485 | =head2 set_colors (start,colors...) |
486 | |
487 | C<SDL::Surface::set_colors> updates the palette starting at index C<start> with the |
488 | supplied colors. The colors may either be SDL::Color objects or SDL_Color* from the |
489 | low level C-style API. |
490 | |
491 | =head2 set_color_key (flag,pixel) or (flag,x,y) |
492 | |
493 | C<SDL::Surface::set_color_key> sets the blit flag, usually SDL_SRCCOLORKEY, |
494 | to the specified L<SDL::Color> object. Optional a SDL_Color* may be passed. |
495 | |
496 | =head2 set_alpha (flag,alpha) |
497 | |
498 | C<SDL::Surface::set_alpha> sets the opacity of the image for alpha blits. |
499 | C<alpha> takes a value from 0x00 to 0xff. |
500 | |
501 | =head2 display_format () |
502 | |
503 | C<SDL::Surface::display_format> converts the surface to the same format as the |
504 | current screen. |
505 | |
506 | =head2 rgb () |
507 | C<SDL::Surface::rgb> converts the surface to a 24 bit rgb format regardless of the |
508 | initial format. |
509 | |
510 | =head2 rgba () |
511 | C<SDL::Surface::rgba> converts the surface to a 32 bit rgba format regarless of the |
512 | initial format. |
513 | |
514 | =head2 print (x,y,text...) |
515 | |
516 | C<SDL::Surface::print> renders the text using the current font onto the image. |
517 | This option is only supported for with SDL_image and SFont. |
518 | |
519 | =head2 save_bmp (filename) |
520 | |
521 | C<SDL::Surface::save_bmp> saves the surface to filename in Windows BMP format. |
522 | |
523 | =head2 video_info () |
524 | |
525 | C<SDL::Surface::video_info> returns a hash describing the current state of the |
526 | video hardware. |
527 | |
528 | =head1 AUTHOR |
529 | |
530 | David J. Goehrig |
531 | |
532 | =head1 SEE ALSO |
533 | |
534 | L<perl> L<SDL::App> L<SDL::Color> L<SDL::Palette> L<SDL::Rect> |
535 | |
536 | =cut |