pods::...Sprite(3pm)
π NAME π
SDLx::Sprite β interact with images quick and easily in SDL.
This is the pods::...Sprite(3pm) page, bestie, straight outta User Contributed Perl Documentation. π«Ά
πΈ CATEGORY πΈ
Extension β¨
π¦ SYNOPSIS π¦
Okay so here's the whole group-chat recap in code, and every line is staying exactly as it is β no edits, no cap:
use SDLx::Sprite;
my $sprite = SDLx::Sprite->new;
# loads image file into a SDL::Surface and
# automatically sets a SDL::Rect inside with
# that image's dimensions.
$sprite->load('hero.png');
# set sprite image transparency
$sprite->alpha_key( $color );
$sprite->alpha(0.5);
# you can set and check the sprite position anytime
say $sprite->x; # rect->x shortcut accessor
$sprite->y(30); # rect->y shortcut accessor
# read-only surface dimensions
$sprite->w; # width
$sprite->h; # height
# you can also fetch the full rect
# (think destination coordinates for ->draw)
my $rect = $sprite->rect;
# you can get the surface object too if you need it
my $surface = $sprite->surface;
# rotation()
# if your SDL has gfx, rotation is also straightforward:
$sprite->rotation( $degrees );
$sprite->rotation( $degrees, $smooth );
# add() / remove() NOT YET IMPLEMENTED
# you can also attach other sprites to it
$sprite->add( armor => $other_sprite );
$sprite->remove('armor');
# blits $sprite (and attached sprites) into $screen,
# in the (x,y) coordinates of the sprite
$sprite->draw($screen);
# if you need to clip the original image/surface
# before drawing it
$sprite->clip->x(10);
$sprite->clip->y(3);
$sprite->clip->w(5);
$sprite->clip->h(5);
# ...or all at once:
$sprite->clip($x,$y,$w,$h);
# spawning can include almost all of the above:
my $sprite = SDLx::Sprite->new(
image => 'hero.png', # or surface => SDL::Surface
rect => SDL::Rect, # or x => $x, y => $y
clip => SDL::Rect,
alpha_key => SDL::Color, # or [$r, $g, $b]
alpha => 1,
rotation => 45, # degrees
);
π DESCRIPTION π
SDLx::Sprite is literally a SDL::Surface on steroids! It lets you quickly load, set up and interact with images in your SDL application, abstracting all the drudge code so you can concentrate on your app's logic instead. π
This module automatically creates and holds SDL::Rect objects for the source and destination surfaces, and gives you several surface manipulation options like alpha blending and rotation. β¨
π¨ WARNING! VOLATILE CODE AHEAD π¨
β οΈ This is a new module and the API is subject to change without notice. If you care, please join the discussion on the#sdlIRC channel inirc.perl.org. All thoughts on further improving the API are welcome. π¬ You have been warned :)
π₯ METHODS π₯
new / new( %options )
Creates a new SDLx::Sprite object. No option is mandatory. Available options are:
image => $filenameβ uses$filenameas the source image for the Sprite's surface. For supported formats, seeSDL::Image. πΌοΈ This option cannot be used together with the'surface'option (see below).surface => SDL::Surfaceβ uses the providedSDL::Surfaceobject as the source surface for this sprite, instead of creating one automatically. This option cannot be used together with the'image'option (see above).clip => SDL::Rectβ uses the providedSDL::Rectobject as the clipping rect for the source surface. Basically the object will only blit that particular area from the surface. βοΈrect => SDL::Rectβ uses the providedSDL::Rectobject as destination coordinates to whatever surface you calldraw()on. You cannot use this option together with'x'and'y'(see below).x => $xβ uses$xas the x-axis positioning (left-to-right,0being leftmost) of the Sprite into the destination you calldraw()upon. This option cannot be used together with'rect'(see above).y => $yβ uses$yas the y-axis positioning (top-to-bottom,0being topmost) of the Sprite into the destination you calldraw()upon. This option cannot be used together with'rect'(see above).draw_xy => $surface, $x, $yβ a shortcut to draw at coordinates quickly. Callsx(),y()anddraw().rotation => $degrees, [$smooth]β uses$degreesas the angle to rotate the surface to, in degrees (0..360, remember? :). This option is only available if your compiled SDL library has support for GFX (seeAlien::SDLfor details). If$smoothis set, the sprite is antialiased. This may mess with youralpha_key. πalpha_key => SDL::Colorβ β οΈ MUST CALLSDL::Video::get_video_modeprior to this. Uses the providedSDL::Colorobject (or an array reference with red, green and blue values) as the color to be turned into transparent (see'alpha'below).alpha => $percentage or $integerβ uses$percentage(0 <-> 1) or$integer(0x01 - 0xff) as how much transparency to add to the surface. If you use this, it is mandatory that you also provide thealpha_key(see above).
load( $filename )
Loads the given image file into the object's internal surface. A new surface is always created, so whatever you had on the previous surface will be lost. π It croaks on errors such as no support built for the image or a file reading error (the error message is SDL::get_error and should give more details). Returns the own Sprite object, to allow method chaining. π
surface() / surface( SDL::Surface )
Returns the object's internal surface, or undef if there is none. If you pass a SDL::Surface to it, it will overwrite the original surface with it, while returning the old (previous) surface. Note that, as such, it will return "undef" if you use it without having previously loaded either an image or a previous surface. It will Carp::confess if you pass anything that's not an SDL::Surface object (or SDL::Surface subclassed objects). π«
rect() / rect( SDL::Rect )
Returns the destination SDL::Rect object used when you call draw(). If you haven't explicitly set it, it will be a SDL::Rect with the same dimensions as the object's internal surface. If no surface was set yet, it will be an empty SDL::Rect (dimensions 0,0,0,0). If you pass it a SDL::Rect object, it will set rect() to that object before returning, but it will overwrite any width and height values, as those are read only and set to the size of the underlying surface. If you want to clip the source surface, set clip() instead. π
clip() / clip( SDL::Rect )
Returns the source SDL::Rect object used when you call draw(). You can use this method to choose only a small subset of the object's internal surface to be used on calls to draw(). If you haven't explicitly set it, it will be a SDL::Rect with the same dimensions as the object's internal surface. If no surface was set yet, it will be an empty SDL::Rect (dimensions 0,0,0,0). If you pass it a SDL::Rect object, it will set clip() to that object before returning. βοΈ
x() / x( $int )
Gets/sets the x-axis (left-to-right, 0 being leftmost) positioning of the Sprite into the destination you call draw() upon. It is a shortcut to $sprite->rect->x. β‘οΈ
y() / y( $int )
Gets/sets the y-axis (top-to-bottom, 0 being topmost) positioning of the Sprite into the destination you call draw() upon. It is a shortcut to $sprite->rect->y. β¬οΈ
w()
Returns the Sprite surface's width. This method is read-only. It is a shortcut to $sprite->surface->w. π
h()
Returns the Sprite surface's height. This method is read-only. It is a shortcut to $sprite->surface->h. π
draw( SDL::Surface )
Draws the Sprite on the provided SDL::Surface object β usually the screen β using the blit_surface SDL function, using the source rect from clip() and the destination rect (position) from rect(). Returns the own Sprite object, to allow method chaining. π¨
π AUTHORS π
See "AUTHORS" in SDL. π
π SEE ALSO π
SDL::Surface, SDL. β¨
okay bye bestie π«Ά β this pods::...Sprite(3pm) page is from perl v5.42.0, dated 2024-05-01. now go be iconic β¨π
π¦ where this page comes from
- opensuse-leap 16.0: perl-SDL 2.548-bp160.1.13 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: perl-SDL 2.548-bp160.1.13 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: perl-SDL 2.548-bp160.1.13 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: perl-SDL 2.548-bp160.1.13 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms