Add Color record struct, small refactor, create a DaggerFramework.SceneGraph namespace.

This commit is contained in:
2023-06-16 00:03:45 +02:00
parent 964b903500
commit 7d5c5f822b
25 changed files with 217 additions and 111 deletions

110
Source/Color.cs Normal file
View File

@@ -0,0 +1,110 @@
namespace DaggerFramework
{
// Based on https://github.com/ppr-game/PPR/blob/engine/PER.Util/src/Color.cs
/// <summary>
/// A record struct representing a color.
/// </summary>
public record struct Color
{
// TODO: add more HTML colors.
public static Color AliceBlue = new(0xF0F8FF);
public static Color AntiqueWhite = new(0xFAEBD7);
public static Color Aqua = new(0x00FFFF);
public static Color Aquamarine = new(0x7FFFD4);
public static Color Azure = new(0xF0FFFF);
public static Color Beige = new(0xF5F5DC);
public static Color Bisque = new(0xFFE4C4);
public static Color Black = new(0x000000);
public static Color BlanchedAlmond = new(0xFFEBCD);
public static Color Blue = new(0x0000FF);
public static Color BlueViolet = new(0x8A2BE2);
public static Color Brown = new(0xA52A2A);
public static Color BurlyWood = new(0xDEB887);
public static Color CadetBlue = new(0x5F9EA0);
public static Color Chartreuse = new(0x7FFF00);
public static Color Chocolate = new(0xD2691E);
public static Color Coral = new(0xFF7F50);
public static Color CornflowerBlue = new(0x6495ED);
public static Color Cornsilk = new(0xFFF8DC);
public static Color Crimson = new(0xDC143C);
public static Color Cyan = new(0x00FFFF);
public static Color DarkBlue = new(0x00008B);
public static Color DarkCyan = new(0x008B8B);
public static Color White = new(0xFFFFFF);
public static Color Green = new(0x00FF00);
public static Color Red = new(0xFF0000);
public float R { get; set; }
public float G { get; set; }
public float B { get; set; }
public float A { get; set; }
public int Argb
{
get
{
int c = (ushort)Math.Round(A * 255f);
c <<= 8;
c |= (ushort)Math.Round(R * 255f);
c <<= 8;
c |= (ushort)Math.Round(G * 255f);
c <<= 8;
c |= (ushort)Math.Round(B * 255f);
return c;
}
}
public Color(float r, float g, float b, float a)
{
R = r;
G = g;
B = b;
A = a;
}
public Color(byte r, byte g, byte b, byte a)
{
R = r / 255f;
G = g / 255f;
B = b / 255f;
A = a / 255f;
}
public Color(int hex)
{
A = 1.0f;
B = (hex & 0xFF) / 255.0f;
hex >>= 8;
G = (hex & 0xFF) / 255.0f;
hex >>= 8;
R = (hex & 0xFF) / 255.0f;
}
public Color Lightened(float amount)
{
var result = this;
result.R = result.R + (1.0f - result.R) * amount;
result.G = result.G + (1.0f - result.G) * amount;
result.B = result.B + (1.0f - result.B) * amount;
return result;
}
public Color Darkened(float amount)
{
var result = this;
result.R = result.R * (1.0f - amount);
result.G = result.G * (1.0f - amount);
result.B = result.B * (1.0f - amount);
return result;
}
public System.Drawing.Color ToSystemColor()
{
var result = System.Drawing.Color.FromArgb(Argb);
return result;
}
}
}

View File

@@ -1,15 +0,0 @@
using DaggerFramework.Rendering;
namespace DaggerFramework
{
public abstract class Drawable2d : Entity2d, IDrawable
{
public void Draw(in Renderer renderer)
{
renderer.SetTransform(position);
OnDraw(in renderer);
}
public abstract void OnDraw(in Renderer renderer);
}
}

View File

@@ -1,10 +0,0 @@
using System.Numerics;
namespace DaggerFramework
{
public class Entity2d : Entity
{
public Vector2 Position { get => position; set => position = value; }
protected Vector2 position;
}
}

View File

@@ -1,9 +0,0 @@
using DaggerFramework.Rendering;
namespace DaggerFramework
{
public interface IDrawable
{
public void Draw(in Renderer renderer);
}
}

View File

@@ -36,7 +36,7 @@ namespace DaggerFramework.Rendering
public override void ClearBackground(Color color)
{
_gl.ClearColor(color);
_gl.ClearColor(color.ToSystemColor());
_gl.Clear((uint)ClearBufferMask.ColorBufferBit);
}

View File

@@ -51,9 +51,9 @@ namespace DaggerFramework.Rendering
Raylib.EndDrawing();
}
public override void ClearBackground(System.Drawing.Color color)
public override void ClearBackground(Color color)
{
Raylib.ClearBackground(SystemColorToRaylibColor(color));
Raylib.ClearBackground(DaggerColorToRaylibColor(color));
}
public override double GetFrameTime()
@@ -61,9 +61,9 @@ namespace DaggerFramework.Rendering
return (double)Raylib.GetFrameTime();
}
public override void DrawCircle(float radius, System.Drawing.Color color)
public override void DrawCircle(float radius, Color color)
{
Raylib.DrawCircle((int)_position.X, (int)_position.Y, radius, SystemColorToRaylibColor(color));
Raylib.DrawCircle((int)_position.X, (int)_position.Y, radius, DaggerColorToRaylibColor(color));
}
public override void SetTransform(Vector2 position, float rotation)
@@ -98,12 +98,12 @@ namespace DaggerFramework.Rendering
return _texturePool.Count - 1;
}
public override void DrawTexture(int id, System.Drawing.Color tint)
public override void DrawTexture(int id, Color tint)
{
Raylib.DrawTextureV(_texturePool[id], _position, SystemColorToRaylibColor(tint));
Raylib.DrawTextureV(_texturePool[id], _position, DaggerColorToRaylibColor(tint));
}
public override void DrawRectangle(Vector2 size, System.Drawing.Color color)
public override void DrawRectangle(Vector2 size, Color color)
{
// Raylib.DrawRectangleV(_position, size, SystemColorToRaylibColor(color));
Raylib.DrawRectanglePro(new Rectangle()
@@ -112,15 +112,15 @@ namespace DaggerFramework.Rendering
y = _position.Y,
width = size.X,
height = size.Y
}, Vector2.Zero, _rotation, SystemColorToRaylibColor(color));
}, Vector2.Zero, _rotation, DaggerColorToRaylibColor(color));
}
public override void DrawDebugText(Vector2 position, string text, int fontSize, System.Drawing.Color color)
public override void DrawDebugText(Vector2 position, string text, int fontSize, Color color)
{
Raylib.DrawText(text, (int)position.X, (int)position.Y, fontSize, SystemColorToRaylibColor(color));
Raylib.DrawText(text, (int)position.X, (int)position.Y, fontSize, DaggerColorToRaylibColor(color));
}
public override void DrawSdfText(Vector2 position, string text, int fontSize, System.Drawing.Color color)
public override void DrawSdfText(Vector2 position, string text, int fontSize, Color color)
{
throw new NotImplementedException();
}
@@ -149,9 +149,9 @@ namespace DaggerFramework.Rendering
Initialize(renderSettings);
}
private Raylib_cs.Color SystemColorToRaylibColor(System.Drawing.Color color)
private Raylib_cs.Color DaggerColorToRaylibColor(Color color)
{
return new Color { r = color.R, g = color.G, b = color.B, a = color.A };
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>();

View File

@@ -1,4 +1,3 @@
using System.Drawing;
using System.Numerics;
namespace DaggerFramework.Rendering

View File

@@ -1,20 +1,18 @@
using System.Drawing;
using DaggerFramework.Rendering;
namespace DaggerFramework
namespace DaggerFramework.SceneGraph
{
public class CircleShape2d : Drawable2d
{
public float Radius { get => _radius; set => _radius = value; }
public Color Color { get => _color; set => _color = value; }
public override void OnDraw(in Renderer renderer)
public override void OnDraw(Renderer renderer)
{
renderer.DrawCircle(_radius, _color);
}
private float _radius;
private System.Drawing.Color _color;
private Color _color;
}
}

View File

@@ -0,0 +1,15 @@
using DaggerFramework.Rendering;
namespace DaggerFramework.SceneGraph
{
public abstract class Drawable2d : Entity2d, IDrawable
{
public void Draw(Renderer renderer)
{
renderer.SetTransform(Position);
OnDraw(renderer);
}
public abstract void OnDraw(Renderer renderer);
}
}

View File

@@ -1,6 +1,6 @@
using DaggerFramework.Audio;
namespace DaggerFramework
namespace DaggerFramework.SceneGraph
{
public class Entity
{

View File

@@ -0,0 +1,9 @@
using System.Numerics;
namespace DaggerFramework.SceneGraph
{
public class Entity2d : Entity
{
public Vector2 Position { get; set; }
}
}

View File

@@ -0,0 +1,9 @@
using DaggerFramework.Rendering;
namespace DaggerFramework.SceneGraph
{
public interface IDrawable
{
public void Draw(Renderer renderer);
}
}

View File

@@ -2,7 +2,7 @@ using System.Drawing;
using System.Numerics;
using DaggerFramework.Rendering;
namespace DaggerFramework
namespace DaggerFramework.SceneGraph
{
// TODO: add oneshot parameter.
public class Particles2d : Drawable2d
@@ -21,7 +21,7 @@ namespace DaggerFramework
CleanupParticles();
InitializeParticles();
}
public override void OnDraw(in Renderer renderer)
public override void OnDraw(Renderer renderer)
{
foreach (var particle in _particles)
{

View File

@@ -2,7 +2,7 @@ using System.Drawing;
using DaggerFramework.Rendering;
namespace DaggerFramework
namespace DaggerFramework.SceneGraph
{
public class Sprite2d : Drawable2d
{
@@ -14,7 +14,7 @@ namespace DaggerFramework
_texId = renderer.LoadTexture(_texture);
}
public override void OnDraw(in Renderer renderer)
public override void OnDraw(Renderer renderer)
{
renderer.DrawTexture(_texId, Color.White);
}

View File

@@ -1,7 +1,7 @@
using DaggerFramework.Rendering;
using System.Drawing;
namespace DaggerFramework
namespace DaggerFramework.SceneGraph
{
public class Text2d : Drawable2d
{
@@ -9,9 +9,9 @@ namespace DaggerFramework
public int FontSize { get => _fontSize; set => _fontSize = value; }
public Color FontColor { get => _fontColor; set => _fontColor = value; }
public override void OnDraw(in Renderer renderer)
public override void OnDraw(Renderer renderer)
{
renderer.DrawDebugText(position, _contents, _fontSize, _fontColor);
renderer.DrawDebugText(Position, _contents, _fontSize, _fontColor);
}
private string _contents = string.Empty;

View File

@@ -1,6 +1,6 @@
using DaggerFramework.Rendering;
namespace DaggerFramework
namespace DaggerFramework.SceneGraph
{
public class EntityLayer : Layer
{
@@ -45,11 +45,11 @@ namespace DaggerFramework
}
}
protected override void OnDraw(in Renderer renderer)
protected override void OnDraw(Renderer renderer)
{
foreach (IDrawable drawable in _entities)
{
drawable.Draw(in renderer);
drawable.Draw(renderer);
}
}

View File

@@ -1,4 +1,4 @@
namespace DaggerFramework
namespace DaggerFramework.SceneGraph
{
public interface IMainLoop
{

View File

@@ -1,14 +1,13 @@
using DaggerFramework.Audio;
using DaggerFramework.Rendering;
namespace DaggerFramework
namespace DaggerFramework.SceneGraph
{
public abstract class Layer : IDrawable
{
public Scene Scene { get; set; }
public InputHandler Input { get; set; }
public void Draw(in Renderer renderer) => OnDraw(in renderer);
public void Draw(Renderer renderer) => OnDraw(renderer);
public void Start() => OnStart();
public void Update(double dt) => OnUpdate(dt);
@@ -17,6 +16,6 @@ namespace DaggerFramework
protected virtual void OnStart() { }
protected virtual void OnUpdate(double dt) { }
protected virtual void OnInput(InputHandler input) { }
protected abstract void OnDraw(in Renderer renderer);
protected abstract void OnDraw(Renderer renderer);
}
}

View File

@@ -4,7 +4,7 @@ using DaggerFramework.Audio;
using DaggerFramework.Rendering;
namespace DaggerFramework
namespace DaggerFramework.SceneGraph
{
public class Scene : IMainLoop
{
@@ -60,7 +60,7 @@ namespace DaggerFramework
Renderer.ClearBackground(Color.Black);
foreach (var layer in _layers.Values)
{
layer.Draw(in _renderer);
layer.Draw(_renderer);
}
Renderer.EndFrame();

View File

@@ -1,24 +0,0 @@
using System.Drawing;
using System.Numerics;
using DaggerFramework.Rendering;
namespace DaggerFramework
{
public class UiLayer : Layer
{
protected override void OnUpdate(double dt)
{
base.OnUpdate(dt);
_time += dt;
}
protected override void OnDraw(in Renderer renderer)
{
renderer.SetTransform(Vector2.Zero);
renderer.DrawRectangle(new Vector2(720 / 2, 1280), Color.Green);
renderer.DrawDebugText(new Vector2(720 / 4 - 24, 64), "UI :)", 24, Color.White);
}
private double _time;
}
}

View File

@@ -4,14 +4,14 @@ using Raylib_cs;
using DaggerFramework.Rendering;
namespace DaggerFramework
namespace DaggerFramework.SceneGraph
{
public class ImGuiRenderLayer : Layer
{
protected override void OnDraw(in Renderer renderer)
protected override void OnDraw(Renderer renderer)
{
Layout();
_controller.Draw(in renderer);
_controller.Draw(renderer);
}
protected virtual void Layout() { }
@@ -258,12 +258,12 @@ namespace DaggerFramework
private Color GetColor(uint hexValue)
{
Color color;
Color color = new Color();
color.r = (byte)(hexValue & 0xFF);
color.g = (byte)((hexValue >> 8) & 0xFF);
color.b = (byte)((hexValue >> 16) & 0xFF);
color.a = (byte)((hexValue >> 24) & 0xFF);
color.R = (byte)(hexValue & 0xFF) / 255f;
color.G = (byte)((hexValue >> 8) & 0xFF) / 255f;
color.B = (byte)((hexValue >> 16) & 0xFF) / 255f;
color.A = (byte)((hexValue >> 24) & 0xFF) / 255f;
return color;
}
@@ -271,7 +271,7 @@ namespace DaggerFramework
void DrawTriangleVertex(ImDrawVertPtr idxVert)
{
Color c = GetColor(idxVert.col);
Rlgl.rlColor4ub(c.r, c.g, c.b, c.a);
Rlgl.rlColor4ub((byte)Math.Round(c.R * 255f), (byte)Math.Round(c.G * 255f), (byte)Math.Round(c.B * 255f), (byte)Math.Round(c.A * 255f));
Rlgl.rlTexCoord2f(idxVert.uv.X, idxVert.uv.Y);
Rlgl.rlVertex2f(idxVert.pos.X, idxVert.pos.Y);
}
@@ -307,7 +307,7 @@ namespace DaggerFramework
Rlgl.rlEnd();
}
public void Draw(in Renderer renderer)
public void Draw(Renderer renderer)
{
ImGui.Render();
RenderCommandLists(ImGui.GetDrawData());

View File

@@ -12,7 +12,9 @@ namespace DaggerFramework
var r = (byte)Lerp(colorA.R, colorB.R, t);
var g = (byte)Lerp(colorA.G, colorB.G, t);
var b = (byte)Lerp(colorA.B, colorB.B, t);
return Color.FromArgb(r, g, b);
var a = (byte)Lerp(colorA.A, colorB.A, t);
return new Color(r, g, b, a);
}
public static Vector2 LerpVector2(Vector2 v1, Vector2 v2, double t)