diff --git a/assets/shaders/basic_vertex.glsl b/assets/shaders/basic_vertex.glsl index 1382aaf..6cf041d 100644 --- a/assets/shaders/basic_vertex.glsl +++ b/assets/shaders/basic_vertex.glsl @@ -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; } \ No newline at end of file diff --git a/objects/Level.cs b/objects/Level.cs index e2ae6aa..c61c8af 100644 --- a/objects/Level.cs +++ b/objects/Level.cs @@ -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 @@ -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(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) @@ -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(this, (DiscWindow)sender), DeltaTime); + ball.OnUpdate(this, DeltaTime); } } } diff --git a/objects/components/Ball.cs b/objects/components/Ball.cs index 3774d16..fdb708b 100644 --- a/objects/components/Ball.cs +++ b/objects/components/Ball.cs @@ -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; @@ -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() @@ -62,24 +63,22 @@ private void Play() beepPlayer.Play(); } - public void OnUpdate(object sender, float DeltaTime) + public void OnUpdate(Level level, float DeltaTime) { - Tuple senders = (Tuple)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); @@ -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)) { @@ -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 @@ -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) diff --git a/objects/components/Paddle.cs b/objects/components/Paddle.cs index 8918dd8..fc819b2 100644 --- a/objects/components/Paddle.cs +++ b/objects/components/Paddle.cs @@ -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; @@ -15,14 +16,18 @@ 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) @@ -30,9 +35,9 @@ 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) @@ -40,7 +45,7 @@ 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) @@ -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(); diff --git a/objects/components/bricks/Brick.cs b/objects/components/bricks/Brick.cs index b3f0960..dee6098 100644 --- a/objects/components/bricks/Brick.cs +++ b/objects/components/bricks/Brick.cs @@ -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; } @@ -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; @@ -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)); } } diff --git a/objects/components/bricks/types/LifeBrick.cs b/objects/components/bricks/types/LifeBrick.cs index 50aaf33..1ce055c 100644 --- a/objects/components/bricks/types/LifeBrick.cs +++ b/objects/components/bricks/types/LifeBrick.cs @@ -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) { } @@ -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); } } } diff --git a/objects/components/bricks/types/NormalBrick.cs b/objects/components/bricks/types/NormalBrick.cs index f2bf995..76aaf9d 100644 --- a/objects/components/bricks/types/NormalBrick.cs +++ b/objects/components/bricks/types/NormalBrick.cs @@ -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() { } diff --git a/renderer/QuadBatchRenderer.cs b/renderer/QuadBatchRenderer.cs index d31a609..0525318 100644 --- a/renderer/QuadBatchRenderer.cs +++ b/renderer/QuadBatchRenderer.cs @@ -20,9 +20,8 @@ public sealed class QuadBatchRenderer : IDisposable private readonly int sid_anti_ghost; private readonly int sid_plain; private readonly int tex_id; + private readonly int vsize = 5 * sizeof(float); private readonly List quadVertices; - public static readonly Matrix4 projectionMatrix = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45f), 0.85915492957f, 0.1f, 100f); - public static readonly Matrix4 viewMatrix = Matrix4.LookAt(new Vector3(0.0f, 0.0f, 5.0f), Vector3.Zero, Vector3.UnitY); private static unsafe int CreateTexture(GlInterface GL, GlExtrasInterface EGL, string path) { @@ -81,165 +80,44 @@ public QuadBatchRenderer(GlInterface gl, GlExtrasInterface ext) vao = vaos[0]; GLE.BindVertexArray(vao); GL.BindBuffer(GL_ARRAY_BUFFER, vbo); + GL.VertexAttribPointer(0, 2, GL_FLOAT, 0, vsize, 0); GL.EnableVertexAttribArray(0); - GL.VertexAttribPointer(0, 2, GL_FLOAT, 0, 7 * sizeof(float), 0); + GL.VertexAttribPointer(1, 1, GL_FLOAT, 0, vsize, 2 * sizeof(float)); GL.EnableVertexAttribArray(1); - GL.VertexAttribPointer(1, 3, GL_FLOAT, 0, 7 * sizeof(float), 2 * sizeof(float)); + GL.VertexAttribPointer(2, 2, GL_FLOAT, 0, vsize, 3 * sizeof(float)); GL.EnableVertexAttribArray(2); - GL.VertexAttribPointer(2, 2, GL_FLOAT, 0, 7 * sizeof(float), 5 * sizeof(float)); GLE.BindVertexArray(0); } - // pre calculate the vertex so that we don't need to spent cpu cycles at rime-time to calculate the vertex - // the can be compliantly expensive - public static Vertex[] PreMakeQuad(Vector2 position, Vector2 size, Colour3 color) - { - var quad = new Vertex[6]; // Use 6 vertices for a quad triangle strip - - // Apply model transformation - Matrix4 modelTransform = Matrix4.Identity; - modelTransform *= Matrix4.CreateTranslation(new Vector3(position.X, position.Y, -1.0f)); - modelTransform *= Matrix4.CreateScale(new Vector3(size.X, size.Y, 1.0f)); - - /* Idea of how vertex are laded out - * 0,0-------------------------------------0,1 - * | ~ | - * | ~ | - * | ~ | - * 1,0-------------------------------------1,1 - */ - - Vector4 transform_0 = (new Vector4(-1.0f, -1.0f, -1.0f, 1.0f) * projectionMatrix * viewMatrix * modelTransform); - Vector3 scale_down_0 = new Vector3(transform_0.X, transform_0.Y, transform_0.Z) / transform_0.Z; - quad[0].Position = scale_down_0.Xy; - quad[0].Color = color; - quad[0].Uvs = new Vector2(0.0f, 0.0f); - - Vector4 transform_1 = (new Vector4(1.0f, -1.0f, -1.0f, 1.0f) * projectionMatrix * viewMatrix * modelTransform); - Vector3 scale_down_1 = new Vector3(transform_1.X, transform_1.Y, transform_1.Z) / transform_1.Z; - quad[1].Position = scale_down_1.Xy; - quad[1].Color = color; - quad[1].Uvs = new Vector2(1.0f, 0.0f); - - Vector4 transform_2 = (new Vector4(-1.0f, 1.0f, -1.0f, 1.0f) * projectionMatrix * viewMatrix * modelTransform); - Vector3 scale_down_2 = new Vector3(transform_2.X, transform_2.Y, transform_2.Z) / transform_2.Z; - quad[2].Position = scale_down_2.Xy; - quad[2].Color = color; - quad[2].Uvs = new Vector2(0.0f, 1.0f); - - Vector4 transform_3 = (new Vector4(1.0f, 1.0f, -1.0f, 1.0f) * projectionMatrix * viewMatrix * modelTransform); - Vector3 scale_down_3 = new Vector3(transform_3.X, transform_3.Y, transform_3.Z) / transform_3.Z; - quad[3].Position = scale_down_3.Xy; - quad[3].Color = color; - quad[3].Uvs = new Vector2(1.0f, 1.0f); - - Vector4 transform_4 = (new Vector4(-1.0f, 1.0f, -1.0f, 1.0f) * projectionMatrix * viewMatrix * modelTransform); - Vector3 scale_down_4 = new Vector3(transform_4.X, transform_4.Y, transform_4.Z) / transform_4.Z; - quad[4].Position = scale_down_4.Xy; - quad[4].Color = color; - quad[4].Uvs = new Vector2(0.0f, 1.0f); - - Vector4 transform_5 = (new Vector4(1.0f, -1.0f, -1.0f, 1.0f) * projectionMatrix * viewMatrix * modelTransform); - Vector3 scale_down_5 = new Vector3(transform_5.X, transform_5.Y, transform_5.Z) / transform_5.Z; - quad[5].Position = scale_down_5.Xy; - quad[5].Color = color; - quad[5].Uvs = new Vector2(1.0f, 0.0f); - return quad; - } - public void AddQuad(Vertex[] Vertices) => quadVertices.AddRange(Vertices); - public Vertex[] AddQuad(Vector2 position, Vector2 size, Colour3 color) + public Vertex[] AddQuad(Vector2 position, Vector2 size, float color) { - var quad = new Vertex[6]; // Use 6 vertices for a quad triangle strip - - // Apply model transformation - Matrix4 modelTransform = Matrix4.Identity; - modelTransform *= Matrix4.CreateTranslation(new Vector3(position.X, position.Y, -1.0f)); - modelTransform *= Matrix4.CreateScale(new Vector3(size.X, size.Y, 1.0f)); - - Vector4 transform_0 = (new Vector4(-1.0f, -1.0f, -1.0f, 1.0f) * projectionMatrix * viewMatrix * modelTransform); - Vector3 scale_down_0 = new Vector3(transform_0.X, transform_0.Y, transform_0.Z) / transform_0.Z; - quad[0].Position = scale_down_0.Xy; - quad[0].Color = color; - quad[0].Uvs = new Vector2(0.0f, 0.0f); - - Vector4 transform_1 = (new Vector4(1.0f, -1.0f, -1.0f, 1.0f) * projectionMatrix * viewMatrix * modelTransform); - Vector3 scale_down_1 = new Vector3(transform_1.X, transform_1.Y, transform_1.Z) / transform_1.Z; - quad[1].Position = scale_down_1.Xy; - quad[1].Color = color; - quad[1].Uvs = new Vector2(1.0f, 0.0f); - - Vector4 transform_2 = (new Vector4(-1.0f, 1.0f, -1.0f, 1.0f) * projectionMatrix * viewMatrix * modelTransform); - Vector3 scale_down_2 = new Vector3(transform_2.X, transform_2.Y, transform_2.Z) / transform_2.Z; - quad[2].Position = scale_down_2.Xy; - quad[2].Color = color; - quad[2].Uvs = new Vector2(0.0f, 1.0f); - - Vector4 transform_3 = (new Vector4(1.0f, 1.0f, -1.0f, 1.0f) * projectionMatrix * viewMatrix * modelTransform); - Vector3 scale_down_3 = new Vector3(transform_3.X, transform_3.Y, transform_3.Z) / transform_3.Z; - quad[3].Position = scale_down_3.Xy; - quad[3].Color = color; - quad[3].Uvs = new Vector2(1.0f, 1.0f); - - Vector4 transform_4 = (new Vector4(-1.0f, 1.0f, -1.0f, 1.0f) * projectionMatrix * viewMatrix * modelTransform); - Vector3 scale_down_4 = new Vector3(transform_4.X, transform_4.Y, transform_4.Z) / transform_4.Z; - quad[4].Position = scale_down_4.Xy; - quad[4].Color = color; - quad[4].Uvs = new Vector2(0.0f, 1.0f); - - Vector4 transform_5 = (new Vector4(1.0f, -1.0f, -1.0f, 1.0f) * projectionMatrix * viewMatrix * modelTransform); - Vector3 scale_down_5 = new Vector3(transform_5.X, transform_5.Y, transform_5.Z) / transform_5.Z; - quad[5].Position = scale_down_5.Xy; - quad[5].Color = color; - quad[5].Uvs = new Vector2(1.0f, 0.0f); - + var quad = VertexUtils.PreMakeQuad(position, size, color); quadVertices.AddRange(quad); return quad; } - public unsafe void Flush() + public unsafe void ShaderFlush(int id) { if (quadVertices.Count == 0) return; GLE.BindVertexArray(vao); fixed (void* pdata = quadVertices.ToArray()) - GL.BufferData(GL_ARRAY_BUFFER, quadVertices.Count * 7 * sizeof(float), new IntPtr(pdata), GL_DYNAMIC_DRAW); + GL.BufferData(GL_ARRAY_BUFFER, quadVertices.Count * vsize, new IntPtr(pdata), GL_DYNAMIC_DRAW); GL.BindTexture(GL_TEXTURE_2D, tex_id); - GL.UseProgram(sid); + GL.UseProgram(id); GL.DrawArrays(GL_TRIANGLES, 0, quadVertices.Count); GLE.BindVertexArray(0); quadVertices.Clear(); } - public unsafe void FlushAntiGhost() - { - if (quadVertices.Count == 0) - return; - GLE.BindVertexArray(vao); - fixed (void* pdata = quadVertices.ToArray()) - GL.BufferData(GL_ARRAY_BUFFER, quadVertices.Count * 7 * sizeof(float), new IntPtr(pdata), GL_DYNAMIC_DRAW); - GL.BindTexture(GL_TEXTURE_2D, tex_id); - GL.UseProgram(sid_anti_ghost); - GL.DrawArrays(GL_TRIANGLES, 0, quadVertices.Count); - GLE.BindVertexArray(0); - quadVertices.Clear(); - } - public unsafe void FlushPlain() - { - if (quadVertices.Count == 0) - return; - GLE.BindVertexArray(vao); - fixed (void* pdata = quadVertices.ToArray()) - GL.BufferData(GL_ARRAY_BUFFER, quadVertices.Count * 7 * sizeof(float), new IntPtr(pdata), GL_DYNAMIC_DRAW); - GL.UseProgram(sid_plain); - GL.DrawArrays(GL_TRIANGLES, 0, quadVertices.Count); - GLE.BindVertexArray(0); - quadVertices.Clear(); - } + public unsafe void Flush() => ShaderFlush(sid); + public unsafe void FlushAntiGhost() => ShaderFlush(sid_anti_ghost); + public unsafe void FlushPlain() => ShaderFlush(sid_plain); public void Dispose() { diff --git a/renderer/Vertex.cs b/renderer/Vertex.cs index bb819e8..e1ae234 100644 --- a/renderer/Vertex.cs +++ b/renderer/Vertex.cs @@ -7,7 +7,7 @@ namespace SquareSmash.renderer public struct Vertex { public Vector2 Position { get; set; } - public Colour3 Color { get; set; } + public float Color { get; set; } public Vector2 Uvs { get; set; } } diff --git a/renderer/Windows/DiscWindow.axaml.cs b/renderer/Windows/DiscWindow.axaml.cs index a9f86b7..5d70a56 100644 --- a/renderer/Windows/DiscWindow.axaml.cs +++ b/renderer/Windows/DiscWindow.axaml.cs @@ -25,49 +25,29 @@ public partial class DiscWindow : Window public Paddle Paddle { get; private set; } - public float GLHeight - { - get => ((1.0f - ((float)Height / (float)Width)) * 2.0f) - 1.0f; - } - private readonly Stopwatch stopwatch = new(); public bool GameRestart { get; private set; } = false; private int CurrentLevel = 1; public Level Level { get; private set; } - protected readonly CancellationTokenSource Cancellation = new CancellationTokenSource(); - private Thread UpdateThread; public ScoreBoard ScoreBoard { get; private set; } public QuadBatchRenderer GLRenderer { get => GLView.Renderer; } - protected void Update() - { - while (!Instance.Cancellation.IsCancellationRequested) - { - float DeltaTime = (float)Instance.stopwatch.Elapsed.TotalMilliseconds; - Instance.stopwatch.Restart(); - var t = Task.Run(() => Instance.Level.OnUpdate(this, DeltaTime)); - Instance.Paddle.OnUpdate(DeltaTime); - t.Wait(); - } - } - public DiscWindow() { InitializeComponent(); Instance = this; KeyDown += OnKeyDown!; Paddle = new(); - Level = new(this, "assets.levels.level_1.json"); + Level = new("assets.levels.level_1.json"); ScoreBoard = ScoreBoard.Load(); stopwatch.Start(); musicPlayer.Init(new Mp3FileReader(AssetUtil.OpenEmbeddedFile("assets.sounds.music.mp3"))); - UpdateThread = new Thread(() => { Instance.Update(); }); - UpdateThread.Start(); //musicPlayer.Play(); + GC.Collect(); } private void OnKeyDown(object sender, KeyEventArgs e) @@ -78,7 +58,7 @@ private void OnKeyDown(object sender, KeyEventArgs e) { GameRestart = false; DisplayText.Text = ""; - Level = new(this, "assets.levels.level_" + Convert.ToString(CurrentLevel) + ".json"); + Level = new("assets.levels.level_" + Convert.ToString(CurrentLevel) + ".json"); } else return; @@ -115,6 +95,10 @@ public override void Render(DrawingContext context) } GameRestart = true; } + float DeltaTime = (float)Instance.stopwatch.Elapsed.TotalMilliseconds; + Instance.stopwatch.Restart(); + Instance.Level.OnUpdate(DeltaTime); + Instance.Paddle.OnUpdate(DeltaTime); GLView.Render(context); base.Render(context); Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Render); @@ -130,8 +114,6 @@ protected void FinishNewUser(int LastScoreData, string UserNameData) protected override void OnClosing(CancelEventArgs e) { - Cancellation.Cancel(); - UpdateThread.Join(); musicPlayer.Stop(); musicPlayer.Dispose(); stopwatch.Stop(); @@ -143,7 +125,7 @@ protected override void OnClosing(CancelEventArgs e) public void LevelWon() { CurrentLevel++; - Level = new Level(this, "assets.levels.level_" + Convert.ToString(CurrentLevel) + ".json"); + Level = new Level("assets.levels.level_" + Convert.ToString(CurrentLevel) + ".json"); } } } diff --git a/renderer/Windows/PopUpWindow.axaml.cs b/renderer/Windows/PopUpWindow.axaml.cs index 288eb98..5846474 100644 --- a/renderer/Windows/PopUpWindow.axaml.cs +++ b/renderer/Windows/PopUpWindow.axaml.cs @@ -23,6 +23,7 @@ public PopUpWindow() Score = 0; Callback = (int n, string s) => { }; InitializeComponent(); + TextInput.Text = ""; } public PopUpWindow(int score, Action callback) @@ -30,6 +31,7 @@ public PopUpWindow(int score, Action callback) Callback = callback; Score = score; InitializeComponent(); + TextInput.Text = ""; } private void OnButtonClick(object sender, RoutedEventArgs e) diff --git a/renderer/opengl/GlExtrasInterface.cs b/renderer/opengl/GlExtrasInterface.cs index 40ae52a..2df5c4f 100644 --- a/renderer/opengl/GlExtrasInterface.cs +++ b/renderer/opengl/GlExtrasInterface.cs @@ -1,4 +1,5 @@ -namespace Avalonia.OpenGL; +using System; +namespace Avalonia.OpenGL; public class GlExtrasInterface : GlInterfaceBase { @@ -32,52 +33,7 @@ public GlExtrasInterface(GlInterface gl) : base(gl.GetProcAddress, gl.ContextInf public GlBindVertexArray BindVertexArray { get; } = null!; public unsafe delegate void GlBindVertexArray(int array); - [GlEntryPoint("glUniform1i")] - public GlUniform1i Uniform1i { get; } = null!; - public unsafe delegate void GlUniform1i(int location, int v); - - [GlEntryPoint("glUniform2f")] - public GlUniform2f Uniform2f { get; } = null!; - public unsafe delegate void GlUniform2f(int location, float x, float y); - - [GlEntryPoint("glUniform3f")] - public GlUniform3f Uniform3f { get; } = null!; - public unsafe delegate void GlUniform3f(int location, float x, float y, float z); - - [GlEntryPoint("glUniform4f")] - public GlUniform4f Uniform4f { get; } = null!; - public unsafe delegate void GlUniform4f(int location, float x, float y, float z, float w); - - [GlEntryPoint("glGetActiveUniform")] - public GlGetActiveUniform GetActiveUniform { get; } = null!; - public unsafe delegate void GlGetActiveUniform(int program, int index, int bufferSize, int* length, int* size, int* type, sbyte* name); - - [GlEntryPoint("glCullFace")] - public GlCullFace CullFace { get; } = null!; - public unsafe delegate void GlCullFace(int face); - - [GlEntryPoint("glPolygonMode")] - public GlPolygonMode PolygonMode { get; } = null!; - public unsafe delegate void GlPolygonMode(int face, int mode); - - [GlEntryPoint("glBlendFunc")] - public GlBlendFunc BlendFunc { get; } = null!; - public unsafe delegate void GlBlendFunc(int sFactor, int dFactor); - - [GlEntryPoint("glBlendEquation")] - public GlBlendEquation BlendEquation { get; } = null!; - public unsafe delegate void GlBlendEquation(int mode); - - [GlEntryPoint("glPixelStorei")] - public GlPixelStorei PixelStorei { get; } = null!; - public unsafe delegate void GlPixelStorei(int name, int param); - [GlEntryPoint("glGenerateMipmap")] public GlGenerateMipmap GenerateMipmap { get; } = null!; public unsafe delegate void GlGenerateMipmap(int target); - - [GlEntryPoint("glDisable")] - public GlDisable Disable { get; } = null!; - public unsafe delegate void GlDisable(int cap); - } \ No newline at end of file diff --git a/utils/Colour3.cs b/utils/Colour3.cs deleted file mode 100644 index 855c599..0000000 --- a/utils/Colour3.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; - -namespace SquareSmash.utils -{ - [Serializable] - public readonly struct Colour3 - { - public readonly float R; - public readonly float G; - public readonly float B; - - public Colour3(float r, float g, float b) - { - R = r; - G = g; - B = b; - } - - public Colour3(byte r, byte g, byte b) - { - R = r / 255f; - G = g / 255f; - B = b / 255f; - } - - public override bool Equals(object? obj) - { - if (obj is Colour3 colour) - return Equals(colour); - return false; - } - - public bool Equals(Colour3 other) - { - if (R == other.R && G == other.G) - return B == other.B; - return false; - } - - public override int GetHashCode() - { - return HashCode.Combine(R, G, B); - } - - public static bool operator ==(Colour3 left, Colour3 right) - { - return left.Equals(right); - } - - public static bool operator !=(Colour3 left, Colour3 right) - { - return !(left == right); - } - } -} diff --git a/utils/NetUtil.cs b/utils/NetUtil.cs deleted file mode 100644 index b4c426e..0000000 --- a/utils/NetUtil.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Security.Cryptography; -using System.Text; - -namespace SquareSmash.utils -{ - internal static class NetUtil - { - public static string MD5Hash(string s) - { - return BitConverter.ToString(MD5.HashData(Encoding.UTF8.GetBytes(s))).Replace("-", "").ToLower(); - } - } -} diff --git a/utils/Util.cs b/utils/Util.cs index de098e5..36ce82f 100644 --- a/utils/Util.cs +++ b/utils/Util.cs @@ -3,10 +3,75 @@ using System.IO; using System.Reflection; using System.Text; +using OpenTK.Mathematics; namespace SquareSmash.utils { + internal static class VertexUtils + { + public static readonly Matrix4 ScreenMatrix = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45f), 0.85915492957f, 0.1f, 100f) + * Matrix4.LookAt(new(0.0f, 0.0f, 5.0f), Vector3.Zero, Vector3.UnitY); + + private static readonly Vector4 pre_transform_0 = (new Vector4(-1.0f, -1.0f, -1.0f, 1.0f) * ScreenMatrix); + private static readonly Vector4 pre_transform_1 = (new Vector4(1.0f, -1.0f, -1.0f, 1.0f) * ScreenMatrix); + // pre calculate the vertex so that we don't need to spent cpu cycles at rime-time to calculate the vertex + // the can be compliantly expensive + public static Vertex[] PreMakeQuad(Vector2 position, Vector2 size, float color) + { + var quad = new Vertex[6]; // Use 6 vertices for a quad triangle strip + UpdateQuad(position, size, ref quad); + quad[0].Color = color; + quad[0].Uvs = new(0.0f, 0.0f); + quad[1].Color = color; + quad[1].Uvs = new(1.0f, 0.0f); + quad[2].Color = color; + quad[2].Uvs = new(0.0f, 1.0f); + quad[3].Color = color; + quad[3].Uvs = new(1.0f, 1.0f); + quad[4].Color = color; + quad[4].Uvs = new(0.0f, 1.0f); + quad[5].Color = color; + quad[5].Uvs = new(1.0f, 0.0f); + return quad; + } + + // pre calculate the vertex so that we don't need to spent cpu cycles at rime-time to calculate the vertex + // the can be compliantly expensive + public static Vertex[] UpdateQuad(Vector2 position, Vector2 size, ref Vertex[] quad) + { + // Apply model transformation + Matrix4 modelTransform = Matrix4.Identity; + modelTransform *= Matrix4.CreateTranslation(new Vector3(position.X, position.Y, -1.0f)); + modelTransform *= Matrix4.CreateScale(new Vector3(size.X, size.Y, 1.0f)); + + Vector4 transform_0 = pre_transform_0 * modelTransform; + Vector3 scale_down_0 = new Vector3(transform_0.X, transform_0.Y, transform_0.Z) / transform_0.Z; + quad[0].Position = scale_down_0.Xy; + + Vector4 transform_1 = pre_transform_1 * modelTransform; + Vector3 scale_down_1 = new Vector3(transform_1.X, transform_1.Y, transform_1.Z) / transform_1.Z; + quad[1].Position = scale_down_1.Xy; + + Vector4 transform_2 = (new Vector4(-1.0f, 1.0f, -1.0f, 1.0f) * ScreenMatrix * modelTransform); + Vector3 scale_down_2 = new Vector3(transform_2.X, transform_2.Y, transform_2.Z) / transform_2.Z; + quad[2].Position = scale_down_2.Xy; + + Vector4 transform_3 = (new Vector4(1.0f, 1.0f, -1.0f, 1.0f) * ScreenMatrix * modelTransform); + Vector3 scale_down_3 = new Vector3(transform_3.X, transform_3.Y, transform_3.Z) / transform_3.Z; + quad[3].Position = scale_down_3.Xy; + + Vector4 transform_4 = (new Vector4(-1.0f, 1.0f, -1.0f, 1.0f) * ScreenMatrix * modelTransform); + Vector3 scale_down_4 = new Vector3(transform_4.X, transform_4.Y, transform_4.Z) / transform_4.Z; + quad[4].Position = scale_down_4.Xy; + + Vector4 transform_5 = (new Vector4(1.0f, -1.0f, -1.0f, 1.0f) * ScreenMatrix * modelTransform); + Vector3 scale_down_5 = new Vector3(transform_5.X, transform_5.Y, transform_5.Z) / transform_5.Z; + quad[5].Position = scale_down_5.Xy; + return quad; + } + } + internal static class CollisonUtil { public static bool TestAABB(Aabb obj_0, Aabb obj_1)