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