Added ability to swap renderers

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-16 16:01:58 +01:00
parent f95c129522
commit 0963869b0c
5 changed files with 197 additions and 34 deletions

View File

@@ -0,0 +1,72 @@
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:wolf_3d_engine/wolf_3d_engine.dart';
import 'package:wolf_3d_input/wolf_3d_input.dart';
// 1. The widget now only requires the engine!
abstract class BaseWolfRenderer extends StatefulWidget {
final WolfEngine engine;
final Wolf3dInput inputManager;
const BaseWolfRenderer({
required this.engine,
required this.inputManager,
super.key,
});
}
abstract class BaseWolfRendererState<T extends BaseWolfRenderer>
extends State<T>
with SingleTickerProviderStateMixin {
late final Ticker gameLoop;
final FocusNode focusNode = FocusNode();
Duration _lastTick = Duration.zero;
@override
void initState() {
super.initState();
// DO NOT initialize the engine here. Just start the loop!
gameLoop = createTicker(_tick)..start();
focusNode.requestFocus();
}
void _tick(Duration elapsed) {
if (!widget.engine.isInitialized) return;
Duration delta = elapsed - _lastTick;
_lastTick = elapsed;
widget.inputManager.update();
// Tick the shared engine
widget.engine.tick(delta, widget.inputManager.currentInput);
performRender();
}
@override
void dispose() {
gameLoop.dispose();
focusNode.dispose();
super.dispose();
}
void performRender();
Widget buildViewport(BuildContext context);
Color get scaffoldColor;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: scaffoldColor,
body: KeyboardListener(
focusNode: focusNode,
autofocus: true,
onKeyEvent: (_) {},
child: Center(
child: buildViewport(context),
),
),
);
}
}