From f4ce5aade6c50ef116935f6d8640cc38ca2e1b3c Mon Sep 17 00:00:00 2001 From: Michael Howard Date: Thu, 23 Apr 2026 16:14:09 -0500 Subject: [PATCH] Optimize performance with frustum culling and audio management - v1.7.0 --- src/main.cpp | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index fd99c9e..eca9c51 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -768,9 +768,13 @@ int main(void) { // Update //---------------------------------------------------------------------------------- - UpdateMusicStream(titleMusic); - UpdateMusicStream(gameplayMusic); - UpdateMusicStream(nightMusic); + // 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;