From 7c455ed83a9153ad86c97ca60de54d5b94004f85 Mon Sep 17 00:00:00 2001 From: falsycat Date: Sun, 13 Oct 2019 00:00:00 +0000 Subject: [PATCH] [update] Improved the codes to load audio files. --- src/sj/SelectScene.d | 38 ++++++++++++++++++++------------------ src/sj/util/audio.d | 9 +++++++++ 2 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 src/sj/util/audio.d diff --git a/src/sj/SelectScene.d b/src/sj/SelectScene.d index bf201e6..fed267a 100644 --- a/src/sj/SelectScene.d +++ b/src/sj/SelectScene.d @@ -7,27 +7,23 @@ import sj.AbstractScene, sj.KeyInput, sj.LobbyWorld, sj.ProgramSet, - sj.SceneInterface; + sj.SceneInterface, + sj.util.audio; /// class SelectScene : AbstractScene { public: - /// - enum SpotlightSound = cast(ubyte[]) import("sounds/spotlight.wav"); /// this(LobbyWorld lobby, ProgramSet program) { lobby_ = lobby; - const buf = SpotlightSound; - spotlight_buffer_ = sfSoundBuffer_createFromMemory(buf.ptr, buf.length); - sound_ = sfSound_create(); - - first_ = true; + sound_ = sfSound_create(); + soundres_.Load(); } ~this() { sfSound_destroy(sound_); - sfSoundBuffer_destroy(spotlight_buffer_); + soundres_.Unload(); } /// @@ -36,12 +32,6 @@ class SelectScene : AbstractScene { } override void Update(KeyInput input) { - if (first_) { - sfSound_setBuffer(sound_, spotlight_buffer_); - sfSound_play(sound_); - } - first_ = false; - if (input.up) GoNextScene(title_scene_); } override void Draw() { @@ -49,13 +39,25 @@ class SelectScene : AbstractScene { } private: + static struct SoundResources { + public: + enum Spotlight = cast(ubyte[]) import("sounds/spotlight.wav"); + + sfSoundBuffer* spotlight; + + void Load() { + spotlight = CreateSoundBufferFromBuffer(Spotlight); + } + void Unload() { + sfSoundBuffer_destroy(spotlight); + } + } + SceneInterface title_scene_; LobbyWorld lobby_; - sfSoundBuffer* spotlight_buffer_; - sfSound* sound_; - bool first_; + SoundResources soundres_; } diff --git a/src/sj/util/audio.d b/src/sj/util/audio.d new file mode 100644 index 0000000..9a50fff --- /dev/null +++ b/src/sj/util/audio.d @@ -0,0 +1,9 @@ +/// License: MIT +module sj.util.audio; + +import derelict.sfml2.audio; + +/// +sfSoundBuffer* CreateSoundBufferFromBuffer(in ubyte[] buf) { + return sfSoundBuffer_createFromMemory(buf.ptr, buf.length); +}