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