From 8b322ea9ebf3e525fda9672c88a8b199b91e687c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 6 Nov 2023 10:01:42 +0300 Subject: [PATCH] Added Chunks.resize method --- src/voxel_engine.cpp | 14 +++++++++++--- src/voxels/Chunks.cpp | 43 +++++++++++++++++++++++++++++++++++++------ src/voxels/Chunks.h | 8 +++++--- src/window/Window.cpp | 1 + src/world/Level.cpp | 4 ++-- 5 files changed, 56 insertions(+), 14 deletions(-) diff --git a/src/voxel_engine.cpp b/src/voxel_engine.cpp index 173fb97c..ac5dba65 100644 --- a/src/voxel_engine.cpp +++ b/src/voxel_engine.cpp @@ -132,6 +132,14 @@ void Engine::updateHotkeys() { if (Events::jpressed(GLFW_KEY_F3)) { level->player->debug = !level->player->debug; } + if (Events::jpressed(GLFW_KEY_F8)) { + if (level->chunks->w >= 40) { + level->chunks->resize(level->chunks->w/4, level->chunks->d / 4); + } + else { + level->chunks->resize(level->chunks->w + 2, level->chunks->d + 2); + } + } if (Events::jpressed(GLFW_KEY_F5)) { for (uint i = 0; i < level->chunks->volume; i++) { shared_ptr chunk = level->chunks->chunks[i]; @@ -149,7 +157,7 @@ void Engine::mainloop() { HudRenderer hud(assets); lastTime = glfwGetTime(); - Window::swapInterval(0); + Window::swapInterval(1); while (!Window::isShouldClose()){ updateTimers(); updateHotkeys(); @@ -172,6 +180,7 @@ Engine::~Engine() { Audio::finalize(); World* world = level->world; + std::cout << "-- saving world" << std::endl; world->write(level); @@ -180,7 +189,6 @@ Engine::~Engine() { std::cout << "-- shutting down" << std::endl; delete assets; - Events::finalize(); Window::terminate(); } @@ -192,7 +200,7 @@ int main() { settings.displayHeight = 720; settings.displaySamples = 4; settings.displayTitle = "VoxelEngine-Cpp v13"; - settings.chunksLoadSpeed = 15; + settings.chunksLoadSpeed = 10; settings.chunksLoadDistance = 12; settings.chunksPadding = 2; Engine engine(settings); diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index ec068e0e..d703597c 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -16,7 +16,8 @@ using glm::vec3; using std::shared_ptr; -Chunks::Chunks(int w, int d, int ox, int oz, LevelEvents* events) : w(w), d(d), ox(ox), oz(oz), events(events) { +Chunks::Chunks(int w, int d, int ox, int oz, WorldFiles* wfile, LevelEvents* events) + : w(w), d(d), ox(ox), oz(oz), worldFiles(wfile), events(events) { volume = (size_t)w*(size_t)d; chunks = new shared_ptr[volume]; chunksSecond = new shared_ptr[volume]; @@ -233,7 +234,7 @@ voxel* Chunks::rayCast(vec3 a, vec3 dir, float maxDist, vec3& end, vec3& norm, v return nullptr; } -void Chunks::setCenter(WorldFiles* worldFiles, int x, int z) { +void Chunks::setCenter(int x, int z) { int cx = x / CHUNK_W; int cz = z / CHUNK_D; cx -= ox; @@ -243,17 +244,17 @@ void Chunks::setCenter(WorldFiles* worldFiles, int x, int z) { cx -= w/2; cz -= d/2; if (cx | cz) { - translate(worldFiles, cx,cz); + translate(cx,cz); } } -void Chunks::translate(WorldFiles* worldFiles, int dx, int dz){ - for (unsigned int i = 0; i < volume; i++){ +void Chunks::translate(int dx, int dz){ + for (uint i = 0; i < volume; i++){ chunksSecond[i] = nullptr; } for (int z = 0; z < d; z++){ for (int x = 0; x < w; x++){ - shared_ptr chunk = chunks[z * w + x]; + shared_ptr chunk = chunks[z * d + x]; int nx = x - dx; int nz = z - dz; if (chunk == nullptr) @@ -275,6 +276,36 @@ void Chunks::translate(WorldFiles* worldFiles, int dx, int dz){ oz += dz; } +void Chunks::resize(int newW, int newD) { + if (newW < w) { + int delta = w - newW; + translate(delta / 2, 0); + translate(-delta, 0); + translate(delta, 0); + } + if (newD < d) { + int delta = d - newD; + translate(0, delta / 2); + translate(0, -delta); + translate(0, delta); + } + const int newVolume = newW * newD; + shared_ptr* newChunks = new shared_ptr[newVolume] {}; + shared_ptr* newChunksSecond = new shared_ptr[newVolume] {}; + for (int z = 0; z < d && z < newD; z++) { + for (int x = 0; x < w && x < newW; x++) { + newChunks[z * newW + x] = chunks[z * w + x]; + } + } + delete[] chunks; + delete[] chunksSecond; + w = newW; + d = newD; + volume = newVolume; + chunks = newChunks; + chunksSecond = newChunksSecond; +} + void Chunks::_setOffset(int x, int z){ ox = x; oz = z; diff --git a/src/voxels/Chunks.h b/src/voxels/Chunks.h index 2e871c18..d1cf0771 100644 --- a/src/voxels/Chunks.h +++ b/src/voxels/Chunks.h @@ -21,9 +21,10 @@ public: size_t chunksCount; int w,d; int ox,oz; + WorldFiles* worldFiles; LevelEvents* events; - Chunks(int w, int d, int ox, int oz, LevelEvents* events); + Chunks(int w, int d, int ox, int oz, WorldFiles* worldFiles, LevelEvents* events); ~Chunks(); bool putChunk(std::shared_ptr chunk); @@ -41,8 +42,9 @@ public: // does not move chunks inside void _setOffset(int x, int z); - void setCenter(WorldFiles* worldFiles, int x, int z); - void translate(WorldFiles* worldFiles, int x, int z); + void setCenter(int x, int z); + void translate(int x, int z); + void resize(int newW, int newD); void clear(); }; diff --git a/src/window/Window.cpp b/src/window/Window.cpp index 3268b0dc..61651077 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -98,6 +98,7 @@ void Window::setCursorMode(int mode){ } void Window::terminate(){ + Events::finalize(); glfwTerminate(); } diff --git a/src/world/Level.cpp b/src/world/Level.cpp index 0eb38e16..a1c1783e 100644 --- a/src/world/Level.cpp +++ b/src/world/Level.cpp @@ -18,7 +18,7 @@ Level::Level(World* world, Player* player, ChunksStorage* chunksStorage, LevelEv events(events) { physics = new PhysicsSolver(vec3(0, -19.6f, 0)); uint matrixSize = (loadDistance+chunksPadding) * 2; - chunks = new Chunks(matrixSize, matrixSize, 0, 0, events); + chunks = new Chunks(matrixSize, matrixSize, 0, 0, world->wfile, events); lighting = new Lighting(chunks); chunksController = new ChunksController(this, chunks, lighting, chunksPadding); playerController = new PlayerController(this); @@ -48,5 +48,5 @@ void Level::update(float delta, bool interactions) { playerController->selectedBlockId = -1; } vec3 position = player->hitbox->position; - chunks->setCenter(world->wfile, position.x, position.z); + chunks->setCenter(position.x, position.z); }