diff --git a/res/block.png.old b/res/block.png.old deleted file mode 100644 index 1fb83555..00000000 Binary files a/res/block.png.old and /dev/null differ diff --git a/res/block.png.old.old b/res/block.png.old.old deleted file mode 100644 index 5e2512c7..00000000 Binary files a/res/block.png.old.old and /dev/null differ diff --git a/res/crosshair.glslf b/res/crosshair.glslf deleted file mode 100644 index 2cfc3346..00000000 --- a/res/crosshair.glslf +++ /dev/null @@ -1,7 +0,0 @@ -#version 330 core - -out vec4 f_color; - -void main(){ - f_color = vec4(1.0); -} diff --git a/res/crosshair.glslv b/res/crosshair.glslv deleted file mode 100644 index 1d67cb55..00000000 --- a/res/crosshair.glslv +++ /dev/null @@ -1,10 +0,0 @@ -#version 330 core - -layout (location = 0) in vec2 v_position; - -uniform float u_ar; -uniform float u_scale; - -void main(){ - gl_Position = vec4(v_position.x * u_ar * u_scale, v_position.y * u_scale, 0.0, 1.0); -} diff --git a/res/img.png b/res/img.png deleted file mode 100644 index 63e4fba6..00000000 Binary files a/res/img.png and /dev/null differ diff --git a/src/assets/AssetsLoader.cpp b/src/assets/AssetsLoader.cpp index 86b85ae4..45b583cd 100644 --- a/src/assets/AssetsLoader.cpp +++ b/src/assets/AssetsLoader.cpp @@ -81,7 +81,6 @@ void AssetsLoader::createDefaults(AssetsLoader& loader) { void AssetsLoader::addDefaults(AssetsLoader& loader) { loader.add(ASSET_SHADER, "res/main", "main"); - loader.add(ASSET_SHADER, "res/crosshair", "crosshair"); loader.add(ASSET_SHADER, "res/lines", "lines"); loader.add(ASSET_SHADER, "res/ui", "ui"); diff --git a/src/maths/voxmaths.h b/src/maths/voxmaths.h index 59fe85c7..da17c815 100644 --- a/src/maths/voxmaths.h +++ b/src/maths/voxmaths.h @@ -25,6 +25,14 @@ inline int min(int a, int b) { return (a < b) ? a : b; } +inline int64_t max(int64_t a, int64_t b) { + return (a > b) ? a : b; +} + +inline int64_t min(int64_t a, int64_t b) { + return (a < b) ? a : b; +} + static unsigned int g_seed; inline void fast_srand(int seed) { diff --git a/src/player_control.cpp b/src/objects/player_control.cpp similarity index 95% rename from src/player_control.cpp rename to src/objects/player_control.cpp index dc4cea9b..c2d75303 100644 --- a/src/player_control.cpp +++ b/src/objects/player_control.cpp @@ -1,15 +1,15 @@ #include "player_control.h" -#include "objects/Player.h" -#include "physics/PhysicsSolver.h" -#include "physics/Hitbox.h" -#include "lighting/Lighting.h" -#include "world/Level.h" -#include "voxels/Block.h" -#include "voxels/voxel.h" -#include "voxels/Chunks.h" -#include "window/Camera.h" -#include "window/Events.h" +#include "Player.h" +#include "../physics/PhysicsSolver.h" +#include "../physics/Hitbox.h" +#include "../lighting/Lighting.h" +#include "../world/Level.h" +#include "../voxels/Block.h" +#include "../voxels/voxel.h" +#include "../voxels/Chunks.h" +#include "../window/Camera.h" +#include "../window/Events.h" #include #define CROUCH_SPEED_MUL 0.25f diff --git a/src/player_control.h b/src/objects/player_control.h similarity index 100% rename from src/player_control.h rename to src/objects/player_control.h diff --git a/src/voxel_engine.cpp b/src/voxel_engine.cpp index b1f670ec..1d4bbbca 100644 --- a/src/voxel_engine.cpp +++ b/src/voxel_engine.cpp @@ -49,12 +49,15 @@ struct EngineSettings { int displayHeight; int displaySamples; const char* title; + /* Max milliseconds that engine uses for chunks loading only */ + uint chunksLoadSpeed; }; class Engine { Assets* assets; Level* level; + EngineSettings settings; uint64_t frame = 0; float lastTime = 0.0f; @@ -70,6 +73,8 @@ public: }; Engine::Engine(const EngineSettings& settings) { + this->settings = settings; + Window::initialize(settings.displayWidth, settings.displayHeight, settings.title, settings.displaySamples); assets = new Assets(); @@ -117,7 +122,7 @@ void Engine::updateHotkeys() { level->player->debug = !level->player->debug; } if (Events::jpressed(GLFW_KEY_F5)) { - for (unsigned i = 0; i < level->chunks->volume; i++) { + for (uint i = 0; i < level->chunks->volume; i++) { shared_ptr chunk = level->chunks->chunks[i]; if (chunk != nullptr && chunk->isReady()) { chunk->setModified(true); @@ -140,7 +145,7 @@ void Engine::mainloop() { updateHotkeys(); level->update(delta, Events::_cursor_locked); - level->chunksController->loadVisible(world->wfile); + level->chunksController->update(settings.chunksLoadSpeed); worldRenderer.draw(camera, occlusion); hud.draw(level, assets); @@ -172,7 +177,7 @@ Engine::~Engine() { int main() { setup_definitions(); try { - Engine engine(EngineSettings{ 1280, 720, 1, "VoxelEngine-Cpp v13" }); + Engine engine(EngineSettings{ 1280, 720, 1, "VoxelEngine-Cpp v13", 10 }); engine.mainloop(); } catch (const initialize_error& err) { diff --git a/src/voxels/ChunksController.cpp b/src/voxels/ChunksController.cpp index 942375f5..ca806c97 100644 --- a/src/voxels/ChunksController.cpp +++ b/src/voxels/ChunksController.cpp @@ -9,9 +9,11 @@ #include "../files/WorldFiles.h" #include "../world/Level.h" #include "../world/World.h" +#include "../maths/voxmaths.h" #include #include #include +#include #if defined(_WIN32) && defined(__MINGW32__) #define _WIN32_WINNT 0x0501 @@ -20,9 +22,13 @@ #include #endif +#define MAX_WORK_PER_FRAME 16 #define MIN_SURROUNDING 9 using std::shared_ptr; +using std::chrono::high_resolution_clock; +using std::chrono::duration_cast; +using std::chrono::microseconds; ChunksController::ChunksController(Level* level, Chunks* chunks, Lighting* lighting) : level(level), chunks(chunks), lighting(lighting){ @@ -31,6 +37,23 @@ ChunksController::ChunksController(Level* level, Chunks* chunks, Lighting* light ChunksController::~ChunksController(){ } +void ChunksController::update(int64_t maxDuration) { + int64_t mcstotal = 0; + for (uint i = 0; i < MAX_WORK_PER_FRAME; i++) { + auto start = high_resolution_clock::now(); + if (loadVisible(level->world->wfile)) { + auto elapsed = high_resolution_clock::now() - start; + int64_t mcs = duration_cast(elapsed).count(); + avgDurationMcs = mcs * 0.2 + avgDurationMcs * 0.8; + if (mcstotal + max(avgDurationMcs, mcs) * 2 < maxDuration * 1000) { + mcstotal += mcs; + continue; + } + } + break; + } +} + bool ChunksController::loadVisible(WorldFiles* worldFiles){ const int w = chunks->w; const int d = chunks->d; diff --git a/src/voxels/ChunksController.h b/src/voxels/ChunksController.h index a3868886..f71dadf4 100644 --- a/src/voxels/ChunksController.h +++ b/src/voxels/ChunksController.h @@ -1,6 +1,8 @@ #ifndef VOXELS_CHUNKSCONTROLLER_H_ #define VOXELS_CHUNKSCONTROLLER_H_ +#include "../typedefs.h" + class Level; class Chunks; class Lighting; @@ -13,10 +15,12 @@ private: Level* level; Chunks* chunks; Lighting* lighting; + int64_t avgDurationMcs = 1000; public: ChunksController(Level* level, Chunks* chunks, Lighting* lighting); ~ChunksController(); + void update(int64_t maxDuration); bool loadVisible(WorldFiles* worldFiles); }; diff --git a/src/world/Level.cpp b/src/world/Level.cpp index c9be21ba..7fb7e0fe 100644 --- a/src/world/Level.cpp +++ b/src/world/Level.cpp @@ -6,10 +6,10 @@ #include "../voxels/Chunks.h" #include "../voxels/ChunksController.h" #include "../voxels/ChunksStorage.h" -#include "../player_control.h" #include "../physics/Hitbox.h" #include "../physics/PhysicsSolver.h" #include "../objects/Player.h" +#include "../objects/player_control.h" Level::Level(World* world, Player* player, Chunks* chunks, ChunksStorage* chunksStorage, PhysicsSolver* physics, LevelEvents* events) : world(world), diff --git a/src/world_render.cpp b/src/world_render.cpp index 67d22708..ac143f0d 100644 --- a/src/world_render.cpp +++ b/src/world_render.cpp @@ -20,7 +20,7 @@ #include "world/LevelEvents.h" #include "objects/Player.h" #include "assets/Assets.h" -#include "player_control.h" +#include "objects/player_control.h" using std::shared_ptr;