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