Optimize performance with frustum culling and audio management - v1.7.0
This commit is contained in:
parent
d3a1f6fcac
commit
f4ce5aade6
36
src/main.cpp
36
src/main.cpp
|
|
@ -768,9 +768,13 @@ int main(void)
|
||||||
{
|
{
|
||||||
// Update
|
// Update
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
UpdateMusicStream(titleMusic);
|
// Update ONLY active music streams to save CPU
|
||||||
UpdateMusicStream(gameplayMusic);
|
if (currentState == MAIN_MENU || currentState == WORLD_SELECT || currentState == CREATE_WORLD) {
|
||||||
UpdateMusicStream(nightMusic);
|
UpdateMusicStream(titleMusic);
|
||||||
|
} else {
|
||||||
|
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
|
||||||
|
|
@ -1095,8 +1099,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.6) in Red
|
// Show Version Number (v1.7.0) in Red
|
||||||
DrawTextEx(customFont, "v1.6.6", (Vector2){ (float)currentWidth - 140, (float)currentHeight - 40 }, 22, 1.0f, RED);
|
DrawTextEx(customFont, "v1.7.0", (Vector2){ (float)currentWidth - 140, (float)currentHeight - 40 }, 22, 1.0f, RED);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2 mousePos = GetMousePosition();
|
Vector2 mousePos = GetMousePosition();
|
||||||
|
|
@ -1568,11 +1572,19 @@ int main(void)
|
||||||
int playerCX = (int)floorf(camera3D.position.x / CHUNK_SIZE);
|
int playerCX = (int)floorf(camera3D.position.x / CHUNK_SIZE);
|
||||||
int playerCZ = (int)floorf(camera3D.position.z / 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++) {
|
for (int renderType = 1; renderType <= 13; renderType++) {
|
||||||
if (renderType == GRASS) {
|
if (renderType == GRASS) {
|
||||||
for (int cx = playerCX - RENDER_DISTANCE; cx <= playerCX + RENDER_DISTANCE; cx++) {
|
for (int cx = playerCX - RENDER_DISTANCE; cx <= playerCX + RENDER_DISTANCE; cx++) {
|
||||||
for (int cz = playerCZ - RENDER_DISTANCE; cz <= playerCZ + RENDER_DISTANCE; cz++) {
|
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});
|
auto it = worldChunks.find({cx, cz});
|
||||||
if (it == worldChunks.end()) continue;
|
if (it == worldChunks.end()) continue;
|
||||||
Chunk* chunk = it->second;
|
Chunk* chunk = it->second;
|
||||||
|
|
@ -1605,6 +1617,12 @@ int main(void)
|
||||||
} else if (renderType == CRAFTING_TABLE) {
|
} else if (renderType == CRAFTING_TABLE) {
|
||||||
for (int cx = playerCX - RENDER_DISTANCE; cx <= playerCX + RENDER_DISTANCE; cx++) {
|
for (int cx = playerCX - RENDER_DISTANCE; cx <= playerCX + RENDER_DISTANCE; cx++) {
|
||||||
for (int cz = playerCZ - RENDER_DISTANCE; cz <= playerCZ + RENDER_DISTANCE; cz++) {
|
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});
|
auto it = worldChunks.find({cx, cz});
|
||||||
if (it == worldChunks.end()) continue;
|
if (it == worldChunks.end()) continue;
|
||||||
Chunk* chunk = it->second;
|
Chunk* chunk = it->second;
|
||||||
|
|
@ -1644,6 +1662,12 @@ int main(void)
|
||||||
|
|
||||||
for (int cx = playerCX - RENDER_DISTANCE; cx <= playerCX + RENDER_DISTANCE; cx++) {
|
for (int cx = playerCX - RENDER_DISTANCE; cx <= playerCX + RENDER_DISTANCE; cx++) {
|
||||||
for (int cz = playerCZ - RENDER_DISTANCE; cz <= playerCZ + RENDER_DISTANCE; cz++) {
|
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});
|
auto it = worldChunks.find({cx, cz});
|
||||||
if (it == worldChunks.end()) continue;
|
if (it == worldChunks.end()) continue;
|
||||||
Chunk* chunk = it->second;
|
Chunk* chunk = it->second;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue