Welcome to Week 4 of the XNA 4.0 Winter Workshop. Over the next week, ending Sunday, July 31st we'll be working together as a cohort to master the knowledge, both of the XNA API as well as game programming in general, to work with 2D Graphics. As you read my little overview below, please remember the goal of these guided workshops is both to point people in the right direction, but mostly to create a community of people working through the information together. It's through this community that questions are asked and answered and real knowledge is obtained. I can't regurgitate what's in the textbook here in my blog, nor can I provided an entire math curriculum for learning vectors, matrices, and trig. But I
CAN most assuredly answer well thought-out and narrowly defined questions; as can all of the other folks following along. So remember, read the text, and then hit the
forums to be part of the community.
This week's reading material is chapters 9 and 10 from our textbook.
Chapter 9 - 2D Basics
The first chapter of the week covers a few different topics related to 2D game programming with XNA. First, it begins with an overview of Sprite Batches. In games, a sprite is generally an actor with a set of animations, but can also be a projectile, a piece of background or foreground environment art, or even the entire background itself. In truth, a sprite can be anything you draw on the screen. Because XNA is really a 3D engine, it draws all of our sprites as a series of 2D quads (two triangles together to form a rectangle). Because sending quads to the video card individually would be time consuming, XNA "batches" them together, and attempts to submit them to the video card all at once. If all of your sprites are pulled from a single texture, it can actually accomplish this quite nicely.
Within chapter 9 it spends some time talking about the properties of a SpriteBatch. In specific it looks at the Blend Modes, the Sort Modes, and the Save State Modes. How these are used has changed slightly between XNA 3.1 and 4.0. I'll make an additional post on that later.
After that, chapter 9 shows a few demos on how to use the things above to create loading screens.
Chapter 9 concludes with a section on drawing text with XNA. When we're in Windows, OS X, or some other operating system, there is a text engine which takes font data from a file in the form of strokes and is able to reproduce the characters of the font on screen at any size desired. This is because these font files are vector based, similar to a postscript or pdf file, while most of the images we see on computers and in games are raster based, that is, they're just a series of pixels. Because our Zune, Windows Phone, and Xbox 360 don't have font engines, it's necessary for us to render our fonts to a texture first, and then use portions of that texture like a sprite sheet in order to draw text to the screen. This is done by creating a .spritefont resource which is then turned into a texture during the build phase of our game by the content pipeline.
Chapter 10 - 2D Effects
The second chapter of the week builds on to the first by looking at Cel Animation, the process of creating animation by flipping rapidly through different cels of a sprite sheet. A demo is made with a class called CalAnimationManager. After that, the chapter looks at how you can manipulate the way sprites (and text) are drawn on the screen by providing different values to the SpriteBatch.Begin and SpriteBatch.Draw methods. These include rotating and scaling sprites, as well tinting the color, or changing the blend mode so that sprites will have alpha or additive blending. Additive blending is then used, in combination with the CelAnimationManager, to create an explosion special effect.
Note, as with Chapter 9, the way in which we set Blend Modes, etc... has changed slightly from XNA 3.1 to 4.0. I'll provide a follow-up to this post that covers those topics individually.
Supplemental Reading
Below is a list of supplemental reading that might help this week's reading make more sense to you. I am likely to add to this list over the course of the week (or even the workshop) and will make an announcement when I've added additional reading or resources.
XNA 4.0 Source Code
Source code for Chapters 9 and 10 coming later this week.
Discussion