From 0d907209a267c971831887ae908c75bbf3a593d7 Mon Sep 17 00:00:00 2001 From: Matt Thorson Date: Thu, 7 May 2020 20:11:20 -0700 Subject: [PATCH] Color struct. Reorg. --- src/Entity.bf | 2 +- src/Game.bf | 3 +- src/Static/Draw.bf | 16 ---- src/{Static => Util}/Calc.bf | 0 src/Util/Color.bf | 132 +++++++++++++++++++++++++++++++ src/Util/Draw.bf | 35 ++++++++ src/{Numerics => Util}/Point.bf | 0 src/{Numerics => Util}/Rect.bf | 0 src/{Static => Util}/Time.bf | 0 src/{Numerics => Util}/Vector.bf | 0 10 files changed, 170 insertions(+), 18 deletions(-) delete mode 100644 src/Static/Draw.bf rename src/{Static => Util}/Calc.bf (100%) create mode 100644 src/Util/Color.bf create mode 100644 src/Util/Draw.bf rename src/{Numerics => Util}/Point.bf (100%) rename src/{Numerics => Util}/Rect.bf (100%) rename src/{Static => Util}/Time.bf (100%) rename src/{Numerics => Util}/Vector.bf (100%) diff --git a/src/Entity.bf b/src/Entity.bf index c20980d..b421143 100644 --- a/src/Entity.bf +++ b/src/Entity.bf @@ -399,7 +399,7 @@ namespace Strawberry // ===== Misc ===== - public void DrawHitbox(SDL2.SDL.Color color) + public void DrawHitbox(Color color) { Draw.Rect(SceneHitbox, color); } diff --git a/src/Game.bf b/src/Game.bf index f2f6067..59e3c5f 100644 --- a/src/Game.bf +++ b/src/Game.bf @@ -26,6 +26,7 @@ namespace Strawberry private bool updating; public SDL.Renderer* Renderer { get; private set; } + public Color ClearColor = Color.Black; private SDL.Rect screenRect; private SDL.Window* window; @@ -175,7 +176,7 @@ namespace Strawberry public void Render() { - SDL.SetRenderDrawColor(Renderer, 0, 0, 0, 255); + SDL.SetRenderDrawColor(Renderer, ClearColor.R, ClearColor.G, ClearColor.B, ClearColor.A); SDL.RenderClear(Renderer); SDL.RenderSetScale(Renderer, WindowScale, WindowScale); Draw(); diff --git a/src/Static/Draw.bf b/src/Static/Draw.bf deleted file mode 100644 index c7fef75..0000000 --- a/src/Static/Draw.bf +++ /dev/null @@ -1,16 +0,0 @@ -namespace Strawberry -{ - static public class Draw - { - static public void Rect(int x, int y, int w, int h, SDL2.SDL.Color color) - { - SDL2.SDL.SetRenderDrawColor(Game.Renderer, color.r, color.g, color.b, color.a); - SDL2.SDL.RenderFillRect(Game.Renderer, &SDL2.SDL.Rect((int32)x, (int32)y, (int32)w, (int32)h)); - } - - static public void Rect(Rect rect, SDL2.SDL.Color color) - { - Rect(rect.X, rect.Y, rect.Width, rect.Height, color); - } - } -} diff --git a/src/Static/Calc.bf b/src/Util/Calc.bf similarity index 100% rename from src/Static/Calc.bf rename to src/Util/Calc.bf diff --git a/src/Util/Color.bf b/src/Util/Color.bf new file mode 100644 index 0000000..189d63d --- /dev/null +++ b/src/Util/Color.bf @@ -0,0 +1,132 @@ +using System; + +namespace Strawberry +{ + public struct Color + { + static public readonly Color White = Color(255, 255, 255); + static public readonly Color Black = Color(0, 0, 0); + static public readonly Color Transparent = Color(0, 0, 0, 0); + static public readonly Color Red = Color(255, 0, 0); + static public readonly Color Green = Color(0, 255, 0); + static public readonly Color Blue = Color(0, 0, 255); + + public uint8 R; + public uint8 G; + public uint8 B; + public uint8 A; + + public this(uint8 red, uint8 green, uint8 blue, uint8 alpha) + { + R = red; + G = green; + B = blue; + A = alpha; + } + + public this(uint8 red, uint8 green, uint8 blue) + : this(red, green, blue, 255) + { + + } + + public this(float red, float green, float blue, float alpha) + { + R = (uint8)(red * 255); + G = (uint8)(green * 255); + B = (uint8)(blue * 255); + A = (uint8)(alpha * 255); + } + + public this(float red, float green, float blue) + : this(red, green, blue, 1f) + { + + } + + public float Rf + { + [Inline] + get + { + return R / 255f; + } + + [Inline] + set mut + { + R = (uint8)(value * 255); + } + } + + public float Gf + { + [Inline] + get + { + return G / 255f; + } + + [Inline] + set mut + { + G = (uint8)(value * 255); + } + } + + public float Bf + { + [Inline] + get + { + return B / 255f; + } + + [Inline] + set mut + { + B = (uint8)(value * 255); + } + } + + public float Af + { + [Inline] + get + { + return A / 255f; + } + + [Inline] + set mut + { + A = (uint8)(value * 255); + } + } + + static public Color Lerp(Color a, Color b, float t) + { + return Color( + Math.Lerp(a.Rf, b.Rf, t), + Math.Lerp(a.Gf, b.Gf, t), + Math.Lerp(a.Bf, b.Bf, t), + Math.Lerp(a.Af, b.Af, t) + ); + } + + static public Color operator/(Color a, Color b) + { + return Lerp(a, b, 0.5f); + } + + static public Color operator*(Color color, float b) + { + return Color(color.R, color.G, color.B, (uint8)(color.A * b)); + } + + static public implicit operator SDL2.SDL.Color(Color color) + { + return SDL2.SDL.Color(color.R, color.G, color.B, color.A); + } + } +} diff --git a/src/Util/Draw.bf b/src/Util/Draw.bf new file mode 100644 index 0000000..d06ba79 --- /dev/null +++ b/src/Util/Draw.bf @@ -0,0 +1,35 @@ +using SDL2; + +namespace Strawberry +{ + static public class Draw + { + static public void Rect(int x, int y, int w, int h, Color color) + { + SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); + SDL.RenderFillRect(Game.Renderer, &SDL.Rect((int32)x, (int32)y, (int32)w, (int32)h)); + } + + static public void Rect(Rect rect, Color color) + { + Rect(rect.X, rect.Y, rect.Width, rect.Height, color); + } + + static public void HollowRect(int x, int y, int w, int h, Color color) + { + SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); + SDL.RenderDrawRect(Game.Renderer, &SDL.Rect((int32)x, (int32)y, (int32)w, (int32)h)); + } + + static public void HollowRect(Rect rect, Color color) + { + HollowRect(rect.X, rect.Y, rect.Width, rect.Height, color); + } + + static public void Line(Point from, Point to, Color color) + { + SDL.SetRenderDrawColor(Game.Renderer, color.R, color.G, color.B, color.A); + SDL.RenderDrawLine(Game.Renderer, (int32)from.X, (int32)from.Y, (int32)to.X, (int32)to.Y); + } + } +} diff --git a/src/Numerics/Point.bf b/src/Util/Point.bf similarity index 100% rename from src/Numerics/Point.bf rename to src/Util/Point.bf diff --git a/src/Numerics/Rect.bf b/src/Util/Rect.bf similarity index 100% rename from src/Numerics/Rect.bf rename to src/Util/Rect.bf diff --git a/src/Static/Time.bf b/src/Util/Time.bf similarity index 100% rename from src/Static/Time.bf rename to src/Util/Time.bf diff --git a/src/Numerics/Vector.bf b/src/Util/Vector.bf similarity index 100% rename from src/Numerics/Vector.bf rename to src/Util/Vector.bf