Andy
Andy

Reputation: 55

How to translate this old XNA code to XNA 4.0 code?

This old code doesn't work in XNA 4.0. What must be changed so that the code works in XNA 4.0?

spriteBatch.Begin(SpriteBlendMode.AlphaBlend,SpriteSortMode.Immediate,SaveStateMode.None,Matrix.Identity);
  graphics.GraphicsDevice.SamplerStates[0].MagFilter = TextureFilter.Point;
  graphics.GraphicsDevice.SamplerStates[0].MinFilter = TextureFilter.Point;
  graphics.GraphicsDevice.SamplerStates[0].MipFilter = TextureFilter.Point;
spriteBatch.End();

I get the following error messages:

'SpriteBlendMode' does not exist in the current context
'SaveStateMode' does not exist in the current context 'Microsoft.Xna.Framework.Graphics.SamplerState' does not contain a definition for 'MagFilter' and no extension method 'MagFilter' accepting a first argument of type 'Microsoft.Xna.Framework.Graphics.SamplerState' could be found (are you missing a using directive or an assembly reference?) 'Microsoft.Xna.Framework.Graphics.SamplerState' does not contain a definition for 'MinFilter' and no extension method 'MinFilter' accepting a first argument of type 'Microsoft.Xna.Framework.Graphics.SamplerState' could be found (are you missing a using directive or an assembly reference?) 'Microsoft.Xna.Framework.Graphics.SamplerState' does not contain a definition for 'MipFilter' and no extension method 'MipFilter' accepting a first argument of type 'Microsoft.Xna.Framework.Graphics.SamplerState' could be found (are you missing a using directive or an assembly reference?)

Upvotes: 1

Views: 4528

Answers (3)

gcode
gcode

Reputation: 2989

There are several things you have to look out for in bringing your code up to date with the 4.0 version of the XNA Framework:

Also, one more thing (which caught me as I was updating my code): It seems that something changed in-between versions 3.1 and 4.0 of the XNA framework that will make it so the SamplerStates collection and SamplerState objects are read-only after the graphics device is initialized. I found that creating my own SamplerState object (and modifying the properties there) worked:

SamplerState sState = new SamplerState();
sState.Filter = TextureFilter.Point;
BaseGame.Device.SamplerStates[0] = sState;

Upvotes: 2

Cyral
Cyral

Reputation: 14153

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, Matrix);
spriteBatch.End();

In XNA 4.0 You cannot change sampler state without re-iniating the Graphics Device, so in your games Initialize() Method, You need to add graphics.GraphicsDevice.SamplerStates[0].Filter = TextureFilter.Point;

To the best of my knowledge, that should be what you are looking for!

Upvotes: 0

jgallant
jgallant

Reputation: 11273

Here is a resource that will show you how to solve these problems:

http://www.nelxon.com/blog/xna-3-1-to-xna-4-0-cheatsheet/

These are all very common problems related to upgrading old XNA code to 4.0

Upvotes: 5

Related Questions