1 /*
2    File:        SoundManager.cpp
3   Description: Sound management
4   Program:     BlockOut
5   Author:      Jean-Luc PONS
6 
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11 
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16 */
17 
18 #include "SoundManager.h"
19 #include "Types.h"
20 #include <stdio.h>
21 
22 #define PLAY(x) if( enabled && x ) Mix_PlayChannel (-1, x, 0);
23 
24 // ------------------------------------------------
25 
SoundManager()26 SoundManager::SoundManager() {
27 
28   enabled = FALSE;
29   blubSound = NULL;
30   wozzSound = NULL;
31   tchhSound = NULL;
32   lineSound = NULL;
33   levelSound = NULL;
34   wellDoneSound = NULL;
35   emptySound = NULL;
36   line2Sound = NULL;
37   level2Sound = NULL;
38   wellDone2Sound = NULL;
39   empty2Sound = NULL;
40   creditsMusic = NULL;
41   hitSound = NULL;
42   strcpy(errMsg,"");
43 
44 }
45 
46 // ------------------------------------------------
47 
Create()48 int SoundManager::Create() {
49 
50   if ( Mix_OpenAudio (44100, AUDIO_S16, 2, 512) < 0 )
51   {
52     sprintf(errMsg, "SDL_mixer: Audio initialisation error");
53     enabled=FALSE;
54     return FALSE;
55   }
56 
57   if( !InitSound(LID(STR("sounds/blub.wav")),&blubSound) ) return FALSE;
58   if( !InitSound(LID(STR("sounds/wozz.wav")),&wozzSound) ) return FALSE;
59   if( !InitSound(LID(STR("sounds/tchh.wav")),&tchhSound) ) return FALSE;
60   if( !InitSound(LID(STR("sounds/line.wav")),&lineSound) ) return FALSE;
61   if( !InitSound(LID(STR("sounds/level.wav")),&levelSound) ) return FALSE;
62   if( !InitSound(LID(STR("sounds/empty.wav")),&emptySound) ) return FALSE;
63   if( !InitSound(LID(STR("sounds/welldone.wav")),&wellDoneSound) ) return FALSE;
64   if( !InitSound(LID(STR("sounds/line2.wav")),&line2Sound) ) return FALSE;
65   if( !InitSound(LID(STR("sounds/level2.wav")),&level2Sound) ) return FALSE;
66   if( !InitSound(LID(STR("sounds/empty2.wav")),&empty2Sound) ) return FALSE;
67   if( !InitSound(LID(STR("sounds/welldone2.wav")),&wellDone2Sound) ) return FALSE;
68   if( !InitSound(LID(STR("sounds/hit.wav")),&hitSound) ) return FALSE;
69 
70   // Demo music on channel 0
71   Mix_ReserveChannels(1);
72 
73   return TRUE;
74 
75 }
76 
77 // ------------------------------------------------
78 
SetEnable(BOOL enable)79 void SoundManager::SetEnable(BOOL enable) {
80   enabled = enable;
81 }
82 
83 // ------------------------------------------------
84 
InitSound(char * fileName,Mix_Chunk ** snd)85 int SoundManager::InitSound(char *fileName,Mix_Chunk **snd) {
86 
87   *snd = Mix_LoadWAV ( fileName );
88   if( *snd == NULL ) {
89     sprintf(errMsg, "Failed to initialise %s : %s",fileName,SDL_GetError());
90   }
91   return ( *snd != NULL );
92 
93 }
94 
95 // ------------------------------------------------
96 
GetErrorMsg()97 char *SoundManager::GetErrorMsg() {
98 
99   return errMsg;
100 
101 }
102 
103 // ------------------------------------------------
104 
PlayBlub()105 void SoundManager::PlayBlub() {
106   PLAY(blubSound);
107 }
PlayWozz()108 void SoundManager::PlayWozz() {
109   PLAY(wozzSound);
110 }
PlayTchh()111 void SoundManager::PlayTchh() {
112   PLAY(tchhSound);
113 }
PlayLine()114 void SoundManager::PlayLine() {
115   PLAY(lineSound);
116 }
PlayLevel()117 void SoundManager::PlayLevel() {
118   PLAY(levelSound);
119 }
PlayEmpty()120 void SoundManager::PlayEmpty() {
121   PLAY(emptySound);
122 }
PlayWellDone()123 void SoundManager::PlayWellDone() {
124   PLAY(wellDoneSound);
125 }
PlayLine2()126 void SoundManager::PlayLine2() {
127   PLAY(line2Sound);
128 }
PlayLevel2()129 void SoundManager::PlayLevel2() {
130   PLAY(level2Sound);
131 }
PlayEmpty2()132 void SoundManager::PlayEmpty2() {
133   PLAY(empty2Sound);
134 }
PlayWellDone2()135 void SoundManager::PlayWellDone2() {
136   PLAY(wellDone2Sound);
137 }
PlayHit()138 void SoundManager::PlayHit() {
139   PLAY(hitSound);
140 }
141 
PlayMusic()142 void SoundManager::PlayMusic() {
143 
144   if( creditsMusic==NULL ) {
145     creditsMusic = Mix_LoadMUS( LID(STR("sounds/music.ogg")) );
146     if( !creditsMusic ) {
147 #ifdef WINDOWS
148     char message[256];
149 	sprintf(message,"Music cannot be played: %s\n",SDL_GetError());
150 	MessageBox(NULL,message,"Warning",MB_OK|MB_ICONWARNING);
151 #else
152       printf("Music cannot be played: %s\n",SDL_GetError());
153 #endif
154       return;
155     }
156   }
157   if( creditsMusic ) {
158     Mix_FadeInMusic(creditsMusic,-1,1000);
159   }
160 
161 }
162 
StopMusic()163 void SoundManager::StopMusic() {
164 
165   if( creditsMusic ) {
166     Mix_HaltMusic();
167      Mix_FreeMusic(creditsMusic);
168     creditsMusic = NULL;
169   }
170 
171 }
172