Hello and good day everybody!
What I'm trying to accomplish is fading in and fading out of sf::Audio related members utilizing the sf::Time functionality. The current code I have does not use the sf::Time (yet), only generic floats. I don't know how to calculate the volume increments I need based on sf::seconds, sf::milliseconds or sf::microseconds.
Here is the sample header and source code, with much noise (no pun intended) omitted. I made sure to comment each and every line so you can follow along with what I'm trying to do.
GameAudio.hpp
#ifndef GAME_AUDIO_H
#define GAME_AUDIO_H
#include <Game.hpp>
#include <SFML/Audio.hpp>
#include <SFML/System/Time.hpp>
class GameAudio {
public:
// Construct and Destruct
GameAudio();
virtual ~GameAudio();
// Public Functions
void fadeBGM(float volume, float duration);
void update();
// Public Member Variables
sf::Music m_BGM; // Background Music
protected:
private:
// Private Member Variables
float m_BGMFadeV = 0.0; // BGM Fade Volume
float m_BGMFadeD = 0.0; // BGM Fade Duration
// Private functions
void updateBGM();
// ...Need to add limited vector for sound queue
};
#endif // GAME_AUDIO_H
GameAudio.cpp
#include <Game.hpp>
#include <GameAudio.hpp>
#include <SFML/Audio.hpp>
#include <SFML/System/Time.hpp>
/*----------------------------------------------------------------------------*/
// ** GameAudio.fadeBGM(volume, duration, mode)
/*----------------------------------------------------------------------------*/
void GameAudio::fadeBGM(float volume, float duration) {
// Set BGM fade volume and duration
m_BGMFadeV = volume;
m_BGMFadeD = duration;
}
/*----------------------------------------------------------------------------*/
// ** GameAudio.update()
/*----------------------------------------------------------------------------*/
void GameAudio::update() {
/* ...Misc code... */
updateBGM();
/* ...Misc code... */
}
/*----------------------------------------------------------------------------*/
// ** GameAudio.updateBGM()
/*----------------------------------------------------------------------------*/
void GameAudio::updateBGM() {
// If fading BGM in
if (m_BGMFadeV > m_BGM.getVolume()) {
// Get new volume value
float value = (m_BGM.getVolume() + 0.1);
// Roll BGM in
m_BGM.setVolume(value);
// If over limit
if (m_BGMFadeV < m_BGM.getVolume()) {
// Sync volume to stop process
m_BGMFadeV = m_BGM.getVolume();
// End function
return;
}
// Print to console
cout << "Fade BGM (Old Volume = " << m_BGM.getVolume() << ", ";
cout << "New Volume = " << value << ")" << endl;
// If fading BGM out
} else if (m_BGMFadeV < m_BGM.getVolume()) {
// Get new volume value
float value = (m_BGM.getVolume() - 0.1);
// Roll BGM out
m_BGM.setVolume(value);
// If under limit
if (m_BGMFadeV > m_BGM.getVolume()) {
// Sync volume to stop process
m_BGMFadeV = m_BGM.getVolume();
// End function
return;
}
// Print to console
cout << "Fade BGM (Old Volume = " << m_BGM.getVolume() << ", ";
cout << "New Volume = " << value << ")" << endl;
}
}
This code does work for fading in and fading out but it uses generic floats and is frame based. I'd like to implement sf::Time and do something like GameAudio.fadeBGM(float volume, sf::seconds(3.0)) and make it time based. Obviously, I know I need to change the 2nd argument to use an sf::Time but how do I calculate something like m_BGM.setVolume(incremental volume change in seconds)?
Thank you to anybody who can help.