summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorsmalltimewizard <[email protected]>2023-07-20 07:50:56 -0400
committerGitHub <[email protected]>2023-07-20 13:50:56 +0200
commit1310617a922c12256259eca69da0d746f5d10b71 (patch)
treeacab4ff703793d33022b74a72151cdcba3f14a41 /src
parent5635e4214cb31b0c83e2d65f32af1cc3d40ffdf6 (diff)
downloadraylib-1310617a922c12256259eca69da0d746f5d10b71.tar.gz
raylib-1310617a922c12256259eca69da0d746f5d10b71.zip
Optimization of ImageDrawRectangleRec() (#3185)
A significant performance increase can be had by copying the first row to all other rows.
Diffstat (limited to 'src')
-rw-r--r--src/rtextures.c28
1 files changed, 15 insertions, 13 deletions
diff --git a/src/rtextures.c b/src/rtextures.c
index bc53e1ce..ca341bc8 100644
--- a/src/rtextures.c
+++ b/src/rtextures.c
@@ -3135,26 +3135,28 @@ void ImageDrawRectangleRec(Image *dst, Rectangle rec, Color color)
if (rec.height < 0) rec.height = 0;
int sy = (int)rec.y;
- int ey = sy + (int)rec.height;
-
int sx = (int)rec.x;
int bytesPerPixel = GetPixelDataSize(1, 1, dst->format);
- for (int y = sy; y < ey; y++)
- {
- // Fill in the first pixel of the row based on image format
- ImageDrawPixel(dst, sx, y, color);
+ // Fill in the first pixel of the first row based on image format
+ ImageDrawPixel(dst, sx, sy, color);
- int bytesOffset = ((y*dst->width) + sx)*bytesPerPixel;
- unsigned char *pSrcPixel = (unsigned char *)dst->data + bytesOffset;
+ int bytesOffset = ((sy*dst->width) + sx)*bytesPerPixel;
+ unsigned char *pSrcPixel = (unsigned char *)dst->data + bytesOffset;
- // Repeat the first pixel data throughout the row
- for (int x = 1; x < (int)rec.width; x++)
- {
- memcpy(pSrcPixel + x*bytesPerPixel, pSrcPixel, bytesPerPixel);
- }
+ // Repeat the first pixel data throughout the row
+ for (int x = 1; x < (int)rec.width; x++)
+ {
+ memcpy(pSrcPixel + x*bytesPerPixel, pSrcPixel, bytesPerPixel);
}
+
+ // Repeat the first row data for all other rows
+ int bytesPerRow = bytesPerPixel * (int)rec.width;
+ for (int y = 1; y < (int)rec.height; y++)
+ {
+ memcpy(pSrcPixel + (y*dst->width)*bytesPerPixel, pSrcPixel, bytesPerRow);
+ }
}
// Draw rectangle lines within an image