@ -39,6 +39,7 @@ All rights reserved.
# include "AdventuresInLestoria.h"
# include "DEFINES.h"
# include "util.h"
# include "LoadingScreen.h"
INCLUDE_game
INCLUDE_DATA
@ -121,12 +122,21 @@ const size_t Audio::LoadAndPlay(const std::string_view sound,const bool loop){
Engine ( ) . Play ( soundID , loop ) ;
return soundID ;
} ;
void Audio : : Play BGM ( const std : : string_view sound , const bool loop ) {
void Audio : : Prepare BGM ( const std : : string_view sound , const bool loop ) {
BGM & track = Self ( ) . bgm [ std : : string ( sound ) ] ;
Self ( ) . fullyLoaded = false ;
StopBGM ( ) ; //Stop any currently playing track.
Self ( ) . playParams = { std : : string ( sound ) , loop } ;
Self ( ) . playBGMWaitTime = 0.7f ;
# pragma region Internal Loading Loop Setup
Self ( ) . trackLoadStarted = false ;
Self ( ) . trackLoadComplete = false ;
Self ( ) . channelPlayingStarted = false ;
Self ( ) . channelPlayingComplete = false ;
int currentLoopIndex = 0 ;
# pragma endregion
Self ( ) . immediatelyLoadAudio = false ;
} ;
void Audio : : StopBGM ( ) {
@ -142,24 +152,35 @@ const bool Audio::BGMIsPlaying(){
return Self ( ) . currentBGM . length ( ) > 0 ;
}
const Volume & Audio : : BGM : : GetVolume ( const Event & eventName , const ChannelID & id ) const {
return eventVolumes . GetVolumes ( eventName ) . at ( id ) ;
const Volume & Audio : : BGM : : GetVolume ( const Event & eventName , const int & in dex ) const {
return eventVolumes . GetVolumes ( eventName ) . at ( in dex ) ;
}
void Audio : : BGM : : Load ( ) {
if ( Self ( ) . BGMIsPlaying ( ) ) {
if ( Self ( ) . GetTrackName ( ) = = songFileName ) return ; //We are already playing the current track.
BGM & bgm = Self ( ) . bgm [ Self ( ) . GetTrackName ( ) ] ;
if ( ! Self ( ) . trackLoadStarted ) {
Self ( ) . trackLoadStarted = true ;
if ( Self ( ) . BGMIsPlaying ( ) ) {
bgm . Unload ( ) ;
if ( Self ( ) . GetTrackName ( ) = = songFileName ) return ; //We are already playing the current track.
BGM & bgm = Self ( ) . bgm [ Self ( ) . GetTrackName ( ) ] ;
if ( Self ( ) . BGMIsPlaying ( ) ) {
bgm . Unload ( ) ;
}
}
}
Self ( ) . currentBGM = songFileName ;
BGM & newBgm = Self ( ) . bgm [ songFileName ] ;
if ( newBgm . channels . size ( ) > 0 ) ERR ( std : : format ( " WARNING! The size of the channels list is greater than zero! Size: {} " , newBgm . channels . size ( ) ) ) ;
for ( const ChannelName & channel : newBgm . GetChannels ( ) ) {
Self ( ) . currentBGM = songFileName ;
Self ( ) . currentLoopIndex = 0 ;
BGM & newBgm = Self ( ) . bgm [ songFileName ] ;
if ( newBgm . channels . size ( ) > 0 ) ERR ( std : : format ( " WARNING! The size of the channels list is greater than zero! Size: {} " , newBgm . channels . size ( ) ) ) ;
} else {
BGM & newBgm = Self ( ) . bgm [ songFileName ] ;
const ChannelName & channel = newBgm . GetChannels ( ) [ Self ( ) . currentLoopIndex ] ;
ChannelID soundID = Engine ( ) . LoadSound ( " bgm_directory " _S + channel ) ;
newBgm . channels . push_back ( soundID ) ;
# pragma region Handle threaded loop indexing
Self ( ) . currentLoopIndex + + ;
if ( Self ( ) . currentLoopIndex > = newBgm . GetChannels ( ) . size ( ) ) {
Self ( ) . trackLoadComplete = true ;
}
# pragma endregion
}
}
@ -255,27 +276,54 @@ const SongName&Audio::GetTrackName(){
return Self ( ) . currentBGM ;
}
void Audio : : Update ( ) {
if ( Self ( ) . playBGMWaitTime > 0.f ) {
Self ( ) . playBGMWaitTime = std : : max ( Self ( ) . playBGMWaitTime - game - > GetElapsedTime ( ) , 0.f ) ;
if ( Self ( ) . playBGMWaitTime = = 0.f ) {
BGM & track = Self ( ) . bgm [ Self ( ) . playParams . sound ] ;
void Audio : : UpdateLoop ( ) {
if ( Self ( ) . playBGMWaitTime = = 0.f ) {
BGM & track = Self ( ) . bgm [ Self ( ) . playParams . sound ] ;
if ( ! Self ( ) . trackLoadComplete ) {
track . Load ( ) ;
Self ( ) . prevVolumes . clear ( ) ;
Self ( ) . targetVolumes . clear ( ) ;
Self ( ) . fadeToTargetVolumeTime = 0.f ;
for ( int channelListIndex = 0 ; int trackID : track . GetChannelIDs ( ) ) {
float channelVol = track . GetVolume ( Self ( ) . currentAudioEvent , channelListIndex ) ;
} else
if ( ! Self ( ) . channelPlayingComplete ) {
if ( ! Self ( ) . channelPlayingStarted ) {
Self ( ) . prevVolumes . clear ( ) ;
Self ( ) . targetVolumes . clear ( ) ;
Self ( ) . fadeToTargetVolumeTime = 0.f ;
Self ( ) . currentLoopIndex = 0 ;
Self ( ) . channelPlayingStarted = true ;
} else {
int trackID = track . GetChannelIDs ( ) [ Self ( ) . currentLoopIndex ] ;
float channelVol = track . GetVolume ( Self ( ) . currentAudioEvent , Self ( ) . currentLoopIndex ) ;
Self ( ) . prevVolumes . push_back ( channelVol ) ;
Self ( ) . targetVolumes . push_back ( channelVol ) ;
Engine ( ) . SetVolume ( trackID , channelVol * GetBGMVolume ( ) ) ;
Engine ( ) . Play ( trackID , Self ( ) . playParams . loop ) ;
channelListIndex + + ;
# pragma region Handle threaded loop indexing
Self ( ) . currentLoopIndex + + ;
if ( Self ( ) . currentLoopIndex > = track . GetChannelIDs ( ) . size ( ) ) {
Self ( ) . channelPlayingComplete = true ;
}
# pragma endregion
}
} else {
Self ( ) . fullyLoaded = true ;
}
}
}
void Audio : : PlayBGM ( const std : : string_view sound , const bool loop ) {
PrepareBGM ( sound , loop ) ;
Self ( ) . immediatelyLoadAudio = true ;
}
void Audio : : Update ( ) {
if ( Self ( ) . playBGMWaitTime > 0.f ) {
Self ( ) . playBGMWaitTime = std : : max ( Self ( ) . playBGMWaitTime - game - > GetElapsedTime ( ) , 0.f ) ;
if ( Self ( ) . playBGMWaitTime = = 0.f & & Self ( ) . immediatelyLoadAudio ) {
while ( ! Self ( ) . BGMFullyLoaded ( ) ) {
UpdateLoop ( ) ; //We immediately load the file. In a loading screen setting we would defer UpdateLoop() such that we have extra time to update the screen, UpdateLoop() is divided into many parts of the music loading process.
}
}
}
if ( Self ( ) . fadeToTargetVolumeTime > 0.f ) {
Self ( ) . fadeToTargetVolumeTime = std : : max ( 0.f , Self ( ) . fadeToTargetVolumeTime - game - > GetElapsedTime ( ) ) ;
for ( int counter = 0 ; float & vol : Self ( ) . prevVolumes ) {