diff --git a/src/coders/png.cpp b/src/coders/png.cpp index 17a52b5c..19089762 100644 --- a/src/coders/png.cpp +++ b/src/coders/png.cpp @@ -20,7 +20,6 @@ int _png_load(const char* file, int* width, int* height){ png_bytepp row_pointers; png_structp png_ptr; GLuint texture; - GLuint texturems; int alpha; if ( !( f = fopen(file, "r" ) ) ) { diff --git a/src/hud_render.cpp b/src/frontend/hud_render.cpp similarity index 94% rename from src/hud_render.cpp rename to src/frontend/hud_render.cpp index 0ccf383d..d74b772e 100644 --- a/src/hud_render.cpp +++ b/src/frontend/hud_render.cpp @@ -4,20 +4,20 @@ #include #include -#include "typedefs.h" -#include "assets/Assets.h" -#include "graphics/Shader.h" -#include "graphics/Batch2D.h" -#include "graphics/Font.h" -#include "graphics/Mesh.h" -#include "window/Camera.h" -#include "window/Window.h" -#include "window/Events.h" -#include "voxels/Chunks.h" -#include "voxels/Block.h" -#include "world/World.h" -#include "world/Level.h" -#include "objects/Player.h" +#include "../typedefs.h" +#include "../assets/Assets.h" +#include "../graphics/Shader.h" +#include "../graphics/Batch2D.h" +#include "../graphics/Font.h" +#include "../graphics/Mesh.h" +#include "../window/Camera.h" +#include "../window/Window.h" +#include "../window/Events.h" +#include "../voxels/Chunks.h" +#include "../voxels/Block.h" +#include "../world/World.h" +#include "../world/Level.h" +#include "../objects/Player.h" HudRenderer::HudRenderer() { diff --git a/src/hud_render.h b/src/frontend/hud_render.h similarity index 100% rename from src/hud_render.h rename to src/frontend/hud_render.h diff --git a/src/world_render.cpp b/src/frontend/world_render.cpp similarity index 90% rename from src/world_render.cpp rename to src/frontend/world_render.cpp index ac143f0d..e1b279d3 100644 --- a/src/world_render.cpp +++ b/src/frontend/world_render.cpp @@ -4,23 +4,23 @@ #include #include -#include "graphics/ChunksRenderer.h" -#include "window/Window.h" -#include "window/Camera.h" -#include "graphics/Mesh.h" -#include "graphics/Shader.h" -#include "graphics/Texture.h" -#include "graphics/LineBatch.h" -#include "graphics/Batch3D.h" -#include "voxels/Chunks.h" -#include "voxels/Chunk.h" -#include "voxels/Block.h" -#include "world/World.h" -#include "world/Level.h" -#include "world/LevelEvents.h" -#include "objects/Player.h" -#include "assets/Assets.h" -#include "objects/player_control.h" +#include "../graphics/ChunksRenderer.h" +#include "../window/Window.h" +#include "../window/Camera.h" +#include "../graphics/Mesh.h" +#include "../graphics/Shader.h" +#include "../graphics/Texture.h" +#include "../graphics/LineBatch.h" +#include "../graphics/Batch3D.h" +#include "../voxels/Chunks.h" +#include "../voxels/Chunk.h" +#include "../voxels/Block.h" +#include "../world/World.h" +#include "../world/Level.h" +#include "../world/LevelEvents.h" +#include "../objects/Player.h" +#include "../assets/Assets.h" +#include "../objects/player_control.h" using std::shared_ptr; diff --git a/src/world_render.h b/src/frontend/world_render.h similarity index 100% rename from src/world_render.h rename to src/frontend/world_render.h diff --git a/src/voxel_engine.cpp b/src/voxel_engine.cpp index 324d36b1..a69d425a 100644 --- a/src/voxel_engine.cpp +++ b/src/voxel_engine.cpp @@ -32,8 +32,8 @@ #include "definitions.h" #include "assets/Assets.h" #include "assets/AssetsLoader.h" -#include "world_render.h" -#include "hud_render.h" +#include "frontend/world_render.h" +#include "frontend/hud_render.h" using std::shared_ptr; @@ -47,11 +47,17 @@ public: struct EngineSettings { int displayWidth; int displayHeight; + /* Anti-aliasing samples */ int displaySamples; - const char* title; + /* Window title */ + const char* displayTitle; /* Max milliseconds that engine uses for chunks loading only */ uint chunksLoadSpeed; + /* Radius of chunks loading zone (chunk is unit) */ + uint chunksLoadDistance; + /* Buffer zone where chunks are not unloading (chunk is unit)*/ + uint chunksPadding; }; @@ -76,7 +82,10 @@ public: Engine::Engine(const EngineSettings& settings) { this->settings = settings; - Window::initialize(settings.displayWidth, settings.displayHeight, settings.title, settings.displaySamples); + Window::initialize(settings.displayWidth, + settings.displayHeight, + settings.displayTitle, + settings.displaySamples); assets = new Assets(); std::cout << "-- loading assets" << std::endl; @@ -95,7 +104,7 @@ Engine::Engine(const EngineSettings& settings) { Camera* camera = new Camera(playerPosition, radians(90.0f)); World* world = new World("world-1", "world/", 42); Player* player = new Player(playerPosition, 4.0f, camera); - level = world->loadLevel(player); + level = world->loadLevel(player, settings.chunksLoadDistance, settings.chunksPadding); std::cout << "-- initializing finished" << std::endl; @@ -178,7 +187,15 @@ Engine::~Engine() { int main() { setup_definitions(); try { - Engine engine(EngineSettings{ 1280, 720, 1, "VoxelEngine-Cpp v13", 15 }); + EngineSettings settings; + settings.displayWidth = 1280; + settings.displayHeight = 720; + settings.displaySamples = 4; + settings.displayTitle = "VoxelEngine-Cpp v13"; + settings.chunksLoadSpeed = 15; + settings.chunksLoadDistance = 12; + settings.chunksPadding = 2; + Engine engine(settings); engine.mainloop(); } catch (const initialize_error& err) { diff --git a/src/voxels/ChunksController.cpp b/src/voxels/ChunksController.cpp index ca806c97..2fc2d976 100644 --- a/src/voxels/ChunksController.cpp +++ b/src/voxels/ChunksController.cpp @@ -31,7 +31,8 @@ using std::chrono::duration_cast; using std::chrono::microseconds; -ChunksController::ChunksController(Level* level, Chunks* chunks, Lighting* lighting) : level(level), chunks(chunks), lighting(lighting){ +ChunksController::ChunksController(Level* level, Chunks* chunks, Lighting* lighting, uint padding) + : level(level), chunks(chunks), lighting(lighting), padding(padding) { } ChunksController::~ChunksController(){ @@ -61,9 +62,9 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){ const int oz = chunks->oz; int nearX = 0; int nearZ = 0; - int minDistance = (w/2)*(w/2); - for (int z = 2; z < d-2; z++){ - for (int x = 2; x < w-2; x++){ + int minDistance = ((w-padding*2)/2)*((w-padding*2)/2); + for (int z = padding; z < d-padding; z++){ + for (int x = padding; x < w-padding; x++){ int index = z * w + x; shared_ptr chunk = chunks->chunks[index]; if (chunk != nullptr){ diff --git a/src/voxels/ChunksController.h b/src/voxels/ChunksController.h index f71dadf4..f5616d99 100644 --- a/src/voxels/ChunksController.h +++ b/src/voxels/ChunksController.h @@ -16,8 +16,9 @@ private: Chunks* chunks; Lighting* lighting; int64_t avgDurationMcs = 1000; + uint padding; public: - ChunksController(Level* level, Chunks* chunks, Lighting* lighting); + ChunksController(Level* level, Chunks* chunks, Lighting* lighting, uint padding); ~ChunksController(); void update(int64_t maxDuration); diff --git a/src/world/Level.cpp b/src/world/Level.cpp index 7fb7e0fe..0eb38e16 100644 --- a/src/world/Level.cpp +++ b/src/world/Level.cpp @@ -11,15 +11,16 @@ #include "../objects/Player.h" #include "../objects/player_control.h" -Level::Level(World* world, Player* player, Chunks* chunks, ChunksStorage* chunksStorage, PhysicsSolver* physics, LevelEvents* events) : +Level::Level(World* world, Player* player, ChunksStorage* chunksStorage, LevelEvents* events, uint loadDistance, uint chunksPadding) : world(world), player(player), - chunks(chunks), chunksStorage(chunksStorage), - physics(physics), events(events) { + physics = new PhysicsSolver(vec3(0, -19.6f, 0)); + uint matrixSize = (loadDistance+chunksPadding) * 2; + chunks = new Chunks(matrixSize, matrixSize, 0, 0, events); lighting = new Lighting(chunks); - chunksController = new ChunksController(this, chunks, lighting); + chunksController = new ChunksController(this, chunks, lighting, chunksPadding); playerController = new PlayerController(this); events->listen(EVT_CHUNK_HIDDEN, [this](lvl_event_type type, Chunk* chunk) { this->chunksStorage->remove(chunk->x, chunk->z); diff --git a/src/world/Level.h b/src/world/Level.h index 03d2079e..899192c1 100644 --- a/src/world/Level.h +++ b/src/world/Level.h @@ -1,6 +1,8 @@ #ifndef WORLD_LEVEL_H_ #define WORLD_LEVEL_H_ +#include "../typedefs.h" + class World; class Player; class Chunks; @@ -22,11 +24,15 @@ public: ChunksController* chunksController; PlayerController* playerController; LevelEvents* events; - Level(World* world, Player* player, Chunks* chunks, ChunksStorage* chunksStorage, PhysicsSolver* physics, LevelEvents* events); + Level(World* world, + Player* player, + ChunksStorage* chunksStorage, + LevelEvents* events, + uint loadDistance, + uint chunksPadding); ~Level(); void update(float delta, bool interactions); - }; #endif /* WORLD_LEVEL_H_ */ diff --git a/src/world/World.cpp b/src/world/World.cpp index 0bd357c5..b5155308 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -36,14 +36,14 @@ void World::write(Level* level) { wfile->writePlayer(level->player); } -Level* World::loadLevel(Player* player) { +Level* World::loadLevel(Player* player, uint loadDistance, uint chunksPadding) { ChunksStorage* storage = new ChunksStorage(); LevelEvents* events = new LevelEvents(); - Level* level = new Level(this, player, new Chunks(16, 16, 0, 0, events), storage, new PhysicsSolver(vec3(0, -19.6f, 0)), events); + Level* level = new Level(this, player, storage, events, loadDistance, chunksPadding); wfile->readPlayer(player); Camera* camera = player->camera; camera->rotation = mat4(1.0f); camera->rotate(player->camY, player->camX, 0); return level; -} \ No newline at end of file +} diff --git a/src/world/World.h b/src/world/World.h index d67b27c6..3f361026 100644 --- a/src/world/World.h +++ b/src/world/World.h @@ -2,6 +2,7 @@ #define WORLD_WORLD_H_ #include +#include "../typedefs.h" class WorldFiles; class Chunks; @@ -18,7 +19,7 @@ public: ~World(); void write(Level* level); - Level* loadLevel(Player* player); + Level* loadLevel(Player* player, uint loadDistance, uint chunksPadding); }; #endif /* WORLD_WORLD_H_ */