Changeset 3020 in orxonox.OLD for orxonox/branches/sound/sound/sound_control.cc
- Timestamp:
- Nov 29, 2004, 1:04:28 PM (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/sound/sound/sound_control.cc
r2979 r3020 18 18 using namespace std; 19 19 20 SoundControl* SoundControl::instance = NULL; 20 int sfx_channel1 = -1; 21 int sfx_channel2 = -1; 22 int finished = 0; 23 static SoundControl* instance = NULL; 24 static SoundControl* sound = SoundControl::getInstance(); 21 25 int volume = SDL_MIX_MAXVOLUME; 22 26 int track_number = 1; 23 27 static Mix_Music* music = NULL; 24 int audio_rate = MIX_DEFAULT_FREQUENCY, audio_channels = MIX_DEFAULT_CHANNELS, audio_buffers = 16384, bits = 0; 28 int audio_rate = 44100, audio_channels = MIX_DEFAULT_CHANNELS, 29 audio_buffers = 16384, bits = 0; 25 30 Uint16 audio_format = MIX_DEFAULT_FORMAT; 31 SDL_Event event; 26 32 27 33 … … 33 39 */ 34 40 SoundControl::SoundControl () { 35 36 /* 37 initializing sound and calling Mix_OpenAudio 38 if(SDL_Init(SDL_INIT_AUDIO)<0){ 41 if(SDL_Init(SDL_INIT_AUDIO)<0) { 39 42 printf("SDL_Init: INIT_AUDIO error.\n"); 40 43 } 41 */ 42 43 if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)){ 44 if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) { 44 45 printf("Mix_OpenAudio: Failed to open audio!\n"); 45 46 } 46 47 47 initialise(); 48 48 } 49 49 50 51 50 /** 52 51 \brief Default destructor 53 52 */ 54 SoundControl::~SoundControl () { 53 SoundControl::~SoundControl () { 55 54 } 56 55 … … 71 70 72 71 /** 73 \brief Is called by SoundControl object to initiate all values 72 \brief Is called by SoundControl object to initiate all values and to output some text 74 73 */ 75 74 void SoundControl::initialise() { 76 77 // Print some info78 75 Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels); 79 76 bits=audio_format&0xFF; … … 82 79 } 83 80 84 85 /** 86 \brief Sets the number of output Channels (should not be used) 81 /** 82 \brief Sets the number of output Channels 87 83 */ 88 84 void SoundControl::setNumberOfChannels (int number_of_channels) { … … 90 86 } 91 87 92 93 88 /** 94 89 \brief May be called from any WorldEntity to play a .xm file 95 90 \param filename: self-explanatory 96 91 */ 97 intSoundControl::playMod (char* fileName) {92 void SoundControl::playMod (char* fileName) { 98 93 Mix_Chunk* chunk = NULL; 99 94 chunk = Mix_LoadWAV(fileName); … … 103 98 } 104 99 105 106 100 /** 107 101 \brief May be called from any WorldEntity to play a .wav file 108 102 \param filename: self-explanatory 109 103 */ 110 intSoundControl::playWav (char* fileName) {104 void SoundControl::playWav (char* fileName) { 111 105 Mix_Chunk* chunk = NULL; 112 106 chunk = Mix_LoadWAV(fileName); … … 116 110 } 117 111 118 119 112 /** 120 113 \brief May be called from any WorldEntity to play a .ogg file 121 114 \param filename: self-explanatory 122 115 */ 123 intSoundControl::playOgg (char* fileName) {116 void SoundControl::playOgg (char* fileName) { 124 117 Mix_Music* music = NULL; 125 118 music = Mix_LoadMUS(fileName); 126 if(Mix_PlayMusic(music, 1) == -1) {119 if(Mix_PlayMusic(music, 1) == -1) { 127 120 printf("Mix_PlayMusic: %s\n",Mix_GetError()); 128 121 } 129 122 Mix_HookMusicFinished(musicDone); 130 123 } 131 132 124 133 125 /** … … 150 142 } 151 143 152 153 144 /** 154 145 \brief Rewinds music to the beginning … … 158 149 } 159 150 160 161 151 /** 162 152 \brief Rewinds the music 5 seconds … … 166 156 } 167 157 168 169 158 /** 170 159 \brief Forwards the music 5 seconds … … 174 163 } 175 164 176 177 165 /** 178 166 \brief Pauses music output … … 182 170 } 183 171 184 185 172 /** 186 173 \brief this function pauses music output … … 189 176 Mix_ResumeMusic(); 190 177 } 191 192 178 193 179 /** … … 199 185 music = NULL; 200 186 } 187 188 /** 189 \brief Handles input events 190 */ 191 void SoundControl::handleKey(SDL_KeyboardEvent key) { 192 switch(key.keysym.sym) { 193 case SDLK_a: 194 if(key.type == SDL_KEYDOWN) { 195 if(sfx_channel1 < 0) { 196 sfx_channel1 = sound->playWav("sound1.wav"); 197 } 198 } else { 199 Mix_HaltChannel(sfx_channel1); 200 sfx_channel1 = -1; 201 } 202 break; 203 case SDLK_s: 204 if(key.type == SDL_KEYDOWN) { 205 if(sfx_channel2 < 0) { 206 sfx_channel2 = sound->playWav("sound2.wav"); 207 } 208 } else { 209 Mix_HaltChannel(sfx_channel2); 210 sfx_channel2 = -1; 211 } 212 break; 213 case SDLK_m: 214 if(key.state == SDL_PRESSED) { 215 sound->playOgg("music.ogg"); 216 } 217 break; 218 case SDLK_q: 219 finished = 1; 220 break; 221 default: 222 break; 223 } 224 } 225 226 int SoundControl::main(void) { 227 SDL_Surface* screen; 228 SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO); 229 screen = SDL_SetVideoMode(320, 240, 0, 0); 230 while(!finished) { 231 while(SDL_PollEvent(&event)) { 232 switch(event.type) { 233 case SDL_QUIT: 234 finished = 1; 235 break; 236 case SDL_KEYDOWN: 237 case SDL_KEYUP: 238 SoundControl::handleKey(event.key); 239 break; 240 default: 241 break; 242 } 243 } 244 SDL_Delay(50); 245 } 246 deleteInstance(); 247 SDL_Quit(); 248 return 0; 249 }
Note: See TracChangeset
for help on using the changeset viewer.