Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Optimization Part 2
Browse files Browse the repository at this point in the history
	- Remove redundant files
	- Remove redundant Allocations
	- Use Less Dynamic Memory
  • Loading branch information
Phoenixfirewingz committed Jun 23, 2023
1 parent 7c2730a commit 65c98a5
Show file tree
Hide file tree
Showing 19 changed files with 307 additions and 309 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
- **Enter** - game restart after death
- **A** - move left
- **D** - move right

**DEBUG:**
- **f1** - instant death

## Setup Instructions

Expand Down
25 changes: 0 additions & 25 deletions SquareSmash.sln

This file was deleted.

Binary file removed assets/Textures/mask.psd
Binary file not shown.
6 changes: 4 additions & 2 deletions assets/shaders/basic_vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
// input
layout(location = 0) in vec2 position;
layout(location = 1) in float colour;
layout (location = 2) in vec2 uv;
layout (location = 2) in float 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));

vec2 uvs[] = vec2[](vec2(0.0, 0.0),vec2(1.0, 0.0),vec2(0.0, 1.0),vec2(1.0, 1.0));

void main()
{
gl_Position = vec4(position, 0.0, 1.0);
vertex_colour = normalize(colours[int(colour)]);
texture_coord = uv;
texture_coord = uvs[int(uv)];
}
11 changes: 0 additions & 11 deletions assets/shaders/striped_fragment.glsl

This file was deleted.

