Saturday, August 6, 2011

XNA Notes–Day 1

I’ve been programming on and off in XNA for a few years now, but I never really tried to learn XNA, just picked it up on the go, googling whatever was needed at the time. But now I’m working on a more dedicated project, and not knowing my tools in depth can only lead to suboptimal solutions. Therefore, I have decided to pick up the XNA Game Studio 4.0 Programming book, by Tom Miller and Dean Johnson, and start studying it from the beginning.

This series of XNA notes are just that, notes from studying, with curious / useful facts from the book, as I have a horrible memory and writing things down is the only way to remember them. Besides, someone else might find something useful here, who knows?

So, without further ado, today’s notes:

Random notes

Game Studio 4.0 is broken up in two different profiles, Reach which encompasses features for all platforms and HiDef, which includes extra features that exist only in W and Xbox.

The origin of coordinates is the top left.

font.MeassureString(string) tells you the width and height of the string in that font.


Tinting in spritebatch.draw multiplies the original color of the image by the color of the tint.

Using a Vector2 instead of a rectangle means we don't do any stretching to the image when we render it on screen, we draw it with the original size.

Different overloads

In the appropriate draw overloads, the source rectangle lets you decide which part of the original image you're going to draw. Useful for frame animations.
In the appropriate draw overloads you can rotate the image using an angle in radians and an origin for the rotation.  You can also scale the image.
You have some crappy SpriteEffect options, which are to flip horizontaly, to flip vertically or none.
There is a Z ordering for the drawing between 1.0 (top) and 0.0 (bottom) that only matter if you play arround with the sorting modes.

Sprite Sorting

Using spritebatch.begin we can specify the draw mode, among others SpriteSortMode…

  • Texture (sort by texture, do all of the same texture at the same time as not to have to change, default),
  • FrontToBack / BackToFront (taking into account the depth specified for each draw call),
  • Deferred (Drawn in the order it is used in the draw calls),
  • Immediate (different from all others bacause hardware render calls are not batched, but instead we have one hardware call per draw call).

Sprite Blending

Using spritebatch.begin we can also specify the blend state, among others BlendState.

  • AlphaBlend (default blending mode, transparency works fine),
  • Opaque (No blending),
  • Aditive (adds the colors together).

No comments:

Post a Comment