| public class Game1 : Microsoft.Xna.Framework.Game |
| { |
| GraphicsDeviceManager graphics; |
| SpriteBatch spriteBatch; |
| GraphicsDevice device; |
| |
| Effect effect; |
| VertexPositionColorTexture[] vertices; |
| VertexPositionColorTexture[] screenVertices; |
| |
| float width, height; |
| int Offset = 0; |
| |
| RenderTarget2D latestRender; |
| RenderTarget2D previousRender; |
| |
| public Game1() |
| { |
| graphics = new GraphicsDeviceManager(this); |
| Content.RootDirectory = "Content"; |
| } |
| |
| protected override void Initialize() |
| { |
| graphics.PreferredBackBufferWidth = 500; |
| graphics.PreferredBackBufferHeight = 500; |
| graphics.IsFullScreen = false; |
| graphics.ApplyChanges(); |
| Window.Title = "Arena of Fire"; |
| |
| base.Initialize(); |
| } |
| |
| protected override void LoadContent() |
| { |
| spriteBatch = new SpriteBatch(GraphicsDevice); |
| |
| device = graphics.GraphicsDevice; |
| |
| latestRender = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.Depth24, 0, RenderTargetUsage.PlatformContents); |
| previousRender = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.Depth24, 0, RenderTargetUsage.DiscardContents); |
| |
| effect = Content.Load<Effect>("DefaultEffect"); |
| effect.Parameters["ViewportWidth"].SetValue(GraphicsDevice.Viewport.Width); |
| effect.Parameters["ViewportHeight"].SetValue(GraphicsDevice.Viewport.Height); |
| |
| width = GraphicsDevice.Viewport.Width; |
| height = GraphicsDevice.Viewport.Height; |
| |
| SetUpVertices(); |
| } |
| |
| protected override void UnloadContent() |
| { |
| } |
| |
| private void SetUpVertices() |
| { |
| const int amount = 1; |
| vertices = new VertexPositionColorTexture[amount * 3]; // TriangleList triangle at half size |
| |
| vertices[0].Position = new Vector3(0, 0.5f, 0); |
| vertices[0].Color = Color.Red; |
| vertices[1].Position = new Vector3(0.5f, -0.5f, 0); |
| vertices[1].Color = Color.Green; |
| vertices[2].Position = new Vector3(-0.5f, -0.5f, 0); |
| vertices[2].Color = Color.Blue; |
| |
| |
| screenVertices = new VertexPositionColorTexture[4]; // TriangleStrip quad |
| |
| screenVertices[0].Position = new Vector3 (1.0f, 1.0f, 0); // top right |
| screenVertices[0].TextureCoordinate = new Vector2(1, 0); |
| screenVertices[0].Color = Color.Magenta; |
| |
| screenVertices[1].Position = new Vector3 (1.0f, -1.0f, 0); // bottom right |
| screenVertices[1].TextureCoordinate = new Vector2(1, 1); |
| screenVertices[1].Color = Color.Cyan; |
| |
| screenVertices[2].Position = new Vector3 (-1.0f, 1.0f, 0); // top left |
| screenVertices[2].TextureCoordinate = new Vector2(0, 0); |
| screenVertices[2].Color = Color.Yellow; |
| |
| // second tess |
| screenVertices[3].Position = new Vector3 (-1.0f, -1.0f, 0); // bottom left |
| screenVertices[3].TextureCoordinate = new Vector2(0, 1); |
| screenVertices[3].Color = Color.Gray; |
| } |
| |
| protected override void Update(GameTime gameTime) |
| { |
| if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || |
| Keyboard.GetState().IsKeyDown(Keys.Escape)) |
| this.Exit(); |
| |
| base.Update(gameTime); |
| } |
| |
| protected override void Draw(GameTime gameTime) |
| { |
| // create looping offset for animation tests |
| Offset++; |
| if (Offset > 100) |
| { |
| Offset = 0; |
| } |
| effect.Parameters["Offset"].SetValue(Offset); |
| |
| // swap buffers, setting latest render to previous render. |
| SwapRenderTargets(); |
| |
| // set default technique |
| effect.CurrentTechnique = effect.Techniques["DefaultTechnique"]; |
| |
| // begin draw |
| DrawData(); |
| |
| DrawRender(); |
| base.Draw(gameTime); |
| } |
| |
| void DrawData() |
| { |
| effect.Parameters["frameTexture"].SetValue(previousRender); // pass previous render to textures |
| |
| device.SetRenderTarget(latestRender); // set as working render |
| device.Clear(Color.Black); // clear "undefined" render target |
| |
| effect.CurrentTechnique.Passes[1].Apply(); // Texture |
| DrawScreenPrimitive(); // draw previous frame as texture |
| |
| effect.CurrentTechnique.Passes[0].Apply(); // Color |
| DrawInput(); // draw colorized triangle |
| |
| device.SetRenderTarget(null); // undo RenderTarget |
| effect.Parameters["frameTexture"].SetValue(latestRender); |
| } |
| |
| void SwapRenderTargets() |
| { |
| RenderTarget2D buffer = previousRender; |
| previousRender = latestRender; |
| latestRender = buffer; |
| } |
| |
| void DrawScreenPrimitive() |
| { |
| device.DrawUserPrimitives(PrimitiveType.TriangleStrip, screenVertices, 0, 2, VertexPositionColorTexture.VertexDeclaration); |
| } |
| |
| void DrawInput() |
| { |
| device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length / 3, VertexPositionColorTexture.VertexDeclaration); |
| } |
| |
| void DrawRender() |
| { |
| effect.Parameters["frameTexture"].SetValue(latestRender); // prepare current render as texture |
| effect.CurrentTechnique.Passes[1].Apply(); // Render data |
| device.Clear(Color.Purple); |
| |
| DrawScreenPrimitive(); // draw |
| } |
| } |