| 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 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 28 | 5 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 5 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | 5 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 31 | 5 | ▶ | use SDL (); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 32 | use SDL::Mixer (); | ||
| 33 | use SDL::Mixer::Music (); | ||
| 34 | use SDL::RWOps (); | ||
| 35 | |||
| 36 | use constant type => 'sound'; | ||
| 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 | my ($self) = @_; | ||
| 49 | my $music = SDL::Mixer::Music::load_MUS_RW( $self->_sound ); | ||
| 50 | SDL::Mixer::Music::play_music( $music, 0 ); | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub _process_content | ||
| 55 | { | ||
| 56 | my ($self, $content) = @_; | ||
| 57 | my $rw = SDL::RWOps->new_const_mem( $content ); | ||
| 58 | $self->_sound( $rw ); | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | no Moose; | ||
| 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 | ||