From 279f17ac0e1b3d019c2b294098e834d249376686 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Tue, 4 Jun 2019 20:49:16 -0400 Subject: Update chickadee manual. --- manuals/chickadee/Sprites.html | 87 +++++++++++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 26 deletions(-) (limited to 'manuals/chickadee/Sprites.html') diff --git a/manuals/chickadee/Sprites.html b/manuals/chickadee/Sprites.html index 29d16ea..a0e308b 100644 --- a/manuals/chickadee/Sprites.html +++ b/manuals/chickadee/Sprites.html @@ -1,6 +1,6 @@ - @@ -94,7 +96,7 @@ Next: ,
-

2.3.3 Sprites

+

2.3.2 Sprites

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-sprite procedure.

-
Procedure: draw-sprite texture position [#:origin] [#:scale] [#:rotation] [#:blend-mode alpha] [#:rect] [#:shader]
+
Procedure: draw-sprite texture position [#:tint white] [#:origin] [#:scale] [#:rotation] [#:blend-mode alpha] [#:rect]

Draw texture at position.

@@ -114,47 +116,80 @@ via the 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. +

+
Procedure: make-sprite-batch texture [#:capacity 256]
+

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

+ +
+
Procedure: sprite-batch? obj
+

Return #t if obj is a sprite batch. +

+ +
+
Procedure: sprite-batch-texture batch
+

Return the texture for batch. +

+ +
+
Procedure: set-sprite-batch-texture! batch texture
+

Set texture for batch to texture. +

+ +
+
Procedure: sprite-batch-add! batch position [#:origin] [#:scale] [:rotation] [#:tint 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. +

+ +
+
Procedure: sprite-batch-clear! batch
+

Reset size of batch to 0. +

+
-
Syntax: with-batched-sprites body
-

Use batched rendering for all draw-sprite calls within -body. +

Procedure: draw-sprite-batch batch [#:blend-mode 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: