00001 #ifndef _ZAKSOUND_H_
00002 #define _ZAKSOUND_H_
00003
00004 #include "irrKlang/irrKlang.h"
00005 #include "Defines.h"
00006 #include "ScriptMgr.h"
00007
00008 #pragma comment(lib, "irrKlang.lib")
00009
00010 using namespace std;
00011 using namespace irr;
00012 using namespace audio;
00013
00014 namespace zak {
00015
00107 class ZAKENGINE_API Sound : public SupportScriptReferences {
00108 public:
00113 void setName(string &name) { _name = name; }
00114
00115
00116
00121
00122 string getName() { return _name; }
00123
00127 void play() {
00128 _sound->setVolume(_volume);
00129 _sound->setIsPaused(false);
00130 }
00131
00135 void stop() { _sound->stop(); _sound->drop(); _sound = _engine->play2D(_pSound, _repeat, true, true);}
00136
00140 void pause() { if (!_sound->isFinished()) _sound->setIsPaused(true); }
00141
00146 void setVolume(float volume) {
00147 _volume = volume;
00148 _sound->setVolume(volume);
00149 }
00150
00155 float getVolume() { return _sound->getVolume(); }
00156
00161 void setRepeat(bool repeat) { _repeat = true; }
00162
00167 bool getRepeat() { return _repeat; }
00168
00173 int getLength() { return _sound->getPlayLength(); }
00174
00179 int getPosition() { return _sound->getPlayPosition(); }
00180
00185 void setPan(float pan) { _sound->setPan(pan); }
00186
00191 float getPan() { return _sound->getPan(); }
00192
00197 bool isPlaying() { return !_sound->isFinished(); }
00198
00199 bool isPaused() { return _sound->getIsPaused(); }
00200
00201 Sound(){
00202 _pSound = NULL;
00203 _engine = NULL;
00204 _sound = NULL;
00205 _repeat = false;
00206 _volume = 0.5f;
00207 }
00208
00209 ~Sound(){ if (_sound) _sound->drop(); _sound = NULL; _pSound = NULL; }
00210
00211 private:
00212 void drop() { if (_sound) _sound->drop(); _sound = NULL; _pSound = NULL; }
00213
00214
00215 void setSoundActive(bool active) { _active = active; }
00216
00217 static Sound &CopyConstruct(Sound& other, Sound& self) {
00218 self = other;
00219 return self;
00220 }
00221
00222 static void Constructor(Sound *obj)
00223 {
00224 new(obj) Sound();
00225 }
00226
00227 static bool RegisterSound(char *className=NULL) {
00228 char _className[100];
00229
00230 if (className != NULL) {
00231 strcpy_s(_className, 100, className);
00232 } else {
00233 strcpy_s(_className, 100, "Sound");
00234
00235 if (ScriptMgr::RegisterObjectType(_className, sizeof(Sound), asOBJ_CLASS |asOBJ_CLASS_CONSTRUCTOR| asOBJ_CLASS_ASSIGNMENT) < 0)
00236 return false;
00237
00238 if (ScriptMgr::RegisterObjectBehaviour(_className, asBEHAVE_ADDREF,"void f()", asMETHOD(SupportScriptReferences, AddRef), asCALL_GENERIC) < 0)
00239 return false;
00240 if (ScriptMgr::RegisterObjectBehaviour(_className, asBEHAVE_RELEASE,"void f()", asMETHOD(SupportScriptReferences, Release), asCALL_GENERIC) < 0)
00241 return false;
00242 if (ScriptMgr::RegisterObjectBehaviour(_className, asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(Constructor), asCALL_CDECL_OBJLAST) < 0)
00243 return false;
00244 if (ScriptMgr::RegisterObjectBehaviour(_className, asBEHAVE_ASSIGNMENT,"Sound &f(Sound&in)", asFUNCTION(CopyConstruct), asCALL_CDECL_OBJLAST) < 0)
00245 return false;
00246 }
00247
00248 if (ScriptMgr::RegisterObjectMethod(_className, "void setName(string&in)", asMETHODPR(Sound, setName, (string&), void), asCALL_THISCALL) < 0)
00249 return false;
00250 if (ScriptMgr::RegisterObjectMethod(_className, "string getName()", asMETHODPR(Sound, getName, (void), void), asCALL_THISCALL) < 0)
00251 return false;
00252 if (ScriptMgr::RegisterObjectMethod(_className, "void play()", asMETHODPR(Sound, play, (void), void), asCALL_THISCALL) < 0)
00253 return false;
00254 if (ScriptMgr::RegisterObjectMethod(_className, "void stop()", asMETHODPR(Sound, stop, (void), void), asCALL_THISCALL) < 0)
00255 return false;
00256 if (ScriptMgr::RegisterObjectMethod(_className, "void pause()", asMETHODPR(Sound, pause, (void), void), asCALL_THISCALL) < 0)
00257 return false;
00258 if (ScriptMgr::RegisterObjectMethod(_className, "void setVolume(float)", asMETHODPR(Sound, setVolume, (float), void), asCALL_THISCALL) < 0)
00259 return false;
00260 if (ScriptMgr::RegisterObjectMethod(_className, "float getVolume()", asMETHODPR(Sound, getVolume, (void), float), asCALL_THISCALL) < 0)
00261 return false;
00262 if (ScriptMgr::RegisterObjectMethod(_className, "void setRepeat(float)", asMETHODPR(Sound, setRepeat, (float), void), asCALL_THISCALL) < 0)
00263 return false;
00264 if (ScriptMgr::RegisterObjectMethod(_className, "bool getRepeat()", asMETHODPR(Sound, getRepeat, (void), bool), asCALL_THISCALL) < 0)
00265 return false;
00266 if (ScriptMgr::RegisterObjectMethod(_className, "int getLength()", asMETHODPR(Sound, getLength, (void), int), asCALL_THISCALL) < 0)
00267 return false;
00268 if (ScriptMgr::RegisterObjectMethod(_className, "void setPan(float)", asMETHODPR(Sound, setPan, (float), void), asCALL_THISCALL) < 0)
00269 return false;
00270 if (ScriptMgr::RegisterObjectMethod(_className, "float getPan()", asMETHODPR(Sound, getPan, (void), float), asCALL_THISCALL) < 0)
00271 return false;
00272 if (ScriptMgr::RegisterObjectMethod(_className, "bool isPlaying()", asMETHODPR(Sound, isPlaying, (void), bool), asCALL_THISCALL) < 0)
00273 return false;
00274 if (ScriptMgr::RegisterObjectMethod(_className, "bool isPaused()", asMETHODPR(Sound, isPaused, (void), bool), asCALL_THISCALL) < 0)
00275 return false;
00276 if (!RegisterVector<Sound>("Sound[]", "Sound", ScriptMgr::GetEngine()))
00277 return false;
00278
00279 return true;
00280 }
00281
00282
00283 Sound(ISoundSource *pSound, ISound *sound, ISoundEngine *engine, bool stream){
00284 _pSound = pSound;
00285 if (stream) _pSound->setForcedStreamingThreshold(25000);
00286 sound->setVolume(_volume);
00287 _engine = engine;
00288 _sound = sound;
00289 _repeat = false;
00290 _volume = 0.5f;
00291 _stream = stream;
00292 }
00293
00294
00295 ISoundSource *_pSound;
00296 ISoundEngine *_engine;
00297 ISound *_sound;
00298
00299 float _volume;
00300 bool _repeat;
00301 bool _stream;
00302
00303 static bool _active;
00304
00305
00306 string _name;
00307
00308 friend class SoundMgr;
00309 };
00310
00311 }
00312
00313 #endif // _ZAKSOUND_H_