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.
Bookmark this page, because it may come in handy if you find a good library written in XNA 3.1.
If a developer is having problems converting their projects, please send them to this page, it may help.
List of errors resolved using this cheat sheet:
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
Other Issues:
XNA model is drawn inside out, looks transparent, missing vertices or just doesn’t look right…
List of Examples Converting XNA 3.1 to XNA 4.0
SpriteBlendMode, SaveStateMode
1
2
sprite.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Deferred, SaveStateMode.SaveState);
|
1
2
sprite.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
|
1
2
3
spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront, SaveStateMode.None);
|
1
2
3
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
|
1
2
sprite.Begin(SpriteBlendMode.None, SpriteSortMode.Immediate, SaveStateMode.SaveState);
|
1
2
sprite.Begin(SpriteSortMode.Immediate, BlendState.Opaque);
|
1
2
3
4
5
6
spriteBatch.Begin(SpriteBlendMode.None);
Viewport viewport = GraphicsDevice.Viewport;
spriteBatch.Draw(background, new Rectangle(0, 0, viewport.Width, viewport.Height), Color.White);
spriteBatch.End();
|
1
2
3
4
5
spriteBatch.Begin(0, BlendState.Opaque);
spriteBatch.Draw(background, GraphicsDevice.Viewport.Bounds, Color.White);
spriteBatch.End();
|
RenderState
1
2
3
4
5
6
7
8
9
10
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
GraphicsDevice.BlendState = BlendState.AlphaBlend;
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
|
1
2
3
4
5
GraphicsDevice.RenderState.AlphaBlendEnable = false;
GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = false;
GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
|
1
2
3
4
GraphicsDevice.BlendState = BlendState.Additive;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
1
2
3
GraphicsDevice.RenderState.DepthBufferEnable = true;
|
1
2
3
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
1
2
3
4
GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
GraphicsDevice.RenderState.DepthBufferEnable = false;
|
1
2
3
GraphicsDevice.DepthStencilState = DepthStencilState.None;
|
1
2
3
4
5
6
GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
GraphicsDevice.RenderState.AlphaBlendEnable = true;
GraphicsDevice.RenderState.SourceBlend = Blend.One;
GraphicsDevice.RenderState.DestinationBlend = Blend.One;
|
1
2
3
4
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
GraphicsDevice.BlendState = BlendState.AlphaBlend;
|
1
2
3
4
5
GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
GraphicsDevice.RenderState.AlphaBlendEnable = false;
GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = false;
|
1
2
3
4
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
1
2
3
4
5
6
7
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
GraphicsDevice.DepthStencilState = DepthStencilState.None;
GraphicsDevice.BlendState = BlendState.AlphaBlend;
|
1
2
3
4
5
GraphicsDevice.RenderState.AlphaBlendEnable = false;
GraphicsDevice.RenderState.AlphaTestEnable = false;
GraphicsDevice.RenderState.DepthBufferEnable = true;
|
1
2
3
4
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
1
2
3
4
5
GraphicsDevice.RenderState.AlphaBlendEnable = true;
GraphicsDevice.RenderState.SourceBlend = Blend.One;
GraphicsDevice.RenderState.DestinationBlend = Blend.One;
|
1
2
3
GraphicsDevice.BlendState = BlendState.Additive;
|
1
2
3
GraphicsDevice.RenderState.DestinationBlend = Blend.One;
|
1
2
3
GraphicsDevice.BlendState = BlendState.Additive;
|
1
2
3
GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
|
1
2
3
GraphicsDevice.BlendState = BlendState.AlphaBlend;
|
1
2
GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
|
1
2
GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
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
DepthStencilState ds = GraphicsDevice.DepthStencilState;
BlendState bs = GraphicsDevice.BlendState;
GraphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
GraphicsDevice.BlendState = BlendState.AlphaBlend;
GraphicsDevice.DepthStencilState = ds;
GraphicsDevice.BlendState = bs;
|
Effect and EffectPass, Begin(), End(), CommitChanges(), Clone()
1
2
3
4
5
6
7
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
blurEffect.CurrentTechnique.Passes[0].Apply();
GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 2);
|
1
2
3
4
effect.Begin(SaveStateMode.SaveState);
effect.CurrentTechnique.Passes[0].Begin();
|
1
2
3
effect.CurrentTechnique.Passes[0].Apply();
|
1
2
3
4
effect.CurrentTechnique.Passes[0].End();
effect.End();
|
1
2
3
Effect newEffect = replacementEffect.Clone(replacementEffect.GraphicsDevice);
|
1
2
3
Effect newEffect = replacementEffect.Clone();
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
postprocessEffect.CurrentTechnique = postprocessEffect.Techniques[effectTechniqueName];
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
postprocessEffect.CurrentTechnique = postprocessEffect.Techniques[effectTechniqueName];
spriteBatch.Begin(0, BlendState.Opaque, null, null, null, postprocessEffect);
spriteBatch.Draw(sceneRenderTarget, Vector2.Zero, Color.White);
spriteBatch.End();
|
ShaderProfile, TotalRealTime, TransparentBlack
1
2
3
graphics.MinimumPixelShaderProfile = ShaderProfile.PS_3_0;
graphics.MinimumVertexShaderProfile = ShaderProfile.VS_3_0;
|
1
2
float myTime = (float)gameTime.TotalRealTime.TotalSeconds * 0.2f;
|
1
2
float myTime = (float)gameTime.TotalGameTime.TotalSeconds * 0.2f;
|
1
2
GraphicsDevice.Clear(Color.TransparentBlack);
|
1
2
GraphicsDevice.Clear(Color.Transparent);
|
ResolveTexture2D, ResolveBackBuffer, RenderTarget2D, GetTexture, DepthStencilBuffer, PresentationParameters, MultiSampleType, MultiSampleQuality, SetRenderTarget
1
2
ResolveTexture2D sceneMap;
|
1
2
3
4
5
6
PresentationParameters pp = GraphicsDevice.PresentationParameters;
sceneMap = new ResolveTexture2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, 1, pp.BackBufferFormat);
|
1
2
3
4
5
6
7
8
9
10
11
PresentationParameters pp = GraphicsDevice.PresentationParameters;
sceneMap = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, false, pp.BackBufferFormat,
pp.DepthStencilFormat);
sceneMap = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, false, pp.BackBufferFormat,
pp.DepthStencilFormat, pp.MultiSampleCount, RenderTargetUsage.DiscardContents);
|
1
2
GraphicsDevice.ResolveBackBuffer(sceneMap);
|
1
2
GraphicsDevice.SetRenderTarget(sceneMap);
|
1
2
3
4
5
6
int width = GraphicsDevice.Viewport.Width;
int height = GraphicsDevice.Viewport.Height;
myRenderTarget = new RenderTarget2D(GraphicsDevice, width, height, 1, SurfaceFormat.Color);
|
1
2
3
4
5
6
int width = GraphicsDevice.Viewport.Width;
int height = GraphicsDevice.Viewport.Height;
myRenderTarget = new RenderTarget2D(GraphicsDevice, width, height, true, SurfaceFormat.Color, DepthFormat.Depth24);
|
1
2
3
4
5
6
PresentationParameters pp = GraphicsDevice.PresentationParameters;
sceneRenderTarget = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, 1,
pp.BackBufferFormat, pp.MultiSampleType, pp.MultiSampleQuality);
|
1
2
3
4
5
6
7
PresentationParameters pp = GraphicsDevice.PresentationParameters;
sceneRenderTarget = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, false,
pp.BackBufferFormat, pp.DepthStencilFormat, pp.MultiSampleCount,
RenderTargetUsage.DiscardContents);
|
1
2
3
4
5
6
7
8
9
/div>
PresentationParameters pp = GraphicsDevice.PresentationParameters;
drawBuffer = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, 1,
SurfaceFormat.Color, pp.MultiSampleType, pp.MultiSampleQuality);
drawDepthBuffer = new DepthStencilBuffer(GraphicsDevice, pp.AutoDepthStencilFormat,
pp.MultiSampleType, pp.MultiSampleQuality);
|
1
2
3
4
5
6
7
8
PresentationParameters pp = GraphicsDevice.PresentationParameters;
drawBuffer = new RenderTarget2D(GraphicsDevice, pp.BackBufferWidth, pp.BackBufferHeight, true,
SurfaceFormat.Color,DepthFormat.Depth24Stencil8,
pp.MultiSampleCount, RenderTargetUsage.DiscardContents);
|
1
2
spriteBatch.Draw(myRenderTarget.GetTexture(), Vector2.Zero, Color.White);
|
1
2
spriteBatch.Draw(myRenderTarget, Vector2.Zero, Color.White);
|
1
2
Texture2D myTexture = myRenderTarget.GetTexture();
|
1
2
Texture2D myTexture = myRenderTarget;
|
1
2
GraphicsDevice.SetRenderTarget(0, myRenderTarget);
|
1
2
GraphicsDevice.SetRenderTarget(myRenderTarget);
|
1
2
3
4
GraphicsDevice.SetRenderTarget(0, colorRT);
GraphicsDevice.SetRenderTarget(1, depthRT);
|
1
2
3
GraphicsDevice.SetRenderTargets(colorRT, depthRT);
|
1
2
GraphicsDevice.SetRenderTarget(0, null);
|
1
2
GraphicsDevice.SetRenderTarget(null);
|
1
2
3
4
5
6
7
8
9
GraphicsDevice.ResolveBackBuffer(depthMap);
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
GraphicsDevice.SetRenderTarget(null);
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
ResolveTexture2D resolveTarget;
RenderTarget2D renderTarget1;
RenderTarget2D renderTarget2;
PresentationParameters pp = GraphicsDevice.PresentationParameters;
int width = pp.BackBufferWidth;
int height = pp.BackBufferHeight;
SurfaceFormat format = pp.BackBufferFormat;
resolveTarget = new ResolveTexture2D(GraphicsDevice, width, height, 1, format);
width /= 2;
height /= 2;
renderTarget1 = new RenderTarget2D(GraphicsDevice, width, height, 1,format);
renderTarget2 = new RenderTarget2D(GraphicsDevice, width, height, 1,format);
GraphicsDevice.ResolveBackBuffer(resolveTarget);
|
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
RenderTarget2D sceneRenderTarget;
RenderTarget2D renderTarget1;
RenderTarget2D renderTarget2;
PresentationParameters pp = GraphicsDevice.PresentationParameters;
int width = pp.BackBufferWidth;
int height = pp.BackBufferHeight;
SurfaceFormat format = pp.BackBufferFormat;
sceneRenderTarget = new RenderTarget2D(GraphicsDevice, width, height, false,
format, pp.DepthStencilFormat, pp.MultiSampleCount,
RenderTargetUsage.DiscardContents);
width /= 2;
height /= 2;
renderTarget1 = new RenderTarget2D(GraphicsDevice, width, height, false, format, DepthFormat.None);
renderTarget2 = new RenderTarget2D(GraphicsDevice, width, height, false, format, DepthFormat.None);
GraphicsDevice.SetRenderTarget(sceneRenderTarget);
GraphicsDevice.SamplerStates[1] = SamplerState.LinearClamp;
|
VertexDeclaration, Vertices, VertexElements, SizeInBytes
1
2
3
GraphicsDevice.VertexDeclaration = new VertexDeclaration(VertexPositionTexture.VertexElements);
|
1
2
3
4
GraphicsDevice.VertexDeclaration = vertexDeclaration;
GraphicsDevice.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionTexture.SizeInBytes);
|
1
2
3
GraphicsDevice.SetVertexBuffer(vertexBuffer);
|
1
2
3
vertexDeclaration = new VertexDeclaration(GraphicsDevice, VertexPositionTexture.VertexElements);
|
1
2
3
vertexDeclaration = new VertexDeclaration(VertexPositionTexture.VertexDeclaration.GetVertexElements());
|
1
2
3
4
GraphicsDevice.VertexDeclaration = null;
GraphicsDevice.Vertices[0].SetSource(null, 0, 0);
|
1
2
3
GraphicsDevice.SetVertexBuffer(null);
|
1
2
3
4
5
6
7
VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[100];
vertexBuffer.SetData<VertexPositionNormalTexture>(VertexPositionNormalTexture.SizeInBytes * vertexCount,
vertices,vertexCount,count,VertexPositionNormalTexture.SizeInBytes);
|
1
2
3
4
5
6
VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[100];
vertexBuffer.SetData<VertexPositionNormalTexture>(vertices);
|
VertexBuffer, StreamOffset, VertexStride, IndexBuffer, BaseVertex
1
2
3
4
5
6
7
8
9
10
11
12
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
if (meshPart.PrimitiveCount > 0)
{
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
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
if (meshPart.PrimitiveCount > 0)
{
GraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer);
GraphicsDevice.Indices = meshPart.IndexBuffer;
...
|
1
2
3
4
5
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,
meshPart.BaseVertex, 0, meshPart.NumVertices,
meshPart.StartIndex, meshPart.PrimitiveCount);
|
1
2
3
4
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0,
meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount);
|
Points, PointSpriteEnable, PointSizeMax, PointList
1
2
3
4
vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionNormalTexture),
250, BufferUsage.WriteOnly | BufferUsage.Points);
|
1
2
3
4
vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionNormalTexture),
250, BufferUsage.WriteOnly | BufferUsage.None);
|
1
2
3
4
GraphicsDevice.RenderState.PointSpriteEnable = true;
GraphicsDevice.RenderState.PointSizeMax = 128;
|
1
2
3
GraphicsDevice.DrawPrimitives(PrimitiveType.PointList, vertexPosition, numberVertices);
|
1
2
3
GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, vertexPosition, numberVertices);
|
OpenContainer, BeginShowStorageDeviceSelector, EndShowStorageDeviceSelector, Path, TitleLocation, FileStream
1
2
3
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
/// <summary>
/// Synchronously opens storage container
/// </summary>
private static StorageContainer OpenContainer(StorageDevice storageDevice, string saveGameName)
{
IAsyncResult result = storageDevice.BeginOpenContainer(saveGameName, null, null);
result.AsyncWaitHandle.WaitOne();
StorageContainer container = storageDevice.EndOpenContainer(result);
result.AsyncWaitHandle.Close();
return container;
}
StorageContainer storageContainer = OpenContainer(storageDevice, "YourGameName");
|
1
2
3
Guide.BeginShowStorageDeviceSelector(GetStorageDeviceResult, retrievalDelegate);
|
1
2
3
4
5
6
if (!Guide.IsVisible)
{
StorageDevice.BeginShowSelector(GetStorageDeviceResult, retrievalDelegate);
}
|
1
2
3
storageDevice = Guide.EndShowStorageDeviceSelector(result);
|
1
2
3
storageDevice = StorageDevice.EndShowSelector(result);
|
1
2
3
string[] filenames = Directory.GetFiles(storageContainer.Path, "LevelSetup*.xml");
|
1
2
3
string[] filenames = storageContainer.GetFileNames("LevelSetup*.xml"");
|
1
2
3
4
5
6
using (FileStream stream = new FileStream(Path.Combine(storageContainer.Path, levelFilename), FileMode.Create))
{
new XmlSerializer(typeof(SaveGameLevel)).Serialize(stream, levelData);
}
|
1
2
3
4
5
6
using (Stream stream = storageContainer.OpenFile(levelFilename, FileMode.Create))
{
new XmlSerializer(typeof(SaveGameLevel)).Serialize(stream, levelData);
}
|
1
2
3
4
5
6
7
8
9
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
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
string levelPath;
while (true)
{
levelPath = String.Format("Levels/{0}.txt", ++levelIndex);
levelPath = Path.Combine(StorageContainer.TitleLocation, "Content/" + levelPath);
if (File.Exists(levelPath))
break;
if (levelIndex == 0)
throw new Exception("No levels found.");
levelIndex = -1;
}
if (level != null)
level.Dispose();
level = new Level(Services, levelPath);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
levelIndex = (levelIndex + 1) % numberOfLevels;
if (level != null)
level.Dispose();
string levelPath = string.Format("Content/Levels/{0}.txt", levelIndex);
using (Stream fileStream = TitleContainer.OpenStream(levelPath))
level = new Level(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
if ((storageDevice == null) || !storageDevice.IsConnected)
{
return;
}
using (StorageContainer storageContainer = storageDevice.OpenContainer(Session.SaveGameContainerName))
{
string filename;
string descriptionFilename;
if (overwriteDescription == null)
{
int saveGameIndex = 0;
string testFilename;
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 = new FileStream(filename, FileMode.Create))
{
using (XmlWriter xmlWriter = XmlWriter.Create(stream))
{
}
}
SaveGameDescription description = new SaveGameDescription();
description.FileName = Path.GetFileName(filename);
description.ChapterName = IsQuestLineComplete ? "Quest Line Complete" : Quest.Name;
description.Description = DateTime.Now.ToString();
using (FileStream stream = new FileStream(Path.Combine(storageContainer.Path, descriptionFilename), FileMode.Create))
{
new XmlSerializer(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
if ((storageDevice == null) || !storageDevice.IsConnected)
{
return;
}
using (StorageContainer storageContainer = OpenContainer(storageDevice, Session.SaveGameContainerName))
{
string filename;
string descriptionFilename;
if (overwriteDescription == null)
{
int saveGameIndex = 0;
string testFilename;
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";
}
using (Stream stream = storageContainer.OpenFile(filename, FileMode.Create))
{
using (XmlWriter xmlWriter = XmlWriter.Create(stream))
{
}
}
SaveGameDescription description = new SaveGameDescription();
description.FileName = Path.GetFileName(filename);
description.ChapterName = IsQuestLineComplete ? "Quest Line Complete" : Quest.Name;
description.Description = DateTime.Now.ToString();
using (Stream stream = storageContainer.OpenFile(descriptionFilename, FileMode.Create))
{
new XmlSerializer(typeof(SaveGameDescription)).Serialize(stream, description);
}
}
|
VertexShader, PixelShader, ps_1_x
Inside your shader (*.fx) files…
1
2
3
4
5
6
7
8
9
VertexShaderOutput VertexShader(...)
{
}
float4 PixelShader(...)
{
}
|
1
2
3
4
5
6
7
8
9
10
11
VertexShaderOutput VertexShaderFunction(...)
{
}
float4 PixelShaderFunction(...)
{
}
|
1
2
3
4
5
6
7
8
9
technique
{
pass
{
VertexShader = compile vs_1_1 VertexShader();
PixelShader = compile ps_1_1 PixelShader();
}
}
|
1
2
3
4
5
6
7
8
9
technique
{
pass
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
|
XNA Model drawn inside out, slightly transparent, missing parts or just looks wrong
Add the following code before the XNA Model Draw code
1
2
3
4
5
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
|
Well That’s all for now, I will continue to add more examples as needed.
Good luck with your games and Remember Indie Games are the future.
-Nelxon Studio