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

@@ -1,7 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:wolf_3d_data_types/wolf_3d_data_types.dart'; import 'package:wolf_3d_data_types/wolf_3d_data_types.dart';
import 'package:wolf_3d_flutter/wolf_3d.dart'; import 'package:wolf_3d_flutter/wolf_3d.dart';
import 'package:wolf_3d_renderer/wolf_3d_flutter_renderer.dart'; import 'package:wolf_3d_gui/screens/game_screen.dart';
class DifficultyScreen extends StatefulWidget { class DifficultyScreen extends StatefulWidget {
const DifficultyScreen({ const DifficultyScreen({
@@ -26,12 +26,18 @@ class _DifficultyScreenState extends State<DifficultyScreen> {
Navigator.of(context).pushReplacement( Navigator.of(context).pushReplacement(
MaterialPageRoute( MaterialPageRoute(
builder: (_) => WolfFlutterRenderer( builder: (context) => GameScreen(
Wolf3d.I.activeGame, data: Wolf3d.I.activeGame,
difficulty: difficulty, difficulty: difficulty,
startingEpisode: Wolf3d.I.activeEpisode, startingEpisode: Wolf3d.I.activeEpisode,
audio: Wolf3d.I.audio, audio: Wolf3d.I.audio,
), ),
// builder: (_) => WolfFlutterRenderer(
// Wolf3d.I.activeGame,
// difficulty: difficulty,
// startingEpisode: Wolf3d.I.activeEpisode,
// audio: Wolf3d.I.audio,
// ),
), ),
); );
} }

View File

@@ -0,0 +1,97 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:wolf_3d_data_types/wolf_3d_data_types.dart';
import 'package:wolf_3d_engine/wolf_3d_engine.dart';
import 'package:wolf_3d_flutter/wolf_3d_input_flutter.dart';
import 'package:wolf_3d_input/wolf_3d_input.dart';
import 'package:wolf_3d_renderer/wolf_3d_ascii_renderer.dart';
import 'package:wolf_3d_renderer/wolf_3d_flutter_renderer.dart';
class GameScreen extends StatefulWidget {
final WolfensteinData data;
final Difficulty difficulty;
final int startingEpisode;
final EngineAudio audio;
const GameScreen({
required this.data,
required this.difficulty,
required this.startingEpisode,
required this.audio,
super.key,
});
@override
State<GameScreen> createState() => _GameScreenState();
}
class _GameScreenState extends State<GameScreen> {
late final WolfEngine _engine;
final Wolf3dInput _inputManager = Wolf3dFlutterInput();
bool _useAsciiMode = false;
@override
void initState() {
super.initState();
// The Host Screen owns the engine lifecycle
_engine = WolfEngine(
data: widget.data,
difficulty: widget.difficulty,
startingEpisode: widget.startingEpisode,
audio: widget.audio,
onGameWon: () => Navigator.of(context).pop(),
);
_engine.init();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
// Render the active view
_useAsciiMode
? WolfAsciiRenderer(
engine: _engine,
inputManager: _inputManager,
)
: WolfFlutterRenderer(
engine: _engine,
inputManager: _inputManager,
),
// Invisible listener to trap the 'Tab' key and swap renderers
Focus(
autofocus: false,
onKeyEvent: (node, event) {
if (event is KeyDownEvent &&
event.logicalKey == LogicalKeyboardKey.tab) {
setState(() {
_useAsciiMode = !_useAsciiMode;
});
return KeyEventResult.handled;
}
return KeyEventResult.ignored;
},
child: const SizedBox.shrink(),
),
// Optional: A little UI hint
Positioned(
top: 16,
right: 16,
child: Text(
'Press TAB to swap renderers',
style: TextStyle(
color: Colors.white.withValues(alpha: 0.5),
fontSize: 12,
),
),
),
],
),
);
}
}

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

View File

@@ -4,21 +4,15 @@ import 'package:wolf_3d_data_types/wolf_3d_data_types.dart';
import 'package:wolf_3d_engine/wolf_3d_engine.dart'; import 'package:wolf_3d_engine/wolf_3d_engine.dart';
import 'package:wolf_3d_flutter/wolf_3d_input_flutter.dart'; import 'package:wolf_3d_flutter/wolf_3d_input_flutter.dart';
import 'package:wolf_3d_input/wolf_3d_input.dart'; import 'package:wolf_3d_input/wolf_3d_input.dart';
import 'package:wolf_3d_renderer/base_renderer.dart';
class WolfAsciiRenderer extends StatefulWidget { class WolfAsciiRenderer extends BaseWolfRenderer {
const WolfAsciiRenderer( const WolfAsciiRenderer({
this.data, { required super.engine,
required this.difficulty, required super.inputManager,
required this.startingEpisode,
required this.audio,
super.key, super.key,
}); });
final WolfensteinData data;
final Difficulty difficulty;
final int startingEpisode;
final EngineAudio audio;
@override @override
State<WolfAsciiRenderer> createState() => _WolfAsciiRendererState(); State<WolfAsciiRenderer> createState() => _WolfAsciiRendererState();
} }
@@ -41,10 +35,10 @@ class _WolfAsciiRendererState extends State<WolfAsciiRenderer>
super.initState(); super.initState();
engine = WolfEngine( engine = WolfEngine(
data: widget.data, data: widget.engine.data,
difficulty: widget.difficulty, difficulty: widget.engine.difficulty,
startingEpisode: widget.startingEpisode, startingEpisode: widget.engine.startingEpisode,
audio: widget.audio, audio: widget.engine.audio,
onGameWon: () { onGameWon: () {
Navigator.of(context).pop(); Navigator.of(context).pop();
}, },
@@ -86,7 +80,7 @@ class _WolfAsciiRendererState extends State<WolfAsciiRenderer>
} }
return Scaffold( return Scaffold(
backgroundColor: Colors.black, backgroundColor: Color.fromARGB(255, 4, 64, 64),
body: KeyboardListener( body: KeyboardListener(
focusNode: _focusNode, focusNode: _focusNode,
autofocus: true, autofocus: true,

View File

@@ -6,21 +6,15 @@ import 'package:wolf_3d_data_types/wolf_3d_data_types.dart';
import 'package:wolf_3d_engine/wolf_3d_engine.dart'; import 'package:wolf_3d_engine/wolf_3d_engine.dart';
import 'package:wolf_3d_flutter/wolf_3d_input_flutter.dart'; import 'package:wolf_3d_flutter/wolf_3d_input_flutter.dart';
import 'package:wolf_3d_input/wolf_3d_input.dart'; import 'package:wolf_3d_input/wolf_3d_input.dart';
import 'package:wolf_3d_renderer/base_renderer.dart';
class WolfFlutterRenderer extends StatefulWidget { class WolfFlutterRenderer extends BaseWolfRenderer {
const WolfFlutterRenderer( const WolfFlutterRenderer({
this.data, { required super.engine,
required this.difficulty, required super.inputManager,
required this.startingEpisode,
required this.audio,
super.key, super.key,
}); });
final WolfensteinData data;
final Difficulty difficulty;
final int startingEpisode;
final EngineAudio audio;
@override @override
State<WolfFlutterRenderer> createState() => _WolfFlutterRendererState(); State<WolfFlutterRenderer> createState() => _WolfFlutterRendererState();
} }
@@ -47,10 +41,10 @@ class _WolfFlutterRendererState extends State<WolfFlutterRenderer>
super.initState(); super.initState();
engine = WolfEngine( engine = WolfEngine(
data: widget.data, data: widget.engine.data,
difficulty: widget.difficulty, difficulty: widget.engine.difficulty,
startingEpisode: widget.startingEpisode, startingEpisode: widget.engine.startingEpisode,
audio: widget.audio, audio: widget.engine.audio,
onGameWon: () { onGameWon: () {
Navigator.of(context).pop(); Navigator.of(context).pop();
}, },