Skip to content

Commit

Permalink
extension
Browse files Browse the repository at this point in the history
  • Loading branch information
renancaraujo committed Dec 14, 2024
1 parent cf62524 commit 61c83c6
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 20 deletions.
9 changes: 4 additions & 5 deletions packages/flame/lib/src/components/mixins/snapshot.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'dart:typed_data';
import 'dart:ui';

import 'package:flame/components.dart';
import 'package:vector_math/vector_math.dart';
import 'package:flame/extensions_matrix4.dart';

/// A mixin that enables caching a component and all its children. If
/// [renderSnapshot] is set to `true`, the component and its children will be
Expand Down Expand Up @@ -49,7 +48,7 @@ mixin Snapshot on PositionComponent {
} else {
final recorder = PictureRecorder();
final canvas = Canvas(recorder);
canvas.transform(Float64List.sublistView(transform.storage));
canvas.transform(transform.storage64);
canvas.drawPicture(_picture!);
final picture = recorder.endRecording();
return picture.toImageSync(width, height);
Expand All @@ -65,7 +64,7 @@ mixin Snapshot on PositionComponent {
final canvas = Canvas(recorder);
final matrix = transformMatrix.clone();
matrix.invert();
canvas.transform(Float64List.sublistView(matrix.storage));
canvas.transform(matrix.storage64);
super.renderTree(canvas);
_picture = recorder.endRecording();
return _picture!;
Expand All @@ -84,7 +83,7 @@ mixin Snapshot on PositionComponent {
takeSnapshot();
}
canvas.save();
canvas.transform(Float64List.sublistView(transformMatrix.storage));
canvas.transform(transformMatrix.storage64);
canvas.drawPicture(_picture!);
canvas.restore();
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/flame/lib/src/extensions/canvas.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:typed_data';
import 'dart:ui';

import 'package:flame/extensions_matrix4.dart';
import 'package:flame/palette.dart';
import 'package:flame/src/extensions/vector2.dart';
import 'package:flame/src/game/transform2d.dart';
Expand Down Expand Up @@ -60,6 +60,6 @@ extension CanvasExtension on Canvas {

/// Use the [Transform2D] object to [transform] the canvas.
void transform2D(Transform2D transform2D) {
transform(Float64List.sublistView(transform2D.transformMatrix.storage));
transform(transform2D.transformMatrix.storage64);
}
}
4 changes: 4 additions & 0 deletions packages/flame/lib/src/extensions/matrix4.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:typed_data';

import 'package:vector_math/vector_math.dart';

export 'package:vector_math/vector_math.dart' hide Colors;
Expand Down Expand Up @@ -78,6 +80,8 @@ extension Matrix4Extension on Matrix4 {
return transform2(out);
}

Float64List get storage64 => Float64List.sublistView(storage);

/// Create a scaled matrix.
///
/// Scale by a [Vector3], [Vector4], or x,y,z
Expand Down
5 changes: 2 additions & 3 deletions packages/flame/lib/src/rendering/rotate3d_decorator.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import 'dart:typed_data';
import 'dart:ui';

import 'package:flame/extensions_matrix4.dart';
import 'package:flame/geometry.dart';
import 'package:flame/src/rendering/decorator.dart';
import 'package:vector_math/vector_math.dart';

/// [Rotate3DDecorator] treats the underlying component as if it was a flat
/// sheet of paper, and applies a 3D rotation to it.
Expand Down Expand Up @@ -62,7 +61,7 @@ class Rotate3DDecorator extends Decorator {
..rotateY(angleY)
..rotateZ(angleZ)
..translate(-center.x, -center.y);
canvas.transform(Float64List.sublistView(matrix.storage));
canvas.transform(matrix.storage64);
draw(canvas);
canvas.restore();
}
Expand Down
5 changes: 2 additions & 3 deletions packages/flame/lib/src/rendering/shadow3d_decorator.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import 'dart:typed_data';
import 'dart:ui';

import 'package:flame/extensions_matrix4.dart';
import 'package:flame/src/palette.dart';
import 'package:flame/src/rendering/decorator.dart';
import 'package:vector_math/vector_math.dart' show Matrix4, Vector2;

/// [Shadow3DDecorator] casts a realistic-looking shadow from the component
/// onto the ground.
Expand Down Expand Up @@ -157,7 +156,7 @@ class Shadow3DDecorator extends Decorator {

canvas.saveLayer(null, _paint!);
canvas.translate(base.x + _shift, base.y);
canvas.transform(Float64List.sublistView(_transformMatrix!.storage));
canvas.transform(_transformMatrix!.storage64);
draw(canvas);
canvas.restore();
draw(canvas);
Expand Down
5 changes: 2 additions & 3 deletions packages/flame/lib/src/rendering/transform2d_decorator.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:typed_data';
import 'dart:ui';

import 'package:flame/extensions_matrix4.dart';
import 'package:flame/src/components/position_component.dart';
import 'package:flame/src/game/transform2d.dart';
import 'package:flame/src/rendering/decorator.dart';
Expand All @@ -18,8 +18,7 @@ class Transform2DDecorator extends Decorator {
@override
void apply(void Function(Canvas) draw, Canvas canvas) {
canvas.save();
canvas.transform(
Float64List.sublistView(transform2d.transformMatrix.storage));
canvas.transform(transform2d.transformMatrix.storage64);
draw(canvas);
canvas.restore();
}
Expand Down
5 changes: 2 additions & 3 deletions packages/flame/lib/src/sprite_batch.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import 'dart:collection';
import 'dart:math' show pi;
import 'dart:typed_data';
import 'dart:ui';

import 'package:flame/cache.dart';
import 'package:flame/extensions.dart';
import 'package:flame/extensions_matrix4.dart';
import 'package:flame/flame.dart';
import 'package:flame/game.dart';
import 'package:meta/meta.dart';
import 'package:vector_math/vector_math.dart';

extension SpriteBatchExtension on Game {
/// Utility method to load and cache the image for a [SpriteBatch] based on
Expand Down Expand Up @@ -433,7 +432,7 @@ class SpriteBatch {

canvas
..save()
..transform(Float64List.sublistView(batchItem.matrix.storage))
..transform(batchItem.matrix.storage64)
..drawRect(batchItem.destination, batchItem.paint)
..drawImageRect(
atlas,
Expand Down
2 changes: 1 addition & 1 deletion packages/flame_forge2d/lib/body_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class BodyComponent<T extends Forge2DGame> extends Component
_lastAngle = angle;
}
canvas.save();
canvas.transform(Float64List.sublistView(matrix.storage));
canvas.transform(matrix.storage64);
super.renderTree(canvas);
canvas.restore();
}
Expand Down

0 comments on commit 61c83c6

Please sign in to comment.