Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 120 lines (94 sloc) 3.207 kb
cb33cd1 @kthakore Firing Laser by pressing space :D
kthakore authored
1 use strict;
2 use warnings;
3 use SDL;
4 use Carp;
5 use SDLx::App;
6 use SDL::Audio;
7 use SDL::Mixer;
8 use SDL::Event;
9 use SDL::Events;
10 use SDL::Mixer::Music;
11 use SDL::Mixer::Samples;
12 use SDL::Mixer::Channels;
13
efd3de3 @kthakore Finished the sound demo
kthakore authored
14 my $app = SDLx::App->new(
15 init => SDL_INIT_AUDIO | SDL_INIT_VIDEO,
16 width => 250,
17 height => 75,
18 title => "Sound Event Demo",
19 eoq => 1
20 );
cb33cd1 @kthakore Firing Laser by pressing space :D
kthakore authored
21
22 # Initialize the Audio
1c38b70 @kthakore Finished the 2nd example of the music stuff
kthakore authored
23 unless ( SDL::Mixer::open_audio( 44100, AUDIO_S16SYS, 2, 4096 ) == 0 ) {
efd3de3 @kthakore Finished the sound demo
kthakore authored
24 Carp::croak "Cannot open audio: " . SDL::get_error();
cb33cd1 @kthakore Firing Laser by pressing space :D
kthakore authored
25 }
26
3c2b773 @kthakore Background Music with play, pause and resume + Volume control
kthakore authored
27 my $channel_volume = 100;
efd3de3 @kthakore Finished the sound demo
kthakore authored
28 my $music_volume = 100;
29 my $laser_status = 'none';
30 my $music_status = 'not playing';
3c2b773 @kthakore Background Music with play, pause and resume + Volume control
kthakore authored
31
cb33cd1 @kthakore Firing Laser by pressing space :D
kthakore authored
32 # Load our sound resources
33 my $laser = SDL::Mixer::Samples::load_WAV('data/sample.wav');
efd3de3 @kthakore Finished the sound demo
kthakore authored
34 unless ($laser) {
35 Carp::croak "Cannot load sound: " . SDL::get_error();
cb33cd1 @kthakore Firing Laser by pressing space :D
kthakore authored
36 }
37
efd3de3 @kthakore Finished the sound demo
kthakore authored
38 my $background_music =
39 SDL::Mixer::Music::load_MUS('data/music/01-PC-Speaker-Sorrow.ogg');
40 unless ($background_music) {
41 Carp::croak "Cannot load music: " . SDL::get_error();
cb33cd1 @kthakore Firing Laser by pressing space :D
kthakore authored
42 }
43
44
efd3de3 @kthakore Finished the sound demo
kthakore authored
45 $app->add_show_handler(
46 sub {
47
48 $app->draw_rect([0,0,$app->w,$app->h], 0 );
49
50 $app->draw_gfx_text( [10,10], [255,0,0,255], "Channel Volume : $channel_volume" );
51 $app->draw_gfx_text( [10,25], [255,0,0,255], "Music Volume : $music_volume" );
52 $app->draw_gfx_text( [10,40], [255,0,0,255], "Laser Status : $laser_status" );
53 $app->draw_gfx_text( [10,55], [255,0,0,255], "Music Status : $music_status" );
54
55 $app->update();
cb33cd1 @kthakore Firing Laser by pressing space :D
kthakore authored
56
57 }
efd3de3 @kthakore Finished the sound demo
kthakore authored
58 );
59
60 $app->add_event_handler(
61 sub {
62 my $event = shift;
63
64 if ( $event->type == SDL_KEYDOWN ) {
65 my $keysym = $event->key_sym;
66 my $keyname = SDL::Events::get_key_name($keysym);
67
68 if ( $keyname eq 'space' ) {
69
70 $laser_status = 'PEW!';
71 #fire lasers!
72 SDL::Mixer::Channels::play_channel( -1, $laser, 0 );
73
74 }
75 elsif ( $keyname eq 'up' ) {
76 $channel_volume += 5 unless $channel_volume == 100;
77 }
78 elsif ( $keyname eq 'down' ) {
79 $channel_volume -= 5 unless $channel_volume == 0;
80 }
81 elsif ( $keyname eq 'right' ) {
82 $music_volume += 5 unless $music_volume == 100;
83 }
84 elsif ( $keyname eq 'left' ) {
85 $music_volume -= 5 unless $music_volume == 0;
86 }
87 elsif ( $keyname eq 'return' ) {
88 my $playing = SDL::Mixer::Music::playing_music();
89 my $paused = SDL::Mixer::Music::paused_music();
90
91 if ( $playing == 0 && $paused == 0 ) {
92 SDL::Mixer::Music::play_music( $background_music, 1 );
93 $music_status = 'playing';
94 }
95 elsif ( $playing && !$paused ) {
96 SDL::Mixer::Music::pause_music();
97 $music_status = 'paused'
98 }
99 elsif ( $playing && $paused ) {
100 SDL::Mixer::Music::resume_music();
101 $music_status = 'resumed playing';
102 }
103
104 }
105
106 SDL::Mixer::Channels::volume( -1, $channel_volume );
107 SDL::Mixer::Music::volume_music($music_volume);
108
109 }
110
111 }
cb33cd1 @kthakore Firing Laser by pressing space :D
kthakore authored
112
113 );
114
115 $app->run();
3c2b773 @kthakore Background Music with play, pause and resume + Volume control
kthakore authored
116
117 SDL::Mixer::Music::halt_music();
118 SDL::Mixer::close_audio;
119
Something went wrong with that request. Please try again.