mirror of
https://github.com/MaddyThorson/StrawberryBF.git
synced 2024-11-28 16:58:56 +08:00
Simplifying PlatformLayer a bit. Setting up texture loading
This commit is contained in:
parent
1c084b55f9
commit
82f1f412eb
|
@ -54,7 +54,7 @@ namespace Strawberry
|
||||||
Directory.SetCurrentDirectory(exeDir);
|
Directory.SetCurrentDirectory(exeDir);
|
||||||
|
|
||||||
platformLayer.Init();
|
platformLayer.Init();
|
||||||
Batcher = platformLayer.CreateBatcher();
|
Batcher = new Batcher();
|
||||||
|
|
||||||
VirtualInputs = new List<VirtualInput>();
|
VirtualInputs = new List<VirtualInput>();
|
||||||
Input.[Friend]Init();
|
Input.[Friend]Init();
|
||||||
|
|
|
@ -1,11 +1,88 @@
|
||||||
|
using System.Collections;
|
||||||
|
|
||||||
namespace Strawberry
|
namespace Strawberry
|
||||||
{
|
{
|
||||||
public abstract class Batcher
|
public class Batcher
|
||||||
{
|
{
|
||||||
public abstract void Draw();
|
private List<Vertex> vertices = new .() ~ delete _;
|
||||||
|
private List<uint32> indices = new .() ~ delete _;
|
||||||
|
|
||||||
protected abstract void PushQuad(Vertex a, Vertex b, Vertex c, Vertex d);
|
private uint32 vaoID;
|
||||||
protected abstract void PushTri(Vertex a, Vertex b, Vertex c);
|
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)
|
public void Rect(float x, float y, float w, float h, Color color)
|
||||||
{
|
{
|
||||||
|
|
|
@ -23,7 +23,5 @@ namespace Strawberry
|
||||||
|
|
||||||
//Graphics
|
//Graphics
|
||||||
public abstract Texture LoadTexture(String path);
|
public abstract Texture LoadTexture(String path);
|
||||||
public abstract Batcher CreateBatcher();
|
|
||||||
public abstract Shader CreateShader(ShaderDef def);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,88 +0,0 @@
|
||||||
using System.Collections;
|
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace Strawberry.SDL2
|
|
||||||
{
|
|
||||||
public class SDL2Batcher : Batcher
|
|
||||||
{
|
|
||||||
private List<Vertex> vertices = new .() ~ delete _;
|
|
||||||
private List<uint32> 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -10,7 +10,7 @@ namespace Strawberry.SDL2
|
||||||
private SDL.Surface* screen;
|
private SDL.Surface* screen;
|
||||||
private SDL.Rect screenRect;
|
private SDL.Rect screenRect;
|
||||||
private SDL.Renderer* renderer;
|
private SDL.Renderer* renderer;
|
||||||
private SDL2Shader shader;
|
private Shader shader;
|
||||||
private SDL.SDL_GameController*[] gamepads;
|
private SDL.SDL_GameController*[] gamepads;
|
||||||
private bool* keyboard;
|
private bool* keyboard;
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ namespace Strawberry.SDL2
|
||||||
SDL.GL_SetSwapInterval(1);
|
SDL.GL_SetSwapInterval(1);
|
||||||
GL.Init(=> SdlGetProcAddress);
|
GL.Init(=> SdlGetProcAddress);
|
||||||
|
|
||||||
shader = new SDL2Shader(String[2] (
|
shader = new Shader(String[2] (
|
||||||
// vertex shader
|
// vertex shader
|
||||||
"""
|
"""
|
||||||
#version 330
|
#version 330
|
||||||
|
@ -167,7 +167,7 @@ namespace Strawberry.SDL2
|
||||||
public override Texture LoadTexture(String path)
|
public override Texture LoadTexture(String path)
|
||||||
{
|
{
|
||||||
var surface = SDLImage.Load(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);
|
SDL.FreeSurface(surface);
|
||||||
|
|
||||||
return tex;
|
return tex;
|
||||||
|
@ -210,15 +210,5 @@ namespace Strawberry.SDL2
|
||||||
else
|
else
|
||||||
return val / 32768f;
|
return val / 32768f;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Batcher CreateBatcher()
|
|
||||||
{
|
|
||||||
return new SDL2Batcher();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Shader CreateShader(ShaderDef def)
|
|
||||||
{
|
|
||||||
return new SDL2Shader(def);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,12 +1,53 @@
|
||||||
namespace Strawberry
|
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)
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,24 @@
|
||||||
namespace Strawberry
|
namespace Strawberry
|
||||||
{
|
{
|
||||||
public abstract class Texture
|
public class Texture
|
||||||
{
|
{
|
||||||
|
public uint32 Handle { get; private set; }
|
||||||
public int Width { get; private set; }
|
public int Width { get; private set; }
|
||||||
public int Height { 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;
|
Width = width;
|
||||||
Height = height;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,7 +88,7 @@ namespace Strawberry
|
||||||
|
|
||||||
static public void Log(StringView str, params Object[] args)
|
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);
|
Log(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user