Skip to content

Commit

Permalink
Optimization Part 1
Browse files Browse the repository at this point in the history
	- remove unnessery realocations
  • Loading branch information
Phoenixfirewingz committed Jun 21, 2023
1 parent b02ee5d commit 7c2730a
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 316 deletions.
7 changes: 5 additions & 2 deletions assets/shaders/basic_vertex.glsl
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#version 330 core
// input
layout(location = 0) in vec2 position;
layout(location = 1) in vec3 colour;
layout(location = 1) in float colour;
layout (location = 2) in vec2 uv;
// output
out vec3 vertex_colour;
out vec2 texture_coord;

//colour look up
vec3 colours[] = vec3[](vec3(255,255,255),vec3(157, 6, 241),vec3(255, 127, 80),vec3(251, 0, 250),vec3(0, 192, 237),vec3(249, 185, 0),vec3(0, 238, 0));

void main()
{
gl_Position = vec4(position, 0.0, 1.0);
vertex_colour = colour;
vertex_colour = normalize(colours[int(colour)]);
texture_coord = uv;
}
20 changes: 10 additions & 10 deletions objects/Level.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ protected enum BrickDataColour

private Brick MakeBrick(BrickType type, Vector2 position, string colour_str)
{
Colour3 colour = (BrickDataColour)Enum.Parse(typeof(BrickDataColour), colour_str) switch
uint colour = (BrickDataColour)Enum.Parse(typeof(BrickDataColour), colour_str) switch
{
BrickDataColour.DiscPink => new(251, 0, 250),
BrickDataColour.DiscBlue => new(0, 192, 237),
BrickDataColour.DiscOrange => new(249, 185, 0),
BrickDataColour.DiscGreen => new(0, 238, 0),
BrickDataColour.DiscPink => 3,
BrickDataColour.DiscBlue => 4,
BrickDataColour.DiscOrange => 5,
BrickDataColour.DiscGreen => 6,
_ => throw new ArgumentException("a colour provided in the level is not one implemented in the gmae"),
};
return type switch
Expand All @@ -75,10 +75,10 @@ private void AddBrick(BrickData brick, ref float x, ref float y)
}
}

public Level(DiscWindow DiscWindow, string json_level)
public Level(string json_level)
{
LevelData data = JsonSerializer.Deserialize<LevelData>(AssetUtil.ReadEmbeddedFile(json_level));
ball = new(DiscWindow.Paddle, data.BaseBallSpeed);
ball = new(DiscWindow.Instance.Paddle, data.BaseBallSpeed);
float x = -39f;
float y = 100;
foreach (BrickData brick in data.Bricks)
Expand All @@ -104,18 +104,18 @@ public void OnRendering(object sender)
}

private bool send = false;
public void OnUpdate(object sender, float DeltaTime)
public void OnUpdate(float DeltaTime)
{
if (bricks_left <= 0)
{
if (!send)
((DiscWindow)sender).LevelWon();
DiscWindow.Instance.LevelWon();
send = true;
return;
}
foreach (Brick brick in bricks)
brick.OnUpdate();
ball.OnUpdate(new Tuple<Level, DiscWindow>(this, (DiscWindow)sender), DeltaTime);
ball.OnUpdate(this, DeltaTime);
}
}
}
25 changes: 12 additions & 13 deletions objects/components/Ball.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace SquareSmash.objects.components
{
public class Ball
{
private static readonly float Size = 0.03f;
private readonly Vector2 Size = new(0.03f, 0.03f);
private int LastScore = 0;
public int Score { get; set; }
private Vector2 Position;
Expand All @@ -28,6 +28,7 @@ public Ball(Paddle paddle, float speed)
Position = paddle.GetPosition();
Position.X += 0;
Position.Y -= 10;
Vertices = VertexUtils.PreMakeQuad(Vector2.Zero, Vector2.Zero, 0f);
}

public void ResetBall()
Expand Down Expand Up @@ -62,24 +63,22 @@ private void Play()
beepPlayer.Play();
}

