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