diff --git a/lib/features/renderer/weapon_painter.dart b/lib/features/renderer/weapon_painter.dart index bbe2c7a..8cf204c 100644 --- a/lib/features/renderer/weapon_painter.dart +++ b/lib/features/renderer/weapon_painter.dart @@ -3,21 +3,38 @@ import 'package:wolf_dart/features/renderer/color_palette.dart'; class WeaponPainter extends CustomPainter { final List> sprite; + + // 1. Initialize a reusable Paint object and disable anti-aliasing + // to keep the pixels perfectly sharp and chunky. + final Paint _paint = Paint() + ..isAntiAlias = false + ..style = PaintingStyle.fill; + WeaponPainter({required this.sprite}); @override void paint(Canvas canvas, Size size) { - double pixelSize = size.width / 64; - final paint = Paint(); + // Calculate width and height separately in case the container isn't a perfect square + double pixelWidth = size.width / 64; + double pixelHeight = size.height / 64; + for (int x = 0; x < 64; x++) { for (int y = 0; y < 64; y++) { int colorByte = sprite[x][y]; + if (colorByte != 255) { - // Transparency check - paint.color = ColorPalette.vga[colorByte]; + // 255 is our transparent magenta + _paint.color = ColorPalette.vga[colorByte]; + canvas.drawRect( - Rect.fromLTWH(x * pixelSize, y * pixelSize, pixelSize, pixelSize), - paint, + Rect.fromLTWH( + x * pixelWidth, + y * pixelHeight, + pixelWidth + + 0.5, // 2. Add a tiny 0.5 overlap to completely eliminate visual seams + pixelHeight + 0.5, + ), + _paint, ); } } @@ -25,5 +42,9 @@ class WeaponPainter extends CustomPainter { } @override - bool shouldRepaint(covariant WeaponPainter oldDelegate) => true; + bool shouldRepaint(covariant WeaponPainter oldDelegate) { + // 3. ONLY repaint if the actual animation frame (sprite) has changed! + // This saves massive amounts of CPU when the player is just walking around. + return oldDelegate.sprite != sprite; + } }