1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 03:28:20 +02:00

Beefy2D enhancements

This commit is contained in:
Brian Fiete 2025-01-14 10:14:56 -08:00
parent d1aa3de25e
commit 89651c4a76
3 changed files with 61 additions and 2 deletions

View file

@ -6,6 +6,7 @@ using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Security.Cryptography;
using Beefy.gfx;
namespace Beefy
{
@ -21,8 +22,6 @@ namespace Beefy
public static float Deg2Rad = Math.PI_f / 180.0f;
public static int32 Rand()
{
return mRandom.NextI32();
@ -33,6 +32,12 @@ namespace Beefy
return (Rand() & 0xFFFFFF) / (float)0xFFFFFF;
}
public static float RandFloatS()
{
#unwarn
return ((Rand() & 0x1FFFFFF) / (float)0xFFFFFF) - 1.0f;
}
public static float Interpolate(float left, float right, float pct)
{
return left + (right - left) * pct;
@ -472,5 +477,11 @@ namespace Beefy
{
val = (float)Math.Round(val * scale);
}
public static Image GetAnimFrame(Image[] images, float pct)
{
int frameNum = (int)(images.Count * (pct - 0.000001f));
return images[frameNum];
}
}
}

View file

@ -46,6 +46,11 @@ namespace Beefy.geom
public Vector2 Normalized => Vector2(mX, mY)..Normalize();
public bool IsLengthLess(float check)
{
return mX * mX + mY * mY < check * check;
}
public static Vector2 Normalize(Vector2 vector)
{
Vector2 newVec;
@ -123,5 +128,10 @@ namespace Beefy.geom
{
return Vector2(vec1.mX / factor, vec1.mY / factor);
}
public override void ToString(String strBuffer)
{
strBuffer.AppendF("{0:0.0#}, {1:0.0#}", mX, mY);
}
}
}

View file

@ -547,6 +547,11 @@ namespace Beefy.gfx
(int)mPixelSnapping, mSmoothing ? 1 : 0, mAdditive ? 1 : 0, (int)g.mColor);*/
}
public void DrawCentered(Image image, float x = 0, float y = 0)
{
Draw(image, x - image.mWidth / 2, y - image.mHeight / 2);
}
public void Draw(IDrawable drawable, Vector2 vec) => Draw(drawable, vec.mX, vec.mY);
public void DrawButton(Image image, float x, float y, float width)
@ -948,6 +953,39 @@ namespace Beefy.gfx
Gfx_SetDrawVertex(5, m.tx + (m.a + m.c), m.ty + (m.b + m.d), 0, 0, 0, Color.Mult(mColor, colorBotRight));
}
public void FillRectGradientEx(float x, float y, float width, float height,
Color colorTopLeft, Color colorTopRight, Color colorBotLeft, Color colorBotRight)
{
Matrix m = Matrix.IdentityMatrix;
m.SetMultiplied(x, y, width, height, ref mMatrix);
int32 r = ((int32)colorTopLeft.R + colorTopRight.R + colorBotLeft.R + colorBotRight.R) / 4;
int32 g = ((int32)colorTopLeft.G + colorTopRight.G + colorBotLeft.G + colorBotRight.G) / 4;
int32 b = ((int32)colorTopLeft.B + colorTopRight.B + colorBotLeft.B + colorBotRight.B) / 4;
int32 a = ((int32)colorTopLeft.A + colorTopRight.A + colorBotLeft.A + colorBotRight.A) / 4;
Color centerColor = .(r, g, b, a);
//TODO: Multiply color
Gfx_AllocTris(mWhiteDot.mNativeTextureSegment, 12);
Gfx_SetDrawVertex(0, m.tx, m.ty, 0, 0, 0, Color.Mult(mColor, colorTopLeft));
Gfx_SetDrawVertex(1, m.tx + m.a, m.ty + m.b, 0, 0, 0, Color.Mult(mColor, colorTopRight));
Gfx_SetDrawVertex(2, m.tx + (m.a + m.c) * 0.5f, m.ty + (m.b + m.d) * 0.5f, 0, 0, 0, Color.Mult(mColor, centerColor));
Gfx_CopyDrawVertex(3, 1); // Top Right
Gfx_SetDrawVertex(4, m.tx + (m.a + m.c), m.ty + (m.b + m.d), 0, 0, 0, Color.Mult(mColor, colorBotRight));
Gfx_CopyDrawVertex(5, 2); // Center
Gfx_CopyDrawVertex(6, 2); // Center
Gfx_CopyDrawVertex(7, 4); // Bottom Right
Gfx_SetDrawVertex(8, m.tx + m.c, m.ty + m.d, 0, 0, 0, Color.Mult(mColor, colorBotLeft));
Gfx_CopyDrawVertex(9, 8); // Bottom Left
Gfx_CopyDrawVertex(10, 0); // Top Left
Gfx_CopyDrawVertex(11, 2); // Center
}
void DoDrawLine(float x0, float y0, float x1, float y1, float width)
{
Image img = GetBorderedWhiteSquare(Math.Max((int)width, 1));