mirror of
https://github.com/raysan5/raylib.git
synced 2025-09-08 04:18:15 +00:00
Faster version of ImageClearBackground and ImageDrawRectangleRec (#1487)
* Don't use DrawRect to clear an image, a pixel loop is an order of magnitude faster. * Update ImageDrawRectangle to be faster too.
This commit is contained in:
@@ -2348,7 +2348,8 @@ Rectangle GetImageAlphaBorder(Image image, float threshold)
|
|||||||
// Clear image background with given color
|
// Clear image background with given color
|
||||||
void ImageClearBackground(Image *dst, Color color)
|
void ImageClearBackground(Image *dst, Color color)
|
||||||
{
|
{
|
||||||
ImageDrawRectangle(dst, 0, 0, dst->width, dst->height, color);
|
for (int i = 0; i < dst->width * dst->height; ++i)
|
||||||
|
ImageDrawPixel(dst, i % dst->width, i / dst->height, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw pixel within an image
|
// Draw pixel within an image
|
||||||
@@ -2546,9 +2547,19 @@ void ImageDrawRectangleRec(Image *dst, Rectangle rec, Color color)
|
|||||||
// Security check to avoid program crash
|
// Security check to avoid program crash
|
||||||
if ((dst->data == NULL) || (dst->width == 0) || (dst->height == 0)) return;
|
if ((dst->data == NULL) || (dst->width == 0) || (dst->height == 0)) return;
|
||||||
|
|
||||||
Image imRec = GenImageColor((int)rec.width, (int)rec.height, color);
|
int sy = (int)rec.y;
|
||||||
ImageDraw(dst, imRec, (Rectangle){ 0, 0, rec.width, rec.height }, rec, WHITE);
|
int ey = sy + (int)rec.height;
|
||||||
UnloadImage(imRec);
|
|
||||||
|
int sx = (int)rec.x;
|
||||||
|
int ex = sx + (int)rec.width;
|
||||||
|
|
||||||
|
for (int y = sy; y < ey; y++)
|
||||||
|
{
|
||||||
|
for (int x = sx; x < ex; x++)
|
||||||
|
{
|
||||||
|
ImageDrawPixel(dst, x, y, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw rectangle lines within an image
|
// Draw rectangle lines within an image
|
||||||
|
Reference in New Issue
Block a user