From 73eb0d20ab3bba0d0cca8dde00a8d9f83096a5d0 Mon Sep 17 00:00:00 2001 From: Lendemor Date: Mon, 4 Sep 2017 12:23:12 +0200 Subject: [PATCH] basic interfacing of sfml sound/music and integration with scenenode --- src/scene/musicnode.cpp | 28 +++++++++++++++++++++++ src/scene/musicnode.h | 22 ++++++++++++++++++ src/scene/soundnode.cpp | 40 +++++++++++++++++++++++++++++++++ src/scene/soundnode.h | 20 +++++++++++++++++ src/sparrowshell/sparrowshell.h | 1 + 5 files changed, 111 insertions(+) create mode 100644 src/scene/musicnode.cpp create mode 100644 src/scene/musicnode.h create mode 100644 src/scene/soundnode.cpp create mode 100644 src/scene/soundnode.h diff --git a/src/scene/musicnode.cpp b/src/scene/musicnode.cpp new file mode 100644 index 0000000..d6670bc --- /dev/null +++ b/src/scene/musicnode.cpp @@ -0,0 +1,28 @@ +#include "musicnode.h" + +MusicNode::MusicNode(std::string musicFileName): + m_music_file(musicFileName) +{ +} + +void MusicNode::init(){ + m_valid = m_music.openFromFile(m_music_file); +} + +void MusicNode::play() +{ + if (m_valid) + m_music.play(); +} + +void MusicNode::pause() +{ + if(m_valid) + m_music.pause(); +} + +void MusicNode::stop() +{ + if(m_valid) + m_music.stop(); +} diff --git a/src/scene/musicnode.h b/src/scene/musicnode.h new file mode 100644 index 0000000..3b542ed --- /dev/null +++ b/src/scene/musicnode.h @@ -0,0 +1,22 @@ +#ifndef MUSICNODE_H +#define MUSICNODE_H + +#include "scenenode.h" +#include "SFML/Audio.hpp" + +class MusicNode : public SceneNode +{ + sf::Music m_music; + std::string m_music_file; + bool m_valid; +public: + MusicNode(std::string musicFileName); + void init(); + void play(); + void pause(); + void stop(); + void setPlayingOffset(int offset); + +}; + +#endif // MUSICNODE_H diff --git a/src/scene/soundnode.cpp b/src/scene/soundnode.cpp new file mode 100644 index 0000000..aa8debf --- /dev/null +++ b/src/scene/soundnode.cpp @@ -0,0 +1,40 @@ +#include "soundnode.h" +#include "resourcemanager.h" + +SoundNode::SoundNode(std::string soundFileName): + m_sound_file(soundFileName) +{ + +} + +void SoundNode::init() +{ + if ((m_valid = RESOURCE_CHECK(sf::SoundBuffer,m_sound_file))) + m_sound.setBuffer(*RESOURCE_GET(sf::SoundBuffer, m_sound_file)); + else{ + sf::SoundBuffer* buffer; + m_valid = buffer->loadFromFile(m_sound_file); + if(m_valid){ + RESOURCE_ADD(buffer,sf::SoundBuffer,m_sound_file); + m_sound.setBuffer(*buffer); + } + } +} + +void SoundNode::play() +{ + if (m_valid) + m_sound.play(); +} + +void SoundNode::pause() +{ + if(m_valid) + m_sound.pause(); +} + +void SoundNode::stop() +{ + if(m_valid) + m_sound.stop(); +} diff --git a/src/scene/soundnode.h b/src/scene/soundnode.h new file mode 100644 index 0000000..ff3c279 --- /dev/null +++ b/src/scene/soundnode.h @@ -0,0 +1,20 @@ +#ifndef SOUNDNODE_H +#define SOUNDNODE_H + +#include "scene/scenenode.h" +#include "SFML/Audio.hpp" + +class SoundNode : public SceneNode +{ + sf::Sound m_sound; + std::string m_sound_file; + bool m_valid; +public: + SoundNode(std::string soundFileName); + void init(); + void play(); + void pause(); + void stop(); +}; + +#endif // SOUNDNODE_H diff --git a/src/sparrowshell/sparrowshell.h b/src/sparrowshell/sparrowshell.h index cce3c9a..835df79 100644 --- a/src/sparrowshell/sparrowshell.h +++ b/src/sparrowshell/sparrowshell.h @@ -85,6 +85,7 @@ public: unsigned int getIndex(){return m_buffer->getIndex();} ShellBuffer* getBuffer(){return m_buffer;} + ScriptNode* getScript(){return m_script;} void setInputs(int cursor_left, int cursor_right, int history_up, int history_down);