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

sound.cpp

Go to the documentation of this file.
00001 #include <SDL/SDL_mixer.h>
00002 #include <iostream>
00003 #include <SDL/SDL.h>
00004 #include <vector>
00005 
00006 #include "sound.h"
00007 #include "sample.h"
00008 #include "music.h"
00009 #include "application.h"
00010 
00011 
00012 using namespace std;
00013 
00014 vector<Sample *>Sound::sEffects;
00015 Music *Sound::theme;
00016 
00017 Sound::Sound(int n) {
00018         //This should initialize the audio NF
00019         if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
00020                 cerr << "SDL_Init Error: " << SDL_GetError() << endl;
00021                 return;
00022         }
00023 
00024         if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
00025                 cerr << "Mix_OpenAudio Error: " << Mix_GetError() << endl;
00026                 return;
00027         }
00028 
00029         Mix_AllocateChannels(n);
00030 
00031         //put the sounds in the game.
00032 
00033         //sound 0: Ufo sound
00034         sEffects.push_back(new Sample(UFO_SOUND, -1));
00035         //sound 1: Gun Shot
00036         sEffects.push_back(new Sample(GUN_SHOT, 1));
00037         //sound 2: Walking sound
00038         sEffects.push_back(new Sample(WALK_SOUND, 2));
00039         //sound 3: Bear walking sound
00040         sEffects.push_back(new Sample(BEAR_WALK_SOUND_1, 3));
00041 
00042         //This should get the music going
00043         theme = new Music(THEME_MUSIC);
00044 }
00045 
00046 Sound::~Sound() {
00047         int numtimesopened, frequency, channels;
00048         Uint16 format;
00049         numtimesopened = Mix_QuerySpec(&frequency, &format, &channels);
00050         
00051         while (numtimesopened) {
00052                 Mix_CloseAudio();
00053                 numtimesopened--;
00054         }
00055     
00056         for(unsigned int k = 0; k < sEffects.size(); ++k)
00057                 delete sEffects[k];
00058         
00059         sEffects.erase(sEffects.begin(), sEffects.end());
00060 
00061         delete theme;
00062 }
00063 
00064 void Sound::PlaySample(sound s) {
00065         switch(s) {
00066                 case UFO_NOISE: sEffects[0]->Play(0); break;
00067                 case GUNSHOT: sEffects[1]->Play(0); break;
00068                 case WALKING: sEffects[2]->Play(0); break;
00069                 case BEAR_WALK: sEffects[3]->Play(0); break;
00070         }
00071 }
00072 
00073 void Sound::PlayTheme(int loops) {
00074         theme->Play(loops);
00075 }
00076 
00077 void Sound::StopTheme() {
00078         theme->Stop();
00079 }
00080 
00081 void Sound::LoadTheme(string path) {
00082         if (ThemeIsPlaying()) StopTheme();
00083         delete theme;
00084         theme = new Music(path);
00085         PlayTheme(-1);
00086 }
00087 
00088 bool Sound::ThemeIsPlaying() {
00089         return theme->IsPlaying();
00090 }
00091 
00092 bool Sound::SampleIsPlaying(sound s) {
00093         switch(s) {
00094                 case UFO_NOISE: return sEffects[0]->isPlaying();
00095                 case GUNSHOT: return sEffects[1]->isPlaying();
00096                 case WALKING: return sEffects[2]->isPlaying();
00097                 case BEAR_WALK: return sEffects[3]->isPlaying();
00098         }
00099         return false;
00100 }
00101 

Generated on Sun Dec 8 12:02:20 2002 for nnc by doxygen1.3-rc1