From 82f1f412ebf86c838204bc5a147d5a686fd961bd Mon Sep 17 00:00:00 2001 From: Matt Thorson Date: Sun, 23 Aug 2020 18:12:15 -0700 Subject: [PATCH] Simplifying PlatformLayer a bit. Setting up texture loading --- src/Core/Game.bf | 2 +- src/PlatformLayer/Batcher.bf | 85 +++++++++++++++++- src/PlatformLayer/PlatformLayer.bf | 2 - src/PlatformLayer/SDL2/SDL2Batcher.bf | 88 ------------------- src/PlatformLayer/SDL2/SDL2Shader.bf | 52 ----------- src/PlatformLayer/SDL2/SDL2Texture.bf | 20 ----- .../{SDL2 => }/SDL2PlatformLayer.bf | 16 +--- src/PlatformLayer/Shader.bf | 45 +++++++++- src/PlatformLayer/Texture.bf | 14 ++- src/Static/Console.bf | 2 +- 10 files changed, 141 insertions(+), 185 deletions(-) delete mode 100644 src/PlatformLayer/SDL2/SDL2Batcher.bf delete mode 100644 src/PlatformLayer/SDL2/SDL2Shader.bf delete mode 100644 src/PlatformLayer/SDL2/SDL2Texture.bf rename src/PlatformLayer/{SDL2 => }/SDL2PlatformLayer.bf (94%) diff --git a/src/Core/Game.bf b/src/Core/Game.bf index bd53731..ddf3a34 100644 --- a/src/Core/Game.bf +++ b/src/Core/Game.bf @@ -54,7 +54,7 @@ namespace Strawberry Directory.SetCurrentDirectory(exeDir); platformLayer.Init(); - Batcher = platformLayer.CreateBatcher(); + Batcher = new Batcher(); VirtualInputs = new List(); Input.[Friend]Init(); diff --git a/src/PlatformLayer/Batcher.bf b/src/PlatformLayer/Batcher.bf index 6a60b87..42efee1 100644 --- a/src/PlatformLayer/Batcher.bf +++ b/src/PlatformLayer/Batcher.bf @@ -1,11 +1,88 @@ +using System.Collections; + namespace Strawberry { - public abstract class Batcher + public class Batcher { - public abstract void Draw(); + private List vertices = new .() ~ delete _; + private List indices = new .() ~ delete _; - protected abstract void PushQuad(Vertex a, Vertex b, Vertex c, Vertex d); - protected abstract void PushTri(Vertex a, Vertex b, Vertex c); + private uint32 vaoID; + private uint32 vertexBufferID; + private uint32 indexBufferID; + + public this() + { + GL.glGenVertexArrays(1, &vaoID); + GL.glBindVertexArray(vaoID); + GL.glGenBuffers(1, &vertexBufferID); + GL.glGenBuffers(1, &indexBufferID); + GL.glBindVertexArray(0); + } + + public ~this() + { + GL.glDeleteBuffers(1, &vertexBufferID); + GL.glDeleteBuffers(1, &indexBufferID); + GL.glDeleteVertexArrays(1, &vaoID); + } + + public void Draw() + { + GL.glDisable(GL.GL_CULL_FACE); + + GL.glBindVertexArray(vaoID); + + GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexBufferID); + GL.glEnableVertexAttribArray(0); + GL.glVertexAttribPointer(0, 2, GL.GL_FLOAT, GL.GL_FALSE, sizeof(Vertex), (void*)0); + GL.glEnableVertexAttribArray(1); + GL.glVertexAttribPointer(1, 2, GL.GL_FLOAT, GL.GL_FALSE, sizeof(Vertex), (void*)8); + GL.glEnableVertexAttribArray(2); + GL.glVertexAttribPointer(2, 4, GL.GL_UNSIGNED_BYTE, GL.GL_TRUE, sizeof(Vertex), (void*)16); + GL.glEnableVertexAttribArray(3); + GL.glVertexAttribPointer(3, 3, GL.GL_UNSIGNED_BYTE, GL.GL_TRUE, sizeof(Vertex), (void*)20); + GL.glBufferData(GL.GL_ARRAY_BUFFER, vertices.Count * sizeof(Vertex), vertices.Ptr, GL.GL_DYNAMIC_DRAW); + + 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); + + GL.glDrawElements(GL.GL_TRIANGLES, indices.Count, GL.GL_UNSIGNED_INT, (void*)0); + GL.glBindVertexArray(0); + + vertices.Clear(); + indices.Clear(); + } + + protected void PushQuad(Vertex a, Vertex b, Vertex c, Vertex d) + { + uint32 count = (uint32)vertices.Count; + + vertices.Add(a); + vertices.Add(b); + vertices.Add(c); + vertices.Add(d); + + indices.Add(count + 0); + indices.Add(count + 1); + indices.Add(count + 2); + indices.Add(count + 0); + indices.Add(count + 2); + indices.Add(count + 3); + } + + protected void PushTri(Vertex a, Vertex b, Vertex c) + { + uint32 count = (uint32)vertices.Count; + + vertices.Add(a); + vertices.Add(b); + vertices.Add(c); + + indices.Add(count + 0); + indices.Add(count + 1); + indices.Add(count + 2); + } public void Rect(float x, float y, float w, float h, Color color) { diff --git a/src/PlatformLayer/PlatformLayer.bf b/src/PlatformLayer/PlatformLayer.bf index d6271ff..c1e8dea 100644 --- a/src/PlatformLayer/PlatformLayer.bf +++ b/src/PlatformLayer/PlatformLayer.bf @@ -23,7 +23,5 @@ namespace Strawberry //Graphics public abstract Texture LoadTexture(String path); - public abstract Batcher CreateBatcher(); - public abstract Shader CreateShader(ShaderDef def); } } diff --git a/src/PlatformLayer/SDL2/SDL2Batcher.bf b/src/PlatformLayer/SDL2/SDL2Batcher.bf deleted file mode 100644 index 28c98e0..0000000 --- a/src/PlatformLayer/SDL2/SDL2Batcher.bf +++ /dev/null @@ -1,88 +0,0 @@ -using System.Collections; -using System; - -namespace Strawberry.SDL2 -{ - public class SDL2Batcher : Batcher - { - private List vertices = new .() ~ delete _; - private List indices = new .() ~ delete _; - - private uint32 vaoID; - private uint32 vertexBufferID; - private uint32 indexBufferID; - - public this() - { - GL.glGenVertexArrays(1, &vaoID); - GL.glBindVertexArray(vaoID); - GL.glGenBuffers(1, &vertexBufferID); - GL.glGenBuffers(1, &indexBufferID); - GL.glBindVertexArray(0); - } - - public ~this() - { - GL.glDeleteBuffers(1, &vertexBufferID); - GL.glDeleteBuffers(1, &indexBufferID); - GL.glDeleteVertexArrays(1, &vaoID); - } - - public override void Draw() - { - GL.glDisable(GL.GL_CULL_FACE); - - GL.glBindVertexArray(vaoID); - - GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexBufferID); - GL.glEnableVertexAttribArray(0); - GL.glVertexAttribPointer(0, 2, GL.GL_FLOAT, GL.GL_FALSE, sizeof(Vertex), (void*)0); - GL.glEnableVertexAttribArray(1); - GL.glVertexAttribPointer(1, 2, GL.GL_FLOAT, GL.GL_FALSE, sizeof(Vertex), (void*)8); - GL.glEnableVertexAttribArray(2); - GL.glVertexAttribPointer(2, 4, GL.GL_UNSIGNED_BYTE, GL.GL_TRUE, sizeof(Vertex), (void*)16); - GL.glEnableVertexAttribArray(3); - GL.glVertexAttribPointer(3, 3, GL.GL_UNSIGNED_BYTE, GL.GL_TRUE, sizeof(Vertex), (void*)20); - GL.glBufferData(GL.GL_ARRAY_BUFFER, vertices.Count * sizeof(Vertex), vertices.Ptr, GL.GL_DYNAMIC_DRAW); - - 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); - - GL.glDrawElements(GL.GL_TRIANGLES, indices.Count, GL.GL_UNSIGNED_INT, (void*)0); - GL.glBindVertexArray(0); - - vertices.Clear(); - indices.Clear(); - } - - protected override void PushQuad(Vertex a, Vertex b, Vertex c, Vertex d) - { - uint32 count = (uint32)vertices.Count; - - vertices.Add(a); - vertices.Add(b); - vertices.Add(c); - vertices.Add(d); - - indices.Add(count + 0); - indices.Add(count + 1); - indices.Add(count + 2); - indices.Add(count + 0); - indices.Add(count + 2); - indices.Add(count + 3); - } - - protected override void PushTri(Vertex a, Vertex b, Vertex c) - { - uint32 count = (uint32)vertices.Count; - - vertices.Add(a); - vertices.Add(b); - vertices.Add(c); - - indices.Add(count + 0); - indices.Add(count + 1); - indices.Add(count + 2); - } - } -} diff --git a/src/PlatformLayer/SDL2/SDL2Shader.bf b/src/PlatformLayer/SDL2/SDL2Shader.bf deleted file mode 100644 index ff992fd..0000000 --- a/src/PlatformLayer/SDL2/SDL2Shader.bf +++ /dev/null @@ -1,52 +0,0 @@ -namespace Strawberry.SDL2 -{ - class SDL2Shader : Shader - { - public uint vertexHandle; - public uint fragmentHandle; - - public this(ShaderDef def) - : base(def) - { - IsValid = true; - int32 logLen = 0; - char8[1024] log; - - vertexHandle = GL.glCreateShader(GL.GL_VERTEX_SHADER); - { - int32 len = (int32)def.Vertex.Length; - char8* data = def.Vertex.CStr(); - GL.glShaderSource(vertexHandle, 1, &data, &len); - GL.glCompileShader(vertexHandle); - GL.glGetShaderInfoLog(vertexHandle, 1024, &logLen, &log); - - if (logLen > 0) - { - Calc.Log(&log, logLen); - IsValid = false; - } - } - - fragmentHandle = GL.glCreateShader(GL.GL_FRAGMENT_SHADER); - { - int32 len = (int32)def.Fragment.Length; - char8* data = def.Fragment.CStr(); - GL.glShaderSource(fragmentHandle, 1, &data, &len); - GL.glCompileShader(fragmentHandle); - GL.glGetShaderInfoLog(fragmentHandle, 1024, &logLen, &log); - - if (logLen > 0) - { - Calc.Log(&log, logLen); - IsValid = false; - } - } - } - - public ~this() - { - GL.glDeleteShader(vertexHandle); - GL.glDeleteShader(fragmentHandle); - } - } -} diff --git a/src/PlatformLayer/SDL2/SDL2Texture.bf b/src/PlatformLayer/SDL2/SDL2Texture.bf deleted file mode 100644 index e9631b2..0000000 --- a/src/PlatformLayer/SDL2/SDL2Texture.bf +++ /dev/null @@ -1,20 +0,0 @@ -namespace Strawberry.SDL2 -{ - class SDL2Texture : Texture - { - private uint32 handle; - - public this(int width, int height, uint8* pixels) - : base(width, height, pixels) - { - GL.glGenTextures(1, &handle); - GL.glBindTexture(GL.GL_TEXTURE_2D, handle); - GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, width, height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, pixels); - } - - public ~this() - { - GL.glDeleteTextures(1, &handle); - } - } -} diff --git a/src/PlatformLayer/SDL2/SDL2PlatformLayer.bf b/src/PlatformLayer/SDL2PlatformLayer.bf similarity index 94% rename from src/PlatformLayer/SDL2/SDL2PlatformLayer.bf rename to src/PlatformLayer/SDL2PlatformLayer.bf index dee6b20..2b063d3 100644 --- a/src/PlatformLayer/SDL2/SDL2PlatformLayer.bf +++ b/src/PlatformLayer/SDL2PlatformLayer.bf @@ -10,7 +10,7 @@ namespace Strawberry.SDL2 private SDL.Surface* screen; private SDL.Rect screenRect; private SDL.Renderer* renderer; - private SDL2Shader shader; + private Shader shader; private SDL.SDL_GameController*[] gamepads; private bool* keyboard; @@ -53,7 +53,7 @@ namespace Strawberry.SDL2 SDL.GL_SetSwapInterval(1); GL.Init(=> SdlGetProcAddress); - shader = new SDL2Shader(String[2] ( + shader = new Shader(String[2] ( // vertex shader """ #version 330 @@ -167,7 +167,7 @@ namespace Strawberry.SDL2 public override Texture LoadTexture(String path) { var surface = SDLImage.Load(path); - var tex = new SDL2Texture(surface.w, surface.h, (uint8*)surface.pixels); + var tex = new Texture(surface.w, surface.h, (uint8*)surface.pixels); SDL.FreeSurface(surface); return tex; @@ -210,15 +210,5 @@ namespace Strawberry.SDL2 else return val / 32768f; } - - public override Batcher CreateBatcher() - { - return new SDL2Batcher(); - } - - public override Shader CreateShader(ShaderDef def) - { - return new SDL2Shader(def); - } } } diff --git a/src/PlatformLayer/Shader.bf b/src/PlatformLayer/Shader.bf index 37d7c13..cffb2ae 100644 --- a/src/PlatformLayer/Shader.bf +++ b/src/PlatformLayer/Shader.bf @@ -1,12 +1,53 @@ namespace Strawberry { - public abstract class Shader + class Shader { - public bool IsValid { get; protected set; } + public bool IsValid { get; private set; } + + public uint vertexHandle; + public uint fragmentHandle; public this(ShaderDef def) { + IsValid = true; + int32 logLen = 0; + char8[1024] log; + vertexHandle = GL.glCreateShader(GL.GL_VERTEX_SHADER); + { + int32 len = (int32)def.Vertex.Length; + char8* data = def.Vertex.CStr(); + GL.glShaderSource(vertexHandle, 1, &data, &len); + GL.glCompileShader(vertexHandle); + GL.glGetShaderInfoLog(vertexHandle, 1024, &logLen, &log); + + if (logLen > 0) + { + Calc.Log(&log, logLen); + IsValid = false; + } + } + + fragmentHandle = GL.glCreateShader(GL.GL_FRAGMENT_SHADER); + { + int32 len = (int32)def.Fragment.Length; + char8* data = def.Fragment.CStr(); + GL.glShaderSource(fragmentHandle, 1, &data, &len); + GL.glCompileShader(fragmentHandle); + GL.glGetShaderInfoLog(fragmentHandle, 1024, &logLen, &log); + + if (logLen > 0) + { + Calc.Log(&log, logLen); + IsValid = false; + } + } + } + + public ~this() + { + GL.glDeleteShader(vertexHandle); + GL.glDeleteShader(fragmentHandle); } } } diff --git a/src/PlatformLayer/Texture.bf b/src/PlatformLayer/Texture.bf index 8b40433..2906675 100644 --- a/src/PlatformLayer/Texture.bf +++ b/src/PlatformLayer/Texture.bf @@ -1,14 +1,24 @@ namespace Strawberry { - public abstract class Texture + public class Texture { + public uint32 Handle { get; private set; } public int Width { get; private set; } public int Height { get; private set; } - public virtual this(int width, int height, uint8* pixels) + public this(int width, int height, uint8* pixels) { Width = width; Height = height; + + GL.glGenTextures(1, &Handle); + GL.glBindTexture(GL.GL_TEXTURE_2D, Handle); + GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, width, height, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, pixels); + } + + public ~this() + { + GL.glDeleteTextures(1, &Handle); } } } diff --git a/src/Static/Console.bf b/src/Static/Console.bf index 9c34fb6..70a888e 100644 --- a/src/Static/Console.bf +++ b/src/Static/Console.bf @@ -88,7 +88,7 @@ namespace Strawberry static public void Log(StringView str, params Object[] args) { - let string = Calc.[Friend]StringArgs(scope String(str), params args); + let string = Calc.StringArgs(scope String(str), params args); Log(string); }