From 52d1d5f38339be5048afa89425728d85c0dc35ae Mon Sep 17 00:00:00 2001 From: Matt Thorson Date: Sun, 6 Sep 2020 23:11:45 -0700 Subject: [PATCH] renderer work --- SampleGame/src/Player.bf | 3 +- SampleGame/src/assets/textures/test.png | Bin 0 -> 141 bytes src/PlatformLayer/Batch.bf | 30 +++++++++++++++++ src/PlatformLayer/Batcher.bf | 42 +++++++++++++++++++++--- src/PlatformLayer/SDL2PlatformLayer.bf | 5 ++- src/PlatformLayer/Vertex.bf | 10 ++++++ src/Static/Assets.bf | 39 +++++++++------------- 7 files changed, 100 insertions(+), 29 deletions(-) create mode 100644 SampleGame/src/assets/textures/test.png create mode 100644 src/PlatformLayer/Batch.bf diff --git a/SampleGame/src/Player.bf b/SampleGame/src/Player.bf index da27f86..99342ad 100644 --- a/SampleGame/src/Player.bf +++ b/SampleGame/src/Player.bf @@ -102,7 +102,8 @@ namespace Strawberry.Sample { base.Draw(); - DrawHitbox(.Red); + DrawHitbox(.Green); + //Game.Batcher.Tex(Assets.Textures["test"], X - 4, Y - 8); } } } diff --git a/SampleGame/src/assets/textures/test.png b/SampleGame/src/assets/textures/test.png new file mode 100644 index 0000000000000000000000000000000000000000..b8293ed4382eb80990d83ac7300a1a1dbd8e1e8d GIT binary patch literal 141 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`A)YRdAr`&KdnD)mncv8Cz@;;d z4G6?i`hK0-DZ!Q3rhfS%>ko!auP4O1ZsgW5YbZRhcw*T`hn!glG8q=KurN batches = new .() ~ delete _; private List vertices = new .() ~ delete _; private List indices = new .() ~ delete _; @@ -47,15 +48,33 @@ namespace Strawberry 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); + for (let b in batches) + { + if (b.Mode == .Shape) + GL.glBindTexture(GL.GL_TEXTURE_2D, 0); + else + GL.glBindTexture(GL.GL_TEXTURE_2D, b.Texture.Handle); + GL.glDrawElements(GL.GL_TRIANGLES, b.IndicesCount, GL.GL_UNSIGNED_INT, (void*)(b.IndicesStart * sizeof(uint32))); + } + GL.glBindVertexArray(0); vertices.Clear(); indices.Clear(); + batches.Clear(); } - protected void PushQuad(Vertex a, Vertex b, Vertex c, Vertex d) + private ref Batch GetBatch(BatchModes mode, Texture texture) { + if (batches.Count == 0 || !batches.Back.Matches(mode, texture)) + batches.Add(Batch(mode, texture, indices.Count)); + return ref batches.Back; + } + + protected void PushQuad(BatchModes mode, Texture texture, Vertex a, Vertex b, Vertex c, Vertex d) + { + GetBatch(mode, texture).IndicesCount += 6; + uint32 count = (uint32)vertices.Count; vertices.Add(a); @@ -71,8 +90,10 @@ namespace Strawberry indices.Add(count + 3); } - protected void PushTri(Vertex a, Vertex b, Vertex c) + protected void PushTri(BatchModes mode, Texture texture, Vertex a, Vertex b, Vertex c) { + GetBatch(mode, texture).IndicesCount += 3; + uint32 count = (uint32)vertices.Count; vertices.Add(a); @@ -86,12 +107,25 @@ namespace Strawberry public void Rect(float x, float y, float w, float h, Color color) { - PushQuad(.Shape(.(x, y), color), .Shape(.(x + w, y), color), .Shape(.(x + w, y + h), color), .Shape(.(x, y + h), color)); + PushQuad(.Shape, null, + .Shape(.(x, y), color), + .Shape(.(x + w, y), color), + .Shape(.(x + w, y + h), color), + .Shape(.(x, y + h), color)); } public void Rect(Rect rect, Color color) { Rect(rect.X, rect.Y, rect.Width, rect.Height, color); } + + public void Tex(Texture texture, float x, float y) + { + PushQuad(.TextureTint, texture, + .Tex(.(x, y), .(0, 1), Color.White), + .Tex(.(x + texture.Width, y), .(1, 1), Color.White), + .Tex(.(x + texture.Width, y + texture.Height), .(1, 0), Color.White), + .Tex(.(x, y + texture.Height), .(0, 0), Color.White)); + } } } diff --git a/src/PlatformLayer/SDL2PlatformLayer.bf b/src/PlatformLayer/SDL2PlatformLayer.bf index 2b063d3..79c989e 100644 --- a/src/PlatformLayer/SDL2PlatformLayer.bf +++ b/src/PlatformLayer/SDL2PlatformLayer.bf @@ -166,7 +166,10 @@ namespace Strawberry.SDL2 public override Texture LoadTexture(String path) { - var surface = SDLImage.Load(path); + let surface = SDLImage.Load(path); + Debug.Assert(surface != null, "Could not load from path."); + Debug.Assert(surface.format.bytesPerPixel == 4, "Surface format incorrect."); + var tex = new Texture(surface.w, surface.h, (uint8*)surface.pixels); SDL.FreeSurface(surface); diff --git a/src/PlatformLayer/Vertex.bf b/src/PlatformLayer/Vertex.bf index 90e9c54..cbe77a5 100644 --- a/src/PlatformLayer/Vertex.bf +++ b/src/PlatformLayer/Vertex.bf @@ -18,5 +18,15 @@ namespace Strawberry v.Mode = (0, 0, 255); return v; } + + static public Vertex Tex(Vector pos, Vector texCoord, Color color) + { + Vertex v = Vertex(); + v.Position = pos; + v.TexCoord = texCoord; + v.Color = color; + v.Mode = (255, 0, 0); + return v; + } } } diff --git a/src/Static/Assets.bf b/src/Static/Assets.bf index 2d5b560..f6f8ee2 100644 --- a/src/Static/Assets.bf +++ b/src/Static/Assets.bf @@ -6,60 +6,53 @@ namespace Strawberry { static public class Assets { - static public Dictionary Sprites { get; private set; } - static public Dictionary Fonts { get; private set; } + static public Dictionary Textures { get; private set; } - static public String ContentRoot { get; private set; } + #if DEBUG + static public readonly String Root = "../../../src/assets/"; + #else + static public readonly String Root = "assets/"; + #endif static public void LoadAll() { - #if DEBUG - ContentRoot = "../../../src/Content/"; - #else - ContentRoot = "Content"; - #endif - - Sprites = new Dictionary(); - Load("Sprites", "*.ase*", Sprites); - - Fonts = new Dictionary(); - Load("Fonts", "*.ttf", Fonts); + Textures = new Dictionary(); + Load("textures", "*.png", Textures, (path) => Game.PlatformLayer.LoadTexture(path)); } static public void DisposeAll() { - DeleteDictionaryAndKeysAndItems!(Sprites); - DeleteDictionaryAndKeysAndItems!(Fonts); + DeleteDictionaryAndKeysAndItems!(Textures); Sprite.[Friend]Dispose(); } - static private void Load(String directory, String wildcard, Dictionary putInto) where T : Asset + static private void Load(String directory, String wildcard, Dictionary putInto, function T(String) loader) { - let root = scope String(ContentRoot); + let root = scope String(Root); root.Append(Path.DirectorySeparatorChar); root.Append(directory); if (Directory.Exists(root)) - LoadDir(root, root, wildcard, putInto); + LoadDir(root, root, wildcard, putInto, loader); else Calc.Log("Create a Content/{0} folder to load {0}", directory); } - static private void LoadDir(String rootDir, String directory, String wildcard, Dictionary putInto) where T : Asset + static private void LoadDir(String rootDir, String directory, String wildcard, Dictionary putInto, function T(String) loader) { //Recursive folder search for (let dir in Directory.EnumerateDirectories(directory)) { let path = scope String(); dir.GetFilePath(path); - LoadDir(rootDir, path, wildcard, putInto); + LoadDir(rootDir, path, wildcard, putInto, loader); } //Load files for (let file in Directory.EnumerateFiles(directory, wildcard)) { - let path = new String(); + let path = scope String(); file.GetFilePath(path); - let asset = new [Friend]T(path); + let asset = loader(path); let key = new String(path); key.Remove(0, rootDir.Length + 1);