Commit bc496459 authored by Viktor Lidholt's avatar Viktor Lidholt

Optimizes sprite performance by reusing Paint objects

parent 0041182d
...@@ -29,6 +29,8 @@ class Sprite extends NodeWithSize { ...@@ -29,6 +29,8 @@ class Sprite extends NodeWithSize {
/// mySprite.transferMode = TransferMode.plusMode; /// mySprite.transferMode = TransferMode.plusMode;
TransferMode transferMode; TransferMode transferMode;
Paint _cachedPaint = new Paint();
/// Creates a new sprite from the provided [texture]. /// Creates a new sprite from the provided [texture].
/// ///
/// var mySprite = new Sprite(myTexture) /// var mySprite = new Sprite(myTexture)
...@@ -94,17 +96,16 @@ class Sprite extends NodeWithSize { ...@@ -94,17 +96,16 @@ class Sprite extends NodeWithSize {
canvas.scale(scaleX, scaleY); canvas.scale(scaleX, scaleY);
// Setup paint object for opacity and transfer mode // Setup paint object for opacity and transfer mode
Paint paint = new Paint(); _cachedPaint.color = new Color.fromARGB((255.0*_opacity).toInt(), 255, 255, 255);
paint.color = new Color.fromARGB((255.0*_opacity).toInt(), 255, 255, 255);
if (colorOverlay != null) { if (colorOverlay != null) {
paint.setColorFilter(new ColorFilter.mode(colorOverlay, TransferMode.srcATop)); _cachedPaint.setColorFilter(new ColorFilter.mode(colorOverlay, TransferMode.srcATop));
} }
if (transferMode != null) { if (transferMode != null) {
paint.setTransferMode(transferMode); _cachedPaint.setTransferMode(transferMode);
} }
// Do actual drawing of the sprite // Do actual drawing of the sprite
texture.drawTexture(canvas, Point.origin, paint); texture.drawTexture(canvas, Point.origin, _cachedPaint);
} else { } else {
// Paint a red square for missing texture // Paint a red square for missing texture
canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, size.width, size.height), canvas.drawRect(new Rect.fromLTRB(0.0, 0.0, size.width, size.height),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment