Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Use 32-bit vector_math #3405

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:flame/extensions.dart' show OffsetExtension;
import 'package:flame/game.dart';
import 'package:flame/geometry.dart';
import 'package:flame/input.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/widgets.dart';

class CameraComponentExample extends FlameGame with PanDetector {
static const description = '''
Expand Down
1 change: 0 additions & 1 deletion packages/flame/lib/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export 'src/extensions/color.dart';
export 'src/extensions/double.dart';
export 'src/extensions/image.dart';
export 'src/extensions/list.dart';
export 'src/extensions/matrix4.dart';
export 'src/extensions/offset.dart';
export 'src/extensions/paint.dart';
export 'src/extensions/picture.dart';
Expand Down
1 change: 1 addition & 0 deletions packages/flame/lib/extensions_matrix4.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'src/extensions/matrix4.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flame/src/camera/viewport.dart';
import 'package:flame/src/components/core/component.dart';
import 'package:flame/src/components/position_component.dart';
import 'package:flame/src/effects/provider_interfaces.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// This behavior will make the [owner] follow the [target].
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:ui';

import 'package:flame/src/camera/viewport.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// A fixed-size viewport in the shape of a circle (or ellipse).
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:ui';

import 'package:flame/src/camera/viewport.dart';
import 'package:meta/meta.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// [FixedAspectRatioViewport] is a rectangular viewport which auto-expands to
/// take as much space as possible within the canvas, while maintaining a fixed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:ui';

import 'package:flame/src/camera/viewport.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// A rectangular viewport with fixed dimensions.
///
Expand Down
2 changes: 1 addition & 1 deletion packages/flame/lib/src/camera/viewports/max_viewport.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:ui';

import 'package:flame/src/camera/viewport.dart';
import 'package:meta/meta.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// The default viewport, which is as big as the game canvas allows.
///
Expand Down
2 changes: 1 addition & 1 deletion packages/flame/lib/src/camera/world.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:ui';
import 'package:flame/src/camera/camera_component.dart';
import 'package:flame/src/components/core/component.dart';
import 'package:flame/src/components/mixins/coordinate_transform.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// The root component for all game world elements.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flame/src/components/core/component.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// Interface to be implemented by components that perform a coordinate change.
///
Expand Down
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,6 +1,7 @@
import 'dart:ui';

import 'package:flame/components.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 @@ -47,7 +48,7 @@ mixin Snapshot on PositionComponent {
} else {
final recorder = PictureRecorder();
final canvas = Canvas(recorder);
canvas.transform(transform.storage);
canvas.transform(transform.storage64);
canvas.drawPicture(_picture!);
final picture = recorder.endRecording();
return picture.toImageSync(width, height);
Expand All @@ -63,7 +64,7 @@ mixin Snapshot on PositionComponent {
final canvas = Canvas(recorder);
final matrix = transformMatrix.clone();
matrix.invert();
canvas.transform(matrix.storage);
canvas.transform(matrix.storage64);
super.renderTree(canvas);
_picture = recorder.endRecording();
return _picture!;
Expand All @@ -82,9 +83,7 @@ mixin Snapshot on PositionComponent {
takeSnapshot();
}
canvas.save();
canvas.transform(
transformMatrix.storage,
);
canvas.transform(transformMatrix.storage64);
canvas.drawPicture(_picture!);
canvas.restore();
} else {
Expand Down
1 change: 1 addition & 0 deletions packages/flame/lib/src/components/position_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:flame/src/game/notifying_vector2.dart';
import 'package:flame/src/game/transform2d.dart';
import 'package:flame/src/rendering/decorator.dart';
import 'package:flame/src/rendering/transform2d_decorator.dart';
import 'package:vector_math/vector_math.dart';

/// A [Component] implementation that represents an object that can be
/// freely moved around the screen, rotated, and scaled.
Expand Down
2 changes: 1 addition & 1 deletion packages/flame/lib/src/effects/anchor_by_effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flame/src/anchor.dart';
import 'package:flame/src/effects/anchor_effect.dart';
import 'package:flame/src/effects/controllers/effect_controller.dart';
import 'package:flame/src/effects/provider_interfaces.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// An [AnchorEffect] that changes its target's anchor by the specified offset.
///
Expand Down
2 changes: 1 addition & 1 deletion packages/flame/lib/src/effects/anchor_to_effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flame/src/anchor.dart';
import 'package:flame/src/effects/anchor_effect.dart';
import 'package:flame/src/effects/controllers/effect_controller.dart';
import 'package:flame/src/effects/provider_interfaces.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// An effect that moves the target's anchor to the specified value.
///
Expand Down
2 changes: 1 addition & 1 deletion packages/flame/lib/src/effects/move_by_effect.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flame/src/effects/controllers/effect_controller.dart';
import 'package:flame/src/effects/move_effect.dart';
import 'package:flame/src/effects/provider_interfaces.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// A [MoveEffect] that moves its target by the specified offset vector.
///
Expand Down
2 changes: 1 addition & 1 deletion packages/flame/lib/src/effects/move_to_effect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flame/src/effects/controllers/effect_controller.dart';
import 'package:flame/src/effects/move_by_effect.dart';
import 'package:flame/src/effects/move_effect.dart';
import 'package:flame/src/effects/provider_interfaces.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// A [MoveEffect] that moves its target towards the given destination point.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flame/geometry.dart';
import 'package:flame/math.dart';
import 'package:flame/src/experimental/geometry/shapes/shape.dart';
import 'package:flame/src/game/transform2d.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// An axis-aligned rectangle with rounded corners.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:flame/src/experimental/geometry/shapes/polygon.dart';
import 'package:flame/src/experimental/geometry/shapes/rectangle.dart';
import 'package:flame/src/experimental/geometry/shapes/rounded_rectangle.dart';
import 'package:flame/src/game/transform2d.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// Base class for various 2D geometric primitives defined on a Cartesian
/// coordinate plane.
Expand Down
3 changes: 2 additions & 1 deletion packages/flame/lib/src/extensions/canvas.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 @@ -59,6 +60,6 @@ extension CanvasExtension on Canvas {

/// Use the [Transform2D] object to [transform] the canvas.
void transform2D(Transform2D transform2D) {
transform(transform2D.transformMatrix.storage);
transform(transform2D.transformMatrix.storage64);
}
}
8 changes: 6 additions & 2 deletions packages/flame/lib/src/extensions/matrix4.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:vector_math/vector_math_64.dart';
import 'dart:typed_data';

export 'package:vector_math/vector_math_64.dart' hide Colors;
import 'package:vector_math/vector_math.dart';

export 'package:vector_math/vector_math.dart' hide Colors;

extension Matrix4Extension on Matrix4 {
/// A first row and first column value.
Expand Down Expand Up @@ -78,6 +80,8 @@ extension Matrix4Extension on Matrix4 {
return transform2(out);
}

Float64List get storage64 => Float64List.fromList(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/extensions/vector2.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import 'dart:math';
import 'dart:ui';

import 'package:vector_math/vector_math_64.dart';

export 'package:vector_math/vector_math_64.dart' hide Colors;
import 'package:vector_math/vector_math.dart' hide Colors, Matrix4;
export 'package:vector_math/vector_math.dart' hide Colors, Matrix4;

extension Vector2Extension on Vector2 {
/// This is a reusable vector that can be used within the [Vector2Extension]
Expand Down
4 changes: 2 additions & 2 deletions packages/flame/lib/src/game/notifying_vector2.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/foundation.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// Extension of the standard [Vector2] class, implementing the [ChangeNotifier]
/// functionality. This allows any interested party to be notified when the
Expand Down Expand Up @@ -185,5 +185,5 @@ class NotifyingVector2 extends Vector2 with ChangeNotifier {
}

@override
Float64List get storage => super.storage.asUnmodifiableView();
Float32List get storage => super.storage.asUnmodifiableView();
}
2 changes: 1 addition & 1 deletion packages/flame/lib/src/game/transform2d.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:math' as math;
import 'package:flame/geometry.dart' as geometry;
import 'package:flame/src/game/notifying_vector2.dart';
import 'package:flutter/foundation.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// This class describes a generic 2D transform, which is a combination of
/// translations, rotations, reflections and scaling. These transforms are
Expand Down
2 changes: 1 addition & 1 deletion packages/flame/lib/src/math/tmp_vector2.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:meta/meta.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

/// Use internally when you need a temporary [Vector2] object but don't want to
/// instantiate a new one due to performance.
Expand Down
4 changes: 2 additions & 2 deletions packages/flame/lib/src/rendering/rotate3d_decorator.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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_64.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 @@ -61,7 +61,7 @@ class Rotate3DDecorator extends Decorator {
..rotateY(angleY)
..rotateZ(angleZ)
..translate(-center.x, -center.y);
canvas.transform(matrix.storage);
canvas.transform(matrix.storage64);
draw(canvas);
canvas.restore();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/flame/lib/src/rendering/shadow3d_decorator.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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_64.dart' show Matrix4, Vector2;

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

canvas.saveLayer(null, _paint!);
canvas.translate(base.x + _shift, base.y);
canvas.transform(_transformMatrix!.storage);
canvas.transform(_transformMatrix!.storage64);
draw(canvas);
canvas.restore();
draw(canvas);
Expand Down
3 changes: 2 additions & 1 deletion packages/flame/lib/src/rendering/transform2d_decorator.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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 @@ -17,7 +18,7 @@ class Transform2DDecorator extends Decorator {
@override
void apply(void Function(Canvas) draw, Canvas canvas) {
canvas.save();
canvas.transform(transform2d.transformMatrix.storage);
canvas.transform(transform2d.transformMatrix.storage64);
draw(canvas);
canvas.restore();
}
Expand Down
3 changes: 2 additions & 1 deletion packages/flame/lib/src/sprite_batch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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';
Expand Down Expand Up @@ -431,7 +432,7 @@ class SpriteBatch {

canvas
..save()
..transform(batchItem.matrix.storage)
..transform(batchItem.matrix.storage64)
..drawRect(batchItem.destination, batchItem.paint)
..drawImageRect(
atlas,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void main() {

const dt = 0.11;
for (var i = 0; i < 20; i++) {
expect(followTarget, closeToVector(Vector2(3, 1 + i * dt), 1e-14));
expect(followTarget, closeToVector(Vector2(3, 1 + i * dt), 1e-6));
game.update(dt);
}
});
Expand All @@ -91,7 +91,7 @@ void main() {
game.update(0.01);
expect(
pursuer.position,
closeToVector(target.position, 1e-12),
closeToVector(target.position, 1e-4),
);
}
});
Expand All @@ -111,7 +111,7 @@ void main() {
final distance = speed * i * dt;
expect(
pursuer.position,
closeToVector(Vector2(distance * 0.6, distance * 0.8), 1e-12),
closeToVector(Vector2(distance * 0.6, distance * 0.8), 1e-3),
);
game.update(dt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flame/camera.dart';
import 'package:flame/experimental.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math.dart';

void main() {
group('ViewportAwareBoundsBehavior', () {
Expand Down
6 changes: 3 additions & 3 deletions packages/flame/test/camera/viewfinder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ void main() {
for (var t = 0.0; t < 1.0; t += 0.1) {
expect(
camera.viewfinder.position,
closeToVector(Vector2(5 * t, 13 * t), 1e-14),
closeToVector(Vector2(5 * t, 13 * t), 1e-6),
);
expect(
camera.viewport.position,
closeToVector(Vector2(40 * t, -77 * t), 1e-12),
closeToVector(Vector2(40 * t, -77 * t), 1e-5),
);
game.update(0.1);
}
Expand Down Expand Up @@ -174,7 +174,7 @@ void main() {
await game.ready();

for (var t = 0.0; t < 1.0; t += 0.1) {
expect(camera.viewfinder.zoom, closeTo(1 + t, 1e-14));
expect(camera.viewfinder.zoom, closeTo(1 + t, 1e-6));
game.update(0.1);
}
},
Expand Down
Loading
Loading