Dart 2 中的最佳渲染循环是啥?

Posted

技术标签:

【中文标题】Dart 2 中的最佳渲染循环是啥?【英文标题】:What is the optimal render loop in Dart 2?Dart 2 中的最佳渲染循环是什么? 【发布时间】:2018-12-18 21:44:51 【问题描述】:

我正在寻找有关 Dart 2 中内部渲染循环的最佳/最小结构的想法,用于 2d 游戏(如果这部分很重要的话)。

澄清/说明:每种框架/语言都有一种有效的方法来: 1)处理时间。 2) 渲染到屏幕上(通过内存、画布、图像或其他方式)。

例如,here is someone that answered this for the C# language。作为 Flutter / Dart 的新手,我的第一次尝试(如下)失败了,截至目前,我无法确定问题出在哪里。

我在这方面找遍了上下,没有找到任何帮助,所以如果你能提供帮助,我将永远感激你。

“byu/inu-no-policemen”(有点旧)在 Reddit 上有一个帖子。我用这个开始。我怀疑它正在破坏垃圾收集器或泄漏内存。

这是我目前所拥有的,但它很快就崩溃了(至少在调试器中):

import 'dart:ui';
import 'dart:typed_data';
import 'dart:math' as math;
import 'dart:async';

main() async 
  var deviceTransform = new Float64List(16)
  ..[0] = 1.0 // window.devicePixelRatio
  ..[5] = 1.0 // window.devicePixelRatio
  ..[10] = 1.0
  ..[15] = 1.0;

  var previous = Duration.zero;

  var initialSize = await Future<Size>(() 
    if (window.physicalSize.isEmpty) 
      var completer = Completer<Size>();
      window.onMetricsChanged = () 
        if (!window.physicalSize.isEmpty) 
          completer.complete(window.physicalSize);
        
      ;
      return completer.future;
    
    return window.physicalSize;
  );

  var world = World(initialSize.width / 2, initialSize.height / 2);

  window.onBeginFrame = (now) 
    // we rebuild the screenRect here since it can change
    var screenRect = Rect.fromLTWH(0.0, 0.0, window.physicalSize.width, window.physicalSize.height);
    var recorder = PictureRecorder();
    var canvas = Canvas(recorder, screenRect);
    var delta = previous == Duration.zero ? Duration.zero : now - previous;
    previous = now;

    var t = delta.inMicroseconds / Duration.microsecondsPerSecond;
    world.update(t);
    world.render(t, canvas);

    var builder = new SceneBuilder()
      ..pushTransform(deviceTransform)
      ..addPicture(Offset.zero, recorder.endRecording())
      ..pop();

    window.render(builder.build());
    window.scheduleFrame();
  ;

  window.scheduleFrame();

  window.onPointerDataPacket = (packet) 
    var p = packet.data.first;
    world.input(p.physicalX, p.physicalY);
  ;


class World 
  static var _objectColor = Paint()..color = Color(0xa0a0a0ff);
  static var _s = 200.0;
  static var _obejectRect = Rect.fromLTWH(-_s / 2, -_s / 2, _s, _s);
  static var _rotationsPerSecond = 0.25;
  var _turn = 0.0;
  double _x;
  double _y;

  World(this._x, this._y);

  void input(double x, double y)  _x = x; _y = y; 
  void update(double t)  _turn += t * _rotationsPerSecond; 
  void render(double t, Canvas canvas) 
    var tau = math.pi * 2;
    canvas.translate(_x, _y);
    canvas.rotate(tau * _turn);
    canvas.drawRect(_obejectRect, _objectColor);
  

【问题讨论】:

真的不清楚你在问什么。如果您看到崩溃,我建议您提交一个错误。 @IanHickson 我添加了更多解释。如果您仍然不清楚,请详细说明。 【参考方案1】:

好吧,经过一个月的反对,我终于想出了正确的问题,这让我明白了: Flutter Layers / Raw

// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This example shows how to perform a simple animation using the raw interface
// to the engine.

import 'dart:math' as math;
import 'dart:typed_data';
import 'dart:ui' as ui;

void beginFrame(Duration timeStamp) 
  // The timeStamp argument to beginFrame indicates the timing information we
  // should use to clock our animations. It's important to use timeStamp rather
  // than reading the system time because we want all the parts of the system to
  // coordinate the timings of their animations. If each component read the
  // system clock independently, the animations that we processed later would be
  // slightly ahead of the animations we processed earlier.

  // PAINT

  final ui.Rect paintBounds = ui.Offset.zero & (ui.window.physicalSize / ui.window.devicePixelRatio);
  final ui.PictureRecorder recorder = ui.PictureRecorder();
  final ui.Canvas canvas = ui.Canvas(recorder, paintBounds);
  canvas.translate(paintBounds.width / 2.0, paintBounds.height / 2.0);

  // Here we determine the rotation according to the timeStamp given to us by
  // the engine.
  final double t = timeStamp.inMicroseconds / Duration.microsecondsPerMillisecond / 1800.0;
  canvas.rotate(math.pi * (t % 1.0));

  canvas.drawRect(ui.Rect.fromLTRB(-100.0, -100.0, 100.0, 100.0),
                  ui.Paint()..color = const ui.Color.fromARGB(255, 0, 255, 0));
  final ui.Picture picture = recorder.endRecording();

  // COMPOSITE

  final double devicePixelRatio = ui.window.devicePixelRatio;
  final Float64List deviceTransform = Float64List(16)
    ..[0] = devicePixelRatio
    ..[5] = devicePixelRatio
    ..[10] = 1.0
    ..[15] = 1.0;
  final ui.SceneBuilder sceneBuilder = ui.SceneBuilder()
    ..pushTransform(deviceTransform)
    ..addPicture(ui.Offset.zero, picture)
    ..pop();
  ui.window.render(sceneBuilder.build());

  // After rendering the current frame of the animation, we ask the engine to
  // schedule another frame. The engine will call beginFrame again when its time
  // to produce the next frame.
  ui.window.scheduleFrame();


void main() 
  ui.window.onBeginFrame = beginFrame;
  ui.window.scheduleFrame();

【讨论】:

绝对可以看出这需要一些头脑才能弄清楚。有帮助,谢谢!

以上是关于Dart 2 中的最佳渲染循环是啥?的主要内容,如果未能解决你的问题,请参考以下文章

在非常相似的组件的情况下,进行条件渲染的最佳方法是啥?

渲染具有大量节点的 Vuetify v-treeview 的最佳方法是啥?

使用 jquery 删除活动存储附件并重新渲染附件有帮助吗?最佳做法是啥?

为 Flash 到 ios 项目渲染水平可拖动的影片剪辑列表的最佳方法是啥

在 iOS 中使用图层和部分渲染来提高速度的最佳方法是啥

像 next.js 这样的服务器端渲染中状态管理的最佳方法是啥?