renderer work

This commit is contained in:
Matt Thorson
2020-09-06 23:11:45 -07:00
parent 82f1f412eb
commit 52d1d5f383
7 changed files with 100 additions and 29 deletions

View File

@ -0,0 +1,30 @@
namespace Strawberry
{
public enum BatchModes
{
TextureTint,
TextureWash,
Shape,
}
public struct Batch
{
public int IndicesStart;
public int IndicesCount;
public Texture Texture;
public BatchModes Mode;
public this(BatchModes mode, Texture texture, int start)
{
Mode = mode;
Texture = texture;
IndicesStart = start;
IndicesCount = 0;
}
public bool Matches(BatchModes mode, Texture texture)
{
return Mode == mode && Texture == texture;
}
}
}

View File

@ -4,6 +4,7 @@ namespace Strawberry
{
public class Batcher
{
private List<Batch> batches = new .() ~ delete _;
private List<Vertex> vertices = new .() ~ delete _;
private List<uint32> 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));
}
}
}

View File

@ -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);

View File

@ -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;
}
}
}