graphics/sprite.bi

Summary
graphics/sprite.bi
LicenseCopyright © 2007-2014, FreeBASIC Extended Library Development Group
ext.gfx
Enumerations
PPOPTIONSUsed in the Sprite class to determine whether to use pixel perfect collision or not.
SpriteFully featured sprite class with pixel perfect collision detection built-in
Functions
constructorCreates a sprite object with a certain number of frames
default constructorCreates an unitialized Sprite object.
copy constructorMakes a deep copy of another Sprite object.
InitUsed to initialize when using an array of Sprite
FromSpritesheetLoads a row of sprite images contained in a single image into the Sprite class.
GetImage
SetImageAssigns a IMAGE to a frame.
DuplicateImageCreates a full copy of the Image
ReplaceImage
DeleteImage
UpdateImageUpdates the collision mask for the specified frame.
isCollidedDetermines if this Sprite object has collided with another Sprite object.
RotateFromRotates one frame to another frame.
RotateFromImageRotates a IMAGE to a frame.
DrawImageDraws the specified sprite either to an image buffer or to the screen.
PostitionSets or Gets the position of the sprite for use with the draw and collision statements.
PostitionSets or Gets the position of the sprite for use with the draw and collision statements.
UpdateUpdates the position of the Sprite in relative terms.
UpdateUpdates the position of the Sprite in relative terms.
Properties
CountReturns the number of frames this Sprite object holds (1 based)
LastIndexReturns the last referenced frame by GetImage

License

Copyright © 2007-2014, 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

ext.gfx

Summary
Enumerations
PPOPTIONSUsed in the Sprite class to determine whether to use pixel perfect collision or not.

Enumerations

PPOPTIONS

Used in the Sprite class to determine whether to use pixel perfect collision or not.

usePPThe default, to use pixel perfect collision.
noPPonly use rectangular collision.

Sprite

Fully featured sprite class with pixel perfect collision detection built-in

Summary
Functions
constructorCreates a sprite object with a certain number of frames
default constructorCreates an unitialized Sprite object.
copy constructorMakes a deep copy of another Sprite object.
InitUsed to initialize when using an array of Sprite
FromSpritesheetLoads a row of sprite images contained in a single image into the Sprite class.
GetImage
SetImageAssigns a IMAGE to a frame.
DuplicateImageCreates a full copy of the Image
ReplaceImage
DeleteImage
UpdateImageUpdates the collision mask for the specified frame.
isCollidedDetermines if this Sprite object has collided with another Sprite object.
RotateFromRotates one frame to another frame.
RotateFromImageRotates a IMAGE to a frame.
DrawImageDraws the specified sprite either to an image buffer or to the screen.
PostitionSets or Gets the position of the sprite for use with the draw and collision statements.
PostitionSets or Gets the position of the sprite for use with the draw and collision statements.
UpdateUpdates the position of the Sprite in relative terms.
UpdateUpdates the position of the Sprite in relative terms.
Properties
CountReturns the number of frames this Sprite object holds (1 based)
LastIndexReturns the last referenced frame by GetImage

Functions

constructor

declare constructor(byval num as uinteger)

Creates a sprite object with a certain number of frames

Parameters

numthe number (1 based) of frames to create

default constructor

Creates an unitialized Sprite object.  Used with Init.

copy constructor

Makes a deep copy of another Sprite object.

Init

declare sub Init(byval num as uinteger)

Used to initialize when using an array of Sprite

Parameters

numthe 1 based number of frames to create

FromSpritesheet

declare function FromSpritesheet(byval srci as image ptr,
byval startx as uinteger,
byval starty as uinteger,
byval spwidth as uinteger,
byval spheight as uinteger,
byval startindex as uinteger,
byval numsp as integer) as integer

Loads a row of sprite images contained in a single image into the Sprite class.

Parameters

srcipointer to FB.image containing the spritesheet data
startxthe x coordinate the first sprite begins at
startythe y coordinate the first sprite begins at
spwidththe width of the individual sprite (all must match)
spheightthe height of the individual sprite (all must match)
startindexthe (0 based) index in the Sprite object to begin adding sprites
numspthe number of sprites to return

Returns

-1 = number of sprites does not match image width 0 = Sprite object cannot hold required number of sprites

0 = the number of sprites loaded, should match numsp

GetImage

declare function GetImage(byval index as uinteger) as IMAGE ptr

Parameters

indexthe (0 based) frame to return

Returns

pointer to IMAGE containing image data.

Usage

If you modify the image returned by this function you must then call UpdateImage to regenerate the collision mask.

SetImage

declare sub SetImage(byval index as uinteger,
byval img as IMAGE ptr)

Assigns a IMAGE to a frame.

Parameters

indexthe frame number to assign to (0 based), does not check if data already exists there.
imgthe IMAGE structure to assign to this frame.

Usage

This object will automatically free the image data when it is no longer needed, you should not deallocate this memory yourself.

DuplicateImage

declare sub DuplicateImage(byval from_index as uinteger,
byval to_index as uinteger)

Creates a full copy of the Image

Parameters

from_indexthe Image to copy.  (0 based)
to_indexthe empty index to copy to.  (0 based)

ReplaceImage

declare sub ReplaceImage(byval index as uinteger,
byval img as IMAGE ptr)

Parameters

indexthe frame number to assign to (0 based)
imgthe IMAGE to replace with.

Usage

This method will free the previous frame if it exists.

DeleteImage

declare sub DeleteImage(byval index as uinteger)

Parameters

indexthe image to destroy (0 based)

UpdateImage

declare sub UpdateImage(byval index as uinteger)

Updates the collision mask for the specified frame.

Parameters

indexthe frame to update the collision mask for (0 based).

Usage

You only need to call this subroutine if you modify the image returned by GetImage

isCollided

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.

Parameters

sprthe second Sprite object to test collision with.
ppcoloptional value to determine collision method.  Defaults to usePP.
_index_optional index into the second Sprite object to test collision on.  (0 based)

Returns

ext.bool.true on collision, ext.bool.false otherwise.

Usage

This function first performs a bounding box collision check to decrease unnecessary calls to the pixel perfect routine.

RotateFrom

declare sub RotateFrom(byval from_index as uinteger,
byval to_index as uinteger,
byval angle as integer)

Rotates one frame to another frame.

Parameters

from_indexthe source image index to rotate.  (0 based)
to_indexthe destination index to overwrite with the rotated image.  (0 based)
anglethe angle to rotate the image to.

RotateFromImage

declare sub RotateFromImage(byval from_image as IMAGE ptr,
byval to_index as uinteger,
byval angle as integer)

Rotates a IMAGE to a frame.

Parameters

from_imagethe source image to rotate.
to_indexthe destination index to overwrite with the rotated image.  (0 based)
anglethe angle to rotate the image to.

DrawImage

declare sub DrawImage(byval src_img as uinteger,  
byval dst_img as IMAGE ptr =  null,
byval method as DrawMethods =  DrawMethods.XOR_)

Draws the specified sprite either to an image buffer or to the screen.

Parameters

src_imgthe Source image to draw.
dst_imgthe destination to draw the image onto, defaults to null which will draw on the screen.
methodthe method to use to draw the image from DrawMethods, defaults to XOR (XOR_) like FBGFX’s PUT statement.

Postition

Sets or Gets the position of the sprite for use with the draw and collision statements.

Parameters

_x_the x coordinate.
_y_the y coordinate.

Usage

To get the position pass one or both coordinates as negative (i.e.  < 0) and the current values will be returned in the variables passed.

Postition

Sets or Gets the position of the sprite for use with the draw and collision statements.

Parameters

_vec_<vector2>(single) containing the x and y coordinates.

Usage

To get the position pass a <vector2>(single) with one or both coordinates as negative (i.e.  < 0) and the current values will be returned in the <vector2> variable passed.

Update

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.

Parameters

_x_relative x position to move sprite to.
_y_relative y position to move sprite to.

Usage

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”.

Update

declare sub Update(byval _vec_ as ext.math.vec2f)

Updates the position of the Sprite in relative terms.

Parameters

_vec_vector2d containing the relative position to move to.

Usage

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”.

Properties

Count

declare property Count( ) as uinteger

Returns the number of frames this Sprite object holds (1 based)

LastIndex

declare property LastIndex( ) as uinteger

Returns the last referenced frame by GetImage

declare constructor(byval num as uinteger)
Creates a sprite object with a certain number of frames
Fully featured sprite class with pixel perfect collision detection built-in
declare sub Init(byval num as uinteger)
Used to initialize when using an array of Sprite
declare function FromSpritesheet(byval srci as image ptr,
byval startx as uinteger,
byval starty as uinteger,
byval spwidth as uinteger,
byval spheight as uinteger,
byval startindex as uinteger,
byval numsp as integer) as integer
Loads a row of sprite images contained in a single image into the Sprite class.
declare function GetImage(byval index as uinteger) as IMAGE ptr
declare sub SetImage(byval index as uinteger,
byval img as IMAGE ptr)
Assigns a IMAGE to a frame.
declare sub DuplicateImage(byval from_index as uinteger,
byval to_index as uinteger)
Creates a full copy of the Image
declare sub ReplaceImage(byval index as uinteger,
byval img as IMAGE ptr)
declare sub DeleteImage(byval index as uinteger)
declare sub UpdateImage(byval index as uinteger)
Updates the collision mask for the specified frame.
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.
declare sub RotateFrom(byval from_index as uinteger,
byval to_index as uinteger,
byval angle as integer)
Rotates one frame to another frame.
declare sub RotateFromImage(byval from_image as IMAGE ptr,
byval to_index as uinteger,
byval angle as integer)
Rotates a IMAGE to a frame.
declare sub DrawImage(byval src_img as uinteger,  
byval dst_img as IMAGE ptr =  null,
byval method as DrawMethods =  DrawMethods.XOR_)
Draws the specified sprite either to an image buffer or to the screen.
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.
declare property Count( ) as uinteger
Returns the number of frames this Sprite object holds (1 based)
declare property LastIndex( ) as uinteger
Returns the last referenced frame by GetImage
Simple 2 dimensional vector.