EngineSettings: chunksLoadDistance, chunksPadding

This commit is contained in:
MihailRis 2023-11-06 01:47:17 +03:00
parent 26935473a3
commit 01eb264f44
12 changed files with 79 additions and 53 deletions

View File

@ -20,7 +20,6 @@ int _png_load(const char* file, int* width, int* height){
png_bytepp row_pointers; png_bytepp row_pointers;
png_structp png_ptr; png_structp png_ptr;
GLuint texture; GLuint texture;
GLuint texturems;
int alpha; int alpha;
if ( !( f = fopen(file, "r" ) ) ) { if ( !( f = fopen(file, "r" ) ) ) {

View File

@ -4,20 +4,20 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "typedefs.h" #include "../typedefs.h"
#include "assets/Assets.h" #include "../assets/Assets.h"
#include "graphics/Shader.h" #include "../graphics/Shader.h"
#include "graphics/Batch2D.h" #include "../graphics/Batch2D.h"
#include "graphics/Font.h" #include "../graphics/Font.h"
#include "graphics/Mesh.h" #include "../graphics/Mesh.h"
#include "window/Camera.h" #include "../window/Camera.h"
#include "window/Window.h" #include "../window/Window.h"
#include "window/Events.h" #include "../window/Events.h"
#include "voxels/Chunks.h" #include "../voxels/Chunks.h"
#include "voxels/Block.h" #include "../voxels/Block.h"
#include "world/World.h" #include "../world/World.h"
#include "world/Level.h" #include "../world/Level.h"
#include "objects/Player.h" #include "../objects/Player.h"
HudRenderer::HudRenderer() { HudRenderer::HudRenderer() {

View File

@ -4,23 +4,23 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <memory> #include <memory>
#include "graphics/ChunksRenderer.h" #include "../graphics/ChunksRenderer.h"
#include "window/Window.h" #include "../window/Window.h"
#include "window/Camera.h" #include "../window/Camera.h"
#include "graphics/Mesh.h" #include "../graphics/Mesh.h"
#include "graphics/Shader.h" #include "../graphics/Shader.h"
#include "graphics/Texture.h" #include "../graphics/Texture.h"
#include "graphics/LineBatch.h" #include "../graphics/LineBatch.h"
#include "graphics/Batch3D.h" #include "../graphics/Batch3D.h"
#include "voxels/Chunks.h" #include "../voxels/Chunks.h"
#include "voxels/Chunk.h" #include "../voxels/Chunk.h"
#include "voxels/Block.h" #include "../voxels/Block.h"
#include "world/World.h" #include "../world/World.h"
#include "world/Level.h" #include "../world/Level.h"
#include "world/LevelEvents.h" #include "../world/LevelEvents.h"
#include "objects/Player.h" #include "../objects/Player.h"
#include "assets/Assets.h" #include "../assets/Assets.h"
#include "objects/player_control.h" #include "../objects/player_control.h"
using std::shared_ptr; using std::shared_ptr;

View File

@ -32,8 +32,8 @@
#include "definitions.h" #include "definitions.h"
#include "assets/Assets.h" #include "assets/Assets.h"
#include "assets/AssetsLoader.h" #include "assets/AssetsLoader.h"
#include "world_render.h" #include "frontend/world_render.h"
#include "hud_render.h" #include "frontend/hud_render.h"
using std::shared_ptr; using std::shared_ptr;
@ -47,11 +47,17 @@ public:
struct EngineSettings { struct EngineSettings {
int displayWidth; int displayWidth;
int displayHeight; int displayHeight;
/* Anti-aliasing samples */ /* Anti-aliasing samples */
int displaySamples; int displaySamples;
const char* title; /* Window title */
const char* displayTitle;
/* Max milliseconds that engine uses for chunks loading only */ /* Max milliseconds that engine uses for chunks loading only */
uint chunksLoadSpeed; 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) { Engine::Engine(const EngineSettings& settings) {
this->settings = 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(); assets = new Assets();
std::cout << "-- loading assets" << std::endl; std::cout << "-- loading assets" << std::endl;
@ -95,7 +104,7 @@ Engine::Engine(const EngineSettings& settings) {
Camera* camera = new Camera(playerPosition, radians(90.0f)); Camera* camera = new Camera(playerPosition, radians(90.0f));
World* world = new World("world-1", "world/", 42); World* world = new World("world-1", "world/", 42);
Player* player = new Player(playerPosition, 4.0f, camera); 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; std::cout << "-- initializing finished" << std::endl;
@ -178,7 +187,15 @@ Engine::~Engine() {
int main() { int main() {
setup_definitions(); setup_definitions();
try { 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(); engine.mainloop();
} }
catch (const initialize_error& err) { catch (const initialize_error& err) {

View File

@ -31,7 +31,8 @@ using std::chrono::duration_cast;
using std::chrono::microseconds; 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(){ ChunksController::~ChunksController(){
@ -61,9 +62,9 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){
const int oz = chunks->oz; const int oz = chunks->oz;
int nearX = 0; int nearX = 0;
int nearZ = 0; int nearZ = 0;
int minDistance = (w/2)*(w/2); int minDistance = ((w-padding*2)/2)*((w-padding*2)/2);
for (int z = 2; z < d-2; z++){ for (int z = padding; z < d-padding; z++){
for (int x = 2; x < w-2; x++){ for (int x = padding; x < w-padding; x++){
int index = z * w + x; int index = z * w + x;
shared_ptr<Chunk> chunk = chunks->chunks[index]; shared_ptr<Chunk> chunk = chunks->chunks[index];
if (chunk != nullptr){ if (chunk != nullptr){

View File

@ -16,8 +16,9 @@ private:
Chunks* chunks; Chunks* chunks;
Lighting* lighting; Lighting* lighting;
int64_t avgDurationMcs = 1000; int64_t avgDurationMcs = 1000;
uint padding;
public: public:
ChunksController(Level* level, Chunks* chunks, Lighting* lighting); ChunksController(Level* level, Chunks* chunks, Lighting* lighting, uint padding);
~ChunksController(); ~ChunksController();
void update(int64_t maxDuration); void update(int64_t maxDuration);

View File

@ -11,15 +11,16 @@
#include "../objects/Player.h" #include "../objects/Player.h"
#include "../objects/player_control.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), world(world),
player(player), player(player),
chunks(chunks),
chunksStorage(chunksStorage), chunksStorage(chunksStorage),
physics(physics),
events(events) { 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); lighting = new Lighting(chunks);
chunksController = new ChunksController(this, chunks, lighting); chunksController = new ChunksController(this, chunks, lighting, chunksPadding);
playerController = new PlayerController(this); playerController = new PlayerController(this);
events->listen(EVT_CHUNK_HIDDEN, [this](lvl_event_type type, Chunk* chunk) { events->listen(EVT_CHUNK_HIDDEN, [this](lvl_event_type type, Chunk* chunk) {
this->chunksStorage->remove(chunk->x, chunk->z); this->chunksStorage->remove(chunk->x, chunk->z);

View File

@ -1,6 +1,8 @@
#ifndef WORLD_LEVEL_H_ #ifndef WORLD_LEVEL_H_
#define WORLD_LEVEL_H_ #define WORLD_LEVEL_H_
#include "../typedefs.h"
class World; class World;
class Player; class Player;
class Chunks; class Chunks;
@ -22,11 +24,15 @@ public:
ChunksController* chunksController; ChunksController* chunksController;
PlayerController* playerController; PlayerController* playerController;
LevelEvents* events; 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(); ~Level();
void update(float delta, bool interactions); void update(float delta, bool interactions);
}; };
#endif /* WORLD_LEVEL_H_ */ #endif /* WORLD_LEVEL_H_ */

View File

@ -36,10 +36,10 @@ void World::write(Level* level) {
wfile->writePlayer(level->player); wfile->writePlayer(level->player);
} }
Level* World::loadLevel(Player* player) { Level* World::loadLevel(Player* player, uint loadDistance, uint chunksPadding) {
ChunksStorage* storage = new ChunksStorage(); ChunksStorage* storage = new ChunksStorage();
LevelEvents* events = new LevelEvents(); 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); wfile->readPlayer(player);
Camera* camera = player->camera; Camera* camera = player->camera;

View File

@ -2,6 +2,7 @@
#define WORLD_WORLD_H_ #define WORLD_WORLD_H_
#include <string> #include <string>
#include "../typedefs.h"
class WorldFiles; class WorldFiles;
class Chunks; class Chunks;
@ -18,7 +19,7 @@ public:
~World(); ~World();
void write(Level* level); void write(Level* level);
Level* loadLevel(Player* player); Level* loadLevel(Player* player, uint loadDistance, uint chunksPadding);
}; };
#endif /* WORLD_WORLD_H_ */ #endif /* WORLD_WORLD_H_ */