Unified asset painter and added to package. Fixes and simplifes sprite rendering.
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
||||
import 'package:wolf_3d_dart/wolf_3d_data_types.dart';
|
||||
import 'package:wolf_3d_dart/wolf_3d_entities.dart';
|
||||
import 'package:wolf_3d_flutter/wolf_3d.dart';
|
||||
import 'package:wolf_3d_renderer/wolf_3d_asset_painter.dart';
|
||||
|
||||
class SpriteGallery extends StatelessWidget {
|
||||
final List<Sprite> sprites;
|
||||
@@ -53,10 +54,7 @@ class SpriteGallery extends StatelessWidget {
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
Expanded(
|
||||
child: CustomPaint(
|
||||
painter: SingleSpritePainter(sprite: sprites[index]),
|
||||
size: const Size(64, 64),
|
||||
),
|
||||
child: WolfAssetPainter.sprite(sprites[index]),
|
||||
),
|
||||
],
|
||||
);
|
||||
@@ -65,28 +63,3 @@ class SpriteGallery extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SingleSpritePainter extends CustomPainter {
|
||||
final Sprite sprite;
|
||||
SingleSpritePainter({required this.sprite});
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
double pixelSize = size.width / 64;
|
||||
for (int x = 0; x < 64; x++) {
|
||||
for (int y = 0; y < 64; y++) {
|
||||
int colorByte = sprite.pixels[x * 64 + y];
|
||||
if (colorByte != 255) {
|
||||
// Skip transparency
|
||||
canvas.drawRect(
|
||||
Rect.fromLTWH(x * pixelSize, y * pixelSize, pixelSize, pixelSize),
|
||||
Paint()..color = Color(ColorPalette.vga32Bit[colorByte]),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(CustomPainter oldDelegate) => false;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:wolf_3d_dart/wolf_3d_data_types.dart';
|
||||
import 'package:wolf_3d_renderer/wolf_3d_asset_painter.dart';
|
||||
|
||||
class VgaGallery extends StatelessWidget {
|
||||
final List<VgaImage> images;
|
||||
@@ -30,14 +31,7 @@ class VgaGallery extends StatelessWidget {
|
||||
const SizedBox(height: 8),
|
||||
Expanded(
|
||||
child: Center(
|
||||
child: CustomPaint(
|
||||
painter: VgaPainter(image: images[index]),
|
||||
// Scale it up so tiny fonts are readable
|
||||
size: Size(
|
||||
images[index].width * 2.0,
|
||||
images[index].height * 2.0,
|
||||
),
|
||||
),
|
||||
child: WolfAssetPainter.vga(images[index]),
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -47,49 +41,3 @@ class VgaGallery extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class VgaPainter extends CustomPainter {
|
||||
final VgaImage image;
|
||||
VgaPainter({required this.image});
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
int planeWidth = image.width ~/ 4;
|
||||
int planeSize = planeWidth * image.height;
|
||||
|
||||
double pixelW = size.width / image.width;
|
||||
double pixelH = size.height / image.height;
|
||||
final Paint paint = Paint()..isAntiAlias = false;
|
||||
|
||||
for (int y = 0; y < image.height; y++) {
|
||||
for (int x = 0; x < image.width; x++) {
|
||||
int plane = x % 4;
|
||||
int sx = x ~/ 4;
|
||||
int colorByte =
|
||||
image.pixels[(plane * planeSize) + (y * planeWidth) + sx];
|
||||
|
||||
if (colorByte != 255) {
|
||||
int abgr = ColorPalette.vga32Bit[colorByte];
|
||||
|
||||
// Extract the bytes
|
||||
int r = abgr & 0xFF;
|
||||
int g = (abgr >> 8) & 0xFF;
|
||||
int b = (abgr >> 16) & 0xFF;
|
||||
int a = (abgr >> 24) & 0xFF;
|
||||
|
||||
// Repack them as ARGB for Flutter's Color object
|
||||
int argb = (a << 24) | (r << 16) | (g << 8) | b;
|
||||
|
||||
paint.color = Color(argb);
|
||||
canvas.drawRect(
|
||||
Rect.fromLTWH(x * pixelW, y * pixelH, pixelW + 0.5, pixelH + 0.5),
|
||||
paint,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user