[update] Improved the codes to load audio files.

This commit is contained in:
falsycat 2019-10-13 00:00:00 +00:00
parent d3b02a184f
commit 7c455ed83a
2 changed files with 29 additions and 18 deletions

View File

@ -7,27 +7,23 @@ import sj.AbstractScene,
sj.KeyInput, sj.KeyInput,
sj.LobbyWorld, sj.LobbyWorld,
sj.ProgramSet, sj.ProgramSet,
sj.SceneInterface; sj.SceneInterface,
sj.util.audio;
/// ///
class SelectScene : AbstractScene { class SelectScene : AbstractScene {
public: public:
///
enum SpotlightSound = cast(ubyte[]) import("sounds/spotlight.wav");
/// ///
this(LobbyWorld lobby, ProgramSet program) { this(LobbyWorld lobby, ProgramSet program) {
lobby_ = lobby; lobby_ = lobby;
const buf = SpotlightSound;
spotlight_buffer_ = sfSoundBuffer_createFromMemory(buf.ptr, buf.length);
sound_ = sfSound_create(); sound_ = sfSound_create();
soundres_.Load();
first_ = true;
} }
~this() { ~this() {
sfSound_destroy(sound_); sfSound_destroy(sound_);
sfSoundBuffer_destroy(spotlight_buffer_); soundres_.Unload();
} }
/// ///
@ -36,12 +32,6 @@ class SelectScene : AbstractScene {
} }
override void Update(KeyInput input) { override void Update(KeyInput input) {
if (first_) {
sfSound_setBuffer(sound_, spotlight_buffer_);
sfSound_play(sound_);
}
first_ = false;
if (input.up) GoNextScene(title_scene_); if (input.up) GoNextScene(title_scene_);
} }
override void Draw() { override void Draw() {
@ -49,13 +39,25 @@ class SelectScene : AbstractScene {
} }
private: 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_; SceneInterface title_scene_;
LobbyWorld lobby_; LobbyWorld lobby_;
sfSoundBuffer* spotlight_buffer_;
sfSound* sound_; sfSound* sound_;
bool first_; SoundResources soundres_;
} }

9
src/sj/util/audio.d Normal file
View File

@ -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);
}