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, 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. All transformations are applied relative to origin, a 2D vector, which defaults to the lower-left corner.
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.
Finally, advanced users may specify shader to change the way the sprite is rendered entirely.
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, which will
quickly lead to poor performance. To deal with this, a technique
known as “sprite batching” can be used. Instead of drawing each
sprite immediately, the sprite batch will build up a large of buffer
of sprites to draw and defer rendering until the last possible moment.
Batching isn’t a panacea, though. Batching only works if the sprites
being drawn share as much in common as possible. Every time you draw
a sprite with a different texture or blend mode, the batch will be
sent off to the GPU. Therefore, batching is most useful if you
minimize such changes. A good strategy for reducing texture changes
is to stuff many bitmaps into a single image file and create a
“texture atlas” (see Textures) to access the sub-images within.
Taking advantage of sprite batching in Chickadee is easy, just wrap
the code that is calling draw-sprite
a lot in the
with-batched-sprites
form.
Use batched rendering for all draw-sprite
calls within
body.
With a basic sprite abstraction in place, it’s possible to build other abstractions on top of it. One such example is the “nine patch”. A nine patch is a sprite that can be rendered at various sizes without becoming distorted. This is achieved by diving up the sprite into nine regions:
The one caveat is that the bitmap regions must be designed in such a way so that they are not distorted when stretched along the affected axes. For example, that means that the top and bottom sides could have varying colored pixels vertically, but not horizontally.
The most common application of this technique is for graphical user interface widgets like buttons and dialog boxes. By using a nine patch, they can be rendered at any size without unappealing scaling artifacts.
Draw a nine patch sprite. A nine patch sprite renders texture as a width x height rectangle whose stretchable areas are defined by the given margin measurements top-margin, bottom-margin, left-margin, and right-margin. The margin argument may be used to configure all four margins at once.
Refer to draw-sprite
(see Sprites) for information about
the other arguments.