Fix errors when parsing audio
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_soloud/flutter_soloud.dart';
|
||||
import 'package:wolf_3d_data_types/wolf_3d_data_types.dart';
|
||||
import 'package:wolf_3d_synth/wolf_3d_synth.dart';
|
||||
import 'package:wolf_dart/features/difficulty/difficulty.dart';
|
||||
import 'package:wolf_dart/features/renderer/renderer.dart';
|
||||
|
||||
class DifficultyScreen extends StatelessWidget {
|
||||
class DifficultyScreen extends StatefulWidget {
|
||||
const DifficultyScreen(
|
||||
this.data, {
|
||||
super.key,
|
||||
@@ -11,7 +13,86 @@ class DifficultyScreen extends StatelessWidget {
|
||||
|
||||
final WolfensteinData data;
|
||||
|
||||
bool get isShareware => data.version == GameVersion.shareware;
|
||||
@override
|
||||
State<DifficultyScreen> createState() => _DifficultyScreenState();
|
||||
}
|
||||
|
||||
class _DifficultyScreenState extends State<DifficultyScreen> {
|
||||
AudioSource? _menuMusicSource;
|
||||
SoundHandle? _menuMusicHandle;
|
||||
|
||||
bool get isShareware => widget.data.version == GameVersion.shareware;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_playMenuMusic();
|
||||
}
|
||||
|
||||
Future<void> _playMenuMusic() async {
|
||||
final soloud = SoLoud.instance;
|
||||
|
||||
if (!soloud.isInitialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. We only want to play music if the IMF data actually exists
|
||||
if (widget.data.music.isNotEmpty) {
|
||||
// Get the first track (usually the menu theme "Wondering About My Loved Ones")
|
||||
final music = widget.data.music.first;
|
||||
|
||||
// Render the hardware instructions into PCM and wrap in a WAV header
|
||||
final pcmSamples = ImfRenderer.render(music);
|
||||
|
||||
final wavBytes = ImfRenderer.createWavFile(pcmSamples);
|
||||
|
||||
// 3. Load the bytes into SoLoud's memory
|
||||
// The 'menu_theme.wav' string is just a dummy name to tell SoLoud it's dealing with a WAV format
|
||||
_menuMusicSource = await soloud.loadMem('menu_theme.wav', wavBytes);
|
||||
|
||||
// 4. Play the source and tell it to loop continuously!
|
||||
_menuMusicHandle = await soloud.play(
|
||||
_menuMusicSource!,
|
||||
looping: true,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_cleanupAudio();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _cleanupAudio() {
|
||||
final soloud = SoLoud.instance;
|
||||
|
||||
// Stop the playback
|
||||
if (_menuMusicHandle != null) {
|
||||
soloud.stop(_menuMusicHandle!);
|
||||
}
|
||||
|
||||
// Free the raw WAV data from C++ memory
|
||||
if (_menuMusicSource != null) {
|
||||
soloud.disposeSource(_menuMusicSource!);
|
||||
}
|
||||
}
|
||||
|
||||
void _startGame(Difficulty difficulty, {bool showGallery = false}) {
|
||||
// Stop the music and clear memory right before we push the new route
|
||||
_cleanupAudio();
|
||||
|
||||
Navigator.of(context).pushReplacement(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => WolfRenderer(
|
||||
widget.data,
|
||||
difficulty: difficulty,
|
||||
isShareware: isShareware,
|
||||
showSpriteGallery: showGallery,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -19,18 +100,7 @@ class DifficultyScreen extends StatelessWidget {
|
||||
backgroundColor: Colors.black,
|
||||
floatingActionButton: FloatingActionButton(
|
||||
backgroundColor: Colors.red[900],
|
||||
onPressed: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => WolfRenderer(
|
||||
data,
|
||||
difficulty: Difficulty.bringEmOn,
|
||||
isShareware: isShareware,
|
||||
showSpriteGallery: true,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
onPressed: () => _startGame(Difficulty.bringEmOn, showGallery: true),
|
||||
child: const Icon(Icons.bug_report, color: Colors.white),
|
||||
),
|
||||
body: Center(
|
||||
@@ -46,6 +116,7 @@ class DifficultyScreen extends StatelessWidget {
|
||||
fontFamily: 'Courier',
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 40),
|
||||
|
||||
// --- Difficulty Buttons ---
|
||||
ListView.builder(
|
||||
@@ -64,17 +135,7 @@ class DifficultyScreen extends StatelessWidget {
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
),
|
||||
onPressed: () {
|
||||
Navigator.of(context).pushReplacement(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => WolfRenderer(
|
||||
data,
|
||||
difficulty: difficulty,
|
||||
isShareware: isShareware,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
onPressed: () => _startGame(difficulty),
|
||||
child: Text(
|
||||
difficulty.title,
|
||||
style: const TextStyle(fontSize: 18),
|
||||
|
||||
Reference in New Issue
Block a user