optimization - Dart 2 中的最佳渲染循环是什么?

标签 optimization dart flutter game-engine skia

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

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

例如,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);
  }
}

最佳答案

好吧,经过一个月的反对,我终于想出了正确的问题,这让我想到了这个: 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();
}

关于optimization - Dart 2 中的最佳渲染循环是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53841546/

相关文章:

Mysql,仅选择可用的公司并按一个或多个日期时间范围排除其余公司(当它们不起作用时)

list - 从 API 获取值后从 Map<String, dynamic> 中检索值

camera - 是否可以使用 flutter 相机插件流式传输视频?

flutter - 为什么我不能定义名称 '$url'?

flutter - 使用 Flutter intl 扩展在 Flutter 中组织 .arb 文件

java - 有没有办法为下次运行保存 JAVA JIT 信息,这样我就不用每天都预热代码了?

c++ - 如何在C++中实现R的 "optimize"函数?

mysql - 如何防止使用 ON cond1 或 cond2 的 SQL INNER JOIN 忽略键并执行全表扫描

dart - polymer Dart 核心样式引用未解决

flutter - Flutter Riverpod小部件调用了两次