55 changes: 28 additions & 27 deletions objects/Level.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using OpenTK.Mathematics;
using SquareSmash.objects.components;
using SquareSmash.objects.components;
using SquareSmash.objects.components.bricks;
using SquareSmash.objects.components.bricks.types;
using SquareSmash.renderer;
using SquareSmash.renderer.Windows;
using SquareSmash.utils;
using System;
Expand All @@ -13,21 +13,15 @@ namespace SquareSmash.objects
{
public class Level
{
private int bricks_left;
protected List<Brick> bricks = new();
protected List<Brick> bricks = new(114);
protected Ball ball;

protected class BrickData
{
public string Colour { get; set; } = "";
public BrickType Type { get; set; } = BrickType.NORMAL;
[JsonPropertyName("Repeat")]
public int? LoopNullable { get; set; } = null;
public int Loop
{
get { return LoopNullable ?? 0; }
set { LoopNullable = value; }
}
public int Loop { get; set; } = 0;
}

protected struct LevelData
Expand All @@ -44,7 +38,7 @@ protected enum BrickDataColour
DiscGreen,
};

private Brick MakeBrick(BrickType type, Vector2 position, string colour_str)
private Brick MakeBrick(BrickType type, float x, float y, string colour_str)
{
uint colour = (BrickDataColour)Enum.Parse(typeof(BrickDataColour), colour_str) switch
{
Expand All @@ -56,17 +50,18 @@ private Brick MakeBrick(BrickType type, Vector2 position, string colour_str)
};
return type switch
{
BrickType.AIR => new NormalBrick(position, colour, this),
BrickType.NORMAL => new NormalBrick(position, colour, this),
BrickType.LIFE => new LifeBrick(position, this),
BrickType.AIR => new NormalBrick(x, y, colour, this),
BrickType.NORMAL => new NormalBrick(x, y, colour, this),
BrickType.LIFE => new LifeBrick(x, y, this),
_ => throw new ArgumentException("a type provided in the level is not one implemented in the gmae"),
};
}

private void AddBrick(BrickData brick, ref float x, ref float y)
{
bricks.Add(MakeBrick(brick.Type, new(x, y), brick.Colour));
bricks_left++;
if (bricks.Count >= 114) throw new ArgumentException("trying to add to many brings to the live");
Brick created = MakeBrick(brick.Type, x, y, brick.Colour);
bricks.Add(created);
x += 6f;
if (x > 39.5f)
{
Expand All @@ -78,7 +73,7 @@ private void AddBrick(BrickData brick, ref float x, ref float y)
public Level(string json_level)
{
LevelData data = JsonSerializer.Deserialize<LevelData>(AssetUtil.ReadEmbeddedFile(json_level));
ball = new(DiscWindow.Instance.Paddle, data.BaseBallSpeed);
ball = new(data.BaseBallSpeed);
float x = -39f;
float y = 100;
foreach (BrickData brick in data.Bricks)
Expand All @@ -89,32 +84,38 @@ public Level(string json_level)
AddBrick(brick, ref x, ref y);
}
}

public Ball GetBall() => ball;
public List<Brick> GetBricks() => bricks;

public void OnBrickDeath(object sender, EventArgs e) => bricks_left--;
public sbyte bricks_to_gc = 5;
public void OnBrickDeath(object sender, EventArgs e)
{
bricks.Remove((Brick)sender);
bricks_to_gc--;
if (bricks_to_gc < 0)
{
bricks_to_gc = 5;
GC.Collect();
}
}

public void OnRendering(object sender)
{
foreach (Brick brick in bricks)
brick.OnRendering(sender);
DiscWindow.Instance.GLRenderer.Flush();
((QuadBatchRenderer)sender).Flush();
ball.OnRendering(sender);
DiscWindow.Instance.GLRenderer.FlushAntiGhost();
((QuadBatchRenderer)sender).FlushAntiGhost();
}

private bool send = false;
public void OnUpdate(float DeltaTime)
{
if (bricks_left <= 0)
if (bricks.Count <= 0)
{
if (!send)
DiscWindow.Instance.LevelWon();
send = true;
DiscWindow.Instance.LevelWon();
return;
}
foreach (Brick brick in bricks)
brick.OnUpdate();
ball.OnUpdate(this, DeltaTime);
}
}
Expand Down
40 changes: 17 additions & 23 deletions objects/components/Ball.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using Avalonia.Input;
using NAudio.Wave;
using OpenTK.Mathematics;
using SquareSmash.objects.components.bricks;
using SquareSmash.renderer;
using SquareSmash.renderer.Windows;
using SquareSmash.utils;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace SquareSmash.objects.components
{
public class Ball
{
private readonly Vector2 Size = new(0.03f, 0.03f);
private int LastScore = 0;
public int Score { get; set; }
private Vector2 Position;
Expand All @@ -22,13 +21,13 @@ public class Ball
private readonly float LaunchSpeed;
private bool Released = false;
private int CoolDown = 0;
public Ball(Paddle paddle, float speed)
public Ball(float speed)
{
LaunchSpeed = Speed = speed;
Position = paddle.GetPosition();
Position = new(DiscWindow.Instance.Paddle.GetPositionX(), -150f);
Position.X += 0;
Position.Y -= 10;
Vertices = VertexUtils.PreMakeQuad(Vector2.Zero, Vector2.Zero, 0f);
Vertices = VertexUtils.PreMakeQuad(0, 0, 0, 0, 0f);
}

public void ResetBall()
Expand All @@ -46,65 +45,60 @@ public int GetScore()

public bool IsAlive() => Released;

public void OnKeyDown(object sender, KeyEventArgs e)
public void OnKeyDown(Key key)
{
if (e.Key == Key.Space && !Released)
if (key == Key.Space && !Released)
{
SoundUtils.PlaySound(SoundUtils.CLICK_SOUND);
DiscWindow.Instance.DisplayText.Text = "";
Velocity.Y = LaunchSpeed;
Velocity.X = Random.Shared.NextSingle() > 0.5f ? -LaunchSpeed : LaunchSpeed;
Released = true;
}
}
private void Play()
{
WaveOutEvent beepPlayer = new();
beepPlayer.Init(new WaveFileReader(AssetUtil.OpenEmbeddedFile("assets.sounds.bounce.wav")));
beepPlayer.Play();
}

public void OnUpdate(Level level, float DeltaTime)
{
if (Released is false)
{
Position = DiscWindow.Instance.Paddle.GetPosition();
Position = new(DiscWindow.Instance.Paddle.GetPositionX(), -150f);
Position.X *= 10;
Position.Y += 5;
VertexUtils.UpdateQuad(Position, Size, ref Vertices);
VertexUtils.UpdateQuad(Position.X, Position.Y, 0.03f, 0.03f, ref Vertices,0);
return;
}

Position += Velocity * DeltaTime;
Position.X = Math.Clamp(Position.X, -171, 171);
Position.Y = Math.Clamp(Position.Y, -171, 171);

if (DiscWindow.Instance.Paddle.DoseFullIntersects(Vertices))
if (DiscWindow.Instance.Paddle.DoseFullIntersects(ref Vertices))
{
_ = Task.Run(Play);
SoundUtils.PlaySound(SoundUtils.BOUNCE_SOUND);
Velocity.Y = Math.Abs(Velocity.Y);
}
else if (Position.Y >= 170)
{
_ = Task.Run(Play);
SoundUtils.PlaySound(SoundUtils.BOUNCE_SOUND);
Velocity.Y = -Math.Abs(Velocity.Y);
}
else if (Position.X <= -170)
{
_ = Task.Run(Play);
SoundUtils.PlaySound(SoundUtils.BOUNCE_SOUND);
Velocity.X = Math.Abs(Velocity.X);
}
else if (Position.X >= 170)
{
_ = Task.Run(Play);
SoundUtils.PlaySound(SoundUtils.BOUNCE_SOUND);
Velocity.X = -Math.Abs(Velocity.X);
}
else
{
foreach (Brick brick in level.GetBricks())
{
if (brick.IsActive() && brick.DoseFullIntersects(Vertices))
if (brick.DoseFullIntersects(ref Vertices))
{
_ = Task.Run(Play);
SoundUtils.PlaySound(SoundUtils.BOUNCE_SOUND);
if (CoolDown == 0)
{
CoolDown = 5;
Expand Down Expand Up @@ -141,7 +135,7 @@ public void OnUpdate(Level level, float DeltaTime)
if (Velocity.Y == 0)
Velocity.Y += LaunchSpeed;

VertexUtils.UpdateQuad(Position, Size, ref Vertices);
VertexUtils.UpdateQuad(Position.X, Position.Y, 0.03f, 0.03f, ref Vertices,0);

if (Position.Y <= -170)
{
Expand Down
Loading

0 comments on commit 65c98a5

Please sign in to comment.