diff options
Diffstat (limited to 'src/first_app.cpp')
-rwxr-xr-x | src/first_app.cpp | 64 |
1 files changed, 50 insertions, 14 deletions
diff --git a/src/first_app.cpp b/src/first_app.cpp index 08f6f0c..43047f7 100755 --- a/src/first_app.cpp +++ b/src/first_app.cpp @@ -1,5 +1,4 @@ #include "first_app.hpp" -#include "chunk.hpp" namespace app { @@ -11,15 +10,11 @@ void FirstApp::run() { Chunk::load(); - for(int32_t x = 0; x < 10; x++) { - for(int32_t z = 0; z < 10; z++) { - Chunk* chunk = Chunk::newChunk(x, z, 53463); - Chunk::createMeshAsync(chunk); - auto chunkObject = xe::GameObject::createGameObject(); - chunkObject.transform.translation = {16.f*x, 0.f, 16.f*z}; - gameObjects.push_back(std::move(chunkObject)); - } - } + auto viewerObject = xe::GameObject::createGameObject(); + viewerObject.transform.translation = {0.f, 10.f, 0.f}; + viewerObject.transform.rotation.y = glm::radians(45.f); + + createGameObjects(viewerObject); SimpleRenderer renderer{xeEngine, Chunk::getTextures()}; @@ -27,9 +22,6 @@ void FirstApp::run() { sound.setLooping(true); sound.play(); - auto viewerObject = xe::GameObject::createGameObject(); - viewerObject.transform.translation = {0.f, 10.f, 0.f}; - viewerObject.transform.rotation.y = glm::radians(45.f); KeyboardMovementController cameraController{xeEngine.getInput(), viewerObject}; while (xeEngine.poll()) { @@ -40,10 +32,12 @@ void FirstApp::run() { xeEngine.getCamera().setViewYXZ(viewerObject.transform.translation, viewerObject.transform.rotation); if(xeEngine.beginFrame()) { - renderer.render(gameObjects, xeEngine.getCamera()); + renderer.render(loadedChunks, xeEngine.getCamera()); xeEngine.endFrame(); } + reloadLoadedChunks(viewerObject); + } xeEngine.close(); @@ -52,4 +46,46 @@ void FirstApp::run() { } +void FirstApp::createGameObjects(xe::GameObject& viewer) { + int width = 2*RENDER_DISTANCE+1; + loadedChunks.clear(); + for(int32_t x = 0; x < width; x++) { + for(int32_t z = 0; z < width; z++) { + auto gameObject = xe::GameObject::createGameObject(); + gameObject.transform.translation = glm::vec3(0.f); + loadedChunks.push_back(std::move(gameObject)); + } + } +} + +void FirstApp::reloadLoadedChunks(xe::GameObject& viewer) { + viewX = static_cast<int>(floor(viewer.transform.translation.x / 16.f)); + viewZ = static_cast<int>(floor(viewer.transform.translation.z / 16.f)); + int width = 2*RENDER_DISTANCE+1; + int minX = viewX - RENDER_DISTANCE; + int minZ = viewZ - RENDER_DISTANCE; + int maxX = viewX + RENDER_DISTANCE; + int maxZ = viewZ + RENDER_DISTANCE; + for(int32_t x = 0; x < width; x++) { + for(int32_t z = 0; z < width; z++) { + auto& gameObject = loadedChunks[x + z * width]; + int gridX = static_cast<int>(floor(gameObject.transform.translation.x / 16.f)); + int gridZ = static_cast<int>(floor(gameObject.transform.translation.z / 16.f)); + int newGridX = minX + x; + int newGridZ = minZ + z; + if(gridX < minX || gridZ < minZ || gridX > maxX || gridZ > maxZ) + Chunk::deleteChunk(gridX, gridZ); + Chunk* chunk = Chunk::getChunk(newGridX, newGridZ); + if(chunk == nullptr) { + chunk = Chunk::newChunk(newGridX, newGridZ, 12345); + Chunk::createMeshAsync(chunk); + Chunk::createMeshAsync(Chunk::getChunk(newGridX-1, newGridZ)); + Chunk::createMeshAsync(Chunk::getChunk(newGridX, newGridZ+1)); + } + gameObject.model = chunk->getMesh(); + gameObject.transform.translation = glm::vec3(newGridX * 16.f, 0, newGridZ * 16.f); + } + } +} + }
\ No newline at end of file |