1 //       _________ __                 __
2 //      /   _____//  |_____________ _/  |______     ____  __ __  ______
3 //      \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
4 //      /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ |
5 //     /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
6 //             \/                  \/          \//_____/            \/
7 //  ______________________                           ______________________
8 //                        T H E   W A R   B E G I N S
9 //         Stratagus - A free fantasy real time strategy game engine
10 //
11 /**@name sound.h - The sound header file. */
12 //
13 //      (c) Copyright 1998-2007 by Lutz Sammer, Fabrice Rossi, and Jimmy Salmon
14 //
15 //      This program is free software; you can redistribute it and/or modify
16 //      it under the terms of the GNU General Public License as published by
17 //      the Free Software Foundation; only version 2 of the License.
18 //
19 //      This program is distributed in the hope that it will be useful,
20 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 //      GNU General Public License for more details.
23 //
24 //      You should have received a copy of the GNU General Public License
25 //      along with this program; if not, write to the Free Software
26 //      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 //      02111-1307, USA.
28 //
29 
30 #ifndef __SOUND_H__
31 #define __SOUND_H__
32 
33 //@{
34 
35 /*----------------------------------------------------------------------------
36 --  Includes
37 ----------------------------------------------------------------------------*/
38 
39 #include "unitsound.h"
40 #include "SDL.h"
41 #include "SDL_mixer.h"
42 
43 /*----------------------------------------------------------------------------
44 --  Declarations
45 ----------------------------------------------------------------------------*/
46 
47 class CUnit;
48 class Missile;
49 class LuaActionListener;
50 
51 /*----------------------------------------------------------------------------
52 --  Definitons
53 ----------------------------------------------------------------------------*/
54 
55 #define MaxSampleVolume 255  /// Maximum sample volume
56 #define NO_SOUND 0           /// No valid sound ID
57 
58 /**
59 **  Voice groups for a unit
60 */
61 enum UnitVoiceGroup {
62 	VoiceSelected,          /// If selected
63 	VoiceAcknowledging,     /// Acknowledge command
64 	VoiceReady,             /// Command completed
65 	VoiceHelpMe,            /// If attacked
66 	VoiceDying,             /// If killed
67 	VoiceWorkCompleted,     /// only worker, work completed
68 	VoiceBuilding,          /// only for building under construction
69 	VoiceDocking,           /// only for transport reaching coast
70 	VoiceRepairing,         /// repairing
71 	VoiceHarvesting,        /// harvesting
72 	VoiceAttack,            /// Attack command
73 	VoiceBuild              /// worker goes to build a building
74 };
75 
76 
77 /**
78 **  Global game sounds, not associated to any unit-type
79 */
80 class GameSound
81 {
82 public:
83 	SoundConfig PlacementError[MAX_RACES];        /// used by ui
84 	SoundConfig PlacementSuccess[MAX_RACES];      /// used by ui
85 	SoundConfig Click;                            /// used by ui
86 	SoundConfig Docking;                          /// ship reaches coast
87 	SoundConfig BuildingConstruction[MAX_RACES];  /// building under construction
88 	SoundConfig WorkComplete[MAX_RACES];          /// building ready
89 	SoundConfig Rescue[MAX_RACES];                /// rescue units
90 	SoundConfig ChatMessage;                      /// chat message
91 	SoundConfig ResearchComplete[MAX_RACES];      /// research complete message
92 	SoundConfig NotEnoughRes[MAX_RACES][MaxCosts];/// not enough resources message
93 	SoundConfig NotEnoughFood[MAX_RACES];         /// not enough food message
94 };
95 
96 /**
97 **  Sound definition.
98 */
99 class CSound
100 {
101 public:
CSound()102 	CSound() : Mapref(0), Range(0), Number(0)
103 	{
104 		memset(&Sound, 0, sizeof(Sound));
105 	}
106 	~CSound();
107 	unsigned int Mapref;
108 	/**
109 	**  Range is a multiplier for ::DistanceSilent.
110 	**  255 means infinite range of the sound.
111 	*/
112 	unsigned char Range;        /// Range is a multiplier for DistanceSilent
113 	unsigned char Number;       /// single, group, or table of sounds.
114 	union {
115 		Mix_Chunk *OneSound;       /// if it's only a simple sound
116 		Mix_Chunk **OneGroup;      /// when it's a simple group
117 		struct {
118 			CSound *First;       /// first group: selected sound
119 			CSound *Second;      /// second group: annoyed sound
120 		} TwoGroups;             /// when it's a double group
121 	} Sound;
122 };
123 
124 /**
125 ** A possible value for Number in the Sound struct: means a simple sound
126 */
127 #define ONE_SOUND 0
128 /**
129 ** A possible value for Number in the Sound struct: means a double group (for
130 ** selection/annoyed sounds)
131 */
132 #define TWO_GROUPS 1
133 
134 /**
135 ** the range value that makes a sound volume distance independent
136 */
137 #define INFINITE_SOUND_RANGE 255
138 /**
139 ** the maximum range value
140 */
141 #define MAX_SOUND_RANGE 254
142 
143 /**
144 **  Origin of a sound
145 */
146 struct Origin {
147 	const void *Base;   /// pointer on a Unit
148 	unsigned Id;        /// unique identifier (if the pointer has been shared)
149 };
150 
151 
152 /*----------------------------------------------------------------------------
153 --  Variables
154 ----------------------------------------------------------------------------*/
155 
156 extern GameSound GameSounds;  /// Game sound configuration
157 
158 extern bool CallbackMusic;  /// flag true callback ccl if stops
159 
160 /// global range control (max cut off distance for sound)
161 extern int DistanceSilent;
162 
163 /*----------------------------------------------------------------------------
164 --  Functions
165 ----------------------------------------------------------------------------*/
166 
167 /// Calculates volume level
168 extern unsigned char CalculateVolume(bool isVolume, int power, unsigned char range);
169 /// Play a unit sound
170 extern void PlayUnitSound(const CUnit &unit, UnitVoiceGroup unit_voice_group, bool sampleUnique = false);
171 /// Play a unit sound
172 extern void PlayUnitSound(const CUnit &unit, CSound *sound);
173 /// Play a missile sound
174 extern void PlayMissileSound(const Missile &missile, CSound *sound);
175 /// Play a game sound
176 extern void PlayGameSound(CSound *sound, unsigned char volume, bool always = false);
177 
178 /// Play a sound file
179 extern int PlayFile(const std::string &name, LuaActionListener *listener = NULL);
180 
181 /// Modify the range of a given sound.
182 extern void SetSoundRange(CSound *sound, unsigned char range);
183 
184 /// Register a sound (can be a simple sound or a group)
185 extern CSound *RegisterSound(const std::vector<std::string> &files);
186 
187 ///  Create a special sound group with two sounds
188 extern CSound *RegisterTwoGroups(CSound *first, CSound *second);
189 
190 /// Initialize client side of the sound layer.
191 extern void InitSoundClient();
192 
193 
194 // music.cpp
195 
196 /// Check if music is finished and play the next song
197 extern void CheckMusicFinished(bool force = false);
198 
199 /// Initialize music
200 extern void InitMusic();
201 
202 /// Turn music stopped callback on
203 #define CallbackMusicOn() \
204 	CallbackMusic = true;
205 /// Turn music stopped callback off
206 #define CallbackMusicOff() \
207 	CallbackMusic = false;
208 
209 
210 // sound_id.cpp
211 
212 /// Map sound to identifier
213 extern void MapSound(const std::string &sound_name, CSound *id);
214 /// Get the sound id bound to an identifier
215 extern CSound *SoundForName(const std::string &sound_name);
216 /// Make a sound bound to identifier
217 extern CSound *MakeSound(const std::string &sound_name, const std::vector<std::string> &files);
218 /// Make a sound group bound to identifier
219 extern CSound *MakeSoundGroup(const std::string &name, CSound *first, CSound *second);
220 
221 extern void FreeSounds();
222 
223 // script_sound.cpp
224 
225 /// register ccl features
226 extern void SoundCclRegister();
227 
228 
229 //@}
230 
231 #endif  // !__SOUND_H__
232