Added Chunks.resize method

This commit is contained in:
MihailRis 2023-11-06 10:01:42 +03:00 committed by GitHub
parent 6c79f3da18
commit 8b322ea9eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 56 additions and 14 deletions

View File

@ -132,6 +132,14 @@ void Engine::updateHotkeys() {
if (Events::jpressed(GLFW_KEY_F3)) { if (Events::jpressed(GLFW_KEY_F3)) {
level->player->debug = !level->player->debug; 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)) { if (Events::jpressed(GLFW_KEY_F5)) {
for (uint i = 0; i < level->chunks->volume; i++) { for (uint i = 0; i < level->chunks->volume; i++) {
shared_ptr<Chunk> chunk = level->chunks->chunks[i]; shared_ptr<Chunk> chunk = level->chunks->chunks[i];
@ -149,7 +157,7 @@ void Engine::mainloop() {
HudRenderer hud(assets); HudRenderer hud(assets);
lastTime = glfwGetTime(); lastTime = glfwGetTime();
Window::swapInterval(0); Window::swapInterval(1);
while (!Window::isShouldClose()){ while (!Window::isShouldClose()){
updateTimers(); updateTimers();
updateHotkeys(); updateHotkeys();
@ -172,6 +180,7 @@ Engine::~Engine() {
Audio::finalize(); Audio::finalize();
World* world = level->world; World* world = level->world;
std::cout << "-- saving world" << std::endl; std::cout << "-- saving world" << std::endl;
world->write(level); world->write(level);
@ -180,7 +189,6 @@ Engine::~Engine() {
std::cout << "-- shutting down" << std::endl; std::cout << "-- shutting down" << std::endl;
delete assets; delete assets;
Events::finalize();
Window::terminate(); Window::terminate();
} }
@ -192,7 +200,7 @@ int main() {
settings.displayHeight = 720; settings.displayHeight = 720;
settings.displaySamples = 4; settings.displaySamples = 4;
settings.displayTitle = "VoxelEngine-Cpp v13"; settings.displayTitle = "VoxelEngine-Cpp v13";
settings.chunksLoadSpeed = 15; settings.chunksLoadSpeed = 10;
settings.chunksLoadDistance = 12; settings.chunksLoadDistance = 12;
settings.chunksPadding = 2; settings.chunksPadding = 2;
Engine engine(settings); Engine engine(settings);

View File

@ -16,7 +16,8 @@
using glm::vec3; using glm::vec3;
using std::shared_ptr; 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; volume = (size_t)w*(size_t)d;
chunks = new shared_ptr<Chunk>[volume]; chunks = new shared_ptr<Chunk>[volume];
chunksSecond = new shared_ptr<Chunk>[volume]; chunksSecond = new shared_ptr<Chunk>[volume];
@ -233,7 +234,7 @@ voxel* Chunks::rayCast(vec3 a, vec3 dir, float maxDist, vec3& end, vec3& norm, v
return nullptr; return nullptr;
} }
void Chunks::setCenter(WorldFiles* worldFiles, int x, int z) { void Chunks::setCenter(int x, int z) {
int cx = x / CHUNK_W; int cx = x / CHUNK_W;
int cz = z / CHUNK_D; int cz = z / CHUNK_D;
cx -= ox; cx -= ox;
@ -243,17 +244,17 @@ void Chunks::setCenter(WorldFiles* worldFiles, int x, int z) {
cx -= w/2; cx -= w/2;
cz -= d/2; cz -= d/2;
if (cx | cz) { if (cx | cz) {
translate(worldFiles, cx,cz); translate(cx,cz);
} }
} }
void Chunks::translate(WorldFiles* worldFiles, int dx, int dz){ void Chunks::translate(int dx, int dz){
for (unsigned int i = 0; i < volume; i++){ for (uint i = 0; i < volume; i++){
chunksSecond[i] = nullptr; chunksSecond[i] = nullptr;
} }
for (int z = 0; z < d; z++){ for (int z = 0; z < d; z++){
for (int x = 0; x < w; x++){ for (int x = 0; x < w; x++){
shared_ptr<Chunk> chunk = chunks[z * w + x]; shared_ptr<Chunk> chunk = chunks[z * d + x];
int nx = x - dx; int nx = x - dx;
int nz = z - dz; int nz = z - dz;
if (chunk == nullptr) if (chunk == nullptr)
@ -275,6 +276,36 @@ void Chunks::translate(WorldFiles* worldFiles, int dx, int dz){
oz += 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<Chunk>* newChunks = new shared_ptr<Chunk>[newVolume] {};
shared_ptr<Chunk>* newChunksSecond = new shared_ptr<Chunk>[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){ void Chunks::_setOffset(int x, int z){
ox = x; ox = x;
oz = z; oz = z;

View File

@ -21,9 +21,10 @@ public:
size_t chunksCount; size_t chunksCount;
int w,d; int w,d;
int ox,oz; int ox,oz;
WorldFiles* worldFiles;
LevelEvents* events; 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(); ~Chunks();
bool putChunk(std::shared_ptr<Chunk> chunk); bool putChunk(std::shared_ptr<Chunk> chunk);
@ -41,8 +42,9 @@ public:
// does not move chunks inside // does not move chunks inside
void _setOffset(int x, int z); void _setOffset(int x, int z);
void setCenter(WorldFiles* worldFiles, int x, int z); void setCenter(int x, int z);
void translate(WorldFiles* worldFiles, int x, int z); void translate(int x, int z);
void resize(int newW, int newD);
void clear(); void clear();
}; };

View File

@ -98,6 +98,7 @@ void Window::setCursorMode(int mode){
} }
void Window::terminate(){ void Window::terminate(){
Events::finalize();
glfwTerminate(); glfwTerminate();
} }

View File

@ -18,7 +18,7 @@ Level::Level(World* world, Player* player, ChunksStorage* chunksStorage, LevelEv
events(events) { events(events) {
physics = new PhysicsSolver(vec3(0, -19.6f, 0)); physics = new PhysicsSolver(vec3(0, -19.6f, 0));
uint matrixSize = (loadDistance+chunksPadding) * 2; 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); lighting = new Lighting(chunks);
chunksController = new ChunksController(this, chunks, lighting, chunksPadding); chunksController = new ChunksController(this, chunks, lighting, chunksPadding);
playerController = new PlayerController(this); playerController = new PlayerController(this);
@ -48,5 +48,5 @@ void Level::update(float delta, bool interactions) {
playerController->selectedBlockId = -1; playerController->selectedBlockId = -1;
} }
vec3 position = player->hitbox->position; vec3 position = player->hitbox->position;
chunks->setCenter(world->wfile, position.x, position.z); chunks->setCenter(position.x, position.z);
} }