Optimizes texture drawing by removing a canvas.translate call

This commit is contained in:
Viktor Lidholt 2015-08-04 10:16:06 -07:00
parent 4ff5339a9a
commit e045452085

View File

@ -74,17 +74,18 @@ class Texture {
}
void drawTexture(PaintingCanvas canvas, Point position, Paint paint) {
// Account for position
// Get drawing position
double x = position.x;
double y = position.y;
bool translate = (x != 0 || y != 0);
if (translate) {
canvas.translate(x, y);
}
// Draw the texture
if (rotated) {
// Account for position
bool translate = (x != 0 || y != 0);
if (translate) {
canvas.translate(x, y);
}
// Calculate the rotated frame and spriteSourceSize
Size originalFrameSize = frame.size;
Rect rotatedFrame = frame.topLeft & new Size(originalFrameSize.height, originalFrameSize.width);
@ -97,14 +98,15 @@ class Texture {
canvas.rotate(-math.PI/2.0);
canvas.drawImageRect(image, rotatedFrame, rotatedSpriteSourceSize, paint);
canvas.rotate(math.PI/2.0);
// Translate back
if (translate) {
canvas.translate(-x, -y);
}
} else {
// Draw the sprite
canvas.drawImageRect(image, frame, spriteSourceSize, paint);
}
// Translate back
if (translate) {
canvas.translate(-x, -y);
Rect dstRect = new Rect.fromLTWH(x + spriteSourceSize.left, y + spriteSourceSize.top, spriteSourceSize.width, spriteSourceSize.height);
canvas.drawImageRect(image, frame, dstRect, paint);
}
}
}