1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
#
# Surface.pm
#
# A package for manipulating SDL_Surface *
#
# Copyright (C) 2003 David J. Goehrig
package SDL::Surface;
use strict;
use SDL;
require SDL::Rect;
require SDL::Color;
require SDL::Palette;
use SDL::Event;
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my %options = @_;
my $self;
verify (%options, qw/ -name -n -flags -fl -width -w -height -h -depth -d
-pitch -p -Rmask -r -Gmask -g -Bmask -b -Amask -a
-from -f /) if $SDL::DEBUG;
if ( $options{-name} ne "" && exists $SDL::{IMGLoad} ) {
$self = \SDL::IMGLoad($options{-name});
} else {
my $f = $options{-flags} || $options{-fl} || SDL_ANYFORMAT();
my $w = $options{-width} || $options{-w} || 1;
my $h = $options{-height} || $options{-h} || 1;
my $d = $options{-depth} || $options{-d} || 8;
my $p = $options{-pitch} || $options{-p} || $w*$d;
my $r = $options{-Rmask} || $options{-r}
|| ( SDL::BigEndian() ? 0xff000000 : 0x000000ff );
my $g = $options{-Gmask} || $options{-g}
|| ( SDL::BigEndian() ? 0x00ff0000 : 0x0000ff00 );
my $b = $options{-Bmask} || $options{-b}
|| ( SDL::BigEndian() ? 0x0000ff00 : 0x00ff0000 );
my $a = $options{-Amask} || $options{-a}
|| ( SDL::BigEndian() ? 0x000000ff : 0xff000000 );
if ( $options{-from}|| $options{-f} ) {
my $src = $options{-from}|| $options{-f};
$self = \SDL::CreateRGBSurfaceFrom($src,$w,$h,$d,$p,$r,$g,$b,$a);
} else {
$self = \SDL::CreateRGBSurface($f,$w,$h,$d,$r,$g,$b,$a);
}
}
die "SDL::Surface::new failed. ", SDL::GetError()
unless ( $$self);
bless $self,$class;
return $self;
}
sub DESTROY {
SDL::FreeSurface(${$_[0]});
}
sub flags {
SDL::SurfaceFlags(${$_[0]});
}
sub palette {
SDL::SurfacePalette(${$_[0]});
}
sub bpp {
SDL::SurfaceBitsPerPixel(${$_[0]});
}
sub bytes_per_pixel {
SDL::SurfaceBytesPerPixel(${$_[0]});
}
sub Rshift {
SDL::SurfaceRshift(${$_[0]});
}
sub Gshift {
SDL::SurfaceGshift(${$_[0]});
}
sub Bshift {
SDL::SurfaceBshift(${$_[0]});
}
sub Ashift {
SDL::SurfaceAshift(${$_[0]});
}
sub Rmask {
SDL::SurfaceRmask(${$_[0]});
}
sub Gmask {
SDL::SurfaceGmask(${$_[0]});
}
sub Bmask {
SDL::SurfaceBmask(${$_[0]});
}
sub Amask {
SDL::SurfaceAmask(${$_[0]});
}
sub color_key {
SDL::SurfaceColorKey(${$_[0]});
}
sub alpha {
SDL::SurfaceAlpha(${$_[0]});
}
sub width {
SDL::SurfaceW(${$_[0]});
}
sub height {
SDL::SurfaceH(${$_[0]});
}
sub pitch {
SDL::SurfacePitch(${$_[0]});
}
sub pixels {
SDL::SurfacePixels(${$_[0]});
}
sub pixel {
die "SDL::Surface::pixel requires a SDL::Color"
if $_[3] && $SDL::DEBUG && !$_[3]->isa("SDL::Color");
$_[3] ?
new SDL::Color -color => SDL::SurfacePixel(${$_[0]},$_[1],$_[2],${$_[3]}) :
new SDL::Color -color => SDL::SurfacePixel(${$_[0]},$_[1],$_[2]);
}
sub fill {
die "SDL::Surface::fill requires a SDL::Rect object"
unless !$SDL::DEBUG || $_[1] == 0 || $_[1]->isa('SDL::Rect');
die "SDL::Surface::fill requires a SDL::Color object"
unless !$SDL::DEBUG || $_[2]->isa('SDL::Color');
if ($_[1] == 0 ) {
SDL::FillRect(${$_[0]},0,${$_[2]});
} else {
SDL::FillRect(${$_[0]},${$_[1]},${$_[2]});
}
}
sub lockp {
SDL::MUSTLOCK(${$_[0]});
}
sub lock {
SDL::SurfaceLock(${$_[0]});
}
sub unlock {
SDL::SurfaceUnlock(${$_[0]});
}
sub update {
my $self = shift;;
if ($SDL::DEBUG) {
for (@_) {
die "SDL::Surface::update requires SDL::Rect objects"
unless $_->isa('SDL::Rect');
}
}
SDL::UpdateRects($$self, map { ${$_} } @_ );
}
sub flip {
SDL::Flip(${$_[0]});
}
sub blit {
if ($SDL::DEBUG) {
die "SDL::Surface::blit requires SDL::Rect objects"
unless ($_[1] == 0 || $_[1]->isa('SDL::Rect'))
&& ($_[1] == 0 || $_[3]->isa('SDL::Rect'));
die "SDL::Surface::blit requires SDL::Surface objects"
unless $_[2]->isa('SDL::Surface');
}
SDL::BlitSurface(map { $_ != 0 ? ${$_} : $_ } @_);
}
sub set_colors {
my $self = shift;
my $start = shift;
for (@_) {
die "SDL::Surface::set_colors requires SDL::Color objects"
unless !$SDL::DEBUG || $_->isa('SDL::Color');
}
return SDL::SetColors($$self, $start, map { ${$_} } @_);
}
sub set_color_key {
die "SDL::Surface::set_color_key requires a SDL::Color object"
unless !$SDL::DEBUG || $_[2]->isa('SDL::Color');
SDL::SetColorKey(${$_[0]},$_[1],${$_[2]});
}
sub set_alpha {
SDL::SetAlpha(${$_[0]},$_[1],$_[2]);
}
sub display_format {
my $self = shift;
my $tmp = SDL::DisplayFormat($$self);
SDL::FreeSurface ($$self);
$$self = $tmp;
$self;
}
sub rgb {
my $self = shift;
my $tmp = SDL::ConvertRGB($$self);
SDL::FreeSurface($$self);
$$self = $tmp;
$self;
}
sub rgba {
my $self = shift;
my $tmp = SDL::ConvertRGBA($$self);
SDL::FreeSurface($$self);
$$self = $tmp;
$self;
}
sub print {
my ($self,$x,$y,@text) = @_;
SDL::PutString( $$self, $x, $y, join('',@text));
}
sub save_bmp {
SDL::SaveBMP( ${$_[0]},$_[1]);
}
sub video_info {
shift;
SDL::VideoInfo();
}
1;
__END__;
=pod
=head1 NAME
SDL::Surface - a SDL perl extension
=head1 SYNOPSIS
use SDL::Surface;
$image = new SDL::Surface(-name=>"yomama.jpg");
=head1 DESCRIPTION
The L<SDL::Surface> module encapsulates the SDL_Surface* structure, and
many of its ancillatory functions. It has a similar interface to the L<SDL::Surface>
class. Where it differs:
=over 4
=item *
All methods require SDL::* objects. If $SDL::DEBUG is false, no type checks will be made.
=item *
C<SDL::Surface::set_color_key> takes a flag and an SDL::Color object only.
=back
=head1 AUTHOR
David J. Goehrig
=head1 SEE ALSO
L<perl> L<SDL::Rect> L<SDL::Color> L<SDL::Surface>
=cut
|