Quick soft fadeout when menu background audio stops playing

This commit is contained in:
Henrik Rydgard 2017-01-28 10:38:38 +01:00
parent 9c55e1e0de
commit 7182c34c1e
2 changed files with 41 additions and 13 deletions

View file

@ -190,7 +190,6 @@ bool DrawEngineCommon::TestBoundingBox(void* control_points, int vertexCount, u3
// Try to skip NormalizeVertices if it's pure positions. No need to bother with a vertex decoder
// and a large vertex format.
if ((vertType & 0xFFFFFF) == GE_VTYPE_POS_FLOAT) {
// memcpy(verts, control_points, 12 * vertexCount);
verts = (float *)control_points;
} else if ((vertType & 0xFFFFFF) == GE_VTYPE_POS_8BIT) {
const s8 *vtx = (const s8 *)control_points;
@ -440,8 +439,8 @@ u32 DrawEngineCommon::NormalizeVertices(u8 *outPtr, u8 *bufPtr, const u8 *inPtr,
if (vertType & GE_VTYPE_TC_MASK) {
reader.ReadUV(sv.uv);
} else {
sv.uv[0] = 0; // This will get filled in during tesselation
sv.uv[1] = 0;
sv.uv[0] = 0.0f; // This will get filled in during tesselation
sv.uv[1] = 0.0f;
}
if (vertType & GE_VTYPE_COL_MASK) {
reader.ReadColor0_8888(sv.color);
@ -452,8 +451,8 @@ u32 DrawEngineCommon::NormalizeVertices(u8 *outPtr, u8 *bufPtr, const u8 *inPtr,
// Normals are generated during tesselation anyway, not sure if any need to supply
reader.ReadNrm((float *)&sv.nrm);
} else {
sv.nrm.x = 0;
sv.nrm.y = 0;
sv.nrm.x = 0.0f;
sv.nrm.y = 0.0f;
sv.nrm.z = 1.0f;
}
reader.ReadPos((float *)&sv.pos);

View file

@ -169,15 +169,22 @@ static AT3PlusReader *at3Reader;
static double gameLastChanged;
static double lastPlaybackTime;
static int buffer[44100];
static bool fadingOut = true;
static float volume;
static float delta = -0.0001f;
static void ClearBackgroundAudio() {
static void ClearBackgroundAudio(bool hard) {
if (!hard) {
fadingOut = true;
volume = 1.0f;
return;
}
if (at3Reader) {
at3Reader->Shutdown();
delete at3Reader;
at3Reader = 0;
at3Reader = nullptr;
}
playbackOffset = 0;
gameLastChanged = 0;
}
void SetBackgroundAudioGame(const std::string &path) {
@ -189,8 +196,15 @@ void SetBackgroundAudioGame(const std::string &path) {
return;
}
ClearBackgroundAudio();
gameLastChanged = time_now_d();
if (path.size() == 0) {
ClearBackgroundAudio(false);
fadingOut = true;
} else {
ClearBackgroundAudio(true);
gameLastChanged = time_now_d();
fadingOut = false;
}
volume = 1.0f;
bgGamePath = path;
}
@ -201,7 +215,7 @@ int PlayBackgroundAudio() {
// Immediately stop the sound if it is turned off while playing.
if (!g_Config.bEnableSound) {
ClearBackgroundAudio();
ClearBackgroundAudio(true);
__PushExternalAudio(0, 0);
return 0;
}
@ -234,8 +248,23 @@ int PlayBackgroundAudio() {
int sz = lastPlaybackTime <= 0.0 ? 44100 / 60 : (int)((now - lastPlaybackTime) * 44100);
sz = std::min((int)ARRAY_SIZE(buffer) / 2, sz);
if (sz >= 16) {
if (at3Reader->Read(buffer, sz))
__PushExternalAudio(buffer, sz);
if (at3Reader->Read(buffer, sz)) {
if (!fadingOut) {
__PushExternalAudio(buffer, sz);
} else {
for (int i = 0; i < sz*2; i += 2) {
buffer[i] *= volume;
buffer[i + 1] *= volume;
volume += delta;
}
__PushExternalAudio(buffer, sz);
if (volume <= 0.0f) {
ClearBackgroundAudio(true);
fadingOut = false;
gameLastChanged = 0;
}
}
}
lastPlaybackTime = now;
}
} else {