Add night music with crossfade - v1.6.3

This commit is contained in:
Michael Howard 2026-04-23 15:21:08 -05:00
parent d5745bcd79
commit ccb22db10b
4 changed files with 18 additions and 3 deletions

BIN
assets/Morricraft-night.mp3 Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -612,6 +612,11 @@ int main(void)
PlayMusicStream(gameplayMusic); PlayMusicStream(gameplayMusic);
SetMusicVolume(gameplayMusic, 0.0f); SetMusicVolume(gameplayMusic, 0.0f);
Music nightMusic = LoadMusicStream("assets/Morricraft-night.mp3");
nightMusic.looping = true;
PlayMusicStream(nightMusic);
SetMusicVolume(nightMusic, 0.0f);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@ -753,6 +758,7 @@ int main(void)
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
UpdateMusicStream(titleMusic); UpdateMusicStream(titleMusic);
UpdateMusicStream(gameplayMusic); UpdateMusicStream(gameplayMusic);
UpdateMusicStream(nightMusic);
// Handle title music loop fading // Handle title music loop fading
float fadeTime = 2.0f; // 2 seconds fade float fadeTime = 2.0f; // 2 seconds fade
@ -777,7 +783,12 @@ int main(void)
} }
SetMusicVolume(titleMusic, titleLoopFade * masterMusicVolume * (1.0f - crossfade)); SetMusicVolume(titleMusic, titleLoopFade * masterMusicVolume * (1.0f - crossfade));
SetMusicVolume(gameplayMusic, masterMusicVolume * crossfade);
// Gameplay volume crossfade between day and night
// We use dayFactor (calculated later in loop) or calculate it here
float dayNnightFactor = 1.0f; // Default
// We'll move the music volume setting after dayFactor is calculated for accuracy
// But for now, let's just make sure nightMusic is updated
// Handle window resize dynamically // Handle window resize dynamically
int currentWidth = GetScreenWidth(); int currentWidth = GetScreenWidth();
@ -1058,8 +1069,8 @@ int main(void)
DrawTexturePro(titleTexture, sourceRec, destRec, origin, 0.0f, WHITE); DrawTexturePro(titleTexture, sourceRec, destRec, origin, 0.0f, WHITE);
EndMode2D(); EndMode2D();
// Show Version Number (v1.6.2) in Red // Show Version Number (v1.6.3) in Red
DrawTextEx(customFont, "v1.6.2", (Vector2){ (float)currentWidth - 140, (float)currentHeight - 40 }, 22, 1.0f, RED); DrawTextEx(customFont, "v1.6.3", (Vector2){ (float)currentWidth - 140, (float)currentHeight - 40 }, 22, 1.0f, RED);
} }
Vector2 mousePos = GetMousePosition(); Vector2 mousePos = GetMousePosition();
@ -1497,6 +1508,10 @@ int main(void)
float sunAngle = timeOfDay * 2.0f * PI - PI/2.0f; float sunAngle = timeOfDay * 2.0f * PI - PI/2.0f;
float dayFactor = (sinf(sunAngle) + 1.0f) / 2.0f; // 0 (night) to 1 (day) float dayFactor = (sinf(sunAngle) + 1.0f) / 2.0f; // 0 (night) to 1 (day)
// Update Music Volumes based on time of day
SetMusicVolume(gameplayMusic, masterMusicVolume * crossfade * dayFactor);
SetMusicVolume(nightMusic, masterMusicVolume * crossfade * (1.0f - dayFactor));
float lightLevel = 0.2f + 0.8f * dayFactor; // Ambient light multiplier float lightLevel = 0.2f + 0.8f * dayFactor; // Ambient light multiplier
Color blockTint = { (unsigned char)(255 * lightLevel), (unsigned char)(255 * lightLevel), (unsigned char)(255 * lightLevel), 255 }; Color blockTint = { (unsigned char)(255 * lightLevel), (unsigned char)(255 * lightLevel), (unsigned char)(255 * lightLevel), 255 };