mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-07 13:15:54 -06:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fcdbe62f99 | |||
| 57c7afcdb4 | |||
| 2e16c2d170 | |||
| f86c106593 |
36
.github/workflows/build.yaml
vendored
36
.github/workflows/build.yaml
vendored
@@ -111,8 +111,42 @@ jobs:
|
|||||||
run_install: true
|
run_install: true
|
||||||
|
|
||||||
- name: Build with Emscripten
|
- name: Build with Emscripten
|
||||||
|
shell: bash
|
||||||
run: |
|
run: |
|
||||||
cargo build --target=wasm32-unknown-emscripten --release
|
# Retry mechanism for Emscripten build - only retry on specific hash errors
|
||||||
|
MAX_RETRIES=3
|
||||||
|
RETRY_DELAY=30
|
||||||
|
|
||||||
|
for attempt in $(seq 1 $MAX_RETRIES); do
|
||||||
|
echo "Build attempt $attempt of $MAX_RETRIES"
|
||||||
|
|
||||||
|
# Capture output and check for specific error while preserving real-time output
|
||||||
|
if cargo build --target=wasm32-unknown-emscripten --release 2>&1 | tee /tmp/build_output.log; then
|
||||||
|
echo "Build successful on attempt $attempt"
|
||||||
|
break
|
||||||
|
else
|
||||||
|
echo "Build failed on attempt $attempt"
|
||||||
|
|
||||||
|
# Check if the failure was due to the specific hash error
|
||||||
|
if grep -q "emcc: error: Unexpected hash:" /tmp/build_output.log; then
|
||||||
|
echo "::warning::Detected 'emcc: error: Unexpected hash:' error - will retry (attempt $attempt of $MAX_RETRIES)"
|
||||||
|
|
||||||
|
if [ $attempt -eq $MAX_RETRIES ]; then
|
||||||
|
echo "::error::All retry attempts failed. Exiting with error."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Waiting $RETRY_DELAY seconds before retry..."
|
||||||
|
sleep $RETRY_DELAY
|
||||||
|
|
||||||
|
# Exponential backoff: double the delay for next attempt
|
||||||
|
RETRY_DELAY=$((RETRY_DELAY * 2))
|
||||||
|
else
|
||||||
|
echo "Build failed but not due to hash error - not retrying"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
- name: Assemble
|
- name: Assemble
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
12
.github/workflows/coverage.yaml
vendored
12
.github/workflows/coverage.yaml
vendored
@@ -18,6 +18,7 @@ jobs:
|
|||||||
uses: dtolnay/rust-toolchain@master
|
uses: dtolnay/rust-toolchain@master
|
||||||
with:
|
with:
|
||||||
toolchain: ${{ env.RUST_TOOLCHAIN }}
|
toolchain: ${{ env.RUST_TOOLCHAIN }}
|
||||||
|
components: llvm-tools-preview
|
||||||
|
|
||||||
- name: Rust Cache
|
- name: Rust Cache
|
||||||
uses: Swatinem/rust-cache@v2
|
uses: Swatinem/rust-cache@v2
|
||||||
@@ -40,20 +41,17 @@ jobs:
|
|||||||
cargo install cargo-vcpkg
|
cargo install cargo-vcpkg
|
||||||
cargo vcpkg -v build
|
cargo vcpkg -v build
|
||||||
|
|
||||||
- name: Install cargo-tarpaulin
|
- uses: taiki-e/install-action@cargo-llvm-cov
|
||||||
run: cargo install cargo-tarpaulin
|
- uses: taiki-e/install-action@nextest
|
||||||
|
|
||||||
# Note: We manually link zlib. This should be synchronized with the flags set for Linux in .cargo/config.toml.
|
# Note: We manually link zlib. This should be synchronized with the flags set for Linux in .cargo/config.toml.
|
||||||
- name: Generate coverage report
|
- name: Generate coverage report
|
||||||
run: |
|
run: |
|
||||||
cargo tarpaulin \
|
cargo llvm-cov --no-fail-fast --lcov --output-path lcov.info nextest
|
||||||
--out Lcov \
|
|
||||||
--output-dir coverage \
|
|
||||||
--rustflags="-C link-arg=-lz"
|
|
||||||
|
|
||||||
- name: Upload coverage to Coveralls
|
- name: Upload coverage to Coveralls
|
||||||
uses: coverallsapp/github-action@v2
|
uses: coverallsapp/github-action@v2
|
||||||
with:
|
with:
|
||||||
files: ./coverage/lcov.info
|
files: ./lcov.info
|
||||||
format: lcov
|
format: lcov
|
||||||
allow-empty: false
|
allow-empty: false
|
||||||
|
|||||||
6
.github/workflows/test.yaml
vendored
6
.github/workflows/test.yaml
vendored
@@ -42,8 +42,10 @@ jobs:
|
|||||||
cargo install cargo-vcpkg
|
cargo install cargo-vcpkg
|
||||||
cargo vcpkg -v build
|
cargo vcpkg -v build
|
||||||
|
|
||||||
- name: Run tests
|
- uses: taiki-e/install-action@nextest
|
||||||
run: cargo test --workspace --verbose
|
|
||||||
|
- name: Run nextest
|
||||||
|
run: cargo nextest run --workspace
|
||||||
|
|
||||||
- name: Run clippy
|
- name: Run clippy
|
||||||
run: cargo clippy -- -D warnings
|
run: cargo clippy -- -D warnings
|
||||||
|
|||||||
145
src/audio.rs
145
src/audio.rs
@@ -10,13 +10,15 @@ const SOUND_ASSETS: [Asset; 4] = [Asset::Wav1, Asset::Wav2, Asset::Wav3, Asset::
|
|||||||
/// The audio system for the game.
|
/// The audio system for the game.
|
||||||
///
|
///
|
||||||
/// This struct is responsible for initializing the audio device, loading sounds,
|
/// This struct is responsible for initializing the audio device, loading sounds,
|
||||||
/// and playing them.
|
/// and playing them. If audio fails to initialize, it will be disabled and all
|
||||||
|
/// functions will silently do nothing.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub struct Audio {
|
pub struct Audio {
|
||||||
_mixer_context: mixer::Sdl2MixerContext,
|
_mixer_context: Option<mixer::Sdl2MixerContext>,
|
||||||
sounds: Vec<Chunk>,
|
sounds: Vec<Chunk>,
|
||||||
next_sound_index: usize,
|
next_sound_index: usize,
|
||||||
muted: bool,
|
muted: bool,
|
||||||
|
disabled: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Audio {
|
impl Default for Audio {
|
||||||
@@ -27,13 +29,27 @@ impl Default for Audio {
|
|||||||
|
|
||||||
impl Audio {
|
impl Audio {
|
||||||
/// Creates a new `Audio` instance.
|
/// Creates a new `Audio` instance.
|
||||||
|
///
|
||||||
|
/// If audio fails to initialize, the audio system will be disabled and
|
||||||
|
/// all functions will silently do nothing.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let frequency = 44100;
|
let frequency = 44100;
|
||||||
let format = DEFAULT_FORMAT;
|
let format = DEFAULT_FORMAT;
|
||||||
let channels = 4;
|
let channels = 4;
|
||||||
let chunk_size = 256; // 256 is minimum for emscripten
|
let chunk_size = 256; // 256 is minimum for emscripten
|
||||||
|
|
||||||
mixer::open_audio(frequency, format, 1, chunk_size).expect("Failed to open audio");
|
// Try to open audio, but don't panic if it fails
|
||||||
|
if let Err(e) = mixer::open_audio(frequency, format, 1, chunk_size) {
|
||||||
|
tracing::warn!("Failed to open audio: {}. Audio will be disabled.", e);
|
||||||
|
return Self {
|
||||||
|
_mixer_context: None,
|
||||||
|
sounds: Vec::new(),
|
||||||
|
next_sound_index: 0,
|
||||||
|
muted: false,
|
||||||
|
disabled: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
mixer::allocate_channels(channels);
|
mixer::allocate_channels(channels);
|
||||||
|
|
||||||
// set channel volume
|
// set channel volume
|
||||||
@@ -41,31 +57,72 @@ impl Audio {
|
|||||||
mixer::Channel(i).set_volume(32);
|
mixer::Channel(i).set_volume(32);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mixer_context = mixer::init(InitFlag::OGG).expect("Failed to initialize SDL2_mixer");
|
// Try to initialize mixer, but don't panic if it fails
|
||||||
|
let mixer_context = match mixer::init(InitFlag::OGG) {
|
||||||
|
Ok(ctx) => ctx,
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!("Failed to initialize SDL2_mixer: {}. Audio will be disabled.", e);
|
||||||
|
return Self {
|
||||||
|
_mixer_context: None,
|
||||||
|
sounds: Vec::new(),
|
||||||
|
next_sound_index: 0,
|
||||||
|
muted: false,
|
||||||
|
disabled: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let sounds: Vec<Chunk> = SOUND_ASSETS
|
// Try to load sounds, but don't panic if any fail
|
||||||
.iter()
|
let mut sounds = Vec::new();
|
||||||
.enumerate()
|
for (i, asset) in SOUND_ASSETS.iter().enumerate() {
|
||||||
.map(|(i, asset)| {
|
match get_asset_bytes(*asset) {
|
||||||
let data = get_asset_bytes(*asset).expect("Failed to load sound asset");
|
Ok(data) => match RWops::from_bytes(&data) {
|
||||||
let rwops = RWops::from_bytes(&data).unwrap_or_else(|_| panic!("Failed to create RWops for sound {}", i + 1));
|
Ok(rwops) => match rwops.load_wav() {
|
||||||
rwops
|
Ok(chunk) => sounds.push(chunk),
|
||||||
.load_wav()
|
Err(e) => {
|
||||||
.unwrap_or_else(|_| panic!("Failed to load sound {} from asset API", i + 1))
|
tracing::warn!("Failed to load sound {} from asset API: {}", i + 1, e);
|
||||||
})
|
}
|
||||||
.collect();
|
},
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!("Failed to create RWops for sound {}: {}", i + 1, e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!("Failed to load sound asset {}: {}", i + 1, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no sounds loaded successfully, disable audio
|
||||||
|
if sounds.is_empty() {
|
||||||
|
tracing::warn!("No sounds loaded successfully. Audio will be disabled.");
|
||||||
|
return Self {
|
||||||
|
_mixer_context: Some(mixer_context),
|
||||||
|
sounds: Vec::new(),
|
||||||
|
next_sound_index: 0,
|
||||||
|
muted: false,
|
||||||
|
disabled: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
Audio {
|
Audio {
|
||||||
_mixer_context: mixer_context,
|
_mixer_context: Some(mixer_context),
|
||||||
sounds,
|
sounds,
|
||||||
next_sound_index: 0,
|
next_sound_index: 0,
|
||||||
muted: false,
|
muted: false,
|
||||||
|
disabled: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Plays the "eat" sound effect.
|
/// Plays the "eat" sound effect.
|
||||||
|
///
|
||||||
|
/// If audio is disabled or muted, this function does nothing.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn eat(&mut self) {
|
pub fn eat(&mut self) {
|
||||||
|
if self.disabled || self.muted || self.sounds.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(chunk) = self.sounds.get(self.next_sound_index) {
|
if let Some(chunk) = self.sounds.get(self.next_sound_index) {
|
||||||
match mixer::Channel(0).play(chunk, 0) {
|
match mixer::Channel(0).play(chunk, 0) {
|
||||||
Ok(channel) => {
|
Ok(channel) => {
|
||||||
@@ -80,7 +137,13 @@ impl Audio {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Instantly mute or unmute all channels.
|
/// Instantly mute or unmute all channels.
|
||||||
|
///
|
||||||
|
/// If audio is disabled, this function does nothing.
|
||||||
pub fn set_mute(&mut self, mute: bool) {
|
pub fn set_mute(&mut self, mute: bool) {
|
||||||
|
if self.disabled {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let channels = 4;
|
let channels = 4;
|
||||||
let volume = if mute { 0 } else { 32 };
|
let volume = if mute { 0 } else { 32 };
|
||||||
for i in 0..channels {
|
for i in 0..channels {
|
||||||
@@ -93,6 +156,11 @@ impl Audio {
|
|||||||
pub fn is_muted(&self) -> bool {
|
pub fn is_muted(&self) -> bool {
|
||||||
self.muted
|
self.muted
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if the audio system is disabled.
|
||||||
|
pub fn is_disabled(&self) -> bool {
|
||||||
|
self.disabled
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -143,7 +211,11 @@ mod tests {
|
|||||||
let audio = Audio::new();
|
let audio = Audio::new();
|
||||||
assert_eq!(audio.is_muted(), false);
|
assert_eq!(audio.is_muted(), false);
|
||||||
assert_eq!(audio.next_sound_index, 0);
|
assert_eq!(audio.next_sound_index, 0);
|
||||||
assert_eq!(audio.sounds.len(), 4);
|
|
||||||
|
// Audio might be disabled if initialization failed
|
||||||
|
if !audio.is_disabled() {
|
||||||
|
assert_eq!(audio.sounds.len(), 4);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -171,6 +243,13 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut audio = Audio::new();
|
let mut audio = Audio::new();
|
||||||
|
|
||||||
|
// Skip test if audio is disabled
|
||||||
|
if audio.is_disabled() {
|
||||||
|
eprintln!("Skipping sound rotation test due to disabled audio");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let initial_index = audio.next_sound_index;
|
let initial_index = audio.next_sound_index;
|
||||||
|
|
||||||
// Test sound rotation
|
// Test sound rotation
|
||||||
@@ -190,6 +269,13 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let audio = Audio::new();
|
let audio = Audio::new();
|
||||||
|
|
||||||
|
// Skip test if audio is disabled
|
||||||
|
if audio.is_disabled() {
|
||||||
|
eprintln!("Skipping sound index bounds test due to disabled audio");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
assert!(audio.next_sound_index < audio.sounds.len());
|
assert!(audio.next_sound_index < audio.sounds.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,6 +289,29 @@ mod tests {
|
|||||||
let audio = Audio::default();
|
let audio = Audio::default();
|
||||||
assert_eq!(audio.is_muted(), false);
|
assert_eq!(audio.is_muted(), false);
|
||||||
assert_eq!(audio.next_sound_index, 0);
|
assert_eq!(audio.next_sound_index, 0);
|
||||||
assert_eq!(audio.sounds.len(), 4);
|
|
||||||
|
// Audio might be disabled if initialization failed
|
||||||
|
if !audio.is_disabled() {
|
||||||
|
assert_eq!(audio.sounds.len(), 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_audio_disabled_state() {
|
||||||
|
if let Err(_) = init_sdl() {
|
||||||
|
eprintln!("Skipping SDL2-dependent tests due to initialization failure");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test that disabled audio doesn't crash when calling functions
|
||||||
|
let mut audio = Audio::new();
|
||||||
|
|
||||||
|
// These should not panic even if audio is disabled
|
||||||
|
audio.eat();
|
||||||
|
audio.set_mute(true);
|
||||||
|
audio.set_mute(false);
|
||||||
|
|
||||||
|
// Test that we can check the disabled state
|
||||||
|
let _is_disabled = audio.is_disabled();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user