From df436f8c32a77db88f3ee36d2d55beec33c2e37a Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Wed, 15 Jul 2020 15:56:56 -0400 Subject: [PATCH] TreeScene: draw a triangle --- Shared/SneakGame.cs | 8 ++++---- Shared/TreeScene.cs | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Shared/SneakGame.cs b/Shared/SneakGame.cs index 05d0946..890969d 100644 --- a/Shared/SneakGame.cs +++ b/Shared/SneakGame.cs @@ -64,11 +64,11 @@ namespace SemiColinGames { private void LoadLevel() { world?.Dispose(); - world = new SneakWorld(GraphicsDevice, Content.LoadString("levels/demo.json")); - // world = new TreeWorld(); + // world = new SneakWorld(GraphicsDevice, Content.LoadString("levels/demo.json")); + world = new TreeWorld(); scene?.Dispose(); - scene = new SneakScene(GraphicsDevice, ((SneakWorld) world).Camera); - // scene = new TreeScene(GraphicsDevice); + // scene = new SneakScene(GraphicsDevice, ((SneakWorld) world).Camera); + scene = new TreeScene(GraphicsDevice); GC.Collect(); GC.WaitForPendingFinalizers(); diff --git a/Shared/TreeScene.cs b/Shared/TreeScene.cs index 230f568..aa09841 100644 --- a/Shared/TreeScene.cs +++ b/Shared/TreeScene.cs @@ -6,12 +6,28 @@ using System; namespace SemiColinGames { public sealed class TreeScene : IScene { - readonly Color backgroundColor = Color.SkyBlue; + const int MAX_LINE_VERTICES = 4; - readonly GraphicsDevice graphics; + private readonly Color backgroundColor = Color.SkyBlue; + private readonly GraphicsDevice graphics; + private readonly BasicEffect basicEffect; + + private VertexPositionColor[] lineVertices; + private VertexBuffer vertexBuffer; public TreeScene(GraphicsDevice graphics) { this.graphics = graphics; + + basicEffect = new BasicEffect(graphics) { + World = Matrix.CreateTranslation(0, 0, 0), + View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up), + VertexColorEnabled = true + }; + basicEffect.Projection = Matrix.CreateOrthographicOffCenter(0, 1920, 1080, 0, -1, 1); + + lineVertices = new VertexPositionColor[MAX_LINE_VERTICES]; + vertexBuffer = new VertexBuffer( + graphics, typeof(VertexPositionColor), MAX_LINE_VERTICES, BufferUsage.WriteOnly); } ~TreeScene() { @@ -19,11 +35,24 @@ namespace SemiColinGames { } public void Dispose() { + vertexBuffer.Dispose(); GC.SuppressFinalize(this); } public void Draw(bool isRunningSlowly, IWorld iworld, bool paused) { graphics.Clear(backgroundColor); + + lineVertices[0] = new VertexPositionColor(new Vector3(100, 100, 0), Color.Black); + lineVertices[1] = new VertexPositionColor(new Vector3(300, 200, 0), Color.Black); + lineVertices[2] = new VertexPositionColor(new Vector3(200, 200, 0), Color.Black); + lineVertices[3] = new VertexPositionColor(new Vector3(100, 100, 0), Color.Black); + + graphics.SetVertexBuffer(vertexBuffer); + vertexBuffer.SetData(lineVertices); + foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) { + pass.Apply(); + graphics.DrawPrimitives(PrimitiveType.LineStrip, 0, 3 /* num lines */); + } } } } \ No newline at end of file