| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name sound_id.cpp - The sound id. */ |
| 11 |
// |
| 12 |
// (c) Copyright 1998-2007 by Lutz Sammer and Fabrice Rossi |
| 13 |
// |
| 14 |
// This program is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
// |
| 28 |
|
| 29 |
//@{ |
| 30 |
|
| 31 |
/*---------------------------------------------------------------------------- |
| 32 |
-- Includes |
| 33 |
----------------------------------------------------------------------------*/ |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
|
| 39 |
#include "stratagus.h" |
| 40 |
|
| 41 |
#include <string> |
| 42 |
#include <map> |
| 43 |
|
| 44 |
#include "sound.h" |
| 45 |
#include "sound_server.h" |
| 46 |
|
| 47 |
/*---------------------------------------------------------------------------- |
| 48 |
-- Variables |
| 49 |
----------------------------------------------------------------------------*/ |
| 50 |
|
| 51 |
static std::map<std::string, CSound *> SoundMap; |
| 52 |
|
| 53 |
/*---------------------------------------------------------------------------- |
| 54 |
-- Functions |
| 55 |
----------------------------------------------------------------------------*/ |
| 56 |
|
| 57 |
/** |
| 58 |
** Add a new mapping (sound name to sound id) in the hash table |
| 59 |
** Create a new mapping between a name and an already valid sound id. |
| 60 |
** |
| 61 |
** @param name Name of the sound (now freed by caller!). |
| 62 |
** @param id Sound identifier. |
| 63 |
*/ |
| 64 |
void MapSound(const std::string &name, CSound *id) |
| 65 |
{ |
| 66 |
SoundMap[name] = id; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
** Maps a sound name to its id |
| 71 |
** |
| 72 |
** @param name Sound name. |
| 73 |
** |
| 74 |
** @return Sound identifier for this name. |
| 75 |
*/ |
| 76 |
CSound *SoundForName(const std::string &name) |
| 77 |
{ |
| 78 |
Assert(!name.empty()); |
| 79 |
|
| 80 |
CSound *result = SoundMap[name]; |
| 81 |
if (!result) { |
| 82 |
DebugPrint("Can't find sound `%s' in sound table\n" _C_ name.c_str()); |
| 83 |
} |
| 84 |
return result; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
** Ask the sound server to register a sound and store the mapping |
| 89 |
** between its name and its id. |
| 90 |
** Register a sound group (or an unique sound if nb==1) and get the |
| 91 |
** corresponding sound id. |
| 92 |
** |
| 93 |
** @param name name of this sound group (Freed by caller). |
| 94 |
** @param file list of sound file names |
| 95 |
** @param nb number of sounds |
| 96 |
** |
| 97 |
** @return the sound id of the created group |
| 98 |
*/ |
| 99 |
CSound *MakeSound(const std::string &name, const char *file[], int nb) |
| 100 |
{ |
| 101 |
CSound *sound; |
| 102 |
|
| 103 |
Assert(nb <= 255); |
| 104 |
|
| 105 |
if ((sound = SoundMap[name])) { |
| 106 |
DebugPrint("re-register sound `%s'\n" _C_ name.c_str()); |
| 107 |
return sound; |
| 108 |
} |
| 109 |
|
| 110 |
sound = RegisterSound(file, nb); |
| 111 |
MapSound(name, sound); |
| 112 |
|
| 113 |
return sound; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
** Ask the sound server to build a special sound group. |
| 118 |
** |
| 119 |
** Register two sound groups together to make a special sound (for |
| 120 |
** selection). Return the corresponding id after registering it under a |
| 121 |
** given name. |
| 122 |
** |
| 123 |
** @param name the name of the group (handled by caller). |
| 124 |
** @param first id of the first group |
| 125 |
** @param second id of the second group |
| 126 |
** |
| 127 |
** @return Registered sound identifier. |
| 128 |
*/ |
| 129 |
CSound *MakeSoundGroup(const std::string &name, CSound *first, CSound *second) |
| 130 |
{ |
| 131 |
CSound *sound; |
| 132 |
|
| 133 |
if ((sound = SoundMap[name])) { |
| 134 |
DebugPrint("re-register sound `%s'\n" _C_ name.c_str()); |
| 135 |
return sound; |
| 136 |
} |
| 137 |
|
| 138 |
sound = RegisterTwoGroups(first, second); |
| 139 |
MapSound(name, sound); |
| 140 |
|
| 141 |
return sound; |
| 142 |
} |
| 143 |
|
| 144 |
#ifdef DEBUG |
| 145 |
void FreeSounds() |
| 146 |
{ |
| 147 |
std::map<std::string, CSound *>::iterator i; |
| 148 |
for (i = SoundMap.begin(); i != SoundMap.end(); ++i) { |
| 149 |
delete (*i).second; |
| 150 |
} |
| 151 |
} |
| 152 |
#endif |
| 153 |
|
| 154 |
//@} |