run method

Future<void> run()

Implementation

Future<void> run() async {
  var previousTick = DateTime.now();
  var timer60hz = Timer.periodic(const Duration(milliseconds: 1000 ~/ 60), timer60hzCallback);
  var stopIsolateReceived = false;

  var subscription = _receivePort.listen((event) {
    if (stopIsolateReceived) return;
    if (event is KeyPressedEvent) _chip8.keysPressedStatus[event.key] = true;
    if (event is KeyReleasedEvent) _chip8.keysPressedStatus[event.key] = false;
    if (event is StopIsolateEvent) stopIsolateReceived = true;
  });

  while (!stopIsolateReceived) {
    tick();
    var now = DateTime.now();

    var diff = now.subtract(Duration(milliseconds: previousTick.millisecondsSinceEpoch)).millisecondsSinceEpoch;

    if (diff <= 1000 / 600) {
      await Future.delayed(Duration(milliseconds: (1000 - diff) ~/ 600));
    }

    previousTick = now;
  }

  timer60hz.cancel();
  subscription.cancel();
  _receivePort.close();

  _sendPort.send("terminate");
}