diff --git a/Debug/src/graphics/subdir.mk b/Debug/src/graphics/subdir.mk index 35070f37..995ec1bb 100644 --- a/Debug/src/graphics/subdir.mk +++ b/Debug/src/graphics/subdir.mk @@ -1,28 +1,27 @@ -################################################################################ -# Automatically-generated file. Do not edit! -################################################################################ - # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ ../src/graphics/LineBatch.cpp \ ../src/graphics/Mesh.cpp \ ../src/graphics/Shader.cpp \ ../src/graphics/Texture.cpp \ -../src/graphics/VoxelRenderer.cpp +../src/graphics/VoxelRenderer.cpp \ +../src/graphics/Batch2D.cpp OBJS += \ ./src/graphics/LineBatch.o \ ./src/graphics/Mesh.o \ ./src/graphics/Shader.o \ ./src/graphics/Texture.o \ -./src/graphics/VoxelRenderer.o +./src/graphics/VoxelRenderer.o \ +./src/graphics/Batch2D.o CPP_DEPS += \ ./src/graphics/LineBatch.d \ ./src/graphics/Mesh.d \ ./src/graphics/Shader.d \ ./src/graphics/Texture.d \ -./src/graphics/VoxelRenderer.d +./src/graphics/VoxelRenderer.d \ +./src/graphics/Batch2D.d # Each subdirectory must supply rules for building sources it contributes diff --git a/Debug/src/objects/subdir.mk b/Debug/src/objects/subdir.mk index 4757b8dd..1b72aeb0 100644 --- a/Debug/src/objects/subdir.mk +++ b/Debug/src/objects/subdir.mk @@ -1,7 +1,3 @@ -################################################################################ -# Automatically-generated file. Do not edit! -################################################################################ - # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ ../src/objects/Player.cpp diff --git a/Debug/src/subdir.mk b/Debug/src/subdir.mk index e4dbf713..e6fcb3f3 100644 --- a/Debug/src/subdir.mk +++ b/Debug/src/subdir.mk @@ -4,13 +4,16 @@ # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ -../src/voxel_engine.cpp +../src/voxel_engine.cpp \ +../src/Assets.cpp OBJS += \ -./src/voxel_engine.o +./src/voxel_engine.o \ +./src/Assets.o CPP_DEPS += \ -./src/voxel_engine.d +./src/voxel_engine.d \ +./src/Assets.d # Each subdirectory must supply rules for building sources it contributes diff --git a/Debug_win/sources.mk b/Debug_win/sources.mk index bb746f07..26037f00 100644 --- a/Debug_win/sources.mk +++ b/Debug_win/sources.mk @@ -22,13 +22,14 @@ CPP_DEPS := C_DEPS := # Every subdirectory with source files must be described here -SUBDIRS := \ -src \ -src/files \ -src/graphics \ -src/lighting \ -src/loaders \ -src/physics \ -src/voxels \ -src/window \ +SUBDIRS := \ +src \ +src/files \ +src/graphics \ +src/lighting \ +src/loaders \ +src/physics \ +src/voxels \ +src/objects \ +src/window \ diff --git a/Debug_win/src/graphics/subdir.mk b/Debug_win/src/graphics/subdir.mk index 2e37caee..2b3a0102 100644 --- a/Debug_win/src/graphics/subdir.mk +++ b/Debug_win/src/graphics/subdir.mk @@ -1,28 +1,27 @@ -################################################################################ -# Automatically-generated file. Do not edit! -################################################################################ - # Add inputs and outputs from these tool invocations to the build variables -CPP_SRCS += \ -../src/graphics/LineBatch.cpp \ -../src/graphics/Mesh.cpp \ -../src/graphics/Shader.cpp \ -../src/graphics/Texture.cpp \ -../src/graphics/VoxelRenderer.cpp +CPP_SRCS += \ +../src/graphics/LineBatch.cpp \ +../src/graphics/Mesh.cpp \ +../src/graphics/Shader.cpp \ +../src/graphics/Texture.cpp \ +../src/graphics/VoxelRenderer.cpp \ +../src/graphics/Batch2D.cpp -OBJS += \ -./src/graphics/LineBatch.o \ -./src/graphics/Mesh.o \ -./src/graphics/Shader.o \ -./src/graphics/Texture.o \ -./src/graphics/VoxelRenderer.o +OBJS += \ +./src/graphics/LineBatch.o \ +./src/graphics/Mesh.o \ +./src/graphics/Shader.o \ +./src/graphics/Texture.o \ +./src/graphics/VoxelRenderer.o \ +./src/graphics/Batch2D.o -CPP_DEPS += \ -./src/graphics/LineBatch.d \ -./src/graphics/Mesh.d \ -./src/graphics/Shader.d \ -./src/graphics/Texture.d \ -./src/graphics/VoxelRenderer.d +CPP_DEPS += \ +./src/graphics/LineBatch.d \ +./src/graphics/Mesh.d \ +./src/graphics/Shader.d \ +./src/graphics/Texture.d \ +./src/graphics/VoxelRenderer.d \ +./src/graphics/Batch2D.d # Each subdirectory must supply rules for building sources it contributes diff --git a/Debug_win/src/objects/subdir.mk b/Debug_win/src/objects/subdir.mk new file mode 100644 index 00000000..ce3e7c3d --- /dev/null +++ b/Debug_win/src/objects/subdir.mk @@ -0,0 +1,20 @@ +# Add inputs and outputs from these tool invocations to the build variables +CPP_SRCS += \ +../src/objects/Player.cpp + +OBJS += \ +./src/objects/Player.o + +CPP_DEPS += \ +./src/objects/Player.d + + +# Each subdirectory must supply rules for building sources it contributes +src/objects/%.o: ../src/objects/%.cpp src/objects/subdir.mk + @echo 'Building file: $<' + @echo 'Invoking: GCC C++ Compiler' + g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$@" -o "$@" "$<" + @echo 'Finished building: $<' + @echo ' ' + + diff --git a/Debug_win/src/subdir.mk b/Debug_win/src/subdir.mk index d41d8884..a0330fab 100644 --- a/Debug_win/src/subdir.mk +++ b/Debug_win/src/subdir.mk @@ -1,16 +1,15 @@ -################################################################################ -# Automatically-generated file. Do not edit! -################################################################################ - # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ -../src/voxel_engine.cpp +../src/voxel_engine.cpp \ +../src/Assets.cpp OBJS += \ -./src/voxel_engine.o +./src/voxel_engine.o \ +./src/Assets.o CPP_DEPS += \ -./src/voxel_engine.d +./src/voxel_engine.d \ +./src/Assets.d # Each subdirectory must supply rules for building sources it contributes diff --git a/res/ascii.png b/res/ascii.png new file mode 100644 index 00000000..3278f04c Binary files /dev/null and b/res/ascii.png differ diff --git a/res/block (copy).png b/res/block (copy).png new file mode 100644 index 00000000..5e2512c7 Binary files /dev/null and b/res/block (copy).png differ diff --git a/src/Assets.cpp b/src/Assets.cpp new file mode 100644 index 00000000..fbaaa21e --- /dev/null +++ b/src/Assets.cpp @@ -0,0 +1,31 @@ +#include "Assets.h" + +#include "graphics/Texture.h" +#include "graphics/Shader.h" + +Assets::~Assets() { + for (auto& iter : shaders){ + delete iter.second; + } + + for (auto& iter : textures){ + delete iter.second; + } +} + +Texture* Assets::getTexture(std::string name){ + return textures[name]; +} + +void Assets::store(Texture* texture, std::string name){ + textures[name] = texture; +} + + +Shader* Assets::getShader(std::string name){ + return shaders[name]; +} + +void Assets::store(Shader* shader, std::string name){ + shaders[name] = shader; +} diff --git a/src/Assets.h b/src/Assets.h new file mode 100644 index 00000000..daca7d19 --- /dev/null +++ b/src/Assets.h @@ -0,0 +1,22 @@ +#ifndef SRC_ASSETS_H_ +#define SRC_ASSETS_H_ + +#include +#include + +class Texture; +class Shader; + +class Assets { + std::unordered_map textures; + std::unordered_map shaders; +public: + ~Assets(); + Texture* getTexture(std::string name); + void store(Texture* texture, std::string name); + + Shader* getShader(std::string name); + void store(Shader* shader, std::string name); +}; + +#endif /* SRC_ASSETS_H_ */ diff --git a/src/declarations.h b/src/declarations.h index 412d2afb..ae6edc38 100644 --- a/src/declarations.h +++ b/src/declarations.h @@ -1,8 +1,8 @@ -#ifndef DECLARATIONS_CPP -#define DECLARATIONS_CPP +#ifndef DECLARATIONS_H +#define DECLARATIONS_H #include - +#include "Assets.h" #include "graphics/Shader.h" #include "graphics/Texture.h" #include "window/Window.h" @@ -10,49 +10,42 @@ #include "voxels/Block.h" -Shader *shader, *linesShader, *crosshairShader; -Texture *texture; - - // Shaders, textures, renderers -int initialize_assets() { - shader = load_shader("res/main.glslv", "res/main.glslf"); + +bool _load_shader(Assets* assets, std::string vertex_file, std::string fragment_file, std::string name){ + Shader* shader = load_shader(vertex_file, fragment_file); if (shader == nullptr){ - std::cerr << "failed to load shader" << std::endl; - Window::terminate(); - return 1; + std::cerr << "failed to load shader '" << name << "'" << std::endl; + return false; } - - crosshairShader = load_shader("res/crosshair.glslv", "res/crosshair.glslf"); - if (crosshairShader == nullptr){ - std::cerr << "failed to load crosshair shader" << std::endl; - Window::terminate(); - return 1; - } - - linesShader = load_shader("res/lines.glslv", "res/lines.glslf"); - if (linesShader == nullptr){ - std::cerr << "failed to load lines shader" << std::endl; - Window::terminate(); - return 1; - } - - texture = load_texture("res/block.png"); - if (texture == nullptr){ - std::cerr << "failed to load texture" << std::endl; - delete shader; - Window::terminate(); - return 1; - } - return 0; + assets->store(shader, name); + return true; } -// Deleting GL objects like shaders, textures -void finalize_assets(){ - delete shader; - delete texture; - delete crosshairShader; - delete linesShader; +bool _load_texture(Assets* assets, std::string filename, std::string name){ + Texture* texture = load_texture(filename); + if (texture == nullptr){ + std::cerr << "failed to load texture '" << name << "'" << std::endl; + return false; + } + assets->store(texture, name); + return true; +} + +int initialize_assets(Assets* assets) { +#define LOAD_SHADER(VERTEX, FRAGMENT, NAME) \ + if (!_load_shader(assets, VERTEX, FRAGMENT, NAME))\ + return 1; +#define LOAD_TEXTURE(FILENAME, NAME) \ + if (!_load_texture(assets, FILENAME, NAME))\ + return 1; + + LOAD_SHADER("res/main.glslv", "res/main.glslf", "main"); + LOAD_SHADER("res/crosshair.glslv", "res/crosshair.glslf", "crosshair"); + LOAD_SHADER("res/lines.glslv", "res/lines.glslf", "lines"); + + LOAD_TEXTURE("res/block.png", "block"); + return 0; } @@ -115,5 +108,5 @@ void setup_definitions() { block->obstacle = false; Block::blocks[block->id] = block; } -#endif // DECLARATIONS_CPP +#endif // DECLARATIONS_H diff --git a/src/graphics/Batch2D.cpp b/src/graphics/Batch2D.cpp new file mode 100644 index 00000000..e6b43770 --- /dev/null +++ b/src/graphics/Batch2D.cpp @@ -0,0 +1,18 @@ +#include "Batch2D.h" +#include "Mesh.h" + +Batch2D::Batch2D(size_t capacity) : capacity(capacity), + offset(0), + color(1.0f, 1.0f, 1.0f, 1.0f){ + const int attrs[] = { + 2, 2, 4, 0 //null terminator + }; + + buffer = new float[capacity]; + mesh = new Mesh(nullptr, 0, attrs); +} + +Batch2D::~Batch2D(){ + delete buffer; + delete mesh; +} diff --git a/src/graphics/Batch2D.h b/src/graphics/Batch2D.h new file mode 100644 index 00000000..7e756eda --- /dev/null +++ b/src/graphics/Batch2D.h @@ -0,0 +1,27 @@ +#ifndef SRC_GRAPHICS_BATCH2D_H_ +#define SRC_GRAPHICS_BATCH2D_H_ + +#include +#include + +class Mesh; + +class Batch2D { + float* buffer; + size_t capacity; + size_t offset; + glm::vec4 color; + Mesh* mesh; + + void vertex(float x, float y, + float u, float v, + float r, float g, float b, float a); +public: + Batch2D(size_t capacity); + ~Batch2D(); + + void rect(float x, float y, float w, float h); + void render(); +}; + +#endif /* SRC_GRAPHICS_BATCH2D_H_ */ diff --git a/src/graphics/Mesh.cpp b/src/graphics/Mesh.cpp index 678115f0..ef99560f 100644 --- a/src/graphics/Mesh.cpp +++ b/src/graphics/Mesh.cpp @@ -12,7 +12,12 @@ Mesh::Mesh(const float* buffer, size_t vertices, const int* attrs) : vertices(ve glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertexSize * vertices, buffer, GL_STATIC_DRAW); + if (buffer){ + glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertexSize * vertices, buffer, GL_STATIC_DRAW); + } else { + glBufferData(GL_ARRAY_BUFFER, 0, {}, GL_STATIC_DRAW); + } + // attributes int offset = 0; diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index 5acec728..2633c2ca 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -5,6 +5,8 @@ Player::Player(glm::vec3 position, float speed, Camera* camera) : speed(speed), - camera(camera){ + camera(camera), + choosenBlock(1), + camX(0.0f), camY(0.0f){ hitbox = new Hitbox(position, vec3(0.2f,0.9f,0.2f)); } diff --git a/src/objects/Player.h b/src/objects/Player.h index 335cb5cd..66174cb6 100644 --- a/src/objects/Player.h +++ b/src/objects/Player.h @@ -11,6 +11,7 @@ public: float speed; Camera* camera; Hitbox* hitbox; + int choosenBlock; float camX, camY; Player(glm::vec3 position, float speed, Camera* camera); ~Player(); diff --git a/src/voxel_engine.cpp b/src/voxel_engine.cpp index 53b8b785..fa117aff 100644 --- a/src/voxel_engine.cpp +++ b/src/voxel_engine.cpp @@ -1,3 +1,5 @@ +// Install dependencies: +// sudo apt install libgl-dev libglew-dev libglfw3-dev libpng-dev libglm-dev #include #define GLEW_STATIC @@ -37,22 +39,15 @@ using namespace glm; #include "physics/Hitbox.h" #include "physics/PhysicsSolver.h" +#include "Assets.h" #include "objects/Player.h" #include "declarations.h" #include "world_render.h" -int WIDTH = 1280; -int HEIGHT = 720; - -Chunks* chunks; -WorldFiles* wfile; - -bool occlusion = false; - // Save all world data to files -void write_world(){ +void write_world(WorldFiles* wfile, Chunks* chunks){ for (unsigned int i = 0; i < chunks->volume; i++){ Chunk* chunk = chunks->chunks[i]; if (chunk == nullptr) @@ -64,27 +59,134 @@ void write_world(){ } // Deleting world data from memory -void close_world(){ +void close_world(WorldFiles* wfile, Chunks* chunks){ delete chunks; delete wfile; } +void update_controls(PhysicsSolver* physics, + Chunks* chunks, + Player* player, + float delta){ + // Controls + Camera* camera = player->camera; + Hitbox* hitbox = player->hitbox; + bool sprint = Events::pressed(GLFW_KEY_LEFT_CONTROL); + bool shift = Events::pressed(GLFW_KEY_LEFT_SHIFT) && hitbox->grounded && !sprint; + + float speed = player->speed; + int substeps = (int)(delta * 1000); + substeps = (substeps <= 0 ? 1 : (substeps > 100 ? 100 : substeps)); + physics->step(chunks, hitbox, delta, substeps, shift); + camera->position.x = hitbox->position.x; + camera->position.y = hitbox->position.y + 0.5f; + camera->position.z = hitbox->position.z; + + float dt = min(1.0f, delta * 16); + if (shift){ + speed *= 0.25f; + camera->position.y -= 0.2f; + camera->zoom = 0.9f * dt + camera->zoom * (1.0f - dt); + } else if (sprint){ + speed *= 1.5f; + camera->zoom = 1.1f * dt + camera->zoom * (1.0f - dt); + } else { + camera->zoom = dt + camera->zoom * (1.0f - dt); + } + if (Events::pressed(GLFW_KEY_SPACE) && hitbox->grounded){ + hitbox->velocity.y = 6.0f; + } + + vec3 dir(0,0,0); + if (Events::pressed(GLFW_KEY_W)){ + dir.x += camera->dir.x; + dir.z += camera->dir.z; + } + if (Events::pressed(GLFW_KEY_S)){ + dir.x -= camera->dir.x; + dir.z -= camera->dir.z; + } + if (Events::pressed(GLFW_KEY_D)){ + dir.x += camera->right.x; + dir.z += camera->right.z; + } + if (Events::pressed(GLFW_KEY_A)){ + dir.x -= camera->right.x; + dir.z -= camera->right.z; + } + if (length(dir) > 0.0f) + dir = normalize(dir); + hitbox->velocity.x = dir.x * speed; + hitbox->velocity.z = dir.z * speed; + + if (Events::_cursor_locked){ + player->camY += -Events::deltaY / Window::height * 2; + player->camX += -Events::deltaX / Window::height * 2; + + if (player->camY < -radians(89.0f)){ + player->camY = -radians(89.0f); + } + if (player->camY > radians(89.0f)){ + player->camY = radians(89.0f); + } + + camera->rotation = mat4(1.0f); + camera->rotate(player->camY, player->camX, 0); + } +} + +void update_interaction(Chunks* chunks, PhysicsSolver* physics, Player* player, Lighting* lighting){ + Camera* camera = player->camera; + { + vec3 end; + vec3 norm; + vec3 iend; + voxel* vox = chunks->rayCast(camera->position, camera->front, 10.0f, end, norm, iend); + if (vox != nullptr){ + lineBatch->box(iend.x+0.5f, iend.y+0.5f, iend.z+0.5f, 1.005f,1.005f,1.005f, 0,0,0,0.5f); + + if (Events::jclicked(GLFW_MOUSE_BUTTON_1)){ + int x = (int)iend.x; + int y = (int)iend.y; + int z = (int)iend.z; + chunks->set(x,y,z, 0); + lighting->onBlockSet(x,y,z,0); + } + if (Events::jclicked(GLFW_MOUSE_BUTTON_2)){ + 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)){ + chunks->set(x, y, z, player->choosenBlock); + lighting->onBlockSet(x,y,z, player->choosenBlock); + } + } + } + } +} + +int WIDTH = 1280; +int HEIGHT = 720; + int main() { setup_definitions(); Window::initialize(WIDTH, HEIGHT, "Window 2.0"); Events::initialize(); - int result = initialize_assets(); + std::cout << "-- loading assets" << std::endl; + Assets* assets = new Assets(); + int result = initialize_assets(assets); if (result){ + delete assets; Window::terminate(); return result; } + std::cout << "-- loading world" << std::endl; - Camera* camera = new Camera(vec3(-320,255,32), radians(90.0f)); - - wfile = new WorldFiles("world/", REGION_VOL * (CHUNK_VOL * 2 + 8)); - chunks = new Chunks(34,1,34, 0,0,0); + 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); Player* player = new Player(vec3(camera->position), 5.0f, camera); @@ -92,9 +194,10 @@ int main() { camera->rotation = mat4(1.0f); camera->rotate(player->camY, player->camX, 0); + std::cout << "-- preparing systems" << std::endl; + VoxelRenderer renderer(1024*1024); PhysicsSolver physics(vec3(0,-16.0f,0)); - Lighting lighting(chunks); init_renderer(); @@ -104,20 +207,20 @@ int main() { float lastTime = glfwGetTime(); float delta = 0.0f; - int choosenBlock = 1; long frame = 0; + bool occlusion = false; + glfwSwapInterval(1); + std::cout << "-- initializing finished" << std::endl; + while (!Window::isShouldClose()){ frame++; float currentTime = glfwGetTime(); delta = currentTime - lastTime; lastTime = currentTime; - if (frame % 240 == 0) - std::cout << 1.0/delta << std::endl; - if (Events::jpressed(GLFW_KEY_O)){ occlusion = !occlusion; } @@ -131,117 +234,32 @@ int main() { for (int i = 1; i < 10; i++){ if (Events::jpressed(GLFW_KEY_0+i)){ - choosenBlock = i; + player->choosenBlock = i; } } - // Controls - Hitbox* hitbox = player->hitbox; - bool sprint = Events::pressed(GLFW_KEY_LEFT_CONTROL); - bool shift = Events::pressed(GLFW_KEY_LEFT_SHIFT) && hitbox->grounded && !sprint; - - float speed = player->speed; - int substeps = (int)(delta * 1000); - substeps = (substeps <= 0 ? 1 : (substeps > 100 ? 100 : substeps)); - physics.step(chunks, hitbox, delta, substeps, shift); - camera->position.x = hitbox->position.x; - camera->position.y = hitbox->position.y + 0.5f; - camera->position.z = hitbox->position.z; - - float dt = min(1.0f, delta * 16); - if (shift){ - speed *= 0.25f; - camera->position.y -= 0.2f; - camera->zoom = 0.9f * dt + camera->zoom * (1.0f - dt); - } else if (sprint){ - speed *= 1.5f; - camera->zoom = 1.1f * dt + camera->zoom * (1.0f - dt); - } else { - camera->zoom = dt + camera->zoom * (1.0f - dt); - } - if (Events::pressed(GLFW_KEY_SPACE) && hitbox->grounded){ - hitbox->velocity.y = 6.0f; - } - - vec3 dir(0,0,0); - if (Events::pressed(GLFW_KEY_W)){ - dir.x += camera->dir.x; - dir.z += camera->dir.z; - } - if (Events::pressed(GLFW_KEY_S)){ - dir.x -= camera->dir.x; - dir.z -= camera->dir.z; - } - if (Events::pressed(GLFW_KEY_D)){ - dir.x += camera->right.x; - dir.z += camera->right.z; - } - if (Events::pressed(GLFW_KEY_A)){ - dir.x -= camera->right.x; - dir.z -= camera->right.z; - } - if (length(dir) > 0.0f) - dir = normalize(dir); - hitbox->velocity.x = dir.x * speed; - hitbox->velocity.z = dir.z * speed; + update_controls(&physics, chunks, player, delta); + update_interaction(chunks, &physics, player, &lighting); chunks->setCenter(wfile, camera->position.x,0,camera->position.z); chunksController._buildMeshes(&renderer, frame); chunksController.loadVisible(wfile); - if (Events::_cursor_locked){ - player->camY += -Events::deltaY / Window::height * 2; - player->camX += -Events::deltaX / Window::height * 2; - - if (player->camY < -radians(89.0f)){ - player->camY = -radians(89.0f); - } - if (player->camY > radians(89.0f)){ - player->camY = radians(89.0f); - } - - camera->rotation = mat4(1.0f); - camera->rotate(player->camY, player->camX, 0); - } - - { - vec3 end; - vec3 norm; - vec3 iend; - voxel* vox = chunks->rayCast(camera->position, camera->front, 10.0f, end, norm, iend); - if (vox != nullptr){ - lineBatch->box(iend.x+0.5f, iend.y+0.5f, iend.z+0.5f, 1.005f,1.005f,1.005f, 0,0,0,0.5f); - - if (Events::jclicked(GLFW_MOUSE_BUTTON_1)){ - int x = (int)iend.x; - int y = (int)iend.y; - int z = (int)iend.z; - chunks->set(x,y,z, 0); - lighting.onBlockSet(x,y,z,0); - } - if (Events::jclicked(GLFW_MOUSE_BUTTON_2)){ - 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, hitbox)){ - chunks->set(x, y, z, choosenBlock); - lighting.onBlockSet(x,y,z, choosenBlock); - } - } - } - } - draw_world(camera, shader, texture, crosshairShader, linesShader, chunks, occlusion); + draw_world(camera, assets, chunks, occlusion); Window::swapBuffers(); Events::pullEvents(); } + std::cout << "-- saving world" << std::endl; wfile->writePlayer(player); - write_world(); - close_world(); + write_world(wfile, chunks); + close_world(wfile, chunks); + std::cout << "-- shutting down" << std::endl; + + delete assets; finalize_renderer(); - finalize_assets(); Window::terminate(); return 0; } diff --git a/src/world_render.h b/src/world_render.h index 630736da..ef8344ab 100644 --- a/src/world_render.h +++ b/src/world_render.h @@ -96,8 +96,7 @@ bool chunks_comparator(size_t i, size_t j) { } -void draw_world(Camera* camera, Shader* shader, Texture* texture, - Shader* crosshairShader, Shader* linesShader, +void draw_world(Camera* camera, Assets* assets, Chunks* chunks, bool occlusion){ glClearColor(0.7f,0.71f,0.73f,1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); @@ -105,6 +104,10 @@ void draw_world(Camera* camera, Shader* shader, Texture* texture, _chunks = chunks; // Draw VAO + Texture* texture = assets->getTexture("block"); + Shader* shader = assets->getShader("main"); + Shader* crosshairShader = assets->getShader("crosshair"); + Shader* linesShader = assets->getShader("lines"); shader->use(); shader->uniformMatrix("u_proj", camera->getProjection()); shader->uniformMatrix("u_view", camera->getView());