Improved weapon rendering

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-13 22:37:22 +01:00
parent cf357b164d
commit 3086e5a8f4

View File

@@ -3,21 +3,38 @@ import 'package:wolf_dart/features/renderer/color_palette.dart';
class WeaponPainter extends CustomPainter {
final List<List<int>> 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;
}
}