From 3c2b8bee3436a6f642316c39d6f3039d412fa8e0 Mon Sep 17 00:00:00 2001 From: Matorio <209939889+Matorio@users.noreply.github.com> Date: Thu, 15 Jan 2026 00:02:59 -0500 Subject: [PATCH] Textures & Images Added extensions for textures and images --- Raylib-cs.Extensions/ImageExtensions.cs | 382 ++++++++++++++++++ .../Raylib-cs.Extensions.csproj | 5 +- Raylib-cs.Extensions/TextureExtensions.cs | 42 ++ 3 files changed, 427 insertions(+), 2 deletions(-) create mode 100644 Raylib-cs.Extensions/ImageExtensions.cs diff --git a/Raylib-cs.Extensions/ImageExtensions.cs b/Raylib-cs.Extensions/ImageExtensions.cs new file mode 100644 index 0000000..454d600 --- /dev/null +++ b/Raylib-cs.Extensions/ImageExtensions.cs @@ -0,0 +1,382 @@ +using System.Numerics; + +namespace Raylib_cs.Extensions; + +public static class ImageExtensions +{ + public static bool IsValid(this Image image) + { + return Raylib.IsImageValid(image); + } + + public static void Unload(this Image image) + { + Raylib.UnloadImage(image); + } + + public static CBool Export(this Image image, string fileName) + { + return Raylib.ExportImage(image, fileName); + } + + public static unsafe byte[] ExportToMemory(this Image image, string fileType) + { + int fileSize = 0; + using AnsiBuffer str1 = fileType.ToAnsiBuffer(); + byte* imgData = Raylib.ExportImageToMemory(image, str1.AsPointer(), &fileSize); + byte[] byteArray = new byte[fileSize]; + for (int i = 0; i < fileSize; i++) + { + byteArray[i] = imgData[i]; + } + Raylib.MemFree(imgData); + return byteArray; + } + + public static CBool ExportAsCode(this Image image, string fileName) + { + return Raylib.ExportImageAsCode(image, fileName); + } + + public static void Format(this ref Image image, PixelFormat format) + { + Raylib.ImageFormat(ref image, format); + } + + public static void ToPOT(this ref Image image, Color colorFill) + { + Raylib.ImageToPOT(ref image, colorFill); + } + + public static void Crop(this ref Image image, Rectangle area) + { + Raylib.ImageCrop(ref image, area); + } + + public static void AlphaCrop(this ref Image image, float threshold) + { + Raylib.ImageAlphaCrop(ref image, threshold); + } + + public static void AlphaClear(this ref Image image, Color color, float threshold) + { + Raylib.ImageAlphaClear(ref image, color, threshold); + } + + public static void AlphaMask(this ref Image image, Image alphaMask) + { + Raylib.ImageAlphaMask(ref image, alphaMask); + } + + public static void AlphaPremultiply(this ref Image image) + { + Raylib.ImageAlphaPremultiply(ref image); + } + + public static void BlurGaussian(this ref Image image, int blurSize) + { + Raylib.ImageBlurGaussian(ref image, blurSize); + } + + public static void KernelConvolution(this ref Image image, float[] kernel) + { + Raylib.ImageKernelConvolution(ref image, kernel); + } + + public static void Resize(this ref Image image, int newWidth, int newHeight) + { + Raylib.ImageResize(ref image, newWidth, newHeight); + } + + public static void Resize(this ref Image image, Vector2 newDimensions) + { + Raylib.ImageResize(ref image, (int)newDimensions.X, (int)newDimensions.Y); + } + + public static void ResizeNN(this ref Image image, int newWidth, int newHeight) + { + Raylib.ImageResizeNN(ref image, newWidth, newHeight); + } + + public static void ResizeNN(this ref Image image, Vector2 newDimensions) + { + Raylib.ImageResizeNN(ref image, (int)newDimensions.X, (int)newDimensions.Y); + } + + public static void ResizeCanvas(this ref Image image, int newWidth, int newHeight, int offsetX, int offsetY, Color fill) + { + Raylib.ImageResizeCanvas(ref image, newWidth, newHeight, offsetX, offsetY, fill); + } + + public static void ResizeCanvas(this ref Image image, Vector2 newDimensions, int offsetX, int offsetY, Color fill) + { + Raylib.ImageResizeCanvas(ref image, (int)newDimensions.X, (int)newDimensions.Y, offsetX, offsetY, fill); + } + + public static void ResizeCanvas(this ref Image image, int newWidth, int newHeight, Vector2 offset, Color fill) + { + Raylib.ImageResizeCanvas(ref image, newWidth, newHeight, (int)offset.X, (int)offset.Y, fill); + } + + public static void ResizeCanvas(this ref Image image, Vector2 newDimensions, Vector2 offset, Color fill) + { + Raylib.ImageResizeCanvas(ref image, (int)newDimensions.X, (int)newDimensions.Y, (int)offset.X, (int)offset.Y, fill); + } + + public static void Mipmaps(this ref Image image) + { + Raylib.ImageMipmaps(ref image); + } + + public static void Dither(this ref Image image, int rBpp, int gBpp, int bBpp, int aBpp) + { + Raylib.ImageDither(ref image, rBpp, gBpp, bBpp, aBpp); + } + + public static void FlipVertical(this ref Image image) + { + Raylib.ImageFlipVertical(ref image); + } + + public static void FlipHorizontal(this ref Image image) + { + Raylib.ImageFlipHorizontal(ref image); + } + + public static void Rotate(this ref Image image, int degrees) + { + Raylib.ImageRotate(ref image, degrees); + } + + public static void RotateCW(this ref Image image) + { + Raylib.ImageRotateCW(ref image); + } + + public static void RotateCCW(this ref Image image) + { + Raylib.ImageRotateCCW(ref image); + } + + public static void ColorTint(this ref Image image, Color color) + { + Raylib.ImageColorTint(ref image, color); + } + + public static void ColorInvert(this ref Image image) + { + Raylib.ImageColorInvert(ref image); + } + + public static void ColorGrayscale(this ref Image image) + { + Raylib.ImageColorGrayscale(ref image); + } + + public static void ColorContrast(this ref Image image, float contrast) + { + Raylib.ImageColorContrast(ref image, contrast); + } + + public static void ColorBrightness(this ref Image image, int brightness) + { + Raylib.ImageColorBrightness(ref image, brightness); + } + + public static void ColorReplace(this ref Image image, Color color, Color replace) + { + Raylib.ImageColorReplace(ref image, color, replace); + } + + public static Rectangle GetAlphaBorder(this Image image, float threshold) + { + return Raylib.GetImageAlphaBorder(image, threshold); + } + + public static Color GetColor(this Image image, int x, int y) + { + return Raylib.GetImageColor(image, x, y); + } + + public static Color GetColor(this Image image, Vector2 pixelPosition) + { + return Raylib.GetImageColor(image, (int)pixelPosition.X, (int)pixelPosition.Y); + } + + + public static void ClearBackground(this ref Image image, Color color) + { + Raylib.ImageClearBackground(ref image, color); + } + + public static void DrawPixel(this ref Image image, int x, int y, Color color) + { + Raylib.ImageDrawPixel(ref image, x, y, color); + } + + public static void DrawPixel(this ref Image image, Vector2 position, Color color) + { + Raylib.ImageDrawPixelV(ref image, position, color); + } + + public static void DrawLine(this ref Image image, int startX, int startY, int endX, int endY, Color color) + { + Raylib.ImageDrawLine(ref image, startX, startY, endX, endY, color); + } + + public static void DrawLine(this ref Image image, Vector2 start, int endX, int endY, Color color) + { + Raylib.ImageDrawLine(ref image, (int)start.X, (int)start.Y, endX, endY, color); + } + + public static void DrawLine(this ref Image image, int startX, int startY, Vector2 end, Color color) + { + Raylib.ImageDrawLine(ref image, startX, startY, (int)end.X, (int)end.Y, color); + } + + public static void DrawLine(this ref Image image, Vector2 start, Vector2 end, Color color) + { + Raylib.ImageDrawLineV(ref image, start, end, color); + } + + public static void DrawLineEx(this ref Image image, int startX, int startY, int endX, int endY, int thick, Color color) + { + Raylib.ImageDrawLineEx(ref image, new Vector2(startX, startY), new Vector2(endX, endY), thick, color); + } + + public static void DrawLineEx(this ref Image image, Vector2 start, int endX, int endY, int thick, Color color) + { + Raylib.ImageDrawLineEx(ref image, start, new Vector2(endX, endY), thick, color); + } + + public static void DrawLineEx(this ref Image image, int startX, int startY, Vector2 end, int thick, Color color) + { + Raylib.ImageDrawLineEx(ref image, new Vector2(startX, startY), end, thick, color); + } + + public static void DrawLineEx(this ref Image image, Vector2 start, Vector2 end, int thick, Color color) + { + Raylib.ImageDrawLineEx(ref image, start, end, thick, color); + } + + public static void DrawCircle(this ref Image image, int centerX, int centerY, int radius, Color color) + { + Raylib.ImageDrawCircle(ref image, centerX, centerY, radius, color); + } + + public static void DrawCircle(this ref Image image, Vector2 center, int radius, Color color) + { + Raylib.ImageDrawCircleV(ref image, center, radius, color); + } + + public static unsafe void DrawCircleLines(this ref Image image, int centerX, int centerY, int radius, Color color) + { + fixed (Image* dst = &image) + { + Raylib.ImageDrawCircleLines(dst, centerX, centerY, radius, color); + } + } + + public static unsafe void DrawCircleLines(this ref Image image, Vector2 center, int radius, Color color) + { + fixed (Image* dst = &image) + { + Raylib.ImageDrawCircleLinesV(dst, center, radius, color); + } + } + + public static unsafe void DrawRectangle(this ref Image image, int positionX, int positionY, int width, int height, Color color) + { + Raylib.ImageDrawRectangle(ref image, positionX, positionY, width, height, color); + } + + public static unsafe void DrawRectangle(this ref Image image, Vector2 position, int width, int height, Color color) + { + Raylib.ImageDrawRectangleV(ref image, position, new Vector2(width, height), color); + } + + public static unsafe void DrawRectangle(this ref Image image, int positionX, int positionY, Vector2 size, Color color) + { + Raylib.ImageDrawRectangleV(ref image, new Vector2(positionX, positionY), size, color); + } + + public static unsafe void DrawRectangle(this ref Image image, Vector2 position, Vector2 size, Color color) + { + Raylib.ImageDrawRectangleV(ref image, position, size, color); + } + + public static unsafe void DrawRectangle(this ref Image image, Rectangle rectangle, Color color) + { + Raylib.ImageDrawRectangleRec(ref image, rectangle, color); + } + + public static unsafe void DrawRectangleLines(this ref Image image, int positionX, int positionY, int width, int height, int thick, Color color) + { + Raylib.ImageDrawRectangleLines(ref image, new Rectangle(positionX, positionY, width, height), thick, color); + } + + public static unsafe void DrawRectangleLines(this ref Image image, Vector2 position, int width, int height, int thick, Color color) + { + Raylib.ImageDrawRectangleLines(ref image, new Rectangle(position, width, height), thick, color); + } + + public static unsafe void DrawRectangleLines(this ref Image image, int positionX, int positionY, Vector2 size, int thick, Color color) + { + Raylib.ImageDrawRectangleLines(ref image, new Rectangle(positionX, positionY, size), thick, color); + } + + public static unsafe void DrawRectangleLines(this ref Image image, Vector2 position, Vector2 size, int thick, Color color) + { + Raylib.ImageDrawRectangleLines(ref image, new Rectangle(position, size), thick, color); + } + + public static unsafe void DrawRectangleLines(this ref Image image, Rectangle rectangle, int thick, Color color) + { + Raylib.ImageDrawRectangleLines(ref image, rectangle, thick, color); + } + + public static void DrawTriangle(this ref Image image, Vector2 v1, Vector2 v2, Vector2 v3, Color color) + { + Raylib.ImageDrawTriangle(ref image, v1, v2, v3, color); + } + + public static void DrawTriangleEx(this ref Image image, Vector2 v1, Vector2 v2, Vector2 v3, Color c1, Color c2, Color c3) + { + Raylib.ImageDrawTriangleEx(ref image, v1, v2, v3, c1, c2, c3); + } + + public static void DrawTriangleLines(this ref Image image, Vector2 v1, Vector2 v2, Vector2 v3, Color color) + { + Raylib.ImageDrawTriangleLines(ref image, v1, v2, v3, color); + } + + public static void DrawTriangleFan(this ref Image image, Vector2[] points, Color color) + { + Raylib.ImageDrawTriangleFan(ref image, points, color); + } + + public static void DrawTriangleStrip(this ref Image image, Vector2[] points, Color color) + { + Raylib.ImageDrawTriangleStrip(ref image, points, color); + } + + public static void Draw(this ref Image image, Image source, Rectangle sourceRec, Rectangle dstRec, Color tint) + { + Raylib.ImageDraw(ref image, source, sourceRec, dstRec, tint); + } + + public static void DrawText(this ref Image image, string text, int positionX, int positionY, int fontSize, Color color) + { + Raylib.ImageDrawText(ref image, text, positionX, positionY, fontSize, color); + } + + public static void DrawText(this ref Image image, string text, Vector2 position, int fontSize, Color color) + { + Raylib.ImageDrawText(ref image, text, (int)position.X, (int)position.Y, fontSize, color); + } + + public static void DrawTextEx(this ref Image image, Font font, string text, Vector2 position, int fontSize, int spacing, Color tint) + { + Raylib.ImageDrawTextEx(ref image, font, text, position, fontSize, spacing, tint); + } +} diff --git a/Raylib-cs.Extensions/Raylib-cs.Extensions.csproj b/Raylib-cs.Extensions/Raylib-cs.Extensions.csproj index e1502a1..f50d7a4 100644 --- a/Raylib-cs.Extensions/Raylib-cs.Extensions.csproj +++ b/Raylib-cs.Extensions/Raylib-cs.Extensions.csproj @@ -3,10 +3,11 @@ net6.0;net8.0 Raylib_cs.Extensions + True - - + + diff --git a/Raylib-cs.Extensions/TextureExtensions.cs b/Raylib-cs.Extensions/TextureExtensions.cs index d2b3ffd..c0a0b94 100644 --- a/Raylib-cs.Extensions/TextureExtensions.cs +++ b/Raylib-cs.Extensions/TextureExtensions.cs @@ -1,9 +1,51 @@ +using System.Numerics; + namespace Raylib_cs.Extensions; public static class TextureExtensions { + public static bool IsValid(this Texture2D texture) + { + return Raylib.IsTextureValid(texture); + } + + public static void Unload(this Texture2D texture) + { + Raylib.UnloadTexture(texture); + } + + public static void SetFilter(this Texture2D texture, TextureFilter filter) + { + Raylib.SetTextureFilter(texture, filter); + } + + public static void SetWrap(this Texture2D texture, TextureWrap wrap) + { + Raylib.SetTextureWrap(texture, wrap); + } + public static void Draw(this Texture2D texture, int posX, int posY, Color tint) { Raylib.DrawTexture(texture, posX, posY, tint); } + + public static void Draw(this Texture2D texture, Vector2 position, Color tint) + { + Raylib.DrawTextureV(texture, position, tint); + } + + public static void DrawEx(this Texture2D texture, Vector2 position, float rotation, float scale, Color tint) + { + Raylib.DrawTextureEx(texture, position, rotation, scale, tint); + } + + public static void DrawPro(this Texture2D texture, Rectangle source, Rectangle dest, Vector2 origin, float rotation, Color tint) + { + Raylib.DrawTexturePro(texture, source, dest, origin, rotation, tint); + } + + public static void DrawNPatch(this Texture2D texture, NPatchInfo patchInfo, Rectangle dest, Vector2 origin, float rotation, Color tint) + { + Raylib.DrawTextureNPatch(texture, patchInfo, dest, origin, rotation, tint); + } }