5 # Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org>
7 # ------------------------------------------------------------------------------
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 # ------------------------------------------------------------------------------
25 # Please feel free to send questions, suggestions or improvements to:
43 my $class = ref($proto) || $proto;
47 verify (%options, qw/ -name -n -flags -fl -width -w -height -h -depth -d
48 -pitch -p -Rmask -r -Gmask -g -Bmask -b -Amask -a
49 -from -f /) if $SDL::DEBUG;
51 if ( defined($options{-name}) && $options{-name} ne "" && exists $SDL::{IMGLoad} ) {
52 $self = \SDL::IMGLoad($options{-name});
54 my $f = $options{-flags} || $options{-fl} || SDL::SDL_ANYFORMAT();
55 my $w = $options{-width} || $options{-w} || 1;
56 my $h = $options{-height} || $options{-h} || 1;
57 my $d = $options{-depth} || $options{-d} || 8;
58 my $p = $options{-pitch} || $options{-p} || $w*$d;
59 my $r = $options{-Rmask} || $options{-r}
60 || ( SDL::BigEndian() ? 0xff000000 : 0x000000ff );
61 my $g = $options{-Gmask} || $options{-g}
62 || ( SDL::BigEndian() ? 0x00ff0000 : 0x0000ff00 );
63 my $b = $options{-Bmask} || $options{-b}
64 || ( SDL::BigEndian() ? 0x0000ff00 : 0x00ff0000 );
65 my $a = $options{-Amask} || $options{-a}
66 || ( SDL::BigEndian() ? 0x000000ff : 0xff000000 );
68 if ( $options{-from}|| $options{-f} ) {
69 my $src = $options{-from}|| $options{-f};
70 $self = \SDL::CreateRGBSurfaceFrom($src,$w,$h,$d,$p,$r,$g,$b,$a);
72 $self = \SDL::CreateRGBSurface($f,$w,$h,$d,$r,$g,$b,$a);
75 croak "SDL::Surface::new failed. ", SDL::GetError()
82 SDL::FreeSurface(${$_[0]});
86 SDL::SurfaceFlags(${$_[0]});
90 SDL::SurfacePalette(${$_[0]});
94 SDL::SurfaceBitsPerPixel(${$_[0]});
98 SDL::SurfaceBytesPerPixel(${$_[0]});
102 SDL::SurfaceRshift(${$_[0]});
106 SDL::SurfaceGshift(${$_[0]});
110 SDL::SurfaceBshift(${$_[0]});
114 SDL::SurfaceAshift(${$_[0]});
118 SDL::SurfaceRmask(${$_[0]});
122 SDL::SurfaceGmask(${$_[0]});
126 SDL::SurfaceBmask(${$_[0]});
130 SDL::SurfaceAmask(${$_[0]});
134 SDL::SurfaceColorKey(${$_[0]});
138 SDL::SurfaceAlpha(${$_[0]});
142 SDL::SurfaceW(${$_[0]});
146 SDL::SurfaceH(${$_[0]});
150 SDL::SurfacePitch(${$_[0]});
154 SDL::SurfacePixels(${$_[0]});
158 croak "SDL::Surface::pixel requires a SDL::Color"
159 if $_[3] && $SDL::DEBUG && !$_[3]->isa("SDL::Color");
161 new SDL::Color -color => SDL::SurfacePixel(${$_[0]},$_[1],$_[2],${$_[3]}) :
162 new SDL::Color -color => SDL::SurfacePixel(${$_[0]},$_[1],$_[2]);
166 croak "SDL::Surface::fill requires a SDL::Rect object"
167 unless !$SDL::DEBUG || $_[1] == 0 || $_[1]->isa('SDL::Rect');
168 croak "SDL::Surface::fill requires a SDL::Color object"
169 unless !$SDL::DEBUG || $_[2]->isa('SDL::Color');
171 SDL::FillRect(${$_[0]},0,${$_[2]});
173 SDL::FillRect(${$_[0]},${$_[1]},${$_[2]});
178 SDL::MUSTLOCK(${$_[0]});
182 SDL::SurfaceLock(${$_[0]});
186 SDL::SurfaceUnlock(${$_[0]});
193 croak "SDL::Surface::update requires SDL::Rect objects"
194 unless $_->isa('SDL::Rect');
197 SDL::UpdateRects($$self, map { ${$_} } @_ );
205 $_[1] = 0 unless defined $_[1];
206 $_[3] = 0 unless defined $_[3];
209 croak "SDL::Surface::blit requires SDL::Rect objects"
210 unless ($_[1] == 0 || $_[1]->isa('SDL::Rect'))
211 && ($_[3] == 0 || $_[3]->isa('SDL::Rect'));
212 croak "SDL::Surface::blit requires SDL::Surface objects"
213 unless $_[2]->isa('SDL::Surface');
215 SDL::BlitSurface(map { (defined($_) && $_ != 0)? ${$_} : $_ } @_) if defined(@_);
222 croak "SDL::Surface::set_colors requires SDL::Color objects"
223 unless !$SDL::DEBUG || $_->isa('SDL::Color');
225 return SDL::SetColors($$self, $start, map { ${$_} } @_);
229 croak "SDL::Surface::set_color_key requires a SDL::Color object"
230 unless !$SDL::DEBUG || (ref($_[2]) && $_[2]->isa('SDL::Color'));
231 SDL::SetColorKey(${$_[0]},$_[1],${$_[2]});
235 SDL::SetAlpha(${$_[0]},$_[1],$_[2]);
240 my $tmp = SDL::DisplayFormat($$self);
241 SDL::FreeSurface ($$self);
248 my $tmp = SDL::ConvertRGB($$self);
249 SDL::FreeSurface($$self);
256 my $tmp = SDL::ConvertRGBA($$self);
257 SDL::FreeSurface($$self);
264 new SDL::Rect -width => $self->width(), -height => $self->height(),
265 -x => $_[0] || 0, -y => $_[1] || 0;
269 my ($self,$x,$y,@text) = @_;
270 SDL::SFont::PutString( $$self, $x, $y, join('',@text));
274 SDL::SaveBMP( ${$_[0]},$_[1]);
290 SDL::Surface - a SDL perl extension
295 $image = new SDL::Surface(-name=>"yomama.jpg");
299 The C<SDL::Surface> module encapsulates the SDL_Surface* structure, and
300 many of its ancillatory functions. Not only is it a workhorse of the
301 OO Layer, it is the base class for the C<SDL::App> class.
305 SDL_SWSURFACE SDL_HWSURFACE
306 SDL_ASYNCBLIT SDL_ANYFORMAT
307 SDL_HWPALETTE SDL_DOUBLEBUF
308 SDL_FULLSCREEN SDL_OPENGL
309 SDL_OPENGLBLIT SDL_RESIZEABLE
310 SDL_NOFRAME SDL_SRCCOLORKEY
311 SDL_RLEACCEL SDL_SRCALPHA
316 =head2 new (-name => 'foo.png')
318 The C<SDL::Surface> class can be instantiated in a number of different ways.
319 If support for the SDL_image library was included when SDL_perl was compiled,
320 the easiest way to create a new surface is to use the C<SDL::Surface::new>
321 method with the C<-name> option. This will load the image from the file
322 and return an object encapsulating the SDL_Surface*.
324 =head2 new (-from => $buffer, ... )
326 If the contents of the new Surface is already in memory, C<SDL::Surface::new>
327 may be called with the C<-from> option to create an image from that section
328 of memory. This method takes the following additional parameters:
334 -width the width of the image in pixels
338 -height the height of the image in pixels
342 -depth the number of bits per pixel
346 -pitch the number of bytes per line
350 -Rmask an optional bitmask for red
354 -Gmask an optional bitmask for green
358 -Bmask an optional bitmask for green
362 -Amask an optional bitmask for alpha
366 =head2 new ( -flags => SDL_SWSURFACE, ... )
368 Finally, C<SDL::Suface::new> may be invoked with the C<-flags> option, in a
369 similar fashion to the C<-from> directive. This invocation takes the same
370 additional options as C<-from> with the exception of C<-pitch> which is ignored.
371 This method returns a new, blank, SDL::Surface option with any of the following
378 SWSURFACE() a non-accelerated surface
382 HWSURFACE() a hardware accelerated surface
386 SRCCOLORKEY() a surface with a transperant color
390 SRCALPHA() an alpha blended, translucent surface
396 C<SDL::Surface::flags> returns the flags with which the surface was initialized.
400 C<SDL::Surface::palette> currently returns a SDL_Palette*, this may change in
405 C<SDL::Surface::bpp> returns the bits per pixel of the surface
407 =head2 bytes_per_pixel ()
409 C<SDL::Surface::bytes_per_pixel> returns the bytes per pixel of the surface
413 C<SDL::Surface::Rshift> returns the bit index of the red field for the surface's pixel format
417 C<SDL::Surface::Gshift> returns the bit index of the green field for the surface's pixel format
421 C<SDL::Surface::Bshift> returns the bit index of the blue field for the surface's pixel format
425 C<SDL::Surface::Ashift> returns the bit index of the alpha field for the surface's pixel format
429 C<SDL::Surface::Rmask> returns the bit mask for the red field for teh surface's pixel format
433 C<SDL::Surface::Gmask> returns the bit mask for the green field for teh surface's pixel format
437 C<SDL::Surface::Bmask> returns the bit mask for the blue field for teh surface's pixel format
441 C<SDL::Surface::Amask> returns the bit mask for the alpha field for teh surface's pixel format
445 C<SDL::Surface::color_key> returns the current color key for the image, which can be set with
446 the C<SDL::Surface::set_color_key> method. Before calling C<SDL::Surface::color_key> on
447 a image, you should fist call C<SDL::Surface::display_format> to convert it to the same
448 format as the display. Failure to do so will result in failure to apply the correct color_key.
452 C<SDL::Surface::alpha> returns the current alpha value for the image, which can be set with
453 the C<SDL::Surface::set_alpha> method.
457 C<SDL::Surface::width> returns the width in pixels of the surface
461 C<SDL::Surface::height> returns the height in pixels of the surface
465 C<SDL::Surface::pitch> returns the width of a surface's scanline in bytes
469 C<SDL::Surface::pixels> returns a Uint8* to the image's pixel data. This is not
470 inherently useful within perl, though may be used to pass image data to user provided
473 =head2 pixel (x,y,[color])
475 C<SDL::Surface::pixel> will set the color value of the pixel at (x,y) to the given
476 color if provided. C<SDL::Surface::pixel> returns a SDL::Color object for the
477 color value of the pixel at (x,y) after any possible modifications.
479 =head2 fill (rect,color)
481 C<SDL::Surface::fill> will fill the given SDL::Rect rectangle with the specified SDL::Color
482 This function optionally takes a SDL_Rect* and a SDL_Color*
486 C<SDL::Surface::lockp> returns true if the surface must be locked
490 C<SDL::Surface::lock> places a hardware lock if necessary, preventing access to
495 C<SDL::Surface::unlock> removes any hardware locks, enabling blits
497 =head2 update ( rects...)
499 C<SDL::Surface::update> takes one or more SDL::Rect's which determine which sections
500 of the image are to be updated. This option is only useful on the appliaction surface.
504 C<SDL::Surface::flip> updates the full surface, using a double buffer if available
506 =head2 blit (srect,dest,drect)
508 C<SDL::Surface::blit> blits the current surface onto the destination surface,
509 according to the provided rectangles. If a rectangle is 0, then the full surface is used.
511 =head2 set_colors (start,colors...)
513 C<SDL::Surface::set_colors> updates the palette starting at index C<start> with the
514 supplied colors. The colors may either be SDL::Color objects or SDL_Color* from the
515 low level C-style API.
517 =head2 set_color_key (flag,pixel) or (flag,x,y)
519 C<SDL::Surface::set_color_key> sets the blit flag, usually SDL_SRCCOLORKEY,
520 to the specified L<SDL::Color> object. Optional a SDL_Color* may be passed.
522 =head2 set_alpha (flag,alpha)
524 C<SDL::Surface::set_alpha> sets the opacity of the image for alpha blits.
525 C<alpha> takes a value from 0x00 to 0xff.
527 =head2 display_format ()
529 C<SDL::Surface::display_format> converts the surface to the same format as the
533 C<SDL::Surface::rgb> converts the surface to a 24 bit rgb format regardless of the
537 C<SDL::Surface::rgba> converts the surface to a 32 bit rgba format regarless of the
540 =head2 print (x,y,text...)
542 C<SDL::Surface::print> renders the text using the current font onto the image.
543 This option is only supported for with SDL_image and SFont.
545 =head2 save_bmp (filename)
547 C<SDL::Surface::save_bmp> saves the surface to filename in Windows BMP format.
551 C<SDL::Surface::video_info> returns a hash describing the current state of the
560 L<perl> L<SDL::App> L<SDL::Color> L<SDL::Palette> L<SDL::Rect>