Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

AudioManager.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TSGL - Teddy Space Game Library
00004     Copyright (C) 2002 Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: AudioManager.cpp,v 1.1 2002/01/08 20:47:04 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "config.h"
00026 #include "Teddy/SysSupport/Messages.h"
00027 #include "Teddy/SpaceGame/AudioManager.h"
00028 #include "Teddy/SpaceGame/Root.h"
00029 #include <cstdio>
00030 using namespace Teddy::SysSupport;
00031 
00032 
00033 #if defined( WIN32 )
00034 extern "C" {
00035 #include "arch/win32/native_midi.h"
00036 }
00037 #endif
00038 
00039 #if defined (HAVE_LIB_SDL_MIXER)
00040 
00041 # include "SDL_mixer.h"
00042 # if defined( _MSC_VER)
00043 #  if defined( _DEBUG )
00044 #   pragma comment (lib, "SDLD_mixer.lib")
00045 #  else
00046 #   pragma comment (lib, "SDL_mixer.lib")
00047 #  endif
00048 # endif
00049 
00050 static Mix_Chunk *crash;
00051 static Mix_Chunk *explode;
00052 static Mix_Chunk *hit_em;
00053 static Mix_Chunk *hyper;
00054 static Mix_Chunk *pulse;
00055 
00056 #else
00057 # include <cstdio>
00058 static void *crash;
00059 static void *explode;
00060 static void *hit_em;
00061 static void *hyper;
00062 static void *pulse;
00063 #endif
00064 
00065 
00066 namespace Teddy     {
00067 namespace SpaceGame {
00068 
00069 
00070 AudioManager::AudioManager( Root *root ){
00071     this->root = root;
00072 #   if defined( WIN32 )
00073     if( root->isEnabled(RO_AUDIO) == true ){
00074         ::native_midi_init();
00075         init_msg( "native_midi_loadsong..." );
00076         char *mus_fname = "audio/adblue.mid";
00077         NativeMidiSong *music = ::native_midi_loadsong( mus_fname );
00078         if( music != NULL ){
00079             ::native_midi_start( music );
00080         }else{
00081             warn_msg( "Could not load %s", mus_fname );
00082         }
00083     }
00084 #   endif
00085 
00086 #   ifdef HAVE_LIB_SDL_MIXER
00087 //  signal( SIGINT,  exit );
00088 //  signal( SIGTERM, exit );
00089 
00090     /* Open the audio device */
00091     if( Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 2048) < 0 ){
00092         error_msg( MSG_HEAD "Couldn't open audio: %s", SDL_GetError() );
00093         return;
00094     }else{
00095 //      Mix_QuerySpec( &audio_rate, &audio_format, &audio_channels );
00096 /*      printf(
00097             "Opened audio at %d Hz %d bit %s\n",
00098             audio_rate,
00099             (audio_format&0xFF),
00100             (audio_channels > 1) ? "stereo" : "mono"
00101         );*/
00102     }
00103 
00104     crash   = Mix_LoadWAV( "audio/crash.wav"   );
00105     explode = Mix_LoadWAV( "audio/explode.wav" );
00106     hit_em  = Mix_LoadWAV( "audio/hitem.wav"   );
00107     hyper   = Mix_LoadWAV( "audio/hyper.wav"   );
00108     pulse   = Mix_LoadWAV( "audio/pulse.wav"   );
00109     if( crash   == NULL ) warn_msg( "Could not load audio/crash.wav"   );
00110     if( explode == NULL ) warn_msg( "Could not load audio/explode.wav" );
00111     if( hit_em  == NULL ) warn_msg( "Could not load audio/hit_em.wav"  );
00112     if( hyper   == NULL ) warn_msg( "Could not load audio/hyper.wav"   );
00113     if( pulse   == NULL ) warn_msg( "Could not load audio/pulse.wav"   );
00114     //printf( "WAV LOAD DONE\n" );
00115 
00116 #   else
00117     warn_msg( "SDL_mixer was not available when built - Audio disabled" );
00118 #   endif
00119 }
00120 
00121 
00122 void AudioManager::playCrash  (){ playWav( crash   ); }
00123 void AudioManager::playExplode(){ playWav( explode ); }
00124 void AudioManager::playHitEm  (){ playWav( hit_em  ); }
00125 void AudioManager::playHyper  (){ playWav( hyper   ); }
00126 void AudioManager::playPulse  (){ playWav( pulse   ); }                                   
00127 
00128 
00129 void AudioManager::playWav( void *chunk ){
00130 #   ifdef HAVE_LIB_SDL_MIXER
00131     if( root->isEnabled(RO_AUDIO) == true ){
00132         Mix_PlayChannel( -1, (Mix_Chunk*)chunk, 0 );
00133         //printf( "audio!!\n" );
00134     }else{
00135         //printf( "disabled audio!!\n" );
00136     }
00137 #   endif
00138 }
00139 
00140 
00141 };  //  namespace SpaceGame
00142 };  //  namespace Teddy
00143