From 279f17ac0e1b3d019c2b294098e834d249376686 Mon Sep 17 00:00:00 2001
From: David Thompson 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
@@ -104,7 +106,7 @@ stored in textures (see Textures) and can b
via the Draw texture at position.
-2.3.3 Sprites
+2.3.2 Sprites
draw-sprite
procedure.
-
draw-sprite
procedure.
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. -
-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.
+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 of 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.
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.
+
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
] [#:texture-region]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. +
Use batched rendering for all draw-sprite
calls within
-body.
+
alpha
]Render batch using blend-mode. Alpha blending is used by +default.
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 +becoming distorted. This is achieved by dividing up the sprite into nine regions:
Draw a nine patch sprite. A nine patch sprite renders texture as a width x height rectangle whose stretchable areas are -- cgit v1.2.3