Debian Codegraph @

engine/sound/script_sound.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name script_sound.cpp - The sound ccl functions. */
11 //
12 // (c) Copyright 1999-2006 by Lutz Sammer, Fabrice Rossi, and Jimmy Salmon
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 <string.h>
36 #include <stdio.h>
37
38 #include "stratagus.h"
39
40 #include <stdlib.h>
41
42 #include "script.h"
43 #include "sound.h"
44 #include "sound_server.h"
45
46 /*----------------------------------------------------------------------------
47 -- Variables
48 ----------------------------------------------------------------------------*/
49
50
51 /*----------------------------------------------------------------------------
52 -- Functions
53 ----------------------------------------------------------------------------*/
54
55 /**
56 ** Glue between c and scheme. Ask the sound system to associate a
57 ** sound id to a sound name.
58 **
59 ** @param l Lua state.
60 */
61 static int CclSoundForName(lua_State *l)
62 {
63 CSound *id;
64 const char *sound_name;
65 LuaUserData *data;
66
67 sound_name = LuaToString(l, -1);
68 id = SoundForName(sound_name);
69
70 data = (LuaUserData *)lua_newuserdata(l, sizeof(LuaUserData));
71 data->Type = LuaSoundType;
72 data->Data = id;
73 return 1;
74 }
75
76 /**
77 ** Get a Game Sound Id from either a lua sound id or a sound name
78 **
79 ** @param l Lua state.
80 **
81 ** @return The C sound id.
82 */
83 static CSound *CclGetSound(lua_State *l)
84 {
85 LuaUserData *data;
86 int pop;
87
88 pop = 0;
89 if (lua_isstring(l, -1)) {
90 CclSoundForName(l);
91 pop = 1;
92 }
93 if (lua_isuserdata(l, -1)) {
94 data = (LuaUserData *)lua_touserdata(l, -1);
95 if (data->Type == LuaSoundType) {
96 if (pop) {
97 lua_pop(l, 1);
98 }
99 return (CSound *)data->Data;
100 }
101 }
102 LuaError(l, "CclGetSound: not a sound");
103 return NULL;
104 }
105
106 /**
107 ** Create a sound.
108 **
109 ** Glue between c and scheme. This function asks the sound system to
110 ** register a sound under a given name, wiht an associated list of files
111 ** (the list can be replaced by only one file).
112 **
113 ** @param l Lua state.
114 **
115 ** @return the sound id of the created sound
116 */
117 static int CclMakeSound(lua_State *l)
118 {
119 CSound *id;
120 std::string c_name;
121 const char *c_file;
122 char **c_files;
123 int args;
124 int j;
125 LuaUserData *data;
126
127 LuaCheckArgs(l, 2);
128
129 c_name = LuaToString(l, 1);
130 if (lua_isstring(l, 2)) {
131 // only one file
132 c_file = LuaToString(l, 2);
133 id = MakeSound(c_name, &c_file, 1);
134 } else if (lua_istable(l, 2)) {
135 // several files
136 args = lua_objlen(l, 2);
137 c_files = new char *[args];
138 for (j = 0; j < args; ++j) {
139 c_files[j] = new_strdup(LuaToString(l, 2, j + 1));
140 }
141 id = MakeSound(c_name, (const char **)c_files, args);
142 for (j = 0; j < args; ++j) {
143 delete[] c_files[j];
144 }
145 delete[] c_files;
146 } else {
147 LuaError(l, "string or table expected");
148 return 0;
149 }
150 data = (LuaUserData *)lua_newuserdata(l, sizeof(LuaUserData));
151 data->Type = LuaSoundType;
152 data->Data = id;
153 return 1;
154 }
155
156 /**
157 ** Glue between c and scheme. This function asks the sound system to
158 ** build a special sound group.
159 **
160 ** @param l Lua state.
161 **
162 ** @return The sound id of the created sound
163 */
164 static int CclMakeSoundGroup(lua_State *l)
165 {
166 CSound *id;
167 std::string c_name;
168 CSound *first;
169 CSound *second;
170 LuaUserData *data;
171
172 LuaCheckArgs(l, 3);
173
174 c_name = LuaToString(l, 1);
175
176 lua_pushvalue(l, 2);
177 first = CclGetSound(l);
178 lua_pop(l, 1);
179 second = CclGetSound(l);
180 id = MakeSoundGroup(c_name, first, second);
181 data = (LuaUserData *)lua_newuserdata(l, sizeof(LuaUserData));
182 data->Type = LuaSoundType;
183 data->Data = id;
184 return 1;
185 }
186
187 /**
188 ** Glue between c and scheme. Ask to the sound system to remap a sound id
189 ** to a given name.
190 **
191 ** @param l Lua state.
192 **
193 ** @return the sound object
194 */
195 static int CclMapSound(lua_State *l)
196 {
197 const char *sound_name;
198
199 LuaCheckArgs(l, 2);
200 sound_name = LuaToString(l, 1);
201 MapSound(sound_name, CclGetSound(l));
202 lua_pushvalue(l, 2);
203 return 1;
204 }
205
206 /**
207 ** Ask the sound system to play the specified sound.
208 **
209 ** @param l Lua state.
210 */
211 static int CclPlaySound(lua_State *l)
212 {
213 CSound *id;
214
215 LuaCheckArgs(l, 1);
216
217 id = CclGetSound(l);
218 PlayGameSound(id, MaxSampleVolume);
219 return 0;
220 }
221
222 /**
223 ** Glue between c and scheme. Allows to specify some global game sounds
224 ** in a ccl file.
225 **
226 ** @param l Lua state.
227 */
228 static int CclDefineGameSounds(lua_State *l)
229 {
230 const char *value;
231 int args;
232 int j;
233 LuaUserData *data;
234
235 args = lua_gettop(l);
236 data = NULL;
237 for (j = 0; j < args; ++j) {
238 value = LuaToString(l, j + 1);
239 ++j;
240
241 // let's handle now the different cases
242 if (!strcmp(value, "click")) {
243 if (!lua_isuserdata(l, j + 1) ||
244 (data = (LuaUserData *)lua_touserdata(l, j + 1))->Type != LuaSoundType) {
245 LuaError(l, "Sound id expected");
246 }
247 GameSounds.Click.Sound = (CSound *)data->Data;
248 } else if (!strcmp(value, "placement-error")) {
249 if (!lua_isuserdata(l, j + 1) ||
250 (data = (LuaUserData *)lua_touserdata(l, j + 1))->Type != LuaSoundType) {
251 LuaError(l, "Sound id expected");
252 }
253 GameSounds.PlacementError.Sound = (CSound *)data->Data;
254 } else if (!strcmp(value, "placement-success")) {
255 if (!lua_isuserdata(l, j + 1) ||
256 (data = (LuaUserData *)lua_touserdata(l, j + 1))->Type != LuaSoundType) {
257 LuaError(l, "Sound id expected");
258 }
259 GameSounds.PlacementSuccess.Sound = (CSound *)data->Data;
260 } else if (!strcmp(value, "rescue")) {
261 if (!lua_isuserdata(l, j + 1) ||
262 (data = (LuaUserData *)lua_touserdata(l, j + 1))->Type != LuaSoundType) {
263 LuaError(l, "Sound id expected");
264 }
265 GameSounds.Rescue.Sound = (CSound *)data->Data;
266 } else if (!strcmp(value, "chat-message")) {
267 if (!lua_isuserdata(l, j + 1) ||
268 (data = (LuaUserData *)lua_touserdata(l, j + 1))->Type != LuaSoundType) {
269 LuaError(l, "Sound id expected");
270 }
271 GameSounds.ChatMessage.Sound = (CSound *)data->Data;
272 } else {
273 LuaError(l, "Unsupported tag: %s" _C_ value);
274 }
275 }
276 return 0;
277 }
278
279 /**
280 ** Set the cut off distance.
281 **
282 ** @param l Lua state.
283 */
284 static int CclSetGlobalSoundRange(lua_State *l)
285 {
286 int d;
287
288 LuaCheckArgs(l, 1);
289
290 d = LuaToNumber(l, 1);
291 if (d > 0) {
292 DistanceSilent = d;
293 }
294 return 0;
295 }
296
297 /**
298 ** Set the range of a given sound.
299 **
300 ** @param l Lua state.
301 */
302 static int CclSetSoundRange(lua_State *l) {
303 unsigned char theRange;
304 int tmp;
305 CSound *id;
306
307 LuaCheckArgs(l, 2);
308
309 tmp = LuaToNumber(l, 2);
310 if (tmp < 0) {
311 theRange = 0;
312 } else if (tmp > 255) {
313 theRange = 255;
314 } else {
315 theRange = (unsigned char)tmp;
316 }
317 lua_pushvalue(l, 1);
318 id = CclGetSound(l);
319 SetSoundRange(id, theRange);
320 return 1;
321 }
322
323 /**
324 ** Register CCL features for sound.
325 */
326 void SoundCclRegister(void)
327 {
328 lua_register(Lua, "SetGlobalSoundRange", CclSetGlobalSoundRange);
329 lua_register(Lua, "DefineGameSounds", CclDefineGameSounds);
330 lua_register(Lua, "MapSound", CclMapSound);
331 lua_register(Lua, "SoundForName", CclSoundForName);
332 lua_register(Lua, "SetSoundRange", CclSetSoundRange);
333 lua_register(Lua, "MakeSound", CclMakeSound);
334 lua_register(Lua, "MakeSoundGroup", CclMakeSoundGroup);
335 lua_register(Lua, "PlaySound", CclPlaySound);
336 }
337
338 //@}