File Coverage

blib/lib/Game/Asset/SDLSound.pm
Criterion Covered Total %
statement 34 38 89.4
branch n/a
condition n/a
subroutine 11 12 91.6
pod 1 1 100.0
total 46 51 90.2


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