XNA: Sprite scaling

cugone

Newcomer
Joined
Apr 30, 2008
I'm 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?
 

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Location
USA
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 doesn't 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.
 

cugone

Newcomer
Joined
Apr 30, 2008
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 doesn't 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.

I'll go ahead and poke around with it, but as it stands, is there a way to tell it NOT to scale and just use what's there? (you know, like what DirectX does NORMALLY)
 

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Location
USA
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 what's there.

How are you drawing the sprites? I'm assuming you are using the SpriteBatch class.
 

cugone

Newcomer
Joined
Apr 30, 2008
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 what's there.

How are you drawing the sprites? I'm assuming you are using the SpriteBatch class.

Yeah, I'm using a spritebatch.

I need to specify an origin and a clipping rectangle (source rectangle in the documentation) because it's a sprite sheet. Due to that, I'm 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.
 

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Location
USA
Maybe you should post your calls to SpriteBatch.Draw. I'm 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.

I've been working on a game that uses sprite sheets for quite a while and haven't run into problems once I got things sorted out. I'm 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 don't think it matters) is changing my graphics card settings to override application settings (forcing anti-aliasing, mip-map levels, etc).
 

cugone

Newcomer
Joined
Apr 30, 2008
Well, that explains it. Apparently after writing a test case where I output all of my graphics device's 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
 
Top Bottom