graphics/ sprite.bi | |
License | Copyright © 2009, FreeBASIC Extended Library Development Group |
ext.gfx | |
Enumerations | |
PPOPTIONS | Used in the Sprite class to determine whether to use pixel perfect collision or not. |
Sprite | Fully featured sprite class with pixel perfect collision detection built-in |
Functions | |
constructor | Creates a sprite object with a certain number of frames |
default constructor | Creates an unitialized Sprite object. |
copy constructor | Makes a deep copy of another Sprite object. |
Init | Used to initialize when using an array of Sprite |
GetImage | |
SetImage | Assigns a FB.IMAGE to a frame. |
DuplicateImage | Creates a full copy of the Image |
ReplaceImage | |
DeleteImage | |
UpdateImage | Updates the collision mask for the specified frame. |
isCollided | Determines if this Sprite object has collided with another Sprite object. |
RotateFrom | Rotates one frame to another frame. |
RotateFromImage | Rotates a FB.IMAGE to a frame. |
DrawImage | Draws the specified sprite either to an image buffer or to the screen. |
Postition | Sets or Gets the position of the sprite for use with the draw and collision statements. |
Postition | Sets or Gets the position of the sprite for use with the draw and collision statements. |
Update | Updates the position of the Sprite in relative terms. |
Update | Updates the position of the Sprite in relative terms. |
Properties | |
Count | Returns the number of frames this Sprite object holds (1 based) |
LastIndex | Returns the last referenced frame by GetImage |
Examples | |
Simple Example of the Sprite Class |
Copyright © 2009, FreeBASIC Extended Library Development Group
Distributed under the FreeBASIC Extended Library Group license. See accompanying file LICENSE.txt or copy at http://code.google.com- /p- /fb-extended-lib- /wiki- /License
Enumerations | |
PPOPTIONS | Used in the Sprite class to determine whether to use pixel perfect collision or not. |
Fully featured sprite class with pixel perfect collision detection built-in
Simple Example of the Sprite Class
Functions | |
constructor | Creates a sprite object with a certain number of frames |
default constructor | Creates an unitialized Sprite object. |
copy constructor | Makes a deep copy of another Sprite object. |
Init | Used to initialize when using an array of Sprite |
GetImage | |
SetImage | Assigns a FB.IMAGE to a frame. |
DuplicateImage | Creates a full copy of the Image |
ReplaceImage | |
DeleteImage | |
UpdateImage | Updates the collision mask for the specified frame. |
isCollided | Determines if this Sprite object has collided with another Sprite object. |
RotateFrom | Rotates one frame to another frame. |
RotateFromImage | Rotates a FB.IMAGE to a frame. |
DrawImage | Draws the specified sprite either to an image buffer or to the screen. |
Postition | Sets or Gets the position of the sprite for use with the draw and collision statements. |
Postition | Sets or Gets the position of the sprite for use with the draw and collision statements. |
Update | Updates the position of the Sprite in relative terms. |
Update | Updates the position of the Sprite in relative terms. |
Properties | |
Count | Returns the number of frames this Sprite object holds (1 based) |
LastIndex | Returns the last referenced frame by GetImage |
Examples | |
Simple Example of the Sprite Class |
Makes a deep copy of another Sprite object.
declare sub Init( byval num as uinteger )
Used to initialize when using an array of Sprite
num | the 1 based number of frames to create |
declare function GetImage( byval index as uinteger ) as FB.IMAGE ptr
index | the (0 based) frame to return |
pointer to FB.IMAGE containing image data.
If you modify the image returned by this function you must then call UpdateImage to regenerate the collision mask.
declare sub SetImage( byval index as uinteger, byval img as FB. IMAGE ptr )
Assigns a FB.IMAGE to a frame.
index | the frame number to assign to (0 based), does not check if data already exists there. |
img | the FB.IMAGE structure to assign to this frame. |
This object will automatically free the image data when it is no longer needed, you should not deallocate this memory yourself.
declare sub UpdateImage( byval index as uinteger )
Updates the collision mask for the specified frame.
index | the frame to update the collision mask for (0 based). |
You only need to call this subroutine if you modify the image returned by GetImage
declare function isCollided( byref spr as Sprite, byval ppcol as PPOPTIONS = usePP, byval _index_ as integer = -1 ) as ext.bool
Determines if this Sprite object has collided with another Sprite object.
spr | the second Sprite object to test collision with. |
ppcol | optional value to determine collision method. Defaults to usePP. |
_index_ | optional index into the second Sprite object to test collision on. (0 based) |
ext.bool.true on collision, ext.bool.false otherwise.
This function first performs a bounding box collision check to decrease unnecessary calls to the pixel perfect routine.
declare sub RotateFrom( byval from_index as uinteger, byval to_index as uinteger, byval angle as integer )
Rotates one frame to another frame.
from_index | the source image index to rotate. (0 based) |
to_index | the destination index to overwrite with the rotated image. (0 based) |
angle | the angle to rotate the image to. |
declare sub RotateFromImage( byval from_image as FB. IMAGE ptr, byval to_index as uinteger, byval angle as integer )
Rotates a FB.IMAGE to a frame.
from_image | the source image to rotate. |
to_index | the destination index to overwrite with the rotated image. (0 based) |
angle | the angle to rotate the image to. |
declare sub DrawImage( byval src_img as uinteger, byval dst_img as FB. IMAGE ptr = null, byval method as DrawMethods = DrawMethods.XOR_ )
Draws the specified sprite either to an image buffer or to the screen.
src_img | the Source image to draw. |
dst_img | the destination to draw the image onto, defaults to null which will draw on the screen. |
method | the method to use to draw the image from DrawMethods, defaults to XOR (XOR_) like FBGFX’s PUT statement. |
Sets or Gets the position of the sprite for use with the draw and collision statements.
_vec_ | vector2d containing the x and y coordinates. |
To get the position pass a vector2d with one or both coordinates as negative (i.e. < 0) and the current values will be returned in the vector2d variable passed.
declare sub Update overload( byval _x_diff as single = 0, byref _y_diff as single = 0 )
Updates the position of the Sprite in relative terms.
_x_ | relative x position to move sprite to. |
_y_ | relative y position to move sprite to. |
Passing a negative value for x will cause the sprite to move “left”, a negative value for y will cause the sprite to move “up”.
declare sub Update( byval _vec_ as ext.math.vector2d )
Updates the position of the Sprite in relative terms.
_vec_ | vector2d containing the relative position to move to. |
Passing a negative value for x coordinate will cause the sprite to move “left”, a negative value for the y coordinate will cause the sprite to move “up”.
declare property LastIndex( ) as uinteger
Returns the last referenced frame by GetImage
# include once "ext/graphics.bi" ''This lets us not have to type ext.gfx before every Sprite object. using ext.gfx screenres 640,480,32 ''This is the normal way to dimension a ext.gfx.Sprite object. var mySprite1 = Sprite(5) ''This is an alternative way that is compatible with arrays. dim mySprite2 as Sprite mySprite2.Init( 5 ) ''Functionally both ways are the same, but using the constructor method is preferred when not using an array of Sprites. ''This loop will make our lead character, a large blue circle. for n as uinteger = 0 to 4 var crcl = imagecreate(100,100,&hFF00FF) circle crcl, (50,50), 20 + (n*5), &h0000FF,,,, F ''You will notice we reuse the crcl variable name since Sprite becomes ''responsible for freeing the image's memory when it is no longer needed. mySprite1.SetImage(n, crcl) next ''This loop creates our enemy, a smaller green circle. for n as uinteger = 0 to 4 var crcl2 = imagecreate(50,50,&hFF00FF) circle crcl2, (25,25), 10 + (n*5), &h00FF00,,,,F mySprite2.SetImage(n, crcl2) next mySprite1.Position( 100, 100 ) mySprite2.Position( 200, 200 ) var n = 0 var tog = 0 ''Loop repeatedly until you press the ESC key on your keyboard. do while not multikey(FB.SC_ESCAPE) ''Lock the screen before drawing to prevent flicker screenlock cls mySprite1.DrawImage n, null, TRANS_ ''is equivalent to: put (x1,y1), mySprite1.GetImage(n), TRANS mySprite2.DrawImage n, null, TRANS_ ''is equivalent to: put (x2,y2), mySprite2.GetImage(n), TRANS ''Lets see if our hero has collided with our enemy. ''You could call the isCollided on either Sprite you wanted to check, it doesn't matter. if mySprite1.isCollided( mySprite2 ) then locate 1,1 print "Boom!" end if screenunlock ''Check for Arrow key presses and adjust our hero's location. if multikey(FB.SC_LEFT) then mySprite1.Update(-2) if multikey(FB.SC_RIGHT) then MySprite1.Update(2) if multikey(FB.SC_UP) then MySprite1.Update(,-2) if multikey(FB.SC_DOWN) then MySprite1.Update(,2) ''Slow things down a bit so you can see everything. sleep 50 ''The tog variable is used to make the circles appear to pulsate. if tog = 0 then n+=1 else n-=1 endif ''Reset tog if it goes out of bounds. if n < 0 then n = 0 tog = 0 end if if n > 4 then n = 4 tog = 1 end if ''Lather, Rinse, Repeat. loop
Creates a sprite object with a certain number of frames
declare constructor( byval num as uinteger )
Used to initialize when using an array of Sprite
declare sub Init( byval num as uinteger )
declare function GetImage( byval index as uinteger ) as FB.IMAGE ptr
Assigns a FB.IMAGE to a frame.
declare sub SetImage( byval index as uinteger, byval img as FB. IMAGE ptr )
Creates a full copy of the Image
declare sub DuplicateImage( byval from_index as uinteger, byval to_index as uinteger )
declare sub ReplaceImage( byval index as uinteger, byval img as FB. IMAGE ptr )
declare sub DeleteImage( byval index as uinteger )
Updates the collision mask for the specified frame.
declare sub UpdateImage( byval index as uinteger )
Determines if this Sprite object has collided with another Sprite object.
declare function isCollided( byref spr as Sprite, byval ppcol as PPOPTIONS = usePP, byval _index_ as integer = -1 ) as ext.bool
Rotates one frame to another frame.
declare sub RotateFrom( byval from_index as uinteger, byval to_index as uinteger, byval angle as integer )
Rotates a FB.IMAGE to a frame.
declare sub RotateFromImage( byval from_image as FB. IMAGE ptr, byval to_index as uinteger, byval angle as integer )
Draws the specified sprite either to an image buffer or to the screen.
declare sub DrawImage( byval src_img as uinteger, byval dst_img as FB. IMAGE ptr = null, byval method as DrawMethods = DrawMethods.XOR_ )
Updates the position of the Sprite in relative terms.
declare sub Update overload( byval _x_diff as single = 0, byref _y_diff as single = 0 )
Returns the number of frames this Sprite object holds (1 based)
declare property Count( ) as uinteger
Returns the last referenced frame by GetImage
declare property LastIndex( ) as uinteger