diff options
| author | Jeffery Myers <[email protected]> | 2020-12-29 11:39:53 -0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-12-29 20:39:53 +0100 |
| commit | 59bb7598559368468051c06be322511f04c4b0e9 (patch) | |
| tree | 4303b909d68ff9d692ea508c4be185f39dc10182 /src/textures.c | |
| parent | 75c6fd047bed24146fc6dca0c4a51c8555bc02e3 (diff) | |
| download | raylib-59bb7598559368468051c06be322511f04c4b0e9.tar.gz raylib-59bb7598559368468051c06be322511f04c4b0e9.zip | |
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.
Diffstat (limited to 'src/textures.c')
| -rw-r--r-- | src/textures.c | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/src/textures.c b/src/textures.c index 8b9d38cd..9291ebbe 100644 --- a/src/textures.c +++ b/src/textures.c @@ -2348,7 +2348,8 @@ Rectangle GetImageAlphaBorder(Image image, float threshold) // Clear image background with given 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 @@ -2546,9 +2547,19 @@ void ImageDrawRectangleRec(Image *dst, Rectangle rec, Color color) // Security check to avoid program crash if ((dst->data == NULL) || (dst->width == 0) || (dst->height == 0)) return; - Image imRec = GenImageColor((int)rec.width, (int)rec.height, color); - ImageDraw(dst, imRec, (Rectangle){ 0, 0, rec.width, rec.height }, rec, WHITE); - UnloadImage(imRec); + int sy = (int)rec.y; + int ey = sy + (int)rec.height; + + 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 |
