Drawing Sprites
Sprites in Amphora consist of two parts: an AmphoraImage
pointer, and any number of framesets.
The AmphoraImage
contains data such as the spritesheet resource to pull from, the coordinates, the scale factor, and the flip state of the current sprite.
A frameset describes an animated state that an AmphoraImage
can be in. Each frameset specifies a unique name, the frames to be used in its animation, and the delay between animation frames. An object in your game might have many framesets attached to it, for example, idle, walking, jumping, falling, and attacking would all be separate framesets, each describing the animation for its state.
Creating Sprites
First, you'll create an AmphoraImage
pointer and ensure it's set to NULL
.
Sprites are created using the Amphora_CreateSprite
function:
Creating an AmphoraImage
with Amphora_CreateSprite
will automatically add it to the render list to be displayed.
If Amphora_CreateSprite
succeeds, the passed in AmphoraImage
pointer will be set to the newly created AmphoraImage
, and the pointer will be returned as well. If it fails, it will return NULL
and the supplied pointer will not be modified.
Description of Parameters
The
spr
parameter is a pointer to anAmphoraImage
pointer. This pointer should beNULL
, otherwise the function will simply return it as is and not create the newAmphoraImage
.The
image_name
parameter is the name of the image resource specified inresources.h
. TheAmphoraImage
does not contain any texture data itself, it uses the image supplied here and draws portions of it based on the coordinates specified in a frameset.The
x
andy
parameters determine the initial position of theAmphoraImage
. This will be the upper left corner. Ifstationary
is true, these values will be the distance from the window/screen edge to the image, with positive x and y values starting from the left and top edges, and negative x and y values starting from the right and bottom edges.The
scale
parameter is used to scale the image drawn. For example, a scale parameter of 2 would cause the image to be drawn with its width and heights doubled.The
flip
parameter controls whether or not the image is drawn flipped horizontally.The
stationary
parameter determines whether the image should be stationary. Similarly to strings, a stationary image is positioned relative to the window/screen edges and its position on screen will not change. For example, a health display would be stationary, an enemy monster would not.The
order
parameter controls the draw order of all drawable objects, with higher numbers drawing on top of lower numbers.
Creating Framesets
Once an AmphoraImage
is created, you'll need to add a frameset to it. This is done with the Amphora_AddFrameset
function:
Description of Parameters
The
spr
parameter to which the created frameset should be attached.The
name
parameter is the name of the frameset to be created. This must be unique among framesets attached to any givenAmphoraImage
, but differentAmphoraImage
instances can have framesets with identical names (ie. anAmpohraImage
player1 cannot have two framesets both called idle, but player1 and player2 can each have a frameset called idle).The
sx
andsy
parameters are the upper left pixel of the first frame of the animation described by the frameset on the spritesheet named in theAmphoraImage
name
parameter.The
w
andh
parameters are the width and height of the frames for the described animation.
The
off_x
andoff_y
parameters specify a pixel offset to be applied to the frames in an animation. These numbers will be subtracted from the position of theAmphoraImage
. Typically, these will be used to maintain the relative center of a sprite when switching between framesets of different sizes. For example, a sprite's idle animation may be 16x32 pixels, but the attack animation might be 32x32 pixels to accommodate a sword swing. In this case, switching from an idle state to an attack state would move the visible image to the right. One would useoff_x
here to maintain the proper position when switching between the framesets.The
num_frames
parameter specifies the number of frames in the animation. These will be read sequentially, left-to-right from the spritesheet, starting from the initial frame specified bysx
,sy
,w
, andh
. If your frameset is a static image that is not animated, you would set this to 1.The
delay
parameter specifies the number of milliseconds between each animation frame change. If your frameset is a static image that is not animated, this value does not matter and can just be set to 0.
Selecting Framesets
There are two methods to set the active frameset for an AmphoraImage
: Amphora_SetFrameset
, and Amphora_PlayOneshot
.
In both of these, spr
is the AmphoraImage
and name
is the name of the frameset that's previously been created.
The difference between the two is that Amphora_SetFrameset
will loop the animation, while Amphora_PlayOneshot
will play the animation once, holding on the last frame, and will execute a supplied callback function once the animation finishes. This callback function is a standard C function pointer to a function that takes no parameters and returns void, or it can be a non-trapping lambda if using C++. If you do not wish to execute a callback function, pass NULL
in C or nullptr
in C++.
Freeing Sprites
Freeing sprites is done with the Amphora_FreeSprite
function:
This will free all resources associated the specified AmphoraImage
and reset the pointer to NULL
.
All existing images are freed automatically on exit, so it is only necessary to manually free images that you no longer need (ie. an enemy that the player defeated).