mirror of
https://github.com/k4zmu2a/SpaceCadetPinball.git
synced 2024-11-17 15:20:17 +01:00
0a2d6847ba
Issue #120.
92 lines
1.6 KiB
C++
92 lines
1.6 KiB
C++
#include "pch.h"
|
|
#include "Sound.h"
|
|
|
|
int Sound::num_channels;
|
|
bool Sound::enabled_flag = false;
|
|
int* Sound::TimeStamps = nullptr;
|
|
int Sound::Volume = MIX_MAX_VOLUME;
|
|
|
|
bool Sound::Init(int channels, bool enableFlag, int volume)
|
|
{
|
|
Volume = volume;
|
|
Mix_Init(MIX_INIT_MID_Proxy);
|
|
auto result = Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024);
|
|
SetChannels(channels);
|
|
Enable(enableFlag);
|
|
return !result;
|
|
}
|
|
|
|
void Sound::Enable(bool enableFlag)
|
|
{
|
|
enabled_flag = enableFlag;
|
|
if (!enableFlag)
|
|
Mix_HaltChannel(-1);
|
|
}
|
|
|
|
void Sound::Activate()
|
|
{
|
|
Mix_Resume(-1);
|
|
}
|
|
|
|
void Sound::Deactivate()
|
|
{
|
|
Mix_Pause(-1);
|
|
}
|
|
|
|
void Sound::Close()
|
|
{
|
|
delete[] TimeStamps;
|
|
TimeStamps = nullptr;
|
|
Mix_CloseAudio();
|
|
Mix_Quit();
|
|
}
|
|
|
|
void Sound::PlaySound(Mix_Chunk* wavePtr, int time)
|
|
{
|
|
if (wavePtr && enabled_flag)
|
|
{
|
|
if (Mix_Playing(-1) == num_channels)
|
|
{
|
|
auto oldestChannel = std::min_element(TimeStamps, TimeStamps + num_channels) - TimeStamps;
|
|
Mix_HaltChannel(oldestChannel);
|
|
}
|
|
|
|
auto channel = Mix_PlayChannel(-1, wavePtr, 0);
|
|
if (channel != -1)
|
|
TimeStamps[channel] = time;
|
|
}
|
|
}
|
|
|
|
Mix_Chunk* Sound::LoadWaveFile(const std::string& lpName)
|
|
{
|
|
auto wavFile = fopenu(lpName.c_str(), "r");
|
|
if (!wavFile)
|
|
return nullptr;
|
|
fclose(wavFile);
|
|
|
|
return Mix_LoadWAV(lpName.c_str());
|
|
}
|
|
|
|
void Sound::FreeSound(Mix_Chunk* wave)
|
|
{
|
|
if (wave)
|
|
Mix_FreeChunk(wave);
|
|
}
|
|
|
|
void Sound::SetChannels(int channels)
|
|
{
|
|
if (channels <= 0)
|
|
channels = 8;
|
|
|
|
num_channels = channels;
|
|
delete[] TimeStamps;
|
|
TimeStamps = new int[num_channels]();
|
|
Mix_AllocateChannels(num_channels);
|
|
SetVolume(Volume);
|
|
}
|
|
|
|
void Sound::SetVolume(int volume)
|
|
{
|
|
Volume = volume;
|
|
Mix_Volume(-1, volume);
|
|
}
|