From 13bdcec283a609d7729cb1be7fca64838bd73b75 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 29 Dec 2020 17:25:59 -0500 Subject: [PATCH] minimise window.Draw() calls using a RenderTexture, improving performance --- Program.cs | 7 ++++++- ball.cs | 4 ++-- collisionHandler.cs | 9 +++++++++ entityList.cs | 2 +- globals.cs | 3 ++- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Program.cs b/Program.cs index 3888a62..59c9453 100644 --- a/Program.cs +++ b/Program.cs @@ -22,11 +22,16 @@ namespace game g.entityList.Add(new ball_t(25f, new Vector2f(300, 100), new Vector2f(.2f, .2f))); g.entityList.Add(new ball_t(25f, new Vector2f(200, 100), new Vector2f(.2f, .2f))); + while (window.IsOpen) { window.DispatchEvents(); - window.Clear(); g.entityList.Run(window); + g.renderTexture.Display(); + window.Clear(); + Sprite sprite = new Sprite(g.renderTexture.Texture); + window.Draw(sprite); + g.renderTexture.Clear(); window.Display(); } } diff --git a/ball.cs b/ball.cs index d775eec..ffef24d 100644 --- a/ball.cs +++ b/ball.cs @@ -47,7 +47,7 @@ namespace game this.velocity /= 2f; this.shape.Position += new Vector2f(0, -.1F); this.shape.Position = new Vector2f(Math.Clamp(this.shape.Position.X, 0, 1280), Math.Clamp(this.shape.Position.Y, 0, 720)); - Console.WriteLine("COLLISION: "+this.velocity.Y.ToString()); + //Console.WriteLine("COLLISION: "+this.velocity.Y.ToString()); } Vector2f oldPosition = this.shape.Position; this.shape.Position += this.velocity; @@ -67,7 +67,7 @@ namespace game public void Run(RenderWindow window) { RunPhysics(new Vector2f(0f, .0001f)); - window.Draw(this.shape); + g.renderTexture.Draw(this.shape); } } } diff --git a/collisionHandler.cs b/collisionHandler.cs index 9d12a35..50728e8 100644 --- a/collisionHandler.cs +++ b/collisionHandler.cs @@ -22,6 +22,15 @@ namespace game { return (float)Math.Sqrt(v.X * v.X + v.Y * v.Y); } + public static float TEMP_vecDist(Vector2f v, Vector2f v2) + { + Vector2f delta = new Vector2f(); + + delta.X = v.X - v2.X; + delta.X = v.Y - v2.Y; + + return TEMP_vecLen(delta); + } public static List registry = new List(); public static collision_t isColliding(ball_t ball) diff --git a/entityList.cs b/entityList.cs index 3f59816..9701eb7 100644 --- a/entityList.cs +++ b/entityList.cs @@ -6,7 +6,7 @@ using game; namespace game { - class entityList : List + class entityList_t : List { public void Run(RenderWindow window) { diff --git a/globals.cs b/globals.cs index fd12e37..26c8923 100644 --- a/globals.cs +++ b/globals.cs @@ -13,6 +13,7 @@ namespace game { public static mouse_t mouse = new mouse_t(); public static Random rand = new Random(); - public static entityList entityList = new entityList(); + public static entityList_t entityList = new entityList_t(); + public static RenderTexture renderTexture = new RenderTexture(1280, 720); } }