Change folder structure, add solution to the root.
This commit is contained in:
49
DaggerFramework/Source/Rendering/ColorRectShader.cs
Executable file
49
DaggerFramework/Source/Rendering/ColorRectShader.cs
Executable file
@@ -0,0 +1,49 @@
|
||||
namespace DaggerFramework.Rendering
|
||||
{
|
||||
public class ColorRectShader : Shader
|
||||
{
|
||||
public override string FragmentSource => @"
|
||||
#version 450
|
||||
layout(location = 0) out vec4 fsout_Color;
|
||||
layout (set = 0, binding = 0) uniform Uniforms
|
||||
{
|
||||
vec4 color;
|
||||
};
|
||||
|
||||
void main()
|
||||
{
|
||||
fsout_Color = color;
|
||||
}
|
||||
";
|
||||
|
||||
public override string VertexSource => @"
|
||||
#version 450
|
||||
layout(location = 0) in vec2 Position;
|
||||
|
||||
// uniform MatrixBlock
|
||||
// {
|
||||
// // mat4 model = mat4();
|
||||
// // mat4 view;
|
||||
// // mat4 proj;
|
||||
// mat4 transform = mat4(1.0, 1.0, 1.0, 1.0,
|
||||
// 1.0, 1.0, 1.0, 1.0,
|
||||
// 1.0, 1.0, 1.0, 1.0,
|
||||
// 1.0, 1.0, 1.0, 1.0);
|
||||
// };
|
||||
|
||||
// mat4 transform = mat4(1.0, 1.0, 0.0, 1.0,
|
||||
// 0.0, 1.0, 1.0, 1.0,
|
||||
// 0.0, 0.0, 1.0, 1.0,
|
||||
// 0.0, 0.0, 0.0, 1.0);
|
||||
|
||||
mat4 transform = mat4(1.0);
|
||||
|
||||
void main()
|
||||
{
|
||||
// gl_Position = proj * view * model * vec4(Position, 0, 1);
|
||||
// gl_Position = vec4(Position, 0, 1);
|
||||
gl_Position = transform * vec4(Position, 0, 1);
|
||||
}";
|
||||
|
||||
}
|
||||
}
|
||||
164
DaggerFramework/Source/Rendering/GlRenderer.cs
Normal file
164
DaggerFramework/Source/Rendering/GlRenderer.cs
Normal file
@@ -0,0 +1,164 @@
|
||||
using System.Drawing;
|
||||
using System.Numerics;
|
||||
|
||||
using Silk.NET.GLFW;
|
||||
using Silk.NET.OpenGL;
|
||||
|
||||
namespace DaggerFramework.Rendering
|
||||
{
|
||||
/// <summary>
|
||||
/// A standard, OpenGL-based renderer.
|
||||
/// </summary>
|
||||
public class StandardRenderer : Renderer
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override Vector2 WindowSize => throw new NotImplementedException();
|
||||
/// <inheritdoc />
|
||||
public override bool ShouldRun => throw new NotImplementedException();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Initialize(RendererSettings settings)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void CreateAndInitialize(WindowSettings windowSettings, RendererSettings renderSettings)
|
||||
{
|
||||
CreateWindow(windowSettings);
|
||||
Initialize(renderSettings);
|
||||
}
|
||||
|
||||
public override void BeginFrame()
|
||||
{
|
||||
_glfw.PollEvents();
|
||||
_gl.Viewport(new Size((int)_windowSize.X, (int)_windowSize.Y));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void EndFrame()
|
||||
{
|
||||
// throw new NotImplementedException();
|
||||
EndFrameUnsafe();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void ClearBackground(Color color)
|
||||
{
|
||||
_gl.ClearColor(color.ToSystemColor());
|
||||
_gl.Clear((uint)ClearBufferMask.ColorBufferBit);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void CreateWindow(string title, Vector2 size) => CreateWindowUnsafe(title, size);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Shutdown()
|
||||
{
|
||||
CloseWindowUnsafe();
|
||||
_glfw.Terminate();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void DrawCircle(float radius, Color color)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void DrawDebugText(string text, int fontSize, Color color)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void DrawRectangle(Vector2 size, Color color)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void DrawTexture(int id, Color tint)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override double GetFrameTime()
|
||||
{
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int LoadTexture(Texture2d texture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SetTargetFps(int fps)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SetTransform(Vector2 position, float rotation = 0)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SetTransform(Matrix4x4 transform)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SetWindowTitle(string title)
|
||||
{
|
||||
SetWindowTitleUnsafe(title);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void SetWindowVSync(bool value)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool WindowShouldClose() => WindowShouldCloseUnsafe();
|
||||
|
||||
private unsafe void CreateWindowUnsafe(string title, Vector2 size)
|
||||
{
|
||||
_glfw = GlfwProvider.GLFW.Value;
|
||||
_glfw.Init();
|
||||
|
||||
_glfw.WindowHint(WindowHintInt.ContextVersionMajor, 4);
|
||||
_glfw.WindowHint(WindowHintInt.ContextVersionMinor, 6);
|
||||
_windowHandle = _glfw.CreateWindow((int)size.X, (int)size.Y, title, null, null);
|
||||
|
||||
_glfw.MakeContextCurrent(_windowHandle);
|
||||
_gl = GL.GetApi(_glfw.GetProcAddress);
|
||||
_glfw.SwapInterval(1);
|
||||
|
||||
_windowSize = size;
|
||||
}
|
||||
private unsafe void CloseWindowUnsafe() => _glfw.DestroyWindow(_windowHandle);
|
||||
private unsafe bool WindowShouldCloseUnsafe() => _glfw.WindowShouldClose(_windowHandle);
|
||||
private unsafe void SetWindowTitleUnsafe(string title) => _glfw.SetWindowTitle(_windowHandle, title);
|
||||
private unsafe void EndFrameUnsafe()
|
||||
{
|
||||
_glfw.SwapBuffers(_windowHandle);
|
||||
}
|
||||
|
||||
public override void DrawSdfText(string text, int fontSize, Color color)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private GL _gl;
|
||||
private Glfw _glfw;
|
||||
private unsafe WindowHandle* _windowHandle;
|
||||
private Vector2 _windowSize;
|
||||
}
|
||||
}
|
||||
162
DaggerFramework/Source/Rendering/RaylibRenderer.cs
Executable file
162
DaggerFramework/Source/Rendering/RaylibRenderer.cs
Executable file
@@ -0,0 +1,162 @@
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
using Raylib_cs;
|
||||
|
||||
namespace DaggerFramework.Rendering
|
||||
{
|
||||
public class RaylibRenderer : Renderer
|
||||
{
|
||||
public override Vector2 WindowSize => _windowSize;
|
||||
public override bool ShouldRun => !WindowShouldClose();
|
||||
|
||||
public override void CreateWindow(string title, Vector2 size)
|
||||
{
|
||||
Raylib.SetTraceLogLevel(TraceLogLevel.LOG_NONE);
|
||||
_windowSize = size;
|
||||
Raylib.InitWindow((int)_windowSize.X, (int)_windowSize.Y, title);
|
||||
}
|
||||
|
||||
public override void SetWindowTitle(string title)
|
||||
{
|
||||
Raylib.SetWindowTitle(title);
|
||||
}
|
||||
|
||||
public override void SetWindowVSync(bool value)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void SetTargetFps(int fps)
|
||||
{
|
||||
Raylib.SetTargetFPS(fps);
|
||||
}
|
||||
|
||||
public override bool WindowShouldClose()
|
||||
{
|
||||
return Raylib.WindowShouldClose();
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
Raylib.CloseWindow();
|
||||
}
|
||||
|
||||
public override void BeginFrame()
|
||||
{
|
||||
Raylib.BeginDrawing();
|
||||
}
|
||||
|
||||
public override void EndFrame()
|
||||
{
|
||||
Raylib.EndDrawing();
|
||||
}
|
||||
|
||||
public override void ClearBackground(Color color)
|
||||
{
|
||||
Raylib.ClearBackground(DaggerColorToRaylibColor(color));
|
||||
}
|
||||
|
||||
public override double GetFrameTime()
|
||||
{
|
||||
return (double)Raylib.GetFrameTime();
|
||||
}
|
||||
|
||||
public override void DrawCircle(float radius, Color color)
|
||||
{
|
||||
Raylib.DrawCircle((int)_position.X, (int)_position.Y, radius, DaggerColorToRaylibColor(color));
|
||||
}
|
||||
|
||||
public override void SetTransform(Vector2 position, float rotation)
|
||||
{
|
||||
_position = position;
|
||||
_rotation = rotation;
|
||||
}
|
||||
|
||||
public override int LoadTexture(Texture2d texture)
|
||||
{
|
||||
Image image = new Image();
|
||||
|
||||
unsafe
|
||||
{
|
||||
fixed (void* dataPtr = texture.Buffer)
|
||||
{
|
||||
image.data = dataPtr;
|
||||
}
|
||||
}
|
||||
|
||||
image.width = texture.Width;
|
||||
image.height = texture.Height;
|
||||
image.mipmaps = 1;
|
||||
image.format = PixelFormat.PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
|
||||
|
||||
Texture2D rayTexture;
|
||||
|
||||
rayTexture = Raylib.LoadTextureFromImage(image);
|
||||
|
||||
_texturePool.Add(rayTexture);
|
||||
|
||||
return _texturePool.Count - 1;
|
||||
}
|
||||
|
||||
public override void DrawTexture(int id, Color tint)
|
||||
{
|
||||
Raylib.DrawTextureV(_texturePool[id], _position, DaggerColorToRaylibColor(tint));
|
||||
}
|
||||
|
||||
public override void DrawRectangle(Vector2 size, Color color)
|
||||
{
|
||||
// Raylib.DrawRectangleV(_position, size, SystemColorToRaylibColor(color));
|
||||
Raylib.DrawRectanglePro(new Rectangle()
|
||||
{
|
||||
x = _position.X,
|
||||
y = _position.Y,
|
||||
width = size.X,
|
||||
height = size.Y
|
||||
}, Vector2.Zero, _rotation, DaggerColorToRaylibColor(color));
|
||||
}
|
||||
|
||||
public override void DrawDebugText(string text, int fontSize, Color color)
|
||||
{
|
||||
Raylib.DrawText(text, (int)_position.X, (int)_position.Y, fontSize, DaggerColorToRaylibColor(color));
|
||||
}
|
||||
|
||||
public override void DrawSdfText(string text, int fontSize, Color color)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void Initialize(RendererSettings settings)
|
||||
{
|
||||
ConfigFlags flags = 0;
|
||||
|
||||
// MSAA
|
||||
flags |= settings.Msaa == Msaa.Msaa4x ? ConfigFlags.FLAG_MSAA_4X_HINT : 0;
|
||||
|
||||
// VSync
|
||||
flags |= settings.UseVSync ? ConfigFlags.FLAG_VSYNC_HINT : 0;
|
||||
|
||||
// Fullscreen
|
||||
flags |= settings.Fullscreen ? ConfigFlags.FLAG_FULLSCREEN_MODE : 0;
|
||||
Raylib.SetConfigFlags(flags);
|
||||
}
|
||||
|
||||
// TODO
|
||||
public override void SetTransform(Matrix4x4 transform) { }
|
||||
|
||||
public override void CreateAndInitialize(WindowSettings windowSettings, RendererSettings renderSettings)
|
||||
{
|
||||
CreateWindow(windowSettings);
|
||||
Initialize(renderSettings);
|
||||
}
|
||||
|
||||
private Raylib_cs.Color DaggerColorToRaylibColor(Color color)
|
||||
{
|
||||
return new Raylib_cs.Color { r = (byte)Math.Round(color.R * 255f), g = (byte)Math.Round(color.G * 255f), b = (byte)Math.Round(color.B * 255f), a = (byte)Math.Round(color.A * 255f) };
|
||||
}
|
||||
|
||||
private List<Texture2D> _texturePool = new List<Texture2D>();
|
||||
private Vector2 _position;
|
||||
private float _rotation;
|
||||
private Vector2 _windowSize;
|
||||
}
|
||||
}
|
||||
150
DaggerFramework/Source/Rendering/Renderer.cs
Executable file
150
DaggerFramework/Source/Rendering/Renderer.cs
Executable file
@@ -0,0 +1,150 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace DaggerFramework.Rendering
|
||||
{
|
||||
/// <summary>
|
||||
/// An abstract class representing the graphics renderer.
|
||||
/// </summary>
|
||||
public abstract class Renderer
|
||||
{
|
||||
// INIT
|
||||
/// <summary>
|
||||
/// Creates the renderer window and initializes internal resources.
|
||||
/// </summary>
|
||||
/// <param name="windowSettings">Settings for the rendering window.</param>
|
||||
/// <param name="renderSettings">Rendering settings.</param>
|
||||
public abstract void CreateAndInitialize(WindowSettings windowSettings, RendererSettings renderSettings);
|
||||
/// <summary>
|
||||
/// Initializes internal resources. Should be called before other methods.
|
||||
/// </summary>
|
||||
/// <param name="settings">Rendering settings.</param>
|
||||
public abstract void Initialize(RendererSettings settings);
|
||||
|
||||
// UTIL
|
||||
/// <summary>
|
||||
/// Indicates if the renderer will render the next frame.
|
||||
/// </summary>
|
||||
public abstract bool ShouldRun { get; }
|
||||
|
||||
// WINDOW
|
||||
/// <summary>
|
||||
/// The size of the render window.
|
||||
/// </summary>
|
||||
public abstract Vector2 WindowSize { get; }
|
||||
/// <summary>
|
||||
/// Creates the window with a given title and size.
|
||||
/// </summary>
|
||||
/// <param name="title">Title of the window.</param>
|
||||
/// <param name="size">Vector2 representing size.</param>
|
||||
public abstract void CreateWindow(string title, Vector2 size);
|
||||
public void CreateWindow(WindowSettings windowSettings)
|
||||
{
|
||||
CreateWindow(windowSettings.Title, windowSettings.Size);
|
||||
}
|
||||
|
||||
// TODO: use properties for these.
|
||||
public abstract void SetWindowTitle(string title);
|
||||
public abstract void SetWindowVSync(bool value);
|
||||
public abstract void SetTargetFps(int fps);
|
||||
public abstract bool WindowShouldClose();
|
||||
public abstract void Shutdown();
|
||||
|
||||
// DRAWING
|
||||
/// <summary>
|
||||
/// Prepares the renderer for drawing the next frame.
|
||||
/// </summary>
|
||||
public abstract void BeginFrame();
|
||||
/// <summary>
|
||||
/// Ends rendering of the frame.
|
||||
/// </summary>
|
||||
public abstract void EndFrame();
|
||||
/// <summary>
|
||||
/// Clears the render canvas and sets a background color.
|
||||
/// </summary>
|
||||
/// <param name="color">Background color.</param>
|
||||
public abstract void ClearBackground(Color color);
|
||||
public abstract double GetFrameTime();
|
||||
|
||||
/// <summary>
|
||||
/// Loads the texture onto the GPU for later use in DrawTexture or other Texture related methods.
|
||||
/// </summary>
|
||||
/// <param name="texture">Texture to load.</param>
|
||||
/// <returns>A texture handler on the GPU.</returns>
|
||||
public abstract int LoadTexture(Texture2d texture);
|
||||
/// <summary>
|
||||
/// Sets transforms for the next draw operation.
|
||||
/// </summary>
|
||||
/// <param name="position">Global transform position.</param>
|
||||
/// <param name="rotation">Rotation.</param>
|
||||
public abstract void SetTransform(Vector2 position, float rotation = 0.0f);
|
||||
/// <summary>
|
||||
/// Sets the transform for the next draw operation.
|
||||
/// </summary>
|
||||
/// <param name="transform">Transform matrix.</param>
|
||||
public abstract void SetTransform(Matrix4x4 transform);
|
||||
/// <summary>
|
||||
/// Draws a filled circle.
|
||||
/// </summary>
|
||||
/// <param name="radius">Radius of a circle.</param>
|
||||
/// <param name="color">Fill color.</param>
|
||||
public abstract void DrawCircle(float radius, Color color);
|
||||
/// <summary>
|
||||
/// Draws a filled rectangle.
|
||||
/// </summary>
|
||||
/// <param name="size">Rectangle size.</param>
|
||||
/// <param name="color">Fill color.</param>
|
||||
public abstract void DrawRectangle(Vector2 size, Color color);
|
||||
/// <summary>
|
||||
/// Draws a debug text with a default font.
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <param name="text"></param>
|
||||
/// <param name="fontSize"></param>
|
||||
/// <param name="color"></param>
|
||||
public abstract void DrawDebugText(string text, int fontSize, Color color);
|
||||
/// <summary>
|
||||
/// Draws text using a signed distance field font atlas.
|
||||
/// </summary>
|
||||
/// <param name="text">Text to draw.</param>
|
||||
/// <param name="fontSize">Size of the font.</param>
|
||||
/// <param name="color">Color of the text.</param>
|
||||
public abstract void DrawSdfText(string text, int fontSize, Color color);
|
||||
/// <summary>
|
||||
/// Draws the texture.
|
||||
/// </summary>
|
||||
/// <param name="id">Texture handle.</param>
|
||||
/// <param name="tint">Texture tint.</param>
|
||||
public abstract void DrawTexture(int id, Color tint);
|
||||
}
|
||||
|
||||
public enum Msaa
|
||||
{
|
||||
None,
|
||||
Msaa2x,
|
||||
Msaa4x,
|
||||
Msaa8x
|
||||
}
|
||||
public struct RendererSettings
|
||||
{
|
||||
public Msaa Msaa;
|
||||
public bool UseVSync;
|
||||
public bool Fullscreen;
|
||||
|
||||
public static RendererSettings Default => new RendererSettings()
|
||||
{
|
||||
UseVSync = true,
|
||||
};
|
||||
}
|
||||
|
||||
public struct WindowSettings
|
||||
{
|
||||
public string Title;
|
||||
public Vector2 Size = new Vector2(640, 480);
|
||||
|
||||
public WindowSettings(string title, Vector2 size)
|
||||
{
|
||||
Title = title;
|
||||
Size = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
8
DaggerFramework/Source/Rendering/Shader.cs
Executable file
8
DaggerFramework/Source/Rendering/Shader.cs
Executable file
@@ -0,0 +1,8 @@
|
||||
namespace DaggerFramework.Rendering
|
||||
{
|
||||
public abstract class Shader
|
||||
{
|
||||
public abstract string FragmentSource { get; }
|
||||
public abstract string VertexSource { get; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user