Sunday, May 10, 2009

Sprite Sizes

I set myself to displaying a nice logo screen before my game came up recently. I figured this would be a fairly straightforward process given the work I had done so far. Just setup a Scene object with one sprite in it and stick it in the center of the screen. Easy!

Well, nothing is ever easy, as soon as I slapped my logo on the screen the image was stretched! It look horrible! Since I was testing this in my monitor's native resolution I knew exactly how the image should look. I began the process of debugging and found that it was reading my logo in as a 1024x1024 image, when the actual image was something more like 600x600.

With a bit of Googling I undercovered that the textures I'm using as sprites need to be loaded in with square resolutions in powers of two (2x2, 4x4, 8x8, 16x16, 32x32, 64x64, 128x128, 256x256, 512x512, 1024x1024, etc). The two colored dot images I was using were 100x100 and 200x200, but as expected it turns out these were being loaded in as 128x128 and 256x256 respectively. I just hadn't noticed because it wasn't too drastic at that size.

Well, the solution to this is to load your sprites in as textures of the allowed size, and the grab out the pieces you need. Example:

sprite.Draw(spriteTexture, textureSize, new Vector3(0, 0, 0), new Vector3(xPos, yPos, 0), this.alphaColor);

The second parameter of the D3D Sprite.Draw method takes a Rectange object (textureSize) that specifies what portion of your texture you want to draw. I created a sprite subclass called BlitSprite (I *think* the term for this is blitting, thus the name) which is overloaded to take a rectangle instead of just using the entire texture square.

You can (and should!) also combine sprites into a single texture. Now I'm pulling three colored dots from one image file like this:



















Also, I added a parameter to my Sprite baseclass that allows me to pass in a black value to the image. So, against a black background that behaves like a fade. I used it to gently fade my logo in and out before the game starts up. Of course, the user can just hit any key to skip it :).

No comments:

Post a Comment