For those who are new to this game, a sprite is a 2D rectangular bitmap that is rendered to the screen. For 2D games, sprites are the most essential graphical abstraction. They are used for drawing maps, players, NPCs, items, particles, text, etc.
In Chickadee, the (chickadee graphics sprite)
module provides the
interface for working with sprites. Bitmaps are stored in textures
(see Textures) and can be used to draw sprites via the
draw-sprite
procedure.
Draw texture at position.
Optionally, other transformations may be applied to the sprite. rotation specifies the angle to rotate the sprite, in radians. scale specifies the scaling factor as a 2D vector. shear specifies the shearing factor as a 2D vector. All transformations are applied relative to origin, a 2D vector, which defaults to the lower-left corner.
tint specifies the color to multiply against all the sprite’s pixels. By default white is used, which does no tinting at all.
Alpha blending is used by default but the blending method can be changed by specifying blend-mode.
The area drawn to is as big as the texture, by default. To draw to an arbitrary section of the screen, specify rect.
It’s not uncommon to need to draw hundreds or thousands of sprites
each frame. However, GPUs (graphics processing units) are tricky
beasts that prefer to be sent few, large chunks of data to render
rather than many, small chunks. Using draw-sprite
on its own
will involve at least one GPU call per sprite. This is fine
for rendering a few dozen sprites, but will become a serious
bottleneck when rendering hundreds or thousands of sprites. To deal
with this, a technique known as “sprite batching” is used. Instead
of drawing each sprite immediately, the sprite batch will build up a
large buffer of sprites to draw and send them to the GPU all at once.
There is one caveat, however. Batching only works if the sprites
being drawn share a common texture. A good strategy for reducing the
number of different textures is to stuff many bitmaps into a single
image file and create a “texture atlas” (see Textures) to access
the sub-images within.
Create a new sprite batch for texture with initial space for capacity sprites. Sprite batches automatically resize when they are full to accomodate as many sprites as necessary.
Return #t
if obj is a sprite batch.
Return the texture for batch.
Set texture for batch to texture.
white
]Add sprite located at position to batch.
To render a subsection of the batch’s texture, a texture object whose parent is the batch texture may be specified as texture-region.
See draw-sprite
for information about the other arguments.
Reset size of batch to 0.
Render batch using blend-mode. Alpha blending is used by default.
A 9-patch is a method of rendering a texture so that it can be stretched to cover an area of any size without becoming distorted. This is achieved by dividing up the sprite into nine regions:
The most common application of this technique is for graphical user
interface widgets like buttons and dialog boxes which are often
dynamically resizable. By using a 9-patch, they can be rendered at
any size without scaling artifacts. The (chickadee graphics
9-patch)
module provides this functionality.
Draw a 9-patch over the area rect using texture whose stretchable/tileable patches are defined by the given margin measurements. The corners are never stretched/tiled, the left and right edges will be stretched/tiled vertically, the top and bottom edges may be stretched/tiled horizontally, and the center may be stretched/tiled in both directions.
mode may be either stretch
(the default) or tile
.
margin specifies the margin size for all sides of the 9-patch. To make margins of differing sizes, the top-margin, bottom-margin, left-margin, and right-margin arguments may be used.
Refer to draw-sprite
for information about the other arguments
as they are the same.