File Coverage

blib/lib/Game/Asset/SDLSound.pm
Criterion Covered Total %
statement 33 39 84.6
branch n/a
condition n/a
subroutine 11 12 91.6
pod 1 1 100.0
total 45 52 86.5


line stmt bran cond sub pod time code
1             # Copyright (c) 2016 Timm Murray
2             # All rights reserved.
3             #
4             # Redistribution and use in source and binary forms, with or without
5             # modification, are permitted provided that the following conditions are met:
6             #
7             # * Redistributions of source code must retain the above copyright notice,
8             # this list of conditions and the following disclaimer.
9             # * Redistributions in binary form must reproduce the above copyright
10             # notice, this list of conditions and the following disclaimer in the
11             # documentation and/or other materials provided with the distribution.
12             #
13             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14             # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16             # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17             # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18             # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19             # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20             # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21             # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22             # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23             # POSSIBILITY OF SUCH DAMAGE.
24             package Game::Asset::SDLSound;
25             $Game::Asset::SDLSound::VERSION = '0.2';
26             # ABSTRACT: Load sound files out of Game::Asset files for playing in SDL
27 6     6   5094991 use strict;
  6         16  
  6         190  
28 6     6   34 use warnings;
  6         14  
  6         181  
29 6     6   541 use Moose;
  6         474927  
  6         46  
30 6     6   41103 use namespace::autoclean;
  6         6329  
  6         57  
31 6     6   3015 use SDL ();
  6         185712  
  6         147  
32 6     6   2524 use SDL::Mixer ();
  6         7773  
  6         130  
33 6     6   2268 use SDL::Mixer::Music ();
  6         7087  
  6         148  
34 6     6   2228 use SDL::RWOps ();
  6         5461  
  6         169  
35              
36 6     6   42 use constant type => 'sound';
  6         15  
  6         978  
37              
38             with 'Game::Asset::Type';
39              
40             has '_content' => (
41             is => 'rw',
42             );
43             has '_sound' => (
44             is => 'rw',
45             isa => 'Maybe[SDL::RWOps]',
46             );
47              
48              
49             sub play
50             {
51 0     0 1 0 my ($self) = @_;
52 0         0 my $content = $self->_content;
53 0         0 my $rw = SDL::RWOps->new_const_mem( $content );
54              
55 0         0 my $music = SDL::Mixer::Music::load_MUS_RW( $rw );
56 0         0 SDL::Mixer::Music::play_music( $music, 0 );
57 0         0 return;
58             }
59              
60             sub _process_content
61             {
62 5     5   44656 my ($self, $content) = @_;
63 5         171 $self->_content( $content );
64 5         17 return;
65             }
66              
67              
68 6     6   65 no Moose;
  6         19  
  6         65  
69             __PACKAGE__->meta->make_immutable;
70             1;
71             __END__
72              
73             =head1 NAME
74              
75             Game::Asset::SDLSound - Load sound files out of Game::Asset files for playing in SDL
76              
77             =head1 SYNOPSIS
78              
79             my $asset = Game::Asset->new({
80             file => 't_data/test.zip',
81             });
82             my $sound = $asset->get_by_name( 'test_wav' );
83              
84             my $sdl = Game::Asset::SDLSound::Manager->new;
85             $sdl->init;
86             $sound->play;
87             while( $sdl->is_playing ) { }
88             $sdl->finish;
89              
90              
91             # In your index.yml for the Game::Asset archive, add:
92             flac: Game::Asset::SDLSound
93             mp3: Game::Asset::SDLSound
94             ogg: Game::Asset::SDLSound
95             wav: Game::Asset::SDLSound
96              
97             =head1 DESCRIPTION
98              
99             Loads sound files from a L<Game::Asset> archive for playing in SDL. Support
100             is provided for FLAC, OGG, MP3, and WAV files. Note that which of these can
101             be played will depend on how your SDL_Mixer library is compiled. For more
102             information on supported formats, see L<SDL::Mixer>.
103              
104             =head1 METHODS
105              
106             =head2 play
107              
108             Starts playing the sound. Before calling this, you will need to setup the
109             SDL mixer environment. See L<Game::Asset::SDLSound::Manager> for more
110             information.
111              
112             =head1 SEE ALSO
113              
114             =over 4
115              
116             =item * L<SDL::Mixer>
117              
118             =item * L<SDL::Mixer::Music>
119              
120             =back
121              
122             =head1 LICENSE
123              
124             Copyright (c) 2016 Timm Murray
125             All rights reserved.
126              
127             Redistribution and use in source and binary forms, with or without
128             modification, are permitted provided that the following conditions are met:
129              
130             * Redistributions of source code must retain the above copyright notice,
131             this list of conditions and the following disclaimer.
132             * Redistributions in binary form must reproduce the above copyright
133             notice, this list of conditions and the following disclaimer in the
134             documentation and/or other materials provided with the distribution.
135              
136             THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
137             AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
138             IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
139             ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
140             LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
141             CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
142             SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
143             INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
144             CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
145             ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
146             POSSIBILITY OF SUCH DAMAGE.
147              
148             =cut