Переглянути джерело

Added draw functions to ImageDraw*() (#1138)

Added draw functions:
ImageClearBackground()
ImageDrawPixel()
ImageDrawLineEx()
ImageDrawCircle()
Sourav Gupta 5 роки тому
батько
коміт
574c689ff7
2 змінених файлів з 61 додано та 0 видалено
  1. 4 0
      src/raylib.h
  2. 57 0
      src/textures.c

+ 4 - 0
src/raylib.h

@@ -1142,6 +1142,10 @@ RLAPI Image ImageTextEx(Font font, const char *text, float fontSize, float spaci
 RLAPI void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec, Color tint);             // Draw a source image within a destination image (tint applied to source)
 RLAPI void ImageDrawRectangle(Image *dst, Rectangle rec, Color color);                                   // Draw rectangle within an image
 RLAPI void ImageDrawRectangleLines(Image *dst, Rectangle rec, int thick, Color color);                   // Draw rectangle lines within an image
+RLAPI void ImageClearBackground(Image *dst, Color color);                                                // Clear image background with given color
+RLAPI void ImageDrawPixel(Image *dst, Vector2 position, Color color);                                    // Draw pixel within an image
+RLAPI void ImageDrawCircle(Image *dst, Vector2 center, int radius, Color color);                         // Draw circle within an image
+RLAPI void ImageDrawLineEx(Image *dst, Vector2 start, Vector2 end, Color color);                         // Draw line within an image
 RLAPI void ImageDrawText(Image *dst, Vector2 position, const char *text, int fontSize, Color color);     // Draw text (default font) within an image (destination)
 RLAPI void ImageDrawTextEx(Image *dst, Vector2 position, Font font, const char *text, float fontSize, float spacing, Color color); // Draw text (custom sprite font) within an image (destination)
 RLAPI void ImageFlipVertical(Image *image);                                                              // Flip image vertically

+ 57 - 0
src/textures.c

@@ -2014,6 +2014,63 @@ void ImageDrawRectangleLines(Image *dst, Rectangle rec, int thick, Color color)
     ImageDrawRectangle(dst, (Rectangle){ rec.x, rec.y + rec.height - thick, rec.width, thick }, color);
 }
 
+// Clear image background with given color
+void ImageClearBackground(Image *dst, Color color)
+{
+    ImageDrawRectangle(dst, (Rectangle){ 0.0f, 0.0f, dst->width, dst->height }, color);
+}
+
+// Draw pixel within an image
+void ImageDrawPixel(Image *dst, Vector2 position, Color color) 
+{  
+    ImageDrawRectangle(dst, (Rectangle){ position.x, position.y, 1.0f, 1.0f }, color);
+}
+
+// Draw circle within an image
+void ImageDrawCircle(Image *dst, Vector2 center, int radius, Color color)
+{
+    int x = 0, y = radius, xc = center.x, yc = center.y;
+    int decesionParameter = 3 - 2 * radius;
+    while (y >= x)
+    {
+        ImageDrawPixel(dst, (Vector2){ xc + x, yc + y }, color);
+        ImageDrawPixel(dst, (Vector2){ xc - x, yc + y }, color);
+        ImageDrawPixel(dst, (Vector2){ xc + x, yc - y }, color);
+        ImageDrawPixel(dst, (Vector2){ xc - x, yc - y }, color);
+        ImageDrawPixel(dst, (Vector2){ xc + y, yc + x }, color);
+        ImageDrawPixel(dst, (Vector2){ xc - y, yc + x }, color);
+        ImageDrawPixel(dst, (Vector2){ xc + y, yc - x }, color);
+        ImageDrawPixel(dst, (Vector2){ xc - y, yc - x }, color);
+        x++;
+        if (decesionParameter > 0)
+        {
+            y--;
+            decesionParameter = decesionParameter + 4 * (x - y) + 10;
+        }
+        else
+            decesionParameter = decesionParameter + 4 * x + 6;
+    }
+}
+
+// Draw line within an image
+void ImageDrawLineEx(Image *dst, Vector2 start, Vector2 end, Color color)
+{
+    int x1 = start.x, y1 = start.y, x2 = end.x, y2 = end.y;
+    int m = 2 * (y2 - y1); 
+    int slopeError = m - (x2 - x1); 
+    for (int x = x1, y = y1; x <= x2; x++) 
+    { 
+        ImageDrawPixel(dst, (Vector2){ x, y }, color);
+        slopeError += m;
+        
+        if (slopeError >= 0) 
+        { 
+            y++; 
+            slopeError -= 2 * (x2 - x1);
+        } 
+    } 
+}
+
 // Draw text (default font) within an image (destination)
 void ImageDrawText(Image *dst, Vector2 position, const char *text, int fontSize, Color color)
 {