From c3a331b73f4d27983d36e022d3638d36597e77e1 Mon Sep 17 00:00:00 2001 From: janwas Date: Mon, 15 Nov 2004 18:23:39 +0000 Subject: [PATCH] add JS sound binding This was SVN commit r1328. --- source/main.cpp | 2 ++ source/sound/JSI_Sound.cpp | 60 ++++++++++++++++++++++++++++++++++++++ source/sound/JSI_Sound.h | 42 ++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100755 source/sound/JSI_Sound.cpp create mode 100755 source/sound/JSI_Sound.h diff --git a/source/main.cpp b/source/main.cpp index 0edfc92f8f..124b7a5dba 100755 --- a/source/main.cpp +++ b/source/main.cpp @@ -72,6 +72,7 @@ #endif #include "sound/CMusicPlayer.h" +#include "sound/JSI_Sound.h" #include "lib/res/snd.h" #include "Network/SessionManager.h" @@ -690,6 +691,7 @@ static void InitScripting() // Register the JavaScript interfaces with the runtime CEntity::ScriptingInit(); CBaseEntity::ScriptingInit(); + JSI_Sound::ScriptingInit(); JSI_IGUIObject::init(); JSI_GUITypes::init(); diff --git a/source/sound/JSI_Sound.cpp b/source/sound/JSI_Sound.cpp new file mode 100755 index 0000000000..8041e4d9a2 --- /dev/null +++ b/source/sound/JSI_Sound.cpp @@ -0,0 +1,60 @@ +#include "precompiled.h" +#include "JSI_Sound.h" + +#include "lib/res/snd.h" +#include "lib/res/h_mgr.h" // h_filename + + +JSI_Sound::JSI_Sound( const CStr& Filename ) +{ + Handle m_Handle = snd_open(Filename.c_str()); +} + +JSI_Sound::~JSI_Sound() +{ + this->free(); +} + + +// start playing the sound (one-shot). +// it will automatically be freed when done. +void JSI_Sound::play() +{ + snd_play(m_Handle); +} + +// request the sound be played until free() is called. returns immediately. +void JSI_Sound::loop() +{ + snd_set_loop(m_Handle, true); + snd_play(m_Handle); +} + +// stop sound if currently playing and free resources. +// doesn't need to be called unless played via loop() - +// sounds are freed automatically when done playing. +void JSI_Sound::free() +{ + snd_free(m_Handle); // resets it to 0 +} + + +// Script-bound functions + + +void JSI_Sound::ScriptingInit() +{ + AddMethod( "toString", 0 ); + + CJSObject::ScriptingInit( "Sound" ); +} + +jsval JSI_Sound::ToString( JSContext* cx, uintN argc, jsval* argv ) +{ + const char* Filename = h_filename(m_Handle); + wchar_t buffer[256]; + swprintf( buffer, 256, L"[object Sound: %ls]", Filename ); + buffer[255] = 0; + utf16string str16(buffer, buffer+wcslen(buffer)); + return( STRING_TO_JSVAL( JS_NewUCStringCopyZ( cx, str16.c_str() ) ) ); +} diff --git a/source/sound/JSI_Sound.h b/source/sound/JSI_Sound.h new file mode 100755 index 0000000000..8ad1724515 --- /dev/null +++ b/source/sound/JSI_Sound.h @@ -0,0 +1,42 @@ +// JS sound binding +// +// Jan Wassenberg (jan@wildfiregames.com) + +#ifndef JSSOUND_INCLUDED +#define JSSOUND_INCLUDED + +#include "scripting/ScriptableObject.h" +#include "lib/res/handle.h" + +class JSI_Sound : public CJSObject +{ +public: + + Handle m_Handle; + + // note: filename is stored by handle manager; no need to keep a copy here. + + JSI_Sound(const CStr& Filename); + ~JSI_Sound(); + + // start playing the sound (one-shot). + // it will automatically be freed when done. + void play(); + + // request the sound be played until free() is called. returns immediately. + void loop(); + + // stop sound if currently playing and free resources. + // doesn't need to be called unless played via loop() - + // sounds are freed automatically when done playing. + void free(); + + + // Script-bound functions + + jsval ToString( JSContext* cx, uintN argc, jsval* argv ); + + static void ScriptingInit(); +}; + +#endif // #ifndef JSSOUND_INCLUDED