Skip to content

Commit

Permalink
access image using data to avoid multiply
Browse files Browse the repository at this point in the history
  • Loading branch information
g-apparence committed Aug 19, 2021
1 parent 5669ebc commit abcd71c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 38 deletions.
4 changes: 2 additions & 2 deletions example/lib/main.dart
Expand Up @@ -27,8 +27,8 @@ class MyPage extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
body: RippleEffect(
pulsations: 2.4,
dampening: .95,
pulsations: 2.2,
dampening: .958,
child: Container(
width: double.infinity,
decoration: BoxDecoration(
Expand Down
6 changes: 0 additions & 6 deletions lib/ripple_effect.dart
@@ -1,7 +1 @@
library ripple_effect;

/// A Calculator.
class Calculator {
/// Returns [value] plus 1.
int addOne(int value) => value + 1;
}
35 changes: 18 additions & 17 deletions lib/ripple_model.dart
Expand Up @@ -22,7 +22,6 @@ class WaterRipple {
final img.Image _image, _sourceImg;
final Pointer<Double> f64;
final Pointer<Int32> i32;
DateTime? lastUpdate;

WaterRipple._({
required this.width,
Expand Down Expand Up @@ -94,41 +93,31 @@ class WaterRipple {
}
}

final f64 = this.f64;
final i32 = this.i32;

for (int y = widthR, yi = 1; y < yLimit; y += widthR, yi++) {
for (int x = 1; x < widthR - 1; x += 1) {
final tileIndex = x + y;
var xOffsetD = (current[tileIndex + 1] - current[tileIndex - 1]);
var yOffsetD = (current[tileIndex + widthR] - current[tileIndex - widthR]);

// HACK: work-around lack of fast toInt().
// Filed http://dartbug.com/46876
// See https://stackoverflow.com/a/17035583/662844 for an explanation
// of this trick.
f64.value = xOffsetD + 6755399441055744.0;
var xOffset = i32.value;

f64.value = yOffsetD + 6755399441055744.0;
var yOffset = i32.value;
var xOffset = round(xOffsetD);
var yOffset = round(yOffsetD);

xOffset = clamp(xOffset, -8, 8);
yOffset = clamp(yOffset, -8, 8);
var pixel2Src = _sourceImg.getPixel(
final index = x + y;
final pixel2Src = _sourceImg.getPixel(
clamp(x + xOffset, 0, widthR - 1),
clamp(yi + yOffset, 0, heightR - 1),
);

_image.setPixel(x, yi, pixel2Src);
_image.data[index] = pixel2Src;
}
}
// copy current to previous
var temp = previous;
previous = current;
current = temp;
updating = false;
lastUpdate = DateTime.now();
// print("updated in ${sw.elapsedMilliseconds}ms");
}

// HACK: int.clamp is not always inlined. filed http://dartbug.com/46879
Expand All @@ -142,6 +131,18 @@ class WaterRipple {
return v;
}

@pragma('vm:prefer-inline')
int round(double value) {
// HACK: work-around lack of fast toInt().
// Filed http://dartbug.com/46876
// See https://stackoverflow.com/a/17035583/662844 for an explanation
// of this trick.
final f64 = this.f64;
final i32 = this.i32;
f64.value = value + 6755399441055744.0;
return i32.value;
}

void touch(double x, double y, int radius, double force) {
var dy = y ~/ ratio;
var dx = x ~/ ratio;
Expand Down
2 changes: 1 addition & 1 deletion lib/ripple_renderobject.dart
Expand Up @@ -15,7 +15,7 @@ class RippleRenderObject extends SingleChildRenderObjectWidget {
void updateRenderObject(BuildContext context, covariant RippleRender renderObject) {
renderObject.image = image;
renderObject.markNeedsPaint();
renderObject.markNeedsLayout();
// renderObject.markNeedsLayout();
}
}

Expand Down
12 changes: 0 additions & 12 deletions test/ripple_effect_test.dart

This file was deleted.

0 comments on commit abcd71c

Please sign in to comment.