I'm developing a 3D game engine in C# and I was just wondering what are the different ways I can approach designing a graphics API abstraction.
My original design was going to be something like this:
public interface IVertexBuffer : IDisposable
{
void Bind();
}
#if DRIVER_TYPE_OPENGL_45
public sealed class OpenGLVertexBuffer : IVertexBuffer
{
public void Bind()
{
// Bind buffer...
}
public void Dispose()
{
// Delete buffer...
}
}
#endif
#if DRIVER_TYPE_OPENGL_ES30
public sealed class OpenGLESVertexBuffer : IVertexBuffer
{
public void Bind()
{
// Bind buffer...
}
public void Dispose()
{
// Delete buffer...
}
}
#endif
In C#, sadly pre-processors don't work the same way they do in C++, so I would have to compile two versions of my project, but that just seems hacky and I would much rather be able to switch between drivers types.
So, I had another idea to do something like this:
public interface IRenderDevice
{
IVertexBuffer CreateVertexBuffer(float[] vertexData, int sizeInBytes);
void DestroyVertexBuffer(IVertexBuffer vertexBuffer);
}
public sealed class OpenGLRenderDevice : IRenderDevice
{
public IVertexBuffer CreateVertexBuffer(float[] vertexData, int sizeInBytes)
{
// Create vertex buffer
}
public void DestroyVertexBuffer(IVertexBuffer vertexBuffer)
{
// Destroy vertex buffer, or should I just call vertexBuffer.Dispose()?
}
}
and then just use an enum to determine which render device to create, but I know this solution will quickly get out of hand when I begin to introduce other things like vertex arrays, index buffers, frame buffers, etc.
What other options do I have that will still maintain readable code and look native to C#?