From 4ceba017c73f039cc91115ccc277606d0fc71473 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 29 Jun 2022 16:07:55 +0300 Subject: [PATCH] Some refactor --- Debug/makefile | 1 + Debug/sources.mk | 1 + Debug/src/world/subdir.mk | 24 +++++++++++++++++++++ src/files/WorldFiles.cpp | 2 +- src/files/WorldFiles.h | 2 +- src/objects/Player.cpp | 3 +++ src/objects/Player.h | 2 ++ src/voxel_engine.cpp | 44 +++++++++++++++++++-------------------- src/world/World.cpp | 13 ++++++++++++ src/world/World.h | 19 +++++++++++++++++ src/world_render.h | 8 +++++-- 11 files changed, 92 insertions(+), 27 deletions(-) create mode 100644 Debug/src/world/subdir.mk create mode 100644 src/world/World.cpp create mode 100644 src/world/World.h diff --git a/Debug/makefile b/Debug/makefile index 9c5b3843..0247a747 100644 --- a/Debug/makefile +++ b/Debug/makefile @@ -8,6 +8,7 @@ RM := rm -rf # All of the sources participating in the build are defined here -include sources.mk +-include src/world/subdir.mk -include src/window/subdir.mk -include src/voxels/subdir.mk -include src/physics/subdir.mk diff --git a/Debug/sources.mk b/Debug/sources.mk index b2849f80..39d73484 100644 --- a/Debug/sources.mk +++ b/Debug/sources.mk @@ -33,4 +33,5 @@ src/objects \ src/physics \ src/voxels \ src/window \ +src/world \ diff --git a/Debug/src/world/subdir.mk b/Debug/src/world/subdir.mk new file mode 100644 index 00000000..bf85112f --- /dev/null +++ b/Debug/src/world/subdir.mk @@ -0,0 +1,24 @@ +################################################################################ +# Automatically-generated file. Do not edit! +################################################################################ + +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../src/world/World.cpp + +OBJS += \ +./src/world/World.o + +CPP_DEPS += \ +./src/world/World.d + + +# Each subdirectory must supply rules for building sources it contributes +src/world/%.o: ../src/world/%.cpp src/world/subdir.mk + @echo 'Building file: $<' + @echo 'Invoking: Cross G++ Compiler' + g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index 17fee0f0..850d045a 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -50,7 +50,7 @@ float bytes2Float(char* srcs, unsigned int offset){ return *(float*)(&value); } -WorldFiles::WorldFiles(const char* directory, size_t mainBufferCapacity) : directory(directory){ +WorldFiles::WorldFiles(std::string directory, size_t mainBufferCapacity) : directory(directory){ mainBufferIn = new char[CHUNK_VOL*2]; mainBufferOut = new char[mainBufferCapacity]; } diff --git a/src/files/WorldFiles.h b/src/files/WorldFiles.h index 9495f58b..9ee11778 100644 --- a/src/files/WorldFiles.h +++ b/src/files/WorldFiles.h @@ -25,7 +25,7 @@ public: char* mainBufferIn; char* mainBufferOut; - WorldFiles(const char* directory, size_t mainBufferCapacity); + WorldFiles(std::string directory, size_t mainBufferCapacity); ~WorldFiles(); void put(const char* chunkData, int x, int y); diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index b8e2b37a..997caf06 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -1,5 +1,8 @@ #include "Player.h" #include "../physics/Hitbox.h" +#include "../physics/PhysicsSolver.h" +#include "../voxels/Chunks.h" +#include "../window/Events.h" #include diff --git a/src/objects/Player.h b/src/objects/Player.h index d3e4e081..2147458f 100644 --- a/src/objects/Player.h +++ b/src/objects/Player.h @@ -5,6 +5,8 @@ class Camera; class Hitbox; +class PhysicsSolver; +class Chunks; class Player { public: diff --git a/src/voxel_engine.cpp b/src/voxel_engine.cpp index e4ba8473..290f70c2 100644 --- a/src/voxel_engine.cpp +++ b/src/voxel_engine.cpp @@ -41,6 +41,7 @@ using namespace glm; #include "lighting/Lighting.h" #include "physics/Hitbox.h" #include "physics/PhysicsSolver.h" +#include "world/World.h" #include "audio/Audio.h" #include "audio/audioutil.h" @@ -49,10 +50,18 @@ using namespace glm; #include "declarations.h" #include "world_render.h" +#include "player_control.h" + + +int WIDTH = 1280; +int HEIGHT = 720; // Save all world data to files -void write_world(WorldFiles* wfile, Chunks* chunks){ +void write_world(World* world){ + WorldFiles* wfile = world->wfile; + Chunks* chunks = world->chunks; + for (unsigned int i = 0; i < chunks->volume; i++){ Chunk* chunk = chunks->chunks[i]; if (chunk == nullptr) @@ -63,17 +72,6 @@ void write_world(WorldFiles* wfile, Chunks* chunks){ wfile->write(); } -// Deleting world data from memory -void close_world(WorldFiles* wfile, Chunks* chunks){ - delete chunks; - delete wfile; -} - -int WIDTH = 1280; -int HEIGHT = 720; - -#include "player_control.h" - int main() { setup_definitions(); @@ -93,13 +91,13 @@ int main() { } std::cout << "-- loading world" << std::endl; - Camera *camera = new Camera(vec3(-320,255,32), radians(90.0f)); - WorldFiles *wfile = new WorldFiles("world/", REGION_VOL * (CHUNK_VOL * 2 + 8)); - Chunks *chunks = new Chunks(34,1,34, 0,0,0); + 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; Player* player = new Player(vec3(camera->position), 4.0f, camera); - wfile->readPlayer(player); + world->wfile->readPlayer(player); camera->rotation = mat4(1.0f); camera->rotate(player->camY, player->camX, 0); @@ -142,24 +140,24 @@ int main() { update_controls(&physics, chunks, player, delta); update_interaction(chunks, &physics, player, &lighting, lineBatch); - chunks->setCenter(wfile, camera->position.x,0,camera->position.z); + 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(wfile); + chunksController.loadVisible(world->wfile); - draw_world(player, camera, assets, chunks, occlusion); - draw_hud(player, assets, chunks, devdata, fps); + draw_world(player, camera, assets, world, occlusion); + draw_hud(player, assets, world, devdata, fps); Window::swapBuffers(); Events::pullEvents(); } std::cout << "-- saving world" << std::endl; - wfile->writePlayer(player); - write_world(wfile, chunks); - close_world(wfile, chunks); + world->wfile->writePlayer(player); + write_world(world); + delete world; std::cout << "-- shutting down" << std::endl; diff --git a/src/world/World.cpp b/src/world/World.cpp new file mode 100644 index 00000000..31cdbf1c --- /dev/null +++ b/src/world/World.cpp @@ -0,0 +1,13 @@ +#include "World.h" + +#include "../files/WorldFiles.h" +#include "../voxels/Chunk.h" +#include "../voxels/Chunks.h" + +World::World(std::string name, std::string directory, Chunks* chunks) : name(name), chunks(chunks) { + wfile = new WorldFiles(directory, REGION_VOL * (CHUNK_VOL * 2 + 8)); +} + +World::~World(){ + delete wfile; +} diff --git a/src/world/World.h b/src/world/World.h new file mode 100644 index 00000000..20343abc --- /dev/null +++ b/src/world/World.h @@ -0,0 +1,19 @@ +#ifndef WORLD_WORLD_H_ +#define WORLD_WORLD_H_ + +#include + +class WorldFiles; +class Chunks; + +class World { +public: + std::string name; + WorldFiles* wfile; + Chunks* chunks; + + World(std::string name, std::string directory, Chunks* chunks); + ~World(); +}; + +#endif /* WORLD_WORLD_H_ */ diff --git a/src/world_render.h b/src/world_render.h index eea5acb7..b2fe3075 100644 --- a/src/world_render.h +++ b/src/world_render.h @@ -110,7 +110,9 @@ bool chunks_comparator(size_t i, size_t j) { } -void draw_hud(Player* player, Assets* assets, Chunks* chunks, bool devdata, int fps){ +void draw_hud(Player* player, Assets* assets, World* world, bool devdata, int fps){ + Chunks* chunks = world->chunks; + glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); Shader* uishader = assets->getShader("ui"); @@ -141,7 +143,9 @@ void draw_hud(Player* player, Assets* assets, Chunks* chunks, bool devdata, int batch->render(); } -void draw_world(Player* player, Camera* camera, Assets* assets, Chunks* chunks, bool occlusion){ +void draw_world(Player* player, Camera* camera, Assets* assets, World* world, bool occlusion){ + Chunks* chunks = world->chunks; + glClearColor(0.7f,0.81f,1.0f,1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST);