From 7603c2e7951e440bfcb5e4441a30e7d4b1b3c6f5 Mon Sep 17 00:00:00 2001 From: Matt Thorson Date: Tue, 22 Sep 2020 15:54:15 -0700 Subject: [PATCH] Rendering Matrix stuff --- src/Core/Game.bf | 1 + src/PlatformLayer/Batch.bf | 8 +++++--- src/PlatformLayer/Batcher.bf | 19 +++++++++++++++++-- src/PlatformLayer/PlatformLayer.bf | 9 +++++++++ src/PlatformLayer/SDL2/SDL2Batcher.bf | 8 ++++---- src/PlatformLayer/SDL2/SDL2PlatformLayer.bf | 6 ------ 6 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/Core/Game.bf b/src/Core/Game.bf index bd53731..5816f97 100644 --- a/src/Core/Game.bf +++ b/src/Core/Game.bf @@ -53,6 +53,7 @@ namespace Strawberry Path.GetDirectoryPath(exePath, exeDir); Directory.SetCurrentDirectory(exeDir); + platformLayer.UpdateScreenMatrix(); platformLayer.Init(); Batcher = platformLayer.CreateBatcher(); diff --git a/src/PlatformLayer/Batch.bf b/src/PlatformLayer/Batch.bf index 35a834d..9ab9ac8 100644 --- a/src/PlatformLayer/Batch.bf +++ b/src/PlatformLayer/Batch.bf @@ -9,22 +9,24 @@ namespace Strawberry public struct Batch { + public Mat4x4 Matrix; public int IndicesStart; public int IndicesCount; public Texture Texture; public BatchModes Mode; - public this(BatchModes mode, Texture texture, int start) + public this(Mat4x4 matrix, BatchModes mode, Texture texture, int start) { + Matrix = matrix; Mode = mode; Texture = texture; IndicesStart = start; IndicesCount = 0; } - public bool Matches(BatchModes mode, Texture texture) + public bool Matches(Mat4x4 matrix, BatchModes mode, Texture texture) { - return Mode == mode && Texture == texture; + return Matrix == matrix && Mode == mode && Texture == texture; } } } diff --git a/src/PlatformLayer/Batcher.bf b/src/PlatformLayer/Batcher.bf index 3e197dd..f40876e 100644 --- a/src/PlatformLayer/Batcher.bf +++ b/src/PlatformLayer/Batcher.bf @@ -8,12 +8,27 @@ namespace Strawberry protected List vertices = new .() ~ delete _; protected List indices = new .() ~ delete _; + public Mat4x4 Matrix; + + public this() + { + Reset(); + } + + public void Reset() + { + Matrix = Game.PlatformLayer.ScreenMatrix; + batches.Clear(); + vertices.Clear(); + indices.Clear(); + } + public abstract void Draw(); protected ref Batch GetBatch(BatchModes mode, Texture texture) { - if (batches.Count == 0 || !batches.Back.Matches(mode, texture)) - batches.Add(Batch(mode, texture, indices.Count)); + if (batches.Count == 0 || !batches.Back.Matches(Matrix, mode, texture)) + batches.Add(Batch(Matrix, mode, texture, indices.Count)); return ref batches.Back; } diff --git a/src/PlatformLayer/PlatformLayer.bf b/src/PlatformLayer/PlatformLayer.bf index 1d2be39..4d5fbf4 100644 --- a/src/PlatformLayer/PlatformLayer.bf +++ b/src/PlatformLayer/PlatformLayer.bf @@ -3,6 +3,15 @@ namespace Strawberry { public abstract class PlatformLayer { + public Mat4x4 ScreenMatrix { get; private set; } + + public void UpdateScreenMatrix() + { + ScreenMatrix = Mat4x4.CreateOrthographic(Game.Width, Game.Height * 0.5f, 0, 1) + * Mat4x4.CreateScale(.(1, -1, 1)) + * Mat4x4.CreateTranslation(.(-1, 1, 0)); + } + public abstract void Init(); public abstract bool Closed(); // Returns whether the game window has been closed diff --git a/src/PlatformLayer/SDL2/SDL2Batcher.bf b/src/PlatformLayer/SDL2/SDL2Batcher.bf index d2f0447..2c7f9c5 100644 --- a/src/PlatformLayer/SDL2/SDL2Batcher.bf +++ b/src/PlatformLayer/SDL2/SDL2Batcher.bf @@ -45,8 +45,10 @@ namespace Strawberry.SDL2 GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, indexBufferID); GL.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, indices.Count * sizeof(uint32), indices.Ptr, GL.GL_DYNAMIC_DRAW); - for (let b in batches) + for (var b in batches) { + GL.glUniformMatrix4fv(platformLayer.TransformMatrixLocation, 1, GL.GL_FALSE, &b.Matrix.Values); + if (b.Mode == .Shape) GL.glBindTexture(GL.GL_TEXTURE_2D, 0); else @@ -62,9 +64,7 @@ namespace Strawberry.SDL2 GL.glBindVertexArray(0); - vertices.Clear(); - indices.Clear(); - batches.Clear(); + Reset(); } protected override void PushQuad(BatchModes mode, Texture texture, Vertex a, Vertex b, Vertex c, Vertex d) diff --git a/src/PlatformLayer/SDL2/SDL2PlatformLayer.bf b/src/PlatformLayer/SDL2/SDL2PlatformLayer.bf index 0063551..e88f05d 100644 --- a/src/PlatformLayer/SDL2/SDL2PlatformLayer.bf +++ b/src/PlatformLayer/SDL2/SDL2PlatformLayer.bf @@ -154,12 +154,6 @@ namespace Strawberry.SDL2 GL.glClearColor(Game.ClearColor.Rf, Game.ClearColor.Gf, Game.ClearColor.Bf, Game.ClearColor.Af); GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); GL.glUseProgram(glProgram); - - Mat4x4 mat = Mat4x4.CreateOrthographic(Game.Width, Game.Height * 0.5f, 0, 1); - mat *= Mat4x4.CreateScale(.(1, -1, 1)); - mat *= Mat4x4.CreateTranslation(.(-1, 1, 0)); - - GL.glUniformMatrix4fv(TransformMatrixLocation, 1, GL.GL_FALSE, &mat.Values); } public override void RenderEnd()