原地址:http://nelxon.com/resources/xna-3-1-to-xna-4-0-cheatsheet.php
Nelxon here again providing yet another resource for XNA Game developers. There are a few sites available to help us convert those XNA 3.1 projects to XNA 4.0, but it seems many of us are still have problems migrating successfully and easily. Its a pain to visit multiple sites and pages only to find corrections for 1 to 3 errors and still have hundreds more left.
After losing many hours of production time, I decided it would be more convenient to have a simple cheat sheet - (collection of code snippets XNA 3.1 to XNA 4.0) to help me convert projects faster with less difficulty. So far, this cheat sheet has helped me convert over a dozen XNA 3.1 projects, which removed at least 90% of the errors due to the code breaking changes from XNA 4.0. The remaining errors I could resolve myself or simply ask for help on the forums. 5 errors are better than 50…
I decided to share this cheat sheet because I am constantly reading forums where developers discuss having the same problems converting their projects. I figured maybe other developers would find this useful as well. These code snippets were created from comparing the XNA 3.1 creator’s club educational samples to the newer XNA 4.0 samples. However do not consider this page as a tutorial, it is a list of examples to help you see the difference between XNA 3.1 and XNA 4.0 syntax and provided to help you learn by example.
DON’T FORGET TO BACKUP YOUR WORK FIRST, BEFORE YOU TRY TO CONVERT IT.
The name ‘SpriteBlendMode‘ does not exist in the current context
The name ‘SaveStateMode‘ does not exist in the current context
‘Microsoft.Xna.Framework.Graphics.GraphicsDevice‘ does not contain a definition for ‘RenderState‘…
‘Microsoft.Xna.Framework.Graphics.Effect‘ does not contain a definition for ‘Begin‘ …
‘Microsoft.Xna.Framework.Graphics.Effect‘ does not contain a definition for ‘End‘..
‘Microsoft.Xna.Framework.Graphics.Effect‘ does not contain a definition for ‘CommitChanges‘ …
‘Microsoft.Xna.Framework.Graphics.EffectPass‘ does not contain a definition for ‘Begin‘ …
‘Microsoft.Xna.Framework.Graphics.EffectPass‘ does not contain a definition for ‘End‘ ….
No overload for method ‘Clone‘ takes 1 arguments
The name ‘ShaderProfile‘ does not exist in the current context
‘Microsoft.Xna.Framework.GameTime‘ does not contain a definition for ‘TotalRealTime‘ …
‘Microsoft.Xna.Framework.Color‘ does not contain a definition for ‘TransparentBlack‘ …
The type or namespace name ‘ResolveTexture2D‘ could not be found …
‘Microsoft.Xna.Framework.Graphics.GraphicsDevice‘ does not contain a definition for ‘ResolveBackBuffer‘…
The type or namespace name ‘DepthStencilBuffer‘ could not be found …
‘Microsoft.Xna.Framework.Graphics.RenderTarget2D‘ does not contain a constructor that takes 5 arguments …
‘Microsoft.Xna.Framework.Graphics.RenderTarget2D‘ does not contain a definition for ‘GetTexture‘ …
‘Microsoft.Xna.Framework.Graphics.PresentationParameters‘ does not contain a definition for ‘MultiSampleType‘ …
‘Microsoft.Xna.Framework.Graphics.PresentationParameters‘ does not contain a definition for ‘MultiSampleQuality‘ …
The best overloaded method match for ‘Microsoft.Xna.Framework.Graphics.GraphicsDevice.SetRenderTarget…
‘Microsoft.Xna.Framework.Graphics.GraphicsDevice‘ does not contain a definition for ‘VertexDeclaration‘
‘Microsoft.Xna.Framework.Graphics.GraphicsDevice‘ does not contain a definition for ‘Vertices‘
‘Microsoft.Xna.Framework.Graphics.VertexPositionTexture‘ does not contain a definition for ‘SizeInBytes‘
‘Microsoft.Xna.Framework.Graphics.VertexPositionTexture‘ does not contain a definition for ‘VertexElements‘
‘Microsoft.Xna.Framework.Graphics.ModelMesh‘ does not contain a definition for ‘IndexBuffer‘
‘Microsoft.Xna.Framework.Graphics.ModelMesh‘ does not contain a definition for ‘VertexBuffer‘
‘Microsoft.Xna.Framework.Graphics.ModelMeshPart‘ does not contain a definition for ‘BaseVertex‘
‘Microsoft.Xna.Framework.Graphics.ModelMeshPart‘ does not contain a definition for ‘StreamOffset‘
‘Microsoft.Xna.Framework.Graphics.ModelMeshPart‘ does not contain a definition for ‘VertexStride‘
‘Microsoft.Xna.Framework.Storage.StorageContainer‘ does not contain a definition for ‘TitleLocation‘
‘Microsoft.Xna.Framework.Storage.StorageContainer‘ does not contain a definition for ‘Path‘
‘Microsoft.Xna.Framework.Storage.StorageDevice‘ does not contain a definition for ‘OpenContainer‘
‘Microsoft.Xna.Framework.GamerServices.Guide‘ does not contain a definition for ‘BeginShowStorageDeviceSelector‘
‘Microsoft.Xna.Framework.GamerServices.Guide‘ does not contain a definition for ‘EndShowStorageDeviceSelector‘
syntax error: unexpected token ‘VertexShader‘
syntax error: unexpected token ‘PixelShader‘
error X3539: ps_1_x is no longer supported
1
2
|
// XNA 3.1
sprite.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
|
1
2
|
// XNA 4.0
sprite.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
|
1
2
3
|
// XNA 3.1
// Sort the object layer and player sprite according to depth
spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.None);
|
1
2
3
|
// XNA 4.0
// Sort the object layer and player sprite according to depth
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
|
1
2
|
// XNA 3.1
sprite.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.SaveState);
|
1
2
|
// XNA 4.0
sprite.Begin(SpriteSortMode.Immediate, BlendState.Opaque);
|
1
2
3
4
5
6
|
// XNA 3.1
// Draw the background image.
spriteBatch.Begin(SpriteBlendMode.None);
Viewport viewport = GraphicsDevice.Viewport;
spriteBatch.Draw(background,newRectangle(0, 0, viewport.Width, viewport.Height), Color.White);
spriteBatch.End();
|
1
2
3
4
5
|
// XNA 4.0
// Draw the background image.
spriteBatch.Begin(0, BlendState.Opaque);
spriteBatch.Draw(background, GraphicsDevice.Viewport.Bounds, Color.White);
spriteBatch.End();
|
1
2
3
4
5
6
7
8
9
10
|
// XNA 3.1
// enable alpha blending and disable depth write
GraphicsDevice.RenderState.AlphaBlendEnable =true;
GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
GraphicsDevice.RenderState.SeparateAlphaBlendEnabled =true;
GraphicsDevice.RenderState.AlphaBlendOperation = BlendFunction.Add;
GraphicsDevice.RenderState.AlphaSourceBlend = Blend.One;
GraphicsDevice.RenderState.AlphaDestinationBlend = Blend.One;
GraphicsDevice.RenderState.DepthBufferWriteEnable =false;
|
1
2
3
4
|
// XNA 4.0
// enable alpha blending and disable depth write
GraphicsDevice.BlendState = BlendState.AlphaBlend;
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
|
1
2
3
4
5
|
// XNA 3.1
// reset blend and depth write
GraphicsDevice.RenderState.AlphaBlendEnable =false;
GraphicsDevice.RenderState.SeparateAlphaBlendEnabled =false;
GraphicsDevice.RenderState.DepthBufferWriteEnable =true;
|
1
2
3
4
|
// XNA 4.0
// reset blend and depth write
GraphicsDevice.BlendState = BlendState.Additive;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
1
2
3
|
// XNA 3.1
// enable depth buffering
GraphicsDevice.RenderState.DepthBufferEnable =true;
|
1
2
3
|
// XNA 4.0
// enable depth buffering
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
1
2
3
4
|
// XNA 3.1
//disable depth buffering
GraphicsDevice.RenderState.DepthBufferWriteEnable =false;
GraphicsDevice.RenderState.DepthBufferEnable =false;
|
1
2
3
|
// XNA 4.0
//disable depth buffering
GraphicsDevice.DepthStencilState = DepthStencilState.None;
|
1
2
3
4
5
6
|
// XNA 3.1
// set additive blend (zero on alpha)
GraphicsDevice.RenderState.DepthBufferWriteEnable =false;
GraphicsDevice.RenderState.AlphaBlendEnable =true;
GraphicsDevice.RenderState.SourceBlend = Blend.One;
GraphicsDevice.RenderState.DestinationBlend = Blend.One;
|
1
2
3
4
|
// XNA 4.0
// set additive blend (zero on alpha)
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
GraphicsDevice.BlendState = BlendState.AlphaBlend;
|
1
2
3
4
5
|
// XNA 3.1
// restore blend modes
GraphicsDevice.RenderState.DepthBufferWriteEnable =true;
GraphicsDevice.RenderState.AlphaBlendEnable =false;
GraphicsDevice.RenderState.SeparateAlphaBlendEnabled =false;
|
1
2
3
4
|
// XNA 4.0
// restore blend modes
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
1
2
3
4
5
6
7
|
// XNA 3.1
// set alpha blend and no depth test or write
GraphicsDevice.RenderState.DepthBufferEnable =false;
GraphicsDevice.RenderState.DepthBufferWriteEnable =false;
GraphicsDevice.RenderState.AlphaBlendEnable =true;
GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
|
1
2
3
4
|
// XNA 4.0
// set alpha blend and no depth test or write
GraphicsDevice.DepthStencilState = DepthStencilState.None;
GraphicsDevice.BlendState = BlendState.AlphaBlend;
|
1
2
3
4
5
|
// XNA 3.1
// Set suitable renderstates for drawing a 3D model
GraphicsDevice.RenderState.AlphaBlendEnable =false;
GraphicsDevice.RenderState.AlphaTestEnable =false;
GraphicsDevice.RenderState.DepthBufferEnable =true;
|
1
2
3
4
|
// XNA 4.0
// Set suitable renderstates for drawing a 3D model
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
1
2
3
4
5
|
// XNA 3.1
// additive blend
GraphicsDevice.RenderState.AlphaBlendEnable =true;
GraphicsDevice.RenderState.SourceBlend = Blend.One;
GraphicsDevice.RenderState.DestinationBlend = Blend.One;
|
1
2
3
|
// XNA 4.0
// additive blend
GraphicsDevice.BlendState = BlendState.Additive;
|
1
2
3
|
// XNA 3.1
// set additive blend
GraphicsDevice.RenderState.DestinationBlend = Blend.One;
|
1
2
3
|
// XNA 4.0
// set additive blend
GraphicsDevice.BlendState = BlendState.Additive;
|
1
2
3
|
// XNA 3.1
// set alpha blend
GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
|
1
2
3
|
// XNA 4.0
// set alpha blend
GraphicsDevice.BlendState = BlendState.AlphaBlend;
|
1
2
|
// XNA 3.1
GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
|
1
2
|
// XNA 4.0
GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// XNA 3.1
// set render states
GraphicsDevice.RenderState.DepthBufferEnable =false;
GraphicsDevice.RenderState.DepthBufferWriteEnable =false;
GraphicsDevice.RenderState.AlphaBlendEnable =true;
GraphicsDevice.RenderState.SourceBlend = Blend.One;
GraphicsDevice.RenderState.DestinationBlend = Blend.One;
GraphicsDevice.RenderState.SeparateAlphaBlendEnabled =true;
GraphicsDevice.RenderState.AlphaBlendOperation = BlendFunction.Add;
GraphicsDevice.RenderState.AlphaDestinationBlend = Blend.Zero;
GraphicsDevice.RenderState.AlphaSourceBlend = Blend.Zero;
// drawing code here..
// restore render states
GraphicsDevice.RenderState.DepthBufferEnable =true;
GraphicsDevice.RenderState.DepthBufferWriteEnable =true;
GraphicsDevice.RenderState.AlphaBlendEnable =false;
GraphicsDevice.RenderState.SeparateAlphaBlendEnabled =false;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// XNA 4.0
//save the states
DepthStencilState ds = GraphicsDevice.DepthStencilState;
BlendState bs = GraphicsDevice.BlendState;
//set render states
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
GraphicsDevice.BlendState = BlendState.AlphaBlend;
// drawing code here..
// restore render states
GraphicsDevice.DepthStencilState = ds;
GraphicsDevice.BlendState = bs;
|
1
2
3
4
5
6
7
|
// apply effect XNA 3.1
blurEffect.CommitChanges();
blurEffect.Begin(SaveStateMode.SaveState);
blurEffect.CurrentTechnique.Passes[0].Begin();
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
blurEffect.CurrentTechnique.Passes[0].End();
blurEffect.End();
|
1
2
3
|
// apply effect XNA 4.0
blurEffect.CurrentTechnique.Passes[0].Apply();
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
|
1
2
3
|
// XNA 3.1
// commit effect changes
effect.CommitChanges();
|
1
2
3
|
// XNA 4.0
// you will need to call EffectPass.Apply() if any of the effect properties change
// Otherwise Delete it.
|
1
2
3
4
|
// XNA 3.1
// begin effect
effect.Begin(SaveStateMode.SaveState);
effect.CurrentTechnique.Passes[0].Begin();
|
1
2
3
|
// XNA 4.0
// begin effect
effect.CurrentTechnique.Passes[0].Apply();
|
1
2
3
4
|
// XNA 3.1
// end effect
effect.CurrentTechnique.Passes[0].End();
effect.End();
|
1
2
|
// XNA 4.0
// Delete it. No longer needed.
|
1
2
3
|
// XNA 3.1
// Make a clone of our replacement effect
Effect newEffect = replacementEffect.Clone(replacementEffect.GraphicsDevice);
|
1
2
3
|
// XNA 4.0
// Make a clone of our replacement effect. 3.1
Effect newEffect = replacementEffect.Clone();
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
// XNA 3.1
// Activate the appropriate effect technique
postprocessEffect.CurrentTechnique = postprocessEffect.Techniques[effectTechniqueName];
// Draw a fullscreen sprite to apply the postprocessing effect.
spriteBatch.Begin(SpriteBlendMode.None,SpriteSortMode.Immediate, SaveStateMode.None);
postprocessEffect.Begin();
postprocessEffect.CurrentTechnique.Passes[0].Begin();
spriteBatch.Draw(sceneRenderTarget.GetTexture(), Vector2.Zero, Color.White);
spriteBatch.End();
postprocessEffect.CurrentTechnique.Passes[0].End();
postprocessEffect.End();
|
1
2
3
4
5
6
7
8
|
// XNA 4.0
// Activate the appropriate effect technique
postprocessEffect.CurrentTechnique = postprocessEffect.Techniques[effectTechniqueName];
// Draw a fullscreen sprite to apply the postprocessing effect.
spriteBatch.Begin(0, BlendState.Opaque,null,null,null, postprocessEffect);
spriteBatch.Draw(sceneRenderTarget, Vector2.Zero, Color.White);
spriteBatch.End();
|
1
2
3
|
// XNA 3.1
graphics.MinimumPixelShaderProfile = ShaderProfile.PS_3_0;//any PS number...
graphics.MinimumVertexShaderProfile = ShaderProfile.VS_3_0;//any VS number...
|
1
2
|
// XNA 4.0
// Delete it. No longer needed.
|
1
2
|
// XNA 3.1
floatmyTime = (float)gameTime.TotalRealTime.TotalSeconds * 0.2f;
|
1
2
|
// XNA 4.0
floatmyTime = (float)gameTime.TotalGameTime.TotalSeconds * 0.2f;
|
1
2
|
// XNA 3.1
GraphicsDevice.Clear(Color.TransparentBlack);
|
1
2
|
// XNA 4.0
GraphicsDevice.Clear(Color.Transparent);
|
1
2
|
// XNA 3.1
ResolveTexture2D sceneMap;
|
1
2
|
// XNA 4.0
RenderTarget2D sceneMap;
|
1
2
3
4
5
6
|
// XNA 3.1
// Look up the resolution and format of our main backbuffer.
PresentationParameters pp = GraphicsDevice.PresentationParameters;
// Create a texture for reading back the backbuffer contents.
sceneMap =newResolveTexture2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, 1, pp.BackBufferFormat);
|
1
2
3
4
5
6
7
8
9
10
11
|
// XNA 4.0
// Look up the resolution and format of our main backbuffer.
PresentationParameters pp = GraphicsDevice.PresentationParameters;
// Create a texture for reading back the backbuffer contents.
sceneMap =newRenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight,false, pp.BackBufferFormat,
pp.DepthStencilFormat);
//or
sceneMap =newRenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight,false, pp.BackBufferFormat,
pp.DepthStencilFormat, pp.MultiSampleCount, RenderTargetUsage.DiscardContents);
|
1
2
|
// XNA 3.1
GraphicsDevice.ResolveBackBuffer(sceneMap);
|
1
2
|
// XNA 4.0
GraphicsDevice.SetRenderTarget(sceneMap);
|
1
2
3
4
5
6
|
// XNA 3.1
intwidth = GraphicsDevice.Viewport.Width;
intheight = GraphicsDevice.Viewport.Height;
// create render target
myRenderTarget =newRenderTarget2D(GraphicsDevice, width, height, 1, SurfaceFormat.Color);
|
1
2
3
4
5
6
|
// XNA 4.0
intwidth = GraphicsDevice.Viewport.Width;
intheight = GraphicsDevice.Viewport.Height;
// create render target
myRenderTarget =newRenderTarget2D(GraphicsDevice, width, height,true, SurfaceFormat.Color, DepthFormat.Depth24);
|
1
2
3
4
5
6
|
// XNA 3.1
PresentationParameters pp = GraphicsDevice.PresentationParameters;
// Create custom rendertarget
sceneRenderTarget =newRenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, 1,
pp.BackBufferFormat, pp.MultiSampleType, pp.MultiSampleQuality);
|
1
2
3
4
5
6
7
|
// XNA 4.0
PresentationParameters pp = GraphicsDevice.PresentationParameters;
// Create custom rendertarget
sceneRenderTarget =newRenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight,false,
pp.BackBufferFormat, pp.DepthStencilFormat, pp.MultiSampleCount,
RenderTargetUsage.DiscardContents);
|
1
2
3
4
5
6
7
8
9
|
// XNA 3.1/div>
PresentationParameters pp = GraphicsDevice.PresentationParameters;
// Setup a DepthBuffer
drawBuffer =newRenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, 1,
SurfaceFormat.Color, pp.MultiSampleType, pp.MultiSampleQuality);
drawDepthBuffer =newDepthStencilBuffer(GraphicsDevice, pp.AutoDepthStencilFormat,
pp.MultiSampleType, pp.MultiSampleQuality);
|
1
2
3
4
5
6
7
8
|
// XNA 4.0
PresentationParameters pp = GraphicsDevice.PresentationParameters;
// Setup a DepthBuffer
drawBuffer =newRenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight,true,
SurfaceFormat.Color,DepthFormat.Depth24Stencil8,
pp.MultiSampleCount, RenderTargetUsage.DiscardContents);
//NOTE: DepthStencilBuffer class no longer exists
|
1
2
|
// XNA 3.1
spriteBatch.Draw(myRenderTarget.GetTexture(), Vector2.Zero, Color.White);
|
1
2
|
// XNA 4.0
spriteBatch.Draw(myRenderTarget, Vector2.Zero, Color.White);// NOTE: ".GetTexure()" No longer needed
|
1
2
|
// XNA 3.1
Texture2D myTexture = myRenderTarget.GetTexture();
|
1
2
|
// XNA 4.0
Texture2D myTexture = myRenderTarget;// NOTE: ".GetTexure()" No longer needed
|
1
2
|
// XNA 3.1
GraphicsDevice.SetRenderTarget(0, myRenderTarget);
|
1
2
|
// XNA 4.0
GraphicsDevice.SetRenderTarget(myRenderTarget);
|
1
2
3
4
|
// XNA 3.1
// Set the two render targets
GraphicsDevice.SetRenderTarget(0, colorRT);
GraphicsDevice.SetRenderTarget(1, depthRT);
|
1
2
3
|
// XNA 4.0
// Set the two render targets
GraphicsDevice.SetRenderTargets(colorRT, depthRT);
|
1
2
|
// XNA 3.1
GraphicsDevice.SetRenderTarget(0,null);
|
1
2
|
// XNA 4.0
GraphicsDevice.SetRenderTarget(null);
|
1
2
3
4
5
6
7
8
9
|
// XNA 3.1
// resolve the backbuffer as the depth map
GraphicsDevice.ResolveBackBuffer(depthMap);
// draw the scene image again, blur it with the depth map
GraphicsDevice.Textures[1] = depthMap;
Viewport viewport = GraphicsDevice.Viewport;
dofEffect.CurrentTechnique = depthBlurTechnique;
DrawFullscreenQuad(sceneMap, viewport.Width, viewport.Height, dofEffect);
|
1
2
3
4
5
6
7
8
9
10
|
// XNA 4.0
// resolve the backbuffer as the depth map
GraphicsDevice.SetRenderTarget(null);
// draw the scene image again, blur it with the depth map
GraphicsDevice.Textures[1] = depthMap;
GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
Viewport viewport = GraphicsDevice.Viewport;
dofEffect.CurrentTechnique = depthBlurTechnique;
DrawFullscreenQuad(sceneMap, viewport.Width, viewport.Height, dofEffect);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
// XNA 3.1
ResolveTexture2D resolveTarget;
RenderTarget2D renderTarget1;
RenderTarget2D renderTarget2;
// Look up the resolution and format of our main backbuffer.
PresentationParameters pp = GraphicsDevice.PresentationParameters;
intwidth = pp.BackBufferWidth;
intheight = pp.BackBufferHeight;
SurfaceFormat format = pp.BackBufferFormat;
// Create a texture for reading back the backbuffer contents.
resolveTarget =newResolveTexture2D(GraphicsDevice, width, height, 1, format);
// Create two rendertargets half size for the bloom processing.
width /= 2;
height /= 2;
renderTarget1 =newRenderTarget2D(GraphicsDevice, width, height, 1,format);
renderTarget2 =newRenderTarget2D(GraphicsDevice, width, height, 1,format);
// ... In the Draw Method...
GraphicsDevice.ResolveBackBuffer(resolveTarget);
// ...apply effect and draw pass 1...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
// XNA 4.0
RenderTarget2D sceneRenderTarget;
RenderTarget2D renderTarget1;
RenderTarget2D renderTarget2;
// Look up the resolution and format of our main backbuffer.
PresentationParameters pp = GraphicsDevice.PresentationParameters;
intwidth = pp.BackBufferWidth;
intheight = pp.BackBufferHeight;
SurfaceFormat format = pp.BackBufferFormat;
// Create a texture for rendering the main scene, prior to applying bloom.
sceneRenderTarget =newRenderTarget2D(GraphicsDevice, width, height,false,
format, pp.DepthStencilFormat, pp.MultiSampleCount,
RenderTargetUsage.DiscardContents);
// Create two rendertargets half size for the bloom processing.
width /= 2;
height /= 2;
renderTarget1 =newRenderTarget2D(GraphicsDevice, width, height,false, format, DepthFormat.None);
renderTarget2 =newRenderTarget2D(GraphicsDevice, width, height,false, format, DepthFormat.None);
// ...In the Draw Method...
GraphicsDevice.SetRenderTarget(sceneRenderTarget);
GraphicsDevice.SamplerStates[1] = SamplerState.LinearClamp;
// ...apply effect and draw pass 1....
|
1
2
3
|
// XNA 3.1
// Vertex declaration for rendering our 3D model.
GraphicsDevice.VertexDeclaration =newVertexDeclaration(VertexPositionTexture.VertexElements);
|
1
2
|
// XNA 4.0
// Delete it. No longer needed.
|
1
2
3
4
|
// XNA 3.1
// set vertex buffer and declaration
GraphicsDevice.VertexDeclaration = vertexDeclaration;
GraphicsDevice.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionTexture.SizeInBytes);
|
1
2
3
|
// XNA 4.0
// set vertex buffer and declaration
GraphicsDevice.SetVertexBuffer(vertexBuffer);
|
1
2
3
|
// XNA 3.1
// create vertex declaration
vertexDeclaration =newVertexDeclaration(GraphicsDevice, VertexPositionTexture.VertexElements);
|
1
2
3
|
// XNA 4.0
// create vertex declaration
vertexDeclaration =newVertexDeclaration(VertexPositionTexture.VertexDeclaration.GetVertexElements());
|
1
2
3
4
|
// XNA 3.1
// reset vertex buffer declaration
GraphicsDevice.VertexDeclaration =null;
GraphicsDevice.Vertices[0].SetSource(null, 0, 0);
|
1
2
3
|
// XNA 4.0
// reset vertex buffer declaration
GraphicsDevice.SetVertexBuffer(null);
|
1
2
3
4
5
6
7
|
// XNA 3.1
// the vertices array
VertexPositionNormalTexture[] vertices =newVertexPositionNormalTexture[100];
// set new particles to vertex buffer
vertexBuffer.SetData<VertexPositionNormalTexture>(VertexPositionNormalTexture.SizeInBytes * vertexCount,
vertices,vertexCount,count,VertexPositionNormalTexture.SizeInBytes);
|
1
2
3
4
5
6
|
// XNA 4.0
// the vertices array
VertexPositionNormalTexture[] vertices =newVertexPositionNormalTexture[100];
// set new particles to vertex buffer
vertexBuffer.SetData<VertexPositionNormalTexture>(vertices);
|
1
2
3
4
5
6
7
8
9
10
11
12
|
// XNA 3.1
// for each mesh part
foreach(ModelMeshPart meshPartinmesh.MeshParts)
{
// if primitives to render
if(meshPart.PrimitiveCount > 0)
{
// setup vertices and indices
GraphicsDevice.VertexDeclaration = meshPart.VertexDeclaration;
GraphicsDevice.Vertices[0].SetSource(mesh.VertexBuffer, meshPart.StreamOffset, meshPart.VertexStride);
GraphicsDevice.Indices = mesh.IndexBuffer;
...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// XNA 4.0
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;// may be needed in some cases...
// for each mesh part
foreach(ModelMeshPart meshPartinmesh.MeshParts)
{
// if primitives to render
if(meshPart.PrimitiveCount > 0)
{
// setup vertices and indices
GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer);
GraphicsDevice.Indices = meshPart.IndexBuffer;
...
|
1
2
3
4
5
|
// XNA 3.1
// draw primitives
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,
meshPart.BaseVertex, 0, meshPart.NumVertices,
meshPart.StartIndex, meshPart.PrimitiveCount);
|
1
2
3
4
|
// XNA 4.0
// draw primitives
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0,
meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount);
|
1
2
3
4
|
// XNA 3.1
// create the vertex buffer
vertexBuffer =newVertexBuffer(GraphicsDevice,typeof(VertexPositionNormalTexture),
250, BufferUsage.WriteOnly | BufferUsage.Points);
|
1
2
3
4
|
// XNA 4.0
// create the vertex buffer
vertexBuffer =newVertexBuffer(GraphicsDevice,typeof(VertexPositionNormalTexture),
250, BufferUsage.WriteOnly | BufferUsage.None);
|
1
2
3
4
|
// XNA 3.1
// enable point sprite 3.1
GraphicsDevice.RenderState.PointSpriteEnable =true;
GraphicsDevice.RenderState.PointSizeMax = 128;
|
1
2
|
// XNA 4.0
// Delete it. No longer available.
|
1
2
3
|
// XNA 3.1
// draw the point sprites
GraphicsDevice.DrawPrimitives(PrimitiveType.PointList, vertexPosition, numberVertices);
|
1
2
3
|
// XNA 4.0
// draw the point sprites
GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, vertexPosition, numberVertices);
|
1
2
3
|
// XNA 3.1
// open the container
StorageContainer storageContainer = storageDevice.OpenContainer("YourGameName");
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
// XNA 4.0
//To make life easier simply create a method to replace the storageDevice.OpenContainer...
/// <summary>
/// Synchronously opens storage container
/// </summary>
privatestaticStorageContainer OpenContainer(StorageDevice storageDevice,stringsaveGameName)
{
IAsyncResult result = storageDevice.BeginOpenContainer(saveGameName,null,null);
// Wait for the WaitHandle to become signaled.
result.AsyncWaitHandle.WaitOne();
StorageContainer container = storageDevice.EndOpenContainer(result);
// Close the wait handle.
result.AsyncWaitHandle.Close();
returncontainer;
}
// open the container
StorageContainer storageContainer = OpenContainer(storageDevice,"YourGameName");
|
1
2
3
|
// XNA 3.1
// retrieve the storage device
Guide.BeginShowStorageDeviceSelector(GetStorageDeviceResult, retrievalDelegate);
|
1
2
3
4
5
6
|
// XNA 4.0
// retrieve the storage device
if(!Guide.IsVisible)
{
StorageDevice.BeginShowSelector(GetStorageDeviceResult, retrievalDelegate);
}
|
1
2
3
|
// XNA 3.1
// retrieve and store the storage device
storageDevice = Guide.EndShowStorageDeviceSelector(result);
|
1
2
3
|
// XNA 4.0
// retrieve and store the storage device
storageDevice = StorageDevice.EndShowSelector(result);
|
1
2
3
|
// XNA 3.1
// get the level setup files
string[] filenames = Directory.GetFiles(storageContainer.Path,"LevelSetup*.xml");
|
1
2
3
|
// XNA 4.0
// get the level setup files
string[] filenames = storageContainer.GetFileNames("LevelSetup*.xml"");
|
1
2
3
4
5
6
|
// XNA 3.1
// save game level data
using(FileStream stream =newFileStream(Path.Combine(storageContainer.Path, levelFilename), FileMode.Create))
{
newXmlSerializer(typeof(SaveGameLevel)).Serialize(stream, levelData);
}
|
1
2
3
4
5
6
|
// XNA 4.0
// save game level data
using(Stream stream = storageContainer.OpenFile(levelFilename, FileMode.Create))
{
newXmlSerializer(typeof(SaveGameLevel)).Serialize(stream, levelData);
}
|
1
2
3
4
5
6
7
8
9
|
// XNA 3.1
// Delete the saved game level data.
using(StorageContainer storageContainer = storageDevice.OpenContainer("saveGameName"))
{
File.Delete(Path.Combine(storageContainer.Path, saveGameLevel.FileName));
File.Delete(Path.Combine(storageContainer.Path,"SaveGameLevel"+
Path.GetFileNameWithoutExtension(saveGameLevel.FileName).Substring(8) +".xml"));
}
|
1
2
3
4
5
6
7
8
9
|
// XNA 4.0
// Delete the saved game level data. NOTE: using OpenContainer method created in previous example
using(StorageContainer storageContainer = OpenContainer(storageDevice,"saveGameName"))
{
storageContainer.DeleteFile(saveGameLevel.FileName);
storageContainer.DeleteFile("SaveGameLevel"+
Path.GetFileNameWithoutExtension(saveGameLevel.FileName).Substring(8) +".xml");
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
// XNA 3.1
//Load the Next Level...
// Find the path of the next level.
stringlevelPath;
// Loop here so we can try again when we can't find a level.
while(true)
{
// Try to find the next level. They are sequentially numbered txt files.
levelPath = String.Format("Levels/{0}.txt", ++levelIndex);
levelPath = Path.Combine(StorageContainer.TitleLocation,"Content/"+ levelPath);
if(File.Exists(levelPath))
break;
// If there isn't even a level 0, something has gone wrong.
if(levelIndex == 0)
thrownewException("No levels found.");
// Whenever we can't find a level, start over again at 0.
levelIndex = -1;
}
// Unloads the content for the current level before loading the next one.
if(level !=null)
level.Dispose();
// Load the level.
level =newLevel(Services, levelPath);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// XNA 4.0
// Load the Next Level...
// move to the next level
levelIndex = (levelIndex + 1) % numberOfLevels;
// Unloads the content for the current level before loading the next one.
if(level !=null)
level.Dispose();
// Load the level.
stringlevelPath =string.Format("Content/Levels/{0}.txt", levelIndex);
using(Stream fileStream = TitleContainer.OpenStream(levelPath))
level =newLevel(Services, fileStream, levelIndex);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
// XNA 3.1
//Save the current state of the session, with the given storage device.
// check the parameter
if((storageDevice ==null) || !storageDevice.IsConnected)
{
return;
}
// open the container
using(StorageContainer storageContainer = storageDevice.OpenContainer(Session.SaveGameContainerName))
{
stringfilename;
stringdescriptionFilename;
// get the filenames
if(overwriteDescription ==null)
{
intsaveGameIndex = 0;
stringtestFilename;
do
{
saveGameIndex++;
testFilename = Path.Combine(storageContainer.Path,"SaveGame"+ saveGameIndex.ToString() +".xml");
}
while(File.Exists(testFilename));
filename = testFilename;
descriptionFilename ="SaveGameDescription"+ saveGameIndex.ToString() +".xml";
}
else
{
filename = Path.Combine(storageContainer.Path, overwriteDescription.FileName);
descriptionFilename ="SaveGameDescription"+
Path.GetFileNameWithoutExtension(overwriteDescription.FileName).Substring(8) +".xml";
}
using(FileStream stream =newFileStream(filename, FileMode.Create))
{
using(XmlWriter xmlWriter = XmlWriter.Create(stream))
{
//create and write xml data...
}
}
// create the save game description
SaveGameDescription description =newSaveGameDescription();
description.FileName = Path.GetFileName(filename);
description.ChapterName = IsQuestLineComplete ?"Quest Line Complete": Quest.Name;
description.Description = DateTime.Now.ToString();
using(FileStream stream =newFileStream(Path.Combine(storageContainer.Path, descriptionFilename), FileMode.Create))
{
newXmlSerializer(typeof(SaveGameDescription)).Serialize(stream, description);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
// XNA 4.0
//Save the current state of the session, with the given storage device.
// check the parameter
if((storageDevice ==null) || !storageDevice.IsConnected)
{
return;
}
// open the container Note: using OpenContainer method from previous examples
using(StorageContainer storageContainer = OpenContainer(storageDevice, Session.SaveGameContainerName))
{
stringfilename;
stringdescriptionFilename;
// get the filenames
if(overwriteDescription ==null)
{
intsaveGameIndex = 0;
stringtestFilename;
do
{
saveGameIndex++;
testFilename ="SaveGame"+ saveGameIndex.ToString() +".xml";
}
while(storageContainer.FileExists(testFilename));
filename = testFilename;
descriptionFilename ="SaveGameDescription"+ saveGameIndex.ToString() +".xml";
}
else
{
filename = overwriteDescription.FileName;
descriptionFilename ="SaveGameDescription"+
Path.GetFileNameWithoutExtension(overwriteDescription.FileName).Substring(8) +".xml";
}
// Note: using Stream instead of FileStream...
using(Stream stream = storageContainer.OpenFile(filename, FileMode.Create))
{
using(XmlWriter xmlWriter = XmlWriter.Create(stream))
{
//create and write xml data...
}
}
// create the save game description
SaveGameDescription description =newSaveGameDescription();
description.FileName = Path.GetFileName(filename);
description.ChapterName = IsQuestLineComplete ?"Quest Line Complete": Quest.Name;
description.Description = DateTime.Now.ToString();
// Note: using Stream instead of FileStream...
using(Stream stream = storageContainer.OpenFile(descriptionFilename, FileMode.Create))
{
newXmlSerializer(typeof(SaveGameDescription)).Serialize(stream, description);
}
}
|
1
2
3
4
5
6
7
8
9
|
// XNA 3.1
VertexShaderOutput VertexShader(...)
{
//some code
}
float4 PixelShader(...)
{
// some code
}
|
1
2
3
4
5
6
7
8
9
10
11
|
// XNA 4.0
// VertexShader can not be used
VertexShaderOutput VertexShaderFunction(...)
{
// some code
}
// PixelShader can not be used
float4 PixelShaderFunction(...)
{
// some code
}
|
1
2
3
4
5
6
7
8
9
|
// XNA 3.1
technique
{
pass
{
VertexShader = compile vs_1_1 VertexShader();
PixelShader = compile ps_1_1 PixelShader();
}
}
|
1
2
3
4
5
6
7
8
9
|
// XNA 4.0
technique
{
pass
{
VertexShader = compile vs_2_0 VertexShaderFunction();//VertexShader can not be used & set vs higher than 1_1
PixelShader = compile ps_2_0 PixelShaderFunction();//PixelShader can not be used & set ps higher than 1_1
}
}
|
1
2
3
4
5
|
// Set suitable renderstates for drawing a 3D model
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
// your model draw code starts here...
|