/lib/pods/SDLx/Sprite.pod

http://github.com/PerlGameDev/SDL · Unknown · 272 lines · 171 code · 101 blank · 0 comment · 0 complexity · 6440acd6a4e9668e887bf3492725217f MD5 · raw file

  1. =head1 NAME
  2. SDLx::Sprite - interact with images quick and easily in SDL
  3. =head1 CATEGORY
  4. Extension
  5. =head1 SYNOPSIS
  6. use SDLx::Sprite;
  7. my $sprite = SDLx::Sprite->new;
  8. # loads image file into a SDL::Surface and
  9. # automatically sets a SDL::Rect inside with
  10. # that image's dimensions.
  11. $sprite->load('hero.png');
  12. # set sprite image transparency
  13. $sprite->alpha_key( $color );
  14. $sprite->alpha(0.5);
  15. # you can set and check the sprite position anytime
  16. say $sprite->x; # rect->x shortcut accessor
  17. $sprite->y(30); # rect->y shortcut accessor
  18. # read-only surface dimensions
  19. $sprite->w; # width
  20. $sprite->h; # height
  21. # you can also fetch the full rect
  22. # (think destination coordinates for ->draw)
  23. my $rect = $sprite->rect;
  24. # you can get the surface object too if you need it
  25. my $surface = $sprite->surface;
  26. # rotation()
  27. # if your SDL has gfx, rotation is also straightforward:
  28. $sprite->rotation( $degrees );
  29. $sprite->rotation( $degrees, $smooth );
  30. # add() / remove() NOT YET IMPLEMENTED
  31. # you can also attach other sprites to it
  32. $sprite->add( armor => $other_sprite );
  33. $sprite->remove('armor');
  34. # blits $sprite (and attached sprites) into $screen,
  35. # in the (x,y) coordinates of the sprite
  36. $sprite->draw($screen);
  37. # if you need to clip the original image/surface
  38. # before drawing it
  39. $sprite->clip->x(10);
  40. $sprite->clip->y(3);
  41. $sprite->clip->w(5);
  42. $sprite->clip->h(5);
  43. # ...or all at once:
  44. $sprite->clip($x,$y,$w,$h);
  45. # spawning can include almost all of the above:
  46. my $sprite = SDLx::Sprite->new(
  47. image => 'hero.png', # or surface => SDL::Surface
  48. rect => SDL::Rect, # or x => $x, y => $y
  49. clip => SDL::Rect,
  50. alpha_key => SDL::Color, # or [$r, $g, $b]
  51. alpha => 1,
  52. rotation => 45, # degrees
  53. );
  54. =head1 DESCRIPTION
  55. SDLx::Sprite is a SDL::Surface on steroids! It let's you quickly
  56. load, setup and interact with images in your SDL application, abstracting
  57. all the drudge code and letting you concentrate on your app's logic instead.
  58. This module automatically creates and holds SDL::Rect objects for the source
  59. and destination surfaces, and provides several surface manipulation options
  60. like alpha blending and rotation.
  61. =head1 WARNING! VOLATILE CODE AHEAD
  62. This is a new module and the API is subject to change without notice.
  63. If you care, please join the discussion on the #sdl IRC channel in
  64. I<irc.perl.org>. All thoughts on further improving the API are welcome.
  65. You have been warned :)
  66. =head1 METHODS
  67. =head2 new
  68. =head2 new( %options )
  69. Creates a new SDLx::Sprite object. No option is mandatory.
  70. Available options are:
  71. =over 4
  72. =item * image => $filename
  73. Uses $filename as source image for the Sprite's surface. See supported
  74. formats in L<< SDL::Image >>. This option B<cannot> be used together
  75. with the 'surface' option (see below).
  76. =item * surface => SDL::Surface
  77. Uses the provided L<< SDL::Surface >> object as source surface for this
  78. sprite, instead of creating one automatically. This option B<cannot> be
  79. used together with the 'image' option (see above).
  80. =item * clip => SDL::Rect
  81. Uses the provided L<< SDL::Rect >> object as clipping rect for the source
  82. surface. This means the object will only blit that particular area from
  83. the surface.
  84. =item * rect => SDL::Rect
  85. Uses the provided L<< SDL::Rect >> object as destination coordinates to
  86. whatever surface you call draw() on. You B<cannot> use this option together
  87. with 'x' and 'y' (see below)
  88. =item * x => $x
  89. Uses $x as the x-axis (left-to-right, 0 being leftmost) positioning of
  90. the Sprite into the destination you call draw() upon. This option B<cannot>
  91. be used together with 'rect' (see above).
  92. =item * y => $y
  93. Uses $y as the y-axis (top-to-bottom, 0 being topmost) positioning of
  94. the Sprite into the destination you call draw() upon. This option B<cannot>
  95. be used together with 'rect' (see above).
  96. =item * draw_xy => $surface, $x, $y
  97. A shortcut to draw at coordinates quickly. Calls x() , y() and draw()
  98. =item * rotation => $degrees, [$smooth]
  99. Uses $degrees as the angle to rotate the surface to, in degrees
  100. (0..360, remember? :). This option is only available if your compiled SDL
  101. library has support for GFX (see L<< Alien::SDL >> for details).
  102. if $smooth is set the sprite is antialiased. This may mess with your alpha_key.
  103. =item * alpha_key => SDL::Color
  104. MUST CALL L<SDL::Video::get_video_mode|SDL::Video/"get_video_mode"> prior to this.
  105. Uses the provided L<< SDL::Color >> object (or an array reference with red,
  106. green and blue values) as the color to be turned into transparent
  107. (see 'alpha' below).
  108. =item * alpha => $percentage or $integer
  109. Uses $percentage (0 <-> 1 ) or $integer ( 0x01 - 0xff) as how much transparency to add to the surface. If you use
  110. this, it is mandatory that you also provide the alpha_key (see above).
  111. =back
  112. =head2 load( $filename )
  113. Loads the given image file into the object's internal surface. A new surface
  114. is B<always> created, so whatever you had on the previous surface will be
  115. lost. Croaks on errors such as no support built for the image or a file
  116. reading error (the error message is SDL::get_error and should give more
  117. details).
  118. Returns the own Sprite object, to allow method chaining.
  119. =head2 surface()
  120. =head2 surface( SDL::Surface )
  121. Returns the object's internal surface, or undef if there is none.
  122. If you pass a SDL::Surface to it, it will overwrite the original surface
  123. with it, while returning the B<old> (previous) surface. Note that, as such,
  124. it will return C<undef> if you use it without having previously loaded
  125. either an image or a previous surface. It will Carp::confess if you pass anything
  126. that's not an SDL::Surface object (or SDL::Surface subclassed objects).
  127. =head2 rect()
  128. =head2 rect( SDL::Rect )
  129. Returns the destination L<< SDL::Rect >> object used when you call draw().
  130. If you haven't explicitly set it, it will be a SDL::Rect with the same
  131. dimensions as the object's internal surface. If no surface was set yet,
  132. it will be an empty SDL::Rect (dimensions 0,0,0,0).
  133. If you pass it a L<< SDL::Rect >> object, it will set rect() to that object
  134. before returning, but it will B<overwrite> any width and height values, as
  135. those are read only and set to the size of the underlying surface.
  136. If you want to clip the source surface, set clip() instead.
  137. =head2 clip()
  138. =head2 clip( SDL::Rect )
  139. Returns the source L<< SDL::Rect >> object used when you call draw().
  140. You can use this method to choose only a small subset of the object's
  141. internal surface to be used on calls to draw().
  142. If you haven't explicitly set it, it will be a SDL::Rect with the same
  143. dimensions as the object's internal surface. If no surface was set yet,
  144. it will be an empty SDL::Rect (dimensions 0,0,0,0).
  145. If you pass it a L<< SDL::Rect >> object, it will set clip() to that object
  146. before returning.
  147. =head2 x()
  148. =head2 x( $int )
  149. Gets/sets the x-axis (left-to-right, 0 being leftmost) positioning of
  150. the Sprite into the destination you call draw() upon.
  151. It is a shortcut to C<< $sprite->rect->x >>.
  152. =head2 y()
  153. =head2 y( $int )
  154. Gets/sets the y-axis (top-to-bottom, 0 being topmost) positioning of
  155. the Sprite into the destination you call draw() upon.
  156. It is a shortcut to C<< $sprite->rect->y >>.
  157. =head2 w()
  158. Returns the Sprite surface's width. This method is read-only.
  159. It is a shortcut to C<< $sprite->surface->w >>.
  160. =head2 h()
  161. Returns the Sprite surface's height. This method is read-only.
  162. It is a shortcut to C<< $sprite->surface->h >>.
  163. =head2 draw( SDL::Surface )
  164. Draws the Sprite on the provided SDL::Surface object - usually the screen -
  165. using the blit_surface SDL function, using the source rect from clip() and the
  166. destination rect (position) from rect().
  167. Returns the own Sprite object, to allow method chaining.
  168. =head1 AUTHORS
  169. See L<SDL/AUTHORS>.
  170. =head1 SEE ALSO
  171. L<< SDL::Surface >>, L<< SDL >>