| 1 | package Games::PangZero::Music; | ||
| 2 | |||
| 3 | use SDL::Mixer; | ||
| 4 | use SDL::Mixer::Samples; | ||
| 5 | use SDL::Mixer::Channels; | ||
| 6 | use SDL::Mixer::Music; | ||
| 7 | use SDL::Mixer::MixChunk; | ||
| 8 | use SDL::Mixer::MixMusic; | ||
| 9 | |||
| 10 | sub LoadMusic { | ||
| 11 | my ($filename) = @_; | ||
| 12 | |||
| 13 | return undef unless -f $filename; | ||
| 14 | return SDL::Mixer::Music::load_MUS($filename); | ||
| 15 | } | ||
| 16 | |||
| 17 | sub LoadSounds { | ||
| 18 | $Mixer = SDL::Mixer::open_audio( 22050, AUDIO_S16, 2, 1024 ) + 1; | ||
| 19 | unless($Mixer) { | ||
| 20 | warn SDL::get_error(); | ||
| 21 | return 0; | ||
| 22 | } | ||
| 23 | |||
| 24 | my ($soundName, $fileName); | ||
| 25 | while (($soundName, $fileName) = each %Sounds) { | ||
| 26 | $Sounds{$soundName} = SDL::Mixer::Music::load_WAV("$Games::PangZero::DataDir/$fileName"); | ||
| 27 | } | ||
| 28 | |||
| 29 | $Games::PangZero::music = LoadMusic("$Games::PangZero::DataDir/UPiPang.mp3"); | ||
| 30 | $Games::PangZero::music = LoadMusic("$Games::PangZero::DataDir/UPiPang.mid") unless $Games::PangZero::music; | ||
| 31 | SetMusicEnabled($MusicEnabled); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub PlaySound { | ||
| 35 | return unless $SoundEnabled; | ||
| 36 | my $sound = shift; | ||
| 37 | $Mixer and $Sounds{$sound} and SDL::Mixer::Channels::play_channel( -1, $Sounds{$sound}, 0 ); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub SetMusicEnabled { | ||
| 41 | return $MusicEnabled = 0 unless $Games::PangZero::music; | ||
| 42 | my $musicEnabled = shift; | ||
| 43 | |||
| 44 | $MusicEnabled = $musicEnabled ? 1 : 0; | ||
| 45 | if ( (not $MusicEnabled) and SDL::Mixer::Music::playing_music() ) { | ||
| 46 | SDL::Mixer::Music::halt_music(); | ||
| 47 | } | ||
| 48 | if ($MusicEnabled and not SDL::Mixer::Music::playing_music()) { | ||
| 49 | SDL::Mixer::Music::play_music($Games::PangZero::music, -1); | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; |