# Sprites

Dear Mom,

I won’t be home this weekend because I’m wanted for treason and I have to clear my name. Also, I took the last Sprite from the fridge.

Love, Steve

— Mac Barnett

A sprite is basically what we called a bitmap image in the previous tutorial. A broader definition would classify sprites as 2D objects within the game world, that can be drawn using a single image on any given frame. Sprites can be used to represent game characters, to draw beautiful backgrounds and to create beautiful user interfaces.

Modern 2D games obviously have hundreds to thousands of sprites, meaning that the sprites constitute the majority of the size of the game. Keeping that in mind, it is important to use sprites as efficiently as possible.

## Drawing Sprites

After the last tutorial, drawing sprites is straightforward. We simply create a class, the Sprite class, to encapsulate the tedious WIC functions, and then we can easily load images from the hard drive and draw them to the screen.

### Example

As you can see, nothing new is happening here, but all the bad stuff is hidden away!

## Painter’s Algorithm

This was too easy! Now in the previous example we intuitively draw the background image before drawing the tetronimo character, just like a painter paints a scene on canvas. This method is therefore known as the Painter’s Algorithm, or priority fill.

The painter’s algorithm simply sorts all sprites in a given scene from back to front and when it is time to draw the sprites, the presorted scene can be traversed and drawn in order. Despite this algorithm being one of the simplest solutions to the visibility problem in computer graphics and its obvious drawback of painting the invisible areas of distant objects, it works suitably well in a 2D environment.

To facilitate the use of the painter’s algorithm, two more member variables were added to the Sprite class, an unsigned int specifying the draw order of the sprite, and an enumeration to specify the layer the sprite is living in: A sprite can be in the background, or the same level as the characters and objects of the game, or used in the user interface. Here is the new definition of the Sprite class:

Obviously, when drawing multiple sprites, the background sprites (in the correct order defined by their draw order member variables) are drawn first, followed by the character sprites before the User-Interface sprites are finally drawn.

To facilitate working with multiple sprites, a new class was created, the

## Sprite List Class

As you can see, the SpriteList class holds three different maps, one for each possible layer. Those maps are ordered by the draw orders of their sprites. All the sprites in the background layer will automatically be placed behind all the sprites in the character layer, and both the background and character layers will show up behind the UI layer. Within each individual layer, the sprites will still be drawn according to their draw order, which is now only relative to the other sprites in the same layer:

The draw command is a simple utility to provide the opportunity to only draw a specified layer.

Actually populating the sprite list, or maps, is easy as well. It is possible to add already existing sprites:

Or to create an entirely new sprite and directly add it to the correct list, or map:

### Example

Here is the same scene as above, with the only difference being that the SpriteList class is used:

This is almost too easy! All we have to do is to create the sprites by specifying the location on the hard drive for the image to load in, and everything else is optional! If we’d like, we can add an initial position, or a destination rectangle specifying where to draw the Sprite to.

You should play around with Sprites for a bit, to get the hang out of them.

In the next tutorial, we will learn how to animate those new favourite toys of ours.

## References

### Literature

(in alphabetic order)

• Game Programming Algorithms, by Sanjay Madhav
• Game Programming Patterns, by Robert Nystrom
• Microsoft Developer Network (MSDN)
• Tricks of the Windows Game Programming Gurus, by André LaMothe
• Wikipedia