DART 2中的最佳渲染循环是什么?



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

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

举例来说,这是一个针对C#语言回答的人。我的第一次尝试(下图)是flutter/dart的新手,目前尚未工作,我无法分辨出问题在哪里。

我在没有找到任何帮助的情况下搜索了高低,因此,如果您可以提供帮助,您就会有我永恒的感激之情。

" BYU/INU-NO-NO-POLOCEMEN"(有点老)在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);
  }
}

好吧,经过一个月的殴打,我终于找到了正确的问题,这使我陷入了困境:颤音层/原始

// 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();
}

相关内容

  • 没有找到相关文章

最新更新