AaronCameron.net
Not Left, nor right. Just correct.
Not a Member? - Login or Create an Account
Tuesday the 22nd of May 2012 @ 01:32am
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cMusic.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2003  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 #include "audio/cMusic.h"
00026 #include "audio/cAudioMixer.h"
00027 
00028 #include "n2l/vfs.h"
00029 
00030 #include "SDL_mixer.h"
00031 
00032 namespace n2l
00033 {
00034 
00035     /**************************************************************************/
00036     cMusic::cMusic() :
00037         mMusic(0)
00038     {
00039     }
00040 
00041 
00042     /**************************************************************************/
00043     cMusic::cMusic( const cVfsNodeInterface &iFile ) :
00044         mMusic(0)
00045     {
00046         load(iFile);
00047     }
00048 
00049 
00050     /**************************************************************************/
00051     cMusic::~cMusic()
00052     {
00053     }
00054 
00055 
00056     /**************************************************************************/
00057     void cMusic::load( const cVfsNodeInterface &iFile )
00058     {
00059         // Release any existing chunk imp.
00060         free();
00061 
00062         if (!cAudioMixer::inited()) return;
00063 
00064         // Is it ok?
00065         if (!iFile.likeFile())
00066             throw cBadDataUseException(
00067                 "cMusic::load", "Node is not like a file: " + iFile.name() );
00068 
00069         #ifdef N2L_FakeMusic
00070             Mix_Chunk * temp(0);
00071             // If we have FS access, use it.
00072             if (iFile.supportsFsName()) {
00073                 temp = Mix_LoadWAV( iFile.fsName().c_str() );
00074             } else {
00075                 temp = Mix_LoadWAV_RW( iFile.getRWops(), 1 );
00076             }
00077             if (0==temp)
00078                 throw cDriverLayerException("cMusic::load",
00079                                             "Failed to load music (fake)",
00080                                             "SDL_mixer", Mix_GetError() );
00081 
00082             // Use it.
00083             mMusic = new cAudioChunkImp( temp );
00084 
00085         #else
00086             Mix_Music * temp(0);
00087 
00088             if (iFile.supportsFsName()) {
00089                 temp = Mix_LoadMUS( iFile.fsName().c_str() );
00090             } else {
00091                 throw cVfsException( "cMusic::load",
00092                     "Music loader requires FsName support" );
00093             }
00094             if (0==temp)
00095                 throw cDriverLayerException( "cMusic::load",
00096                     "Failed to load music", "SDL_mixer", Mix_GetError() );
00097 
00098             // Use it.
00099             mMusic = temp;
00100 
00101         #endif
00102     }
00103 
00104 
00105     /**************************************************************************/
00106     void cMusic::play( const tMusicOptions iOptions ) const
00107     {
00108         if (!mMusic) {
00109             if (cAudioMixer::inited())
00110                 throw cBadDataUseException( "cMusic::play",
00111                     "No music is loaded." );
00112             else
00113                 return;
00114         }
00115 #       ifdef N2L_FakeMusic
00116             const tSint Reps( iOptions&MusicOpt_Loop?-1:0 );
00117             Mix_PlayChannel(cAudioMixer::MusicChannel,mMusic->chunk(),Reps);
00118 #       else
00119             const tSint Reps( iOptions&MusicOpt_Loop?-1:1 );
00120             Mix_PlayMusic( mMusic,Reps );
00121 #       endif
00122     }
00123 
00124     /**************************************************************************/
00125     void cMusic::free()
00126     {
00127 #       ifdef N2L_FakeMusic
00128             mMusic = 0;
00129 #       else
00130             if (mMusic) Mix_FreeMusic(mMusic);
00131 #       endif
00132     }
00133 
00134 }
00135 
©2012 Aaron Cameron