import 'dart:ui'; import 'package:flutter/foundation.dart'; import 'flame.dart'; class SpriteBatch { Image atlas; List rects = []; List transforms = []; List colors = []; static const defaultBlendMode = BlendMode.srcOver; static const defaultCullRect = null; static final defaultPaint = Paint(); static final defaultTransform = RSTransform(1, 0, 0, 0); static const defaultColor = const Color(0x00000000); // transparent SpriteBatch(String fileName) { Flame.images.load(fileName).then((image) => atlas = image); } SpriteBatch.fromImage(this.atlas); int get width => atlas?.width; int get height => atlas?.height; Size get size => atlas == null ? null : Size(width.toDouble(), height.toDouble()); void addTransform({ @required Rect rect, RSTransform transform, Color color, }) { rects.add(rect); transforms.add(transform ?? defaultTransform); colors.add(color ?? defaultColor); } void add({ @required Rect rect, double scale = 1.0, Offset anchor = Offset.zero, double rotation = 0, Offset offset = Offset.zero, Color color, }) { final transform = RSTransform.fromComponents( scale: scale, anchorX: anchor.dx, anchorY: anchor.dy, rotation: rotation, translateX: offset.dx, translateY: offset.dy, ); addTransform(rect: rect, transform: transform, color: color); } void clear() { rects.clear(); transforms.clear(); colors.clear(); } void render( Canvas canvas, { BlendMode blendMode, Rect cullRect, Paint paint, }) { canvas.drawAtlas( atlas, transforms, rects, colors, blendMode ?? defaultBlendMode, cullRect ?? defaultCullRect, paint ?? defaultPaint, ); } }