Improved weapon rendering
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -3,21 +3,38 @@ import 'package:wolf_dart/features/renderer/color_palette.dart';
|
|||||||
|
|
||||||
class WeaponPainter extends CustomPainter {
|
class WeaponPainter extends CustomPainter {
|
||||||
final List<List<int>> sprite;
|
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});
|
WeaponPainter({required this.sprite});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void paint(Canvas canvas, Size size) {
|
void paint(Canvas canvas, Size size) {
|
||||||
double pixelSize = size.width / 64;
|
// Calculate width and height separately in case the container isn't a perfect square
|
||||||
final paint = Paint();
|
double pixelWidth = size.width / 64;
|
||||||
|
double pixelHeight = size.height / 64;
|
||||||
|
|
||||||
for (int x = 0; x < 64; x++) {
|
for (int x = 0; x < 64; x++) {
|
||||||
for (int y = 0; y < 64; y++) {
|
for (int y = 0; y < 64; y++) {
|
||||||
int colorByte = sprite[x][y];
|
int colorByte = sprite[x][y];
|
||||||
|
|
||||||
if (colorByte != 255) {
|
if (colorByte != 255) {
|
||||||
// Transparency check
|
// 255 is our transparent magenta
|
||||||
paint.color = ColorPalette.vga[colorByte];
|
_paint.color = ColorPalette.vga[colorByte];
|
||||||
|
|
||||||
canvas.drawRect(
|
canvas.drawRect(
|
||||||
Rect.fromLTWH(x * pixelSize, y * pixelSize, pixelSize, pixelSize),
|
Rect.fromLTWH(
|
||||||
paint,
|
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
|
@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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user