Optimize performance with frustum culling and audio management - v1.7.0

This commit is contained in:
Michael Howard 2026-04-23 16:14:09 -05:00
parent d3a1f6fcac
commit f4ce5aade6
1 changed files with 30 additions and 6 deletions

View File

@ -768,9 +768,13 @@ int main(void)
{
// Update
//----------------------------------------------------------------------------------
// Update ONLY active music streams to save CPU
if (currentState == MAIN_MENU || currentState == WORLD_SELECT || currentState == CREATE_WORLD) {
UpdateMusicStream(titleMusic);
} else {
UpdateMusicStream(gameplayMusic);
UpdateMusicStream(nightMusic);
}
// Handle title music loop fading
float fadeTime = 2.0f; // 2 seconds fade
@ -1095,8 +1099,8 @@ int main(void)
DrawTexturePro(titleTexture, sourceRec, destRec, origin, 0.0f, WHITE);
EndMode2D();
// Show Version Number (v1.6.6) in Red
DrawTextEx(customFont, "v1.6.6", (Vector2){ (float)currentWidth - 140, (float)currentHeight - 40 }, 22, 1.0f, RED);
// Show Version Number (v1.7.0) in Red
DrawTextEx(customFont, "v1.7.0", (Vector2){ (float)currentWidth - 140, (float)currentHeight - 40 }, 22, 1.0f, RED);
}
Vector2 mousePos = GetMousePosition();
@ -1568,11 +1572,19 @@ int main(void)
int playerCX = (int)floorf(camera3D.position.x / CHUNK_SIZE);
int playerCZ = (int)floorf(camera3D.position.z / CHUNK_SIZE);
// Optimized Render Loop: Pre-fetch neighbor chunks to eliminate millions of map lookups
Vector3 camForward = Vector3Normalize(Vector3Subtract(camera3D.target, camera3D.position));
// Optimized Render Loop: Pre-fetch neighbor chunks and apply Frustum Culling
for (int renderType = 1; renderType <= 13; renderType++) {
if (renderType == GRASS) {
for (int cx = playerCX - RENDER_DISTANCE; cx <= playerCX + RENDER_DISTANCE; cx++) {
for (int cz = playerCZ - RENDER_DISTANCE; cz <= playerCZ + RENDER_DISTANCE; cz++) {
// FRUSTUM CULLING: Skip chunks not in view
Vector3 chunkCenter = { (float)(cx * CHUNK_SIZE + 8), 32.0f, (float)(cz * CHUNK_SIZE + 8) };
Vector3 toChunk = Vector3Normalize(Vector3Subtract(chunkCenter, camera3D.position));
if (Vector3Length(Vector3Subtract(chunkCenter, camera3D.position)) > 24.0f &&
Vector3DotProduct(toChunk, camForward) < 0.4f) continue;
auto it = worldChunks.find({cx, cz});
if (it == worldChunks.end()) continue;
Chunk* chunk = it->second;
@ -1605,6 +1617,12 @@ int main(void)
} else if (renderType == CRAFTING_TABLE) {
for (int cx = playerCX - RENDER_DISTANCE; cx <= playerCX + RENDER_DISTANCE; cx++) {
for (int cz = playerCZ - RENDER_DISTANCE; cz <= playerCZ + RENDER_DISTANCE; cz++) {
// FRUSTUM CULLING
Vector3 chunkCenter = { (float)(cx * CHUNK_SIZE + 8), 32.0f, (float)(cz * CHUNK_SIZE + 8) };
Vector3 toChunk = Vector3Normalize(Vector3Subtract(chunkCenter, camera3D.position));
if (Vector3Length(Vector3Subtract(chunkCenter, camera3D.position)) > 24.0f &&
Vector3DotProduct(toChunk, camForward) < 0.4f) continue;
auto it = worldChunks.find({cx, cz});
if (it == worldChunks.end()) continue;
Chunk* chunk = it->second;
@ -1644,6 +1662,12 @@ int main(void)
for (int cx = playerCX - RENDER_DISTANCE; cx <= playerCX + RENDER_DISTANCE; cx++) {
for (int cz = playerCZ - RENDER_DISTANCE; cz <= playerCZ + RENDER_DISTANCE; cz++) {
// FRUSTUM CULLING
Vector3 chunkCenter = { (float)(cx * CHUNK_SIZE + 8), 32.0f, (float)(cz * CHUNK_SIZE + 8) };
Vector3 toChunk = Vector3Normalize(Vector3Subtract(chunkCenter, camera3D.position));
if (Vector3Length(Vector3Subtract(chunkCenter, camera3D.position)) > 24.0f &&
Vector3DotProduct(toChunk, camForward) < 0.4f) continue;
auto it = worldChunks.find({cx, cz});
if (it == worldChunks.end()) continue;
Chunk* chunk = it->second;