minimise window.Draw() calls using a RenderTexture, improving performance

master
BuildTools 5 years ago
parent 39126672c0
commit 13bdcec283
  1. 7
      Program.cs
  2. 4
      ball.cs
  3. 9
      collisionHandler.cs
  4. 2
      entityList.cs
  5. 3
      globals.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(300, 100), new Vector2f(.2f, .2f)));
g.entityList.Add(new ball_t(25f, new Vector2f(200, 100), new Vector2f(.2f, .2f))); g.entityList.Add(new ball_t(25f, new Vector2f(200, 100), new Vector2f(.2f, .2f)));
while (window.IsOpen) while (window.IsOpen)
{ {
window.DispatchEvents(); window.DispatchEvents();
window.Clear();
g.entityList.Run(window); g.entityList.Run(window);
g.renderTexture.Display();
window.Clear();
Sprite sprite = new Sprite(g.renderTexture.Texture);
window.Draw(sprite);
g.renderTexture.Clear();
window.Display(); window.Display();
} }
} }

@ -47,7 +47,7 @@ namespace game
this.velocity /= 2f; this.velocity /= 2f;
this.shape.Position += new Vector2f(0, -.1F); 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)); 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; Vector2f oldPosition = this.shape.Position;
this.shape.Position += this.velocity; this.shape.Position += this.velocity;
@ -67,7 +67,7 @@ namespace game
public void Run(RenderWindow window) public void Run(RenderWindow window)
{ {
RunPhysics(new Vector2f(0f, .0001f)); RunPhysics(new Vector2f(0f, .0001f));
window.Draw(this.shape); g.renderTexture.Draw(this.shape);
} }
} }
} }

@ -22,6 +22,15 @@ namespace game
{ {
return (float)Math.Sqrt(v.X * v.X + v.Y * v.Y); 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<ball_t> registry = new List<ball_t>(); public static List<ball_t> registry = new List<ball_t>();
public static collision_t isColliding(ball_t ball) public static collision_t isColliding(ball_t ball)

@ -6,7 +6,7 @@ using game;
namespace game namespace game
{ {
class entityList : List<ball_t> class entityList_t : List<ball_t>
{ {
public void Run(RenderWindow window) public void Run(RenderWindow window)
{ {

@ -13,6 +13,7 @@ namespace game
{ {
public static mouse_t mouse = new mouse_t(); public static mouse_t mouse = new mouse_t();
public static Random rand = new Random(); 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);
} }
} }

Loading…
Cancel
Save