XNA: Sprite scaling

cugone

Member
Joined
Apr 30, 2008
Messages
19
Im having issues with sprite scaling in XNA. The original file is the size I want the various frames of the sprite sheet, but whenever I want the game engine to draw the texture it requests a "Uniform multiple to scale the height and width" or "a vector to scale the width and height by the x- and y- values". Problem is, telling it 1.0 scales by 100%, 0.0 scales it to 0, effectively erasing it, and scaling by 0.5 makes it roughly the size I want (not perfect), but BLURRY. :/

Any suggestions?
 
Yes, the scale you specify is a scaling factor, i.e. the size in texels is multiplied by the scale you specified to get the final size in pixels. If it is not 1.0 (100% scale) then the texture doesnt match the screen pixels 1 for 1 and has to be filtered. The default is usually linear filtering, which is medium quality. For higher quality, you could try changing the filter, like...
C#:
deviceManager.GraphicsDevice.SamplerStates[0].MagFilter = TextureFilter.GaussianQuad;
Nearest neighbor (TextureFilter.None) would be the least blurry, but tends to look blocky.
 
Yes, the scale you specify is a scaling factor, i.e. the size in texels is multiplied by the scale you specified to get the final size in pixels. If it is not 1.0 (100% scale) then the texture doesnt match the screen pixels 1 for 1 and has to be filtered. The default is usually linear filtering, which is medium quality. For higher quality, you could try changing the filter, like...
C#:
deviceManager.GraphicsDevice.SamplerStates[0].MagFilter = TextureFilter.GaussianQuad;
Nearest neighbor (TextureFilter.None) would be the least blurry, but tends to look blocky.

Ill go ahead and poke around with it, but as it stands, is there a way to tell it NOT to scale and just use whats there? (you know, like what DirectX does NORMALLY)
 
Do you want to scale the image?
scaling by 0.5 makes it roughly the size I want
If you specify a scale of 1.0 or use an overload that does not accept a scale the texture should be drawn at actual size and there will be no interpolation residue. Specifying a scale of 0.5 is telling XNA to scale and not just use whats there.

How are you drawing the sprites? Im assuming you are using the SpriteBatch class.
 
Do you want to scale the image?

If you specify a scale of 1.0 or use an overload that does not accept a scale the texture should be drawn at actual size and there will be no interpolation residue. Specifying a scale of 0.5 is telling XNA to scale and not just use whats there.

How are you drawing the sprites? Im assuming you are using the SpriteBatch class.

Yeah, Im using a spritebatch.

I need to specify an origin and a clipping rectangle (source rectangle in the documentation) because its a sprite sheet. Due to that, Im forced to specify a scale.

I noticed when I just use the default definition of (texture, position, tint) it still draws (the entire) texture way larger than it should, and blurry.
 
Maybe you should post your calls to SpriteBatch.Draw. Im not really sure what could be going wrong. It may be that there is some misunderstanding on how scale/origin/rotation works. I know it took my a lot of tinkering with them to get a good understanding of how to render sprites that properly rotate and scale with the correct source rectangle.

Ive been working on a game that uses sprite sheets for quite a while and havent run into problems once I got things sorted out. Im using a scale of 1.0 which renders as expected. Also, one thing that has caused me issues in the past (with MDX, but I dont think it matters) is changing my graphics card settings to override application settings (forcing anti-aliasing, mip-map levels, etc).
 
Well, that explains it. Apparently after writing a test case where I output all of my graphics devices capbilities "Powers of 2 Required" was true. Meaning I can only have textures that are square and a power of 2. :/ Nice of the documentation to actually EXPLAIN this. I had to read it from a reference book. :P
 
Back
Top