-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c504dd2
commit 335aa64
Showing
6 changed files
with
125 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
|
||
namespace Pulsar4X.Client.Rendering; | ||
|
||
public enum RendererType | ||
{ | ||
OpenGL, | ||
// TODO: someone can write these renderers if they desire :D | ||
//Vulkan, | ||
//DirectX | ||
} | ||
|
||
public interface IRenderer : IDisposable | ||
{ | ||
void Initialize(IntPtr windowHandle); | ||
void BeginFrame(); | ||
void EndFrame(); | ||
void Clear(float r, float g, float b, float a); | ||
|
||
int CreateDefaultFontTexture(int width, int height, IntPtr pixels); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System; | ||
using ImGuiSDL2CS; | ||
using SDL2; | ||
|
||
namespace Pulsar4X.Client.Rendering; | ||
|
||
public class OpenGLRenderer : IRenderer | ||
{ | ||
private IntPtr _glContext; | ||
private IntPtr _windowHandle; | ||
|
||
public void Initialize(IntPtr windowHandle) | ||
{ | ||
_windowHandle = windowHandle; | ||
|
||
// Set the OpenGL context attributes | ||
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, 4); | ||
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, 5); | ||
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, (int)SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE); | ||
|
||
// Create the OpenGL context | ||
_glContext = SDL.SDL_GL_CreateContext(_windowHandle); | ||
if(_glContext == IntPtr.Zero) | ||
{ | ||
throw new Exception($"Failed to create OpenGL context: {SDL.SDL_GetError()}"); | ||
} | ||
|
||
// Make the OpenGL context current | ||
SDL.SDL_GL_MakeCurrent(_windowHandle, _glContext); | ||
} | ||
|
||
public void BeginFrame() | ||
{ | ||
GL.Clear(GL.Enum.GL_COLOR_BUFFER_BIT); | ||
} | ||
|
||
public void EndFrame() | ||
{ | ||
SDL.SDL_GL_SwapWindow(_windowHandle); | ||
} | ||
|
||
public void Clear(float r, float g, float b, float a) | ||
{ | ||
GL.ClearColor(r, g, b, a); | ||
} | ||
|
||
public int CreateDefaultFontTexture(int width, int height, IntPtr pixels) | ||
{ | ||
// Create OpenGL texture | ||
GL.GenTextures(1, out int fontTextureID); | ||
GL.BindTexture(GL.Enum.GL_TEXTURE_2D, fontTextureID); | ||
GL.TexParameteri(GL.Enum.GL_TEXTURE_2D, GL.Enum.GL_TEXTURE_MIN_FILTER, (int) GL.Enum.GL_LINEAR); | ||
GL.TexParameteri(GL.Enum.GL_TEXTURE_2D, GL.Enum.GL_TEXTURE_MAG_FILTER, (int) GL.Enum.GL_LINEAR); | ||
GL.PixelStorei(GL.Enum.GL_UNPACK_ROW_LENGTH, 0); | ||
GL.TexImage2D( | ||
GL.Enum.GL_TEXTURE_2D, | ||
0, | ||
(int) GL.Enum.GL_ALPHA, | ||
width, | ||
height, | ||
0, | ||
GL.Enum.GL_ALPHA, | ||
GL.Enum.GL_UNSIGNED_BYTE, | ||
pixels | ||
); | ||
|
||
return fontTextureID; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if(_glContext != IntPtr.Zero) | ||
{ | ||
SDL.SDL_GL_DeleteContext(_glContext); | ||
_glContext = IntPtr.Zero; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
|
||
namespace Pulsar4X.Client.Rendering; | ||
|
||
public static class RendererFactory | ||
{ | ||
public static IRenderer CreateRenderer(RendererType rendererType) | ||
{ | ||
return rendererType switch | ||
{ | ||
RendererType.OpenGL => new OpenGLRenderer(), | ||
_ => throw new ArgumentException("Unsupported renderer backend") | ||
}; | ||
} | ||
} |