Files
Voile/Source/Rendering/RaylibRenderer.cs
2023-02-28 20:58:31 +01:00

160 lines
4.7 KiB
C#
Executable File

using System.Numerics;
using System.Text;
using Raylib_cs;
namespace DaggerFramework.Rendering
{
public class RaylibRenderer : Renderer
{
public override Vector2 WindowSize => _windowSize;
public override void DrawCircle(float radius, System.Drawing.Color color)
{
Raylib.DrawCircle((int)_position.X, (int)_position.Y, radius, SystemColorToRaylibColor(color));
}
public override void SetTransform(Vector2 position, float rotation)
{
_position = position;
_rotation = rotation;
}
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 CloseWindow()
{
Raylib.CloseWindow();
}
public override void BeginFrame()
{
Raylib.BeginDrawing();
}
public override void EndFrame()
{
Raylib.EndDrawing();
}
public override void ClearBackground(System.Drawing.Color color)
{
Raylib.ClearBackground(SystemColorToRaylibColor(color));
}
public override double GetFrameTime()
{
return (double)Raylib.GetFrameTime();
}
private Raylib_cs.Color SystemColorToRaylibColor(System.Drawing.Color color)
{
return new Color { r = color.R, g = color.G, b = color.B, a = color.A };
}
public override int LoadTexture(Texture2d texture)
{
Image image;
Texture2D rayTexture;
string ext = "png";
byte[] extBytes = Encoding.ASCII.GetBytes(ext);
// TODO: This can cause memory leaks.
unsafe
{
fixed (byte* textureData = texture.Data)
{
fixed (byte* bExt = extBytes)
{
sbyte* sbExt = (sbyte*)*bExt;
image = Raylib.LoadImageFromMemory(sbExt, textureData, texture.Data.Length);
}
}
}
rayTexture = Raylib.LoadTextureFromImage(image);
Raylib.UnloadImage(image);
_texturePool.Add(rayTexture);
return _texturePool.Count - 1;
}
public override int LoadTexture(string path)
{
Image image = Raylib.LoadImage(path);
var rayTexture = Raylib.LoadTextureFromImage(image);
Raylib.UnloadImage(image);
_texturePool.Add(rayTexture);
return _texturePool.Count - 1;
}
public override void DrawTexture(int id, System.Drawing.Color tint)
{
Raylib.DrawTextureV(_texturePool[id], _position, SystemColorToRaylibColor(tint));
}
public override void DrawRectangle(Vector2 size, System.Drawing.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, SystemColorToRaylibColor(color));
}
public override void DrawDebugText(Vector2 position, string text, int fontSize, System.Drawing.Color color)
{
Raylib.DrawText(text, (int)position.X, (int)position.Y, fontSize, SystemColorToRaylibColor(color));
}
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) { }
private List<Texture2D> _texturePool = new List<Texture2D>();
private Vector2 _position;
private float _rotation;
private Vector2 _windowSize;
}
}