36static bool sMusicFinished;
39static void musicFinishedCallBack()
41 sMusicFinished =
true;
44static void channelFinishedCallBack(
int channel)
46 sChannelFinished[channel] =
true;
51 Mix_HookMusicFinished(musicFinishedCallBack);
52 Mix_ChannelFinished(channelFinishedCallBack);
57 Mix_HookMusicFinished(
nullptr);
58 Mix_ChannelFinished(
nullptr);
67 Log::info(
"Sound::init() Initializing sound...");
69 if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1)
71 Log::info(
"Sound::init() Failed to initialize audio subsystem");
75 const size_t audioBuffer = 4096;
77 const int res = Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT,
78 MIX_DEFAULT_CHANNELS, audioBuffer);
81 Log::info(
"Sound::init Could not initialize audio: %s",
102 SDL_version compiledVersion;
103 const SDL_version *linkedVersion;
104 const char *format =
"Unknown";
106 Uint16 audioFormat = 0;
109 MIX_VERSION(&compiledVersion);
110 linkedVersion = Mix_Linked_Version();
112 const char *driver = SDL_GetCurrentAudioDriver();
114 Mix_QuerySpec(&rate, &audioFormat, &channels);
117 case AUDIO_U8: format =
"U8";
break;
118 case AUDIO_S8: format =
"S8";
break;
119 case AUDIO_U16LSB: format =
"U16LSB";
break;
120 case AUDIO_S16LSB: format =
"S16LSB";
break;
121 case AUDIO_U16MSB: format =
"U16MSB";
break;
122 case AUDIO_S16MSB: format =
"S16MSB";
break;
125 Log::info(
"Sound::info() SDL_mixer: %i.%i.%i (compiled)",
126 compiledVersion.major,
127 compiledVersion.minor,
128 compiledVersion.patch);
129 Log::info(
"Sound::info() SDL_mixer: %i.%i.%i (linked)",
130 linkedVersion->major,
131 linkedVersion->minor,
132 linkedVersion->patch);
133 Log::info(
"Sound::info() Driver: %s", driver);
134 Log::info(
"Sound::info() Format: %s", format);
135 Log::info(
"Sound::info() Rate: %i", rate);
136 Log::info(
"Sound::info() Channels: %i", channels);
204 Log::info(
"Sound::fadeOutMusic() Fading-out (%i ms)", ms);
208 Mix_FadeOutMusic(ms);
214 sMusicFinished =
true;
228 sMusicFinished =
false;
240 if (sChannelFinished[i])
242 sChannelFinished[i] =
false;
254 if (!path.compare(0, 4,
"sfx/"))
263 Log::info(
"Sound::playSfx() Playing: %s", path.c_str());
269 const int dx = std::abs((
int) pos.
x - x);
270 const int dy = std::abs((
int) pos.
y - y);
271 const int dist = std::max(dx, dy);
274 vol -= std::min(120, dist / 4);
277 int channel =
sound->play(0, vol);
285 const std::string fullPath =
paths.
getValue(
"sfx",
"sfx/") + path;
302 Log::info(
"Sound::close() Shutting down sound...");
const Vector & getPosition() const
Returns the pixel position of this actor.
std::string getValue(const std::string &key, const std::string &deflt) const
Gets a value as string.
std::string getStringValue(const std::string &key) const
bool play(int loops=-1, int fadeIn=0)
Plays the music.
A class for loading and managing resources.
static ResourceManager * getInstance()
Returns an instance of the class, creating one if it does not already exist.
ResourceRef< Music > getMusic(const std::string &path)
Loads the Music resource found at the given path.
ResourceRef< SoundEffect > getSoundEffect(const std::string &path)
Loads the SoundEffect resource found at the given path.
Automatically counting Resource reference.
void fadeOutAndPlayMusic(const std::string &fileName, int ms=1000)
Fades out a background music and play a new one.
void playMusic(const std::string &fileName)
Starts background music.
void fadeOutMusic(int ms=1000)
Fades out currently running background music track.
void init()
Installs the sound engine.
void fadeInMusic(const std::string &fileName, int ms=1000)
Fades in background music.
void setMusicVolume(int volume)
void info()
Logs various info about sound device.
void haltMusic()
Halts and frees currently playing music.
void logic()
The sound logic.
void stopMusic()
Stops currently running background music track.
void close()
Removes all sound functionalities.
void setSfxVolume(int volume)
std::string mCurrentMusicFile
void setNotificationsVolume(int volume)
void playSfx(const std::string &path, int x=0, int y=0)
Plays a sound at the specified location.
ResourceRef< Music > mMusic
void playNotification(const std::string &path)
Plays a sound on the notification channel.
ResourceRef< SoundEffect > mSounds[CHANNEL_COUNT]
std::string mNextMusicFile
When calling fadeOutAndPlayMusic(), the music file below will then be played.
Configuration paths
XML default paths information reader.
LocalPlayer * local_player
void info(const char *log_text,...) LOG_PRINTF_ATTR