From e8ca11894d5930edd31ac610a65b9155aba67ba6 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 29 Jun 2022 17:12:58 +0300 Subject: [PATCH] Added class Level for runtime world stuff --- Debug/src/world/subdir.mk | 3 +++ src/player_control.cpp | 8 +++++-- src/player_control.h | 4 ++-- src/voxel_engine.cpp | 47 +++++++++++++++++++-------------------- src/world/Level.cpp | 19 ++++++++++++++++ src/world/Level.h | 21 +++++++++++++++++ src/world/World.cpp | 2 +- src/world/World.h | 3 +-- src/world_render.h | 9 ++++---- 9 files changed, 81 insertions(+), 35 deletions(-) create mode 100644 src/world/Level.cpp create mode 100644 src/world/Level.h diff --git a/Debug/src/world/subdir.mk b/Debug/src/world/subdir.mk index bf85112f..0d5d11aa 100644 --- a/Debug/src/world/subdir.mk +++ b/Debug/src/world/subdir.mk @@ -4,12 +4,15 @@ # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ +../src/world/Level.cpp \ ../src/world/World.cpp OBJS += \ +./src/world/Level.o \ ./src/world/World.o CPP_DEPS += \ +./src/world/Level.d \ ./src/world/World.d diff --git a/src/player_control.cpp b/src/player_control.cpp index 33e71aa0..26223c43 100644 --- a/src/player_control.cpp +++ b/src/player_control.cpp @@ -5,6 +5,7 @@ #include "physics/PhysicsSolver.h" #include "physics/Hitbox.h" #include "lighting/Lighting.h" +#include "world/Level.h" #include "voxels/voxel.h" #include "voxels/Chunks.h" #include "window/Camera.h" @@ -162,8 +163,11 @@ void update_controls(PhysicsSolver* physics, } } -void update_interaction(Chunks* chunks, PhysicsSolver* physics, Player* player, Lighting* lighting, LineBatch* lineBatch){ +void update_interaction(Level* level, LineBatch* lineBatch){ + Chunks* chunks = level->chunks; + Player* player = level->player; Camera* camera = player->camera; + Lighting* lighting = level->lighting; vec3 end; vec3 norm; vec3 iend; @@ -182,7 +186,7 @@ void update_interaction(Chunks* chunks, PhysicsSolver* physics, Player* player, int x = (int)(iend.x)+(int)(norm.x); int y = (int)(iend.y)+(int)(norm.y); int z = (int)(iend.z)+(int)(norm.z); - if (!physics->isBlockInside(x,y,z, player->hitbox)){ + if (!level->physics->isBlockInside(x,y,z, player->hitbox)){ chunks->set(x, y, z, player->choosenBlock); lighting->onBlockSet(x,y,z, player->choosenBlock); } diff --git a/src/player_control.h b/src/player_control.h index e3a91407..d486b9b8 100644 --- a/src/player_control.h +++ b/src/player_control.h @@ -4,10 +4,10 @@ class PhysicsSolver; class Chunks; class Player; -class Lighting; class LineBatch; +class Level; void update_controls(PhysicsSolver* physics, Chunks* chunks, Player* player, float delta); -void update_interaction(Chunks* chunks, PhysicsSolver* physics, Player* player, Lighting* lighting, LineBatch* lineBatch); +void update_interaction(Level* level, LineBatch* lineBatch); #endif /* PLAYER_CONTROL_H_ */ diff --git a/src/voxel_engine.cpp b/src/voxel_engine.cpp index 290f70c2..e40a4d5c 100644 --- a/src/voxel_engine.cpp +++ b/src/voxel_engine.cpp @@ -42,6 +42,7 @@ using namespace glm; #include "physics/Hitbox.h" #include "physics/PhysicsSolver.h" #include "world/World.h" +#include "world/Level.h" #include "audio/Audio.h" #include "audio/audioutil.h" @@ -58,9 +59,9 @@ int HEIGHT = 720; // Save all world data to files -void write_world(World* world){ +void write_world(World* world, Level* level){ WorldFiles* wfile = world->wfile; - Chunks* chunks = world->chunks; + Chunks* chunks = level->chunks; for (unsigned int i = 0; i < chunks->volume; i++){ Chunk* chunk = chunks->chunks[i]; @@ -72,6 +73,18 @@ void write_world(World* world){ wfile->write(); } +void update_level(World* world, Level* level, VoxelRenderer* renderer, vec3 position, float delta, long frame){ + update_controls(level->physics, level->chunks, level->player, delta); + update_interaction(level, lineBatch); + + level->chunks->setCenter(world->wfile, position.x,0,position.z); + level->chunksController->_buildMeshes(renderer, frame); + + int freeLoaders = level->chunksController->countFreeLoaders(); + for (int i = 0; i < freeLoaders; i++) + level->chunksController->loadVisible(world->wfile); +} + int main() { setup_definitions(); @@ -91,12 +104,11 @@ int main() { } std::cout << "-- loading world" << std::endl; - Camera* camera = new Camera(vec3(-320,255,32), radians(90.0f)); - World* world = new World("world-1", "world/", new Chunks(34,1,34, 0,0,0)); - Chunks* chunks = world->chunks; - - + Camera* camera = new Camera(vec3(-320,200,32), radians(90.0f)); + World* world = new World("world-1", "world/"); Player* player = new Player(vec3(camera->position), 4.0f, camera); + Level* level = new Level(player, new Chunks(34,1,34, 0,0,0), new PhysicsSolver(vec3(0, -9.8f*2.0f, 0))); + world->wfile->readPlayer(player); camera->rotation = mat4(1.0f); camera->rotate(player->camY, player->camX, 0); @@ -104,13 +116,9 @@ int main() { std::cout << "-- preparing systems" << std::endl; VoxelRenderer renderer(1024*1024); - PhysicsSolver physics(vec3(0,-9.8f*2.0f,0)); - Lighting lighting(chunks); init_renderer(); - ChunksController chunksController(chunks, &lighting); - float lastTime = glfwGetTime(); float delta = 0.0f; @@ -137,18 +145,9 @@ int main() { devdata = !devdata; } - update_controls(&physics, chunks, player, delta); - update_interaction(chunks, &physics, player, &lighting, lineBatch); - - chunks->setCenter(world->wfile, camera->position.x,0,camera->position.z); - chunksController._buildMeshes(&renderer, frame); - - int freeLoaders = chunksController.countFreeLoaders(); - for (int i = 0; i < freeLoaders; i++) - chunksController.loadVisible(world->wfile); - - draw_world(player, camera, assets, world, occlusion); - draw_hud(player, assets, world, devdata, fps); + update_level(world, level, &renderer, camera->position, delta, frame); + draw_world(world, level, camera, assets, occlusion); + draw_hud(world, level, assets, devdata, fps); Window::swapBuffers(); Events::pullEvents(); @@ -156,7 +155,7 @@ int main() { std::cout << "-- saving world" << std::endl; world->wfile->writePlayer(player); - write_world(world); + write_world(world, level); delete world; std::cout << "-- shutting down" << std::endl; diff --git a/src/world/Level.cpp b/src/world/Level.cpp new file mode 100644 index 00000000..3faa316f --- /dev/null +++ b/src/world/Level.cpp @@ -0,0 +1,19 @@ +#include "Level.h" +#include "../lighting/Lighting.h" +#include "../voxels/ChunksController.h" + +Level::Level(Player* player, Chunks* chunks, PhysicsSolver* physics) : + player(player), + chunks(chunks), + physics(physics) { + lighting = new Lighting(chunks); + chunksController = new ChunksController(chunks, lighting); +} + +Level::~Level(){ + delete chunks; + delete physics; + delete player; + delete lighting; + delete chunksController; +} diff --git a/src/world/Level.h b/src/world/Level.h new file mode 100644 index 00000000..8284cbb0 --- /dev/null +++ b/src/world/Level.h @@ -0,0 +1,21 @@ +#ifndef WORLD_LEVEL_H_ +#define WORLD_LEVEL_H_ + +class Player; +class Chunks; +class Lighting; +class PhysicsSolver; +class ChunksController; + +class Level { +public: + Player* player; + Chunks* chunks; + PhysicsSolver* physics; + Lighting* lighting; + ChunksController* chunksController; + Level(Player* player, Chunks* chunks, PhysicsSolver* physics); + ~Level(); +}; + +#endif /* WORLD_LEVEL_H_ */ diff --git a/src/world/World.cpp b/src/world/World.cpp index 31cdbf1c..92e7c0e2 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -4,7 +4,7 @@ #include "../voxels/Chunk.h" #include "../voxels/Chunks.h" -World::World(std::string name, std::string directory, Chunks* chunks) : name(name), chunks(chunks) { +World::World(std::string name, std::string directory) : name(name) { wfile = new WorldFiles(directory, REGION_VOL * (CHUNK_VOL * 2 + 8)); } diff --git a/src/world/World.h b/src/world/World.h index 20343abc..019601bd 100644 --- a/src/world/World.h +++ b/src/world/World.h @@ -10,9 +10,8 @@ class World { public: std::string name; WorldFiles* wfile; - Chunks* chunks; - World(std::string name, std::string directory, Chunks* chunks); + World(std::string name, std::string directory); ~World(); }; diff --git a/src/world_render.h b/src/world_render.h index b2fe3075..60a7c1a7 100644 --- a/src/world_render.h +++ b/src/world_render.h @@ -110,8 +110,9 @@ bool chunks_comparator(size_t i, size_t j) { } -void draw_hud(Player* player, Assets* assets, World* world, bool devdata, int fps){ - Chunks* chunks = world->chunks; +void draw_hud(World* world, Level* level, Assets* assets, bool devdata, int fps){ + Chunks* chunks = level->chunks; + Player* player = level->player; glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); @@ -143,8 +144,8 @@ void draw_hud(Player* player, Assets* assets, World* world, bool devdata, int fp batch->render(); } -void draw_world(Player* player, Camera* camera, Assets* assets, World* world, bool occlusion){ - Chunks* chunks = world->chunks; +void draw_world(World* world, Level* level, Camera* camera, Assets* assets, bool occlusion){ + Chunks* chunks = level->chunks; glClearColor(0.7f,0.81f,1.0f,1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);