public void OnUpdate(object sender, float DeltaTime)
public void OnUpdate(Level level, float DeltaTime)
{
Tuple<Level, DiscWindow> senders = (Tuple<Level, DiscWindow>)sender;
if (Released is false)
{
Position = senders.Item2.Paddle.GetPosition();
Position = DiscWindow.Instance.Paddle.GetPosition();
Position.X *= 10;
Position.Y += 5;
Vertices = Task.Run(() => QuadBatchRenderer.PreMakeQuad(Position, new(Size, Size), new(byte.MaxValue, byte.MaxValue, byte.MaxValue))).Result;
VertexUtils.UpdateQuad(Position, Size, ref Vertices);
return;
}

Position += Velocity * DeltaTime;
Position = new(Math.Clamp(Position.X, -171, 171), Math.Clamp(Position.Y, -171, 171));
var task = Task.Run(() => QuadBatchRenderer.PreMakeQuad(Position, new(Size, Size), new(byte.MaxValue, byte.MaxValue, byte.MaxValue)));
//this is very inefficent but idk any other way
Position.X = Math.Clamp(Position.X, -171, 171);
Position.Y = Math.Clamp(Position.Y, -171, 171);

if (senders.Item2.Paddle.DoseFullIntersects(Vertices))
if (DiscWindow.Instance.Paddle.DoseFullIntersects(Vertices))
{
_ = Task.Run(Play);
Velocity.Y = Math.Abs(Velocity.Y);
Expand All @@ -101,7 +100,7 @@ public void OnUpdate(object sender, float DeltaTime)
}
else
{
foreach (Brick brick in senders.Item1.GetBricks())
foreach (Brick brick in level.GetBricks())
{
if (brick.IsActive() && brick.DoseFullIntersects(Vertices))
{
Expand All @@ -113,7 +112,7 @@ public void OnUpdate(object sender, float DeltaTime)
Speed *= 1.0000001f;
Score++;
if (brick.GetBrickType() == BrickType.LIFE)
senders.Item2.Paddle.AddLife();
DiscWindow.Instance.Paddle.AddLife();
Velocity.Y = -(Math.Abs(Velocity.Y) + Speed);
}
else
Expand Down Expand Up @@ -142,14 +141,14 @@ public void OnUpdate(object sender, float DeltaTime)
if (Velocity.Y == 0)
Velocity.Y += LaunchSpeed;

Vertices = task.Result;
VertexUtils.UpdateQuad(Position, Size, ref Vertices);

if (Position.Y <= -170)
{
ResetBall();
LastScore = Score;
Speed = LaunchSpeed;
senders.Item2.Paddle.ResetPaddle();
DiscWindow.Instance.Paddle.ResetPaddle();
}
}
public void OnRendering(object sender)
Expand Down
17 changes: 11 additions & 6 deletions objects/components/Paddle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace SquareSmash.objects.components
{
public class Paddle
{
private readonly Vector2 Size = new(0.3f, 0.03f);
private Vertex[] Vertices = new Vertex[1];
private Vector2 Position = new(1.0f, -150.0f);
private Vector2 Velocity;
Expand All @@ -15,32 +16,36 @@ public class Paddle
public bool IsDead() => Lives < 0;
public void AddLife() => Lives++;

public Paddle() => ResetPaddle();
public Paddle()
{
Vertices = VertexUtils.PreMakeQuad( Vector2.Zero, Vector2.Zero, 1f);
ResetPaddle();
}

public void ResetPaddle()
{
Lives -= IsDead() ? -3 : 1;
Velocity = Vector2.Zero;
Position = new(1.0f, -150.0f);
Vertices = QuadBatchRenderer.PreMakeQuad(Position, new(0.3f, 0.03f), new(157, 6, 241));
VertexUtils.UpdateQuad(Position, Size, ref Vertices);
}

public void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.F1)
Lives = -1;
if (e.Key == Key.A && Position.X < 14)
Velocity.X = 12f;
Velocity.X = 0.1f;
else if (e.Key == Key.D && Position.X > -14)
Velocity.X = -12f;
Velocity.X = -0.1f;
}

public void OnUpdate(float DeltaTime)
{
Position.X += Velocity.X * DeltaTime;
Velocity.X = 0;
Position.X = MathHelper.Clamp(Position.X, -15, 15);
Vertices = QuadBatchRenderer.PreMakeQuad(Position, new(0.3f, 0.03f), new(157, 6, 241));
VertexUtils.UpdateQuad(Position, Size, ref Vertices);
}

public void OnRendering(object sender)
Expand All @@ -49,7 +54,7 @@ public void OnRendering(object sender)
float x = -33;
for (uint i = 0; i <= Lives; i++)
{
((QuadBatchRenderer)sender).AddQuad(new(x, 49f), new(0.1f, 0.1f), new(byte.MaxValue, 127, 80));
((QuadBatchRenderer)sender).AddQuad(new(x, 49f), new(0.1f, 0.1f), 2u);
x -= 7f;
}
((QuadBatchRenderer)sender).FlushAntiGhost();
Expand Down
10 changes: 6 additions & 4 deletions objects/components/bricks/Brick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ namespace SquareSmash.objects.components.bricks
public abstract class Brick
{
private readonly Vertex[] Vertices;
private readonly Aabb hitbox;
private bool is_active = true;
public delegate void OnBrickDeathEventHandler(object sender, EventArgs e);
public event OnBrickDeathEventHandler OnBrickDeath;
public abstract BrickType GetBrickType();
public abstract void OnUpdate();
protected Brick(Vector2 position, Colour3 colour, Level level)
protected Brick(Vector2 position, float colour, Level level)
{
Vertices = QuadBatchRenderer.PreMakeQuad(position, new(0.1225f, 0.04f), colour);
Vertices = VertexUtils.PreMakeQuad(position, new(0.1225f, 0.04f), colour);
hitbox = new(Vertices);
OnBrickDeath += level.OnBrickDeath;
}

Expand All @@ -25,7 +27,7 @@ public void Die()
}
public bool IsActive() => is_active;

protected void UpdateColour(Colour3 colour)
protected void UpdateColour(float colour)
{
for (int i = 0; i < Vertices.Length; i++)
Vertices[i].Color = colour;
Expand All @@ -37,6 +39,6 @@ public void OnRendering(object sender)
((QuadBatchRenderer)sender).AddQuad(Vertices);
}
public bool DoseFullIntersects(Vertex[] testing_quad)
=> CollisonUtil.TestAABB(new(Vertices), new(testing_quad));
=> CollisonUtil.TestAABB(hitbox, new(testing_quad));
}
}
5 changes: 3 additions & 2 deletions objects/components/bricks/types/LifeBrick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace SquareSmash.objects.components.bricks.types
public class LifeBrick : Brick
{
private int wait = 0;
public LifeBrick(Vector2 position, Level level) : base(position, new(byte.MaxValue, byte.MaxValue, byte.MaxValue), level)
public LifeBrick(Vector2 position, Level level) : base(position, 0.0f, level)
{
}

Expand All @@ -20,7 +20,8 @@ public override void OnUpdate()
return;
}
wait = 0;
UpdateColour(new(Random.Shared.NextSingle(), Random.Shared.NextSingle(), Random.Shared.NextSingle()));

UpdateColour(Random.Shared.NextSingle() * 10 % 5);
}
}
}
4 changes: 1 addition & 3 deletions objects/components/bricks/types/NormalBrick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ namespace SquareSmash.objects.components.bricks.types
{
public class NormalBrick : Brick
{
public NormalBrick(Vector2 position, Colour3 colour, Level level) : base(position, colour, level)
public NormalBrick(Vector2 position, float colour, Level level) : base(position, colour, level)
{
}



public override BrickType GetBrickType() => BrickType.NORMAL;

public override void OnUpdate() { }
Expand Down
Loading

0 comments on commit 7c2730a

Please sign in to comment.