mirror of
https://github.com/MaddyThorson/StrawberryBF.git
synced 2025-07-19 20:11:55 +08:00
Reorg
This commit is contained in:
151
src/Struct/Color.bf
Normal file
151
src/Struct/Color.bf
Normal file
@ -0,0 +1,151 @@
|
||||
using System;
|
||||
|
||||
namespace Strawberry
|
||||
{
|
||||
public struct Color
|
||||
{
|
||||
static public readonly Color White = 0xFFFFFFFF;
|
||||
static public readonly Color Black = 0x000000FF;
|
||||
static public readonly Color Transparent = 0x00000000;
|
||||
static public readonly Color Red = 0xFF0000FF;
|
||||
static public readonly Color Green = 0x00FF00FF;
|
||||
static public readonly Color Blue = 0x0000FFFF;
|
||||
static public readonly Color Cyan = 0xFF00FFFF;
|
||||
static public readonly Color Magenta = 0xFFFF00FF;
|
||||
static public readonly Color Yellow = 0x00FFFFFF;
|
||||
|
||||
public uint8 R;
|
||||
public uint8 G;
|
||||
public uint8 B;
|
||||
public uint8 A;
|
||||
|
||||
public this(uint8 red, uint8 green, uint8 blue, uint8 alpha = 255)
|
||||
{
|
||||
R = red;
|
||||
G = green;
|
||||
B = blue;
|
||||
A = alpha;
|
||||
}
|
||||
|
||||
public this(float red, float green, float blue, float alpha = 1f)
|
||||
{
|
||||
R = (uint8)(red * 255);
|
||||
G = (uint8)(green * 255);
|
||||
B = (uint8)(blue * 255);
|
||||
A = (uint8)(alpha * 255);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public override void ToString(String strBuffer)
|
||||
{
|
||||
strBuffer.Set("Color [ ");
|
||||
((uint)R).ToString(strBuffer);
|
||||
strBuffer.Append(", ");
|
||||
((uint)G).ToString(strBuffer);
|
||||
strBuffer.Append(", ");
|
||||
((uint)B).ToString(strBuffer);
|
||||
strBuffer.Append(", ");
|
||||
((uint)A).ToString(strBuffer);
|
||||
strBuffer.Append(" ]");
|
||||
}
|
||||
|
||||
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 implicit operator Color(uint32 from)
|
||||
{
|
||||
return Color(
|
||||
(uint8)((from >> 24) & 0xFF),
|
||||
(uint8)((from >> 16) & 0xFF),
|
||||
(uint8)((from >> 8) & 0xFF),
|
||||
(uint8)(from & 0xFF)
|
||||
);
|
||||
}
|
||||
|
||||
static public implicit operator uint32(Color from)
|
||||
{
|
||||
return (((uint32)from.R) << 24) | (((uint32)from.G) << 16) | (((uint32)from.B) << 8) | ((uint32)from.A);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
69
src/Struct/Point.bf
Normal file
69
src/Struct/Point.bf
Normal file
@ -0,0 +1,69 @@
|
||||
using System;
|
||||
|
||||
namespace Strawberry
|
||||
{
|
||||
public struct Point
|
||||
{
|
||||
static public readonly Point Right = .(1, 0);
|
||||
static public readonly Point Left = .(-1, 0);
|
||||
static public readonly Point Up = .(0, -1);
|
||||
static public readonly Point Down = .(0, 1);
|
||||
static public readonly Point UnitX = .(1, 0);
|
||||
static public readonly Point UnitY = .(0, 1);
|
||||
static public readonly Point Zero = .(0, 0);
|
||||
static public readonly Point One = .(1, 1);
|
||||
|
||||
public int X;
|
||||
public int Y;
|
||||
|
||||
public this()
|
||||
{
|
||||
this = default;
|
||||
}
|
||||
|
||||
public this(int x, int y)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
}
|
||||
|
||||
public override void ToString(String strBuffer)
|
||||
{
|
||||
strBuffer.Set("Point [ ");
|
||||
X.ToString(strBuffer);
|
||||
strBuffer.Append(", ");
|
||||
Y.ToString(strBuffer);
|
||||
strBuffer.Append(" ]");
|
||||
}
|
||||
|
||||
static public explicit operator Point(Vector a)
|
||||
{
|
||||
return Point((int)a.X, (int)a.Y);
|
||||
}
|
||||
|
||||
static public bool operator==(Point a, Point b)
|
||||
{
|
||||
return a.X == b.X && a.Y == b.Y;
|
||||
}
|
||||
|
||||
static public Point operator+(Point a, Point b)
|
||||
{
|
||||
return Point(a.X + b.X, a.Y + b.Y);
|
||||
}
|
||||
|
||||
static public Point operator-(Point a, Point b)
|
||||
{
|
||||
return Point(a.X - b.X, a.Y - b.Y);
|
||||
}
|
||||
|
||||
static public Point operator*(Point a, int b)
|
||||
{
|
||||
return Point(a.X * b, a.Y * b);
|
||||
}
|
||||
|
||||
static public Point operator/(Point a, int b)
|
||||
{
|
||||
return Point(a.X / b, a.Y / b);
|
||||
}
|
||||
}
|
||||
}
|
142
src/Struct/Rect.bf
Normal file
142
src/Struct/Rect.bf
Normal file
@ -0,0 +1,142 @@
|
||||
using System;
|
||||
|
||||
namespace Strawberry
|
||||
{
|
||||
public struct Rect
|
||||
{
|
||||
public int X;
|
||||
public int Y;
|
||||
public int Width;
|
||||
public int Height;
|
||||
|
||||
public this()
|
||||
{
|
||||
this = default;
|
||||
}
|
||||
|
||||
public this(int x, int y, int width, int height)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
Width = width;
|
||||
Height = height;
|
||||
}
|
||||
|
||||
public int Left
|
||||
{
|
||||
[Inline]
|
||||
get
|
||||
{
|
||||
return X;
|
||||
}
|
||||
|
||||
[Inline]
|
||||
set mut
|
||||
{
|
||||
X = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int Right
|
||||
{
|
||||
[Inline]
|
||||
get
|
||||
{
|
||||
return X + Width;
|
||||
}
|
||||
|
||||
[Inline]
|
||||
set mut
|
||||
{
|
||||
X = value - Width;
|
||||
}
|
||||
}
|
||||
|
||||
public int Top
|
||||
{
|
||||
[Inline]
|
||||
get
|
||||
{
|
||||
return Y;
|
||||
}
|
||||
|
||||
[Inline]
|
||||
set mut
|
||||
{
|
||||
Y = value;
|
||||
}
|
||||
}
|
||||
|
||||
public int Bottom
|
||||
{
|
||||
[Inline]
|
||||
get
|
||||
{
|
||||
return Y + Height;
|
||||
}
|
||||
|
||||
[Inline]
|
||||
set mut
|
||||
{
|
||||
Y = value - Height;
|
||||
}
|
||||
}
|
||||
|
||||
public Rect MirrorX(int axis = 0)
|
||||
{
|
||||
var rect = this;
|
||||
rect.X = axis - X - Width;
|
||||
return rect;
|
||||
}
|
||||
|
||||
public Rect MirrorY(int axis = 0)
|
||||
{
|
||||
var rect = this;
|
||||
rect.Y = axis - Y - Height;
|
||||
return rect;
|
||||
}
|
||||
|
||||
public Rect Inflate(int amount)
|
||||
{
|
||||
return Rect(X - amount, Y - amount, Width + amount * 2, Height + amount * 2);
|
||||
}
|
||||
|
||||
public bool Intersects(Rect rect)
|
||||
{
|
||||
return (X + Width) > rect.X && (rect.X + rect.Width) > X && (Y + Height) > rect.Y && (rect.Y + rect.Height) > Y;
|
||||
}
|
||||
|
||||
public bool Contains(Point point)
|
||||
{
|
||||
return point.X >= X && point.X < X + Width && point.Y >= Y && point.Y < Y + Height;
|
||||
}
|
||||
|
||||
public override void ToString(String strBuffer)
|
||||
{
|
||||
strBuffer.Set("Rect [ ");
|
||||
X.ToString(strBuffer);
|
||||
strBuffer.Append(", ");
|
||||
Y.ToString(strBuffer);
|
||||
strBuffer.Append(", ");
|
||||
Width.ToString(strBuffer);
|
||||
strBuffer.Append(", ");
|
||||
Height.ToString(strBuffer);
|
||||
strBuffer.Append(" ]");
|
||||
}
|
||||
|
||||
static public bool operator==(Rect a, Rect b)
|
||||
{
|
||||
return a.X == b.X && a.Y == b.Y && a.Width == b.Width && a.Height == b.Height;
|
||||
}
|
||||
|
||||
static public Rect operator+(Rect a, Point b)
|
||||
{
|
||||
return Rect(a.X + b.X, a.Y + b.Y, a.Width, a.Height);
|
||||
}
|
||||
|
||||
static public Rect operator-(Rect a, Point b)
|
||||
{
|
||||
return Rect(a.X - b.X, a.Y - b.Y, a.Width, a.Height);
|
||||
}
|
||||
}
|
||||
}
|
93
src/Struct/Vector.bf
Normal file
93
src/Struct/Vector.bf
Normal file
@ -0,0 +1,93 @@
|
||||
using System;
|
||||
|
||||
namespace Strawberry
|
||||
{
|
||||
public struct Vector
|
||||
{
|
||||
static public readonly Vector Right = .(1, 0);
|
||||
static public readonly Vector Left = .(-1, 0);
|
||||
static public readonly Vector Up = .(0, -1);
|
||||
static public readonly Vector Down = .(0, 1);
|
||||
static public readonly Vector UnitX = .(1, 0);
|
||||
static public readonly Vector UnitY = .(0, 1);
|
||||
static public readonly Vector Zero = .(0, 0);
|
||||
static public readonly Vector One = .(1, 1);
|
||||
|
||||
public float X;
|
||||
public float Y;
|
||||
|
||||
public this()
|
||||
{
|
||||
this = default;
|
||||
}
|
||||
|
||||
public this(float x, float y)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
}
|
||||
|
||||
public float Length
|
||||
{
|
||||
[Inline]
|
||||
get
|
||||
{
|
||||
return Math.Sqrt(LengthSquared);
|
||||
}
|
||||
}
|
||||
|
||||
public float LengthSquared
|
||||
{
|
||||
[Inline]
|
||||
get
|
||||
{
|
||||
return X * X + Y * Y;
|
||||
}
|
||||
}
|
||||
|
||||
[Inline]
|
||||
public Point Round()
|
||||
{
|
||||
return Point((int)Math.Round(X), (int)Math.Round(Y));
|
||||
}
|
||||
|
||||
public override void ToString(String strBuffer)
|
||||
{
|
||||
strBuffer.Set("Vector [ ");
|
||||
X.ToString(strBuffer);
|
||||
strBuffer.Append(", ");
|
||||
Y.ToString(strBuffer);
|
||||
strBuffer.Append(" ]");
|
||||
}
|
||||
|
||||
static public operator Vector(Point a)
|
||||
{
|
||||
return Vector(a.X, a.Y);
|
||||
}
|
||||
|
||||
static public bool operator==(Vector a, Vector b)
|
||||
{
|
||||
return a.X == b.X && a.Y == b.Y;
|
||||
}
|
||||
|
||||
static public Vector operator+(Vector a, Vector b)
|
||||
{
|
||||
return Vector(a.X + b.X, a.Y + b.Y);
|
||||
}
|
||||
|
||||
static public Vector operator-(Vector a, Vector b)
|
||||
{
|
||||
return Vector(a.X - b.X, a.Y - b.Y);
|
||||
}
|
||||
|
||||
static public Vector operator*(Vector a, float b)
|
||||
{
|
||||
return Vector(a.X * b, a.Y * b);
|
||||
}
|
||||
|
||||
static public Vector operator/(Vector a, float b)
|
||||
{
|
||||
return Vector(a.X / b, a.Y / b);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user