148 lines
3.5 KiB
Dart
148 lines
3.5 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:wolf_3d_dart/wolf_3d_engine.dart';
|
|
import 'package:wolf_3d_dart/wolf_3d_input.dart';
|
|
import 'package:wolf_3d_dart/wolf_3d_rasterizer.dart';
|
|
|
|
class CliGameLoop {
|
|
CliGameLoop({
|
|
required this.engine,
|
|
required this.onExit,
|
|
}) : input = engine.input is CliInput
|
|
? engine.input as CliInput
|
|
: throw ArgumentError.value(
|
|
engine.input,
|
|
'engine.input',
|
|
'CliGameLoop requires a CliInput instance.',
|
|
),
|
|
primaryRasterizer = SixelRasterizer(),
|
|
secondaryRasterizer = AsciiRasterizer(isTerminal: true) {
|
|
_rasterizer = primaryRasterizer;
|
|
}
|
|
|
|
final WolfEngine engine;
|
|
final CliRasterizer primaryRasterizer;
|
|
final CliRasterizer secondaryRasterizer;
|
|
final CliInput input;
|
|
final void Function(int code) onExit;
|
|
|
|
final Stopwatch _stopwatch = Stopwatch();
|
|
final Stream<List<int>> _stdinStream = stdin.asBroadcastStream();
|
|
late CliRasterizer _rasterizer;
|
|
StreamSubscription<List<int>>? _stdinSubscription;
|
|
Timer? _timer;
|
|
bool _isRunning = false;
|
|
Duration _lastTick = Duration.zero;
|
|
|
|
Future<void> start() async {
|
|
if (_isRunning) {
|
|
return;
|
|
}
|
|
|
|
if (primaryRasterizer is SixelRasterizer) {
|
|
final sixel = primaryRasterizer as SixelRasterizer;
|
|
sixel.isSixelSupported = await SixelRasterizer.checkTerminalSixelSupport(
|
|
inputStream: _stdinStream,
|
|
);
|
|
}
|
|
|
|
if (stdin.hasTerminal) {
|
|
try {
|
|
stdin.echoMode = false;
|
|
stdin.lineMode = false;
|
|
} catch (_) {
|
|
// Keep running without raw mode when stdin is not mutable.
|
|
}
|
|
}
|
|
|
|
stdout.write('\x1b[?25l\x1b[2J');
|
|
|
|
_stdinSubscription = _stdinStream.listen(_handleInput);
|
|
_stopwatch.start();
|
|
_timer = Timer.periodic(const Duration(milliseconds: 33), _tick);
|
|
_isRunning = true;
|
|
}
|
|
|
|
void stop() {
|
|
if (!_isRunning) {
|
|
return;
|
|
}
|
|
|
|
_timer?.cancel();
|
|
_timer = null;
|
|
_stdinSubscription?.cancel();
|
|
_stdinSubscription = null;
|
|
|
|
if (_stopwatch.isRunning) {
|
|
_stopwatch.stop();
|
|
}
|
|
|
|
if (stdin.hasTerminal) {
|
|
try {
|
|
stdin.echoMode = true;
|
|
stdin.lineMode = true;
|
|
} catch (_) {
|
|
// Ignore cleanup failures if stdin is no longer a mutable TTY.
|
|
}
|
|
}
|
|
|
|
if (stdout.hasTerminal) {
|
|
stdout.write('\x1b[0m\x1b[?25h');
|
|
}
|
|
|
|
_isRunning = false;
|
|
}
|
|
|
|
void _handleInput(List<int> bytes) {
|
|
if (bytes.contains(113) || bytes.contains(27)) {
|
|
stop();
|
|
onExit(0);
|
|
return;
|
|
}
|
|
|
|
if (bytes.contains(9)) {
|
|
_rasterizer = identical(_rasterizer, secondaryRasterizer)
|
|
? primaryRasterizer
|
|
: secondaryRasterizer;
|
|
stdout.write('\x1b[2J\x1b[H');
|
|
return;
|
|
}
|
|
|
|
input.handleKey(bytes);
|
|
}
|
|
|
|
void _tick(Timer timer) {
|
|
if (!_isRunning) {
|
|
return;
|
|
}
|
|
|
|
if (stdout.hasTerminal) {
|
|
final int cols = stdout.terminalColumns;
|
|
final int rows = stdout.terminalLines;
|
|
if (!_rasterizer.prepareTerminalFrame(
|
|
engine,
|
|
columns: cols,
|
|
rows: rows,
|
|
)) {
|
|
stdout.write('\x1b[2J\x1b[H');
|
|
stdout.write(
|
|
_rasterizer.buildTerminalSizeWarning(columns: cols, rows: rows),
|
|
);
|
|
|
|
_lastTick = _stopwatch.elapsed;
|
|
return;
|
|
}
|
|
}
|
|
|
|
final Duration currentTick = _stopwatch.elapsed;
|
|
final Duration elapsed = currentTick - _lastTick;
|
|
_lastTick = currentTick;
|
|
|
|
stdout.write('\x1b[H');
|
|
|
|
engine.tick(elapsed);
|
|
stdout.write(_rasterizer.render(engine));
|
|
}
|
|
}
|