Permalink
Browse files

Firing Laser by pressing space :D

  • Loading branch information...
kthakore
kthakore committed Dec 24, 2010
1 parent 5f85f83 commit cb33cd1d32d820d46a3554b306c65bac2b056774
Showing with 70 additions and 8 deletions.
  1. +0 −1 code_listings/music_sound.pl
  2. +62 −0 code_listings/sound_event.pl
  3. +8 −7 src/08-music_and_sound.pod
@@ -14,7 +14,6 @@
Carp::croak "Cannot open audio: ".SDL::get_error();
}
-
my $sample = SDL::Mixer::Samples::load_WAV('data/sample.wav');
unless( $sample)
@@ -0,0 +1,62 @@
+use strict;
+use warnings;
+use SDL;
+use Carp;
+use SDLx::App;
+use SDL::Audio;
+use SDL::Mixer;
+use SDL::Event;
+use SDL::Events;
+use SDL::Mixer::Music;
+use SDL::Mixer::Samples;
+use SDL::Mixer::Channels;
+
+
+my $app = SDLx::App->new( init => SDL_INIT_AUDIO|SDL_INIT_VIDEO,
+ title => "Sound Event Demo",
+ eoq => 1 );
+
+
+# Initialize the Audio
+unless( SDL::Mixer::open_audio( 44100, AUDIO_S16SYS, 2, 4096 ) == 0 )
+{
+ Carp::croak "Cannot open audio: ".SDL::get_error();
+}
+
+# Load our sound resources
+my $laser = SDL::Mixer::Samples::load_WAV('data/sample.wav');
+unless( $laser)
+{
+ Carp::croak "Cannot load sound: ".SDL::get_error();
+}
+
+my $background_music = SDL::Mixer::Music::load_MUS('data/music/01-PC-Speaker-Sorrow.ogg');
+
+unless( $background_music )
+{
+ Carp::croak "Cannot load music: ".SDL::get_error() ;
+}
+
+$app->add_event_handler(
+sub{
+my $event = shift;
+
+ if($event->type == SDL_KEYDOWN)
+ {
+ my $keysym = $event->key_sym;
+ my $keyname = SDL::Events::get_key_name( $keysym );
+
+ if( $keyname =~ 'space' )
+ {
+ #fire lasers!
+ SDL::Mixer::Channels::play_channel( -1, $laser, 0 );
+
+ }
+ }
+
+
+}
+
+);
+
+$app->run();
View
@@ -70,14 +70,15 @@ Next we will use C<SDL::Mixer::Music> to add a background music to our script he
use SDL::Mixer::Channels;
+use SDL::Mixer::Music;
- +#Load our awesome music from U<http://8bitcollective.com>
- +my $background_music = SDL::Mixer::Music::load_MUS('data/music/01-PC-Speaker-Sorrow.ogg');
+ +#Load our awesome music from U<http://8bitcollective.com>
+ +my $background_music =
+ + SDL::Mixer::Music::load_MUS('data/music/01-PC-Speaker-Sorrow.ogg');
- +unless( $background_music )
- +{
- + Carp::croak "Cannot load music file data/music/01-PC-Speaker-Sorrow.ogg: ".SDL::get_error() ;
- +}
+ +unless( $background_music )
+ +{
+ + Carp::croak "Cannot load music file data/music/01-PC-Speaker-Sorrow.ogg: ".SDL::get_error() ;
+ +}
Music types in C<SDL::Mixer> run in a seperate channel from our samples which allows us to have sound effects (like jump, or lasers etc) to play at the same time.
@@ -140,7 +141,7 @@ Volumes can be set at anytime and range from C<1-100>.
unless( $background_music )
{
- Carp::croak "Cannot load music file data/music/01-PC-Speaker-Sorrow.ogg: ".SDL::get_error() ;
+ Carp::croak "Cannot load music file data/music/01-PC-Speaker-Sorrow.ogg:".SDL::get_error();
}
SDL::Mixer::Music::play_music( $background_music,0 );

0 comments on commit cb33cd1

Please sign in to comment.