XNA UK Usergroup
A quick plug or the people at XNA UK Usergroup: http://xna-uk.net/ . Great people, great information and examples.
A quick plug or the people at XNA UK Usergroup: http://xna-uk.net/ . Great people, great information and examples.
Somebody has taken the time to list the differences between XNA 3.1 and 4.0. This would have saved everyone many many hours if this was done a few months ago. Well done Nelxon!
http://www.nelxon.com/blog/xna-3-1-to-xna-4-0-cheatsheet/
XNA 3.1 code causes following error when compiled with XNA 4.0:
Error 5 ’Microsoft.Xna.Framework.Graphics.RenderTarget2D’ does not contain a constructor that takes 5 arguments.
XNA 3.1 code: renderTarget =new RenderTarget2D(device, width, height, 1, device.DisplayMode.Format);
XNA 3.1 had 4 overload versions:
RenderTarget2D.RenderTarget2D(Graphics Device graphicsDevice, int width, int height, int numberLevels, SurfaceFormat format)
RenderTarget2D.RenderTarget2D(Graphics Device graphicsDevice, int width, int height, int numberLevels, SurfaceFormat format, RenderTargetUsage useage)
RenderTarget2D.RenderTarget2D(Graphics Device graphicsDevice, int width, int height, int numberLevels, SurfaceFormat format, MultiSampleType multiSampleType, int multiSampleQuality)
RenderTarget2D.RenderTarget2D(Graphics Device graphicsDevice, int width, int height, int numberLevels, SurfaceFormat format, MultiSampleType multiSampleType, int multiSampleQuality, RenderTargetUsage useage)
In XNA 4.0 this has reduced to:
RenderTarget2D.RenderTarget2D(Graphics Device graphicsDevice, int width, int height)
RenderTarget2D.RenderTarget2D(Graphics Device graphicsDevice, int width, int height, bool mipMap, SurfaceFormat prefferedFormat, DepthFormat prefferedDepthFormat)
RenderTarget2D.RenderTarget2D(Graphics Device graphicsDevice, int width, int height, bool mipMap, SurfaceFormat prefferedFormat, DepthFormat prefferedDepthFormat, int prefferedMultiSampleCount, RenderTargetUsage useage)
The changes to RenderTarget were required as there were too many opportunites to generate errors (eg if you changed the rendertarget but not the depthbufffer) . See Shawn Hargreaves Blog and Shawn Hargreaves Blog for full explanation.
Although it compiles and now runs, there are still a few edits required. The obvious problems are visual and result in problems with the transparency of the 3D tiles.
GraphicsDevice.RenderState.AlphaBlendEnable = true;
is no longer valid and I had not replaced all with the new alternative:
GraphicsDevice.BlenState = BlendState.AlphaBlend;
Some 3D tile objects were transparent to other 3D objects. This was due to the removal of:
In XNA 4 this was replaced by:
DepthStencilState depthBufferState;
depthBufferState = new DepthStencilState();
depthBufferState.DepthBufferEnable = true;
GraphicsDevice.DepthStencilState = depthBufferState;
And now everything works again. Simple!
Here are notes on code that I needed modify to allow me to move from XNA 3.1 to XNA 4:
The name ‘SpriteBlendMode’ does not exist in the current context
This is due to a change to SpriteBatch.Begin. The SpriteBlendMode enum is now gone and we should now specify the state object directly. Also SaveStateMode is now also not required.
XNA 3.1: spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
XNA 4: spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend,null,null,null);
‘Microsoft.Xna.Framework.Graphics.RenderTarget2D’ does not contain a constructor that takes 5 arguments
XNA 3.1 :renderTarget = new RenderTarget2D(device, 512, 512, 1, SurfaceFormat.Color);
RenderTarget2D now inherits direct from Texture2D.
‘Microsoft.Xna.Framework.Graphics.GraphicsDevice’ does not contain a definition for ‘RenderState’ and no extension method ‘RenderState’ accepting a first argument of type ‘Microsoft.Xna.Framework.Graphics.GraphicsDevice’ could be found
XNA 3.1 :graphics.GraphicsDevice.RenderState.AlphaBlendEnable =false;
Error 10 ’Microsoft.Xna.Framework.Graphics.BasicEffect’ does not contain a definition for ‘Begin’ and no extension method ‘Begin’ accepting a first argument of type ‘Microsoft.Xna.Framework.Graphics.BasicEffect’ could be found
Error 15 ’Microsoft.Xna.Framework.Graphics.EffectPass’ does not contain a definition for ‘End’ and no extension method ‘End’ accepting a first argument of type ‘Microsoft.Xna.Framework.Graphics.EffectPass’ could be found
XNA 3.1: effectPass.Begin();
XNA 4: effectPass.Apply();
XNA 3.1: effectPass.End(); No longer required to begin and end the effect.
BasicEffect has been extended for XNA 4.0 to support 20 vertex shaders (was 12) and 10 pixel shaders (was 4), mainly due to the inclusion of Windows Phone 7 to be supported by the framework.
Error 35 ’Microsoft.Xna.Framework.Graphics.BasicEffect’ does not contain a constructor that takes 2 arguments Now only 1 argument
XNA 3.1: new BasicEffect(GraphicsDevice, null);
XNA 4: new BasicEffect(GraphicsDevice);
The EffectPool is no longer required, even if it was only ever null in my cases
Error 39 ’Microsoft.Xna.Framework.Graphics.EffectPass’ does not contain a definition for ‘Begin’ and no extension method ‘Begin’ accepting a first argument of type ‘Microsoft.Xna.Framework.Graphics.EffectPass’ could be found
XNA 3.1: effect.Begin();
XNA 3.1: effect.End(); No longer required to begin and end the effect.
Error 35 ’Microsoft.Xna.Framework.GameTime’ does not contain a definition for ‘ElapsedRealTime’ and no extension method ‘ElapsedRealTime’ accepting a first argument of type ‘Microsoft.Xna.Framework.GameTime’ could be found
Error 36 ’Microsoft.Xna.Framework.GameTime’ does not contain a definition for ‘TotalRealTime’ and no extension method ‘TotalRealTime’ accepting a first argument of type ‘Microsoft.Xna.Framework.GameTime’ could be found
ElapsedRealTime and TotalRealTime nolonger supported by the GameTime class. In my case it was OK to use ElapsedGameTime and TotalGameTime. Alternatively .Net timer API could be used if really need ‘real’ time.
Error 24 ’Microsoft.Xna.Framework.Graphics.VertexPositionTexture’ does not contain a definition for ‘SizeInBytes’
VertexBuffer is now a stronly typed container using an associated VertexDeclation. This was not the case prior to XNA 4.0:
XNA 3.1: vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, VertexPositionTexture.SizeInBytes * hexVertices.Length, BufferUsage.WriteOnly);
XNA 4: vertexBuffer = new VertexBuffer(graphics.GraphicsDevice, typeof(VertexPositionTexture), 36, BufferUsage.WriteOnly);
Error 88 ’Microsoft.Xna.Framework.Graphics.VertexPositionTexture’ does not contain a definition for ‘VertexElements’
XNA 3.1: hexVertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionTexture.VertexElements);
VertexDeclaration constructor no longer needs a GraphicsDevice.
Error 56 ’Microsoft.Xna.Framework.Graphics.GraphicsDevice’ does not contain a definition for ‘VertexDeclaration’ and no extension method ‘VertexDeclaration’ accepting a first argument of type ‘Microsoft.Xna.Framework.Graphics.GraphicsDevice’ could be found
XNA 3.1: graphics.GraphicsDevice.VertexDeclaration = hexTiles.hexVertexDeclaration;
No longer required at all as associated declaration will automatically be found by the associated device. We can now delete the above graphics.GraphicsDevice.VertexDeclarion.
Error 11 ’Microsoft.Xna.Framework.Graphics.VertexPositionTexture’ does not contain a definition for ‘VertexElements’
XNA 3.1: hexVertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionTexture.VertexElements);
The initial 165 compile ‘errors’ were soon reduced to zero by resolving the above.
Now all compiles well but still a few visual problems mainly with tile transparency. Previously this was controlled by GraphicsDevice.RenderState.AlphaBlendableEnable which is no more.