'Assets', some refactor

This commit is contained in:
MihailRis
2022-03-03 18:14:34 +03:00
committed by GitHub
parent b7e8ff5bea
commit 2aba3e91db
19 changed files with 350 additions and 213 deletions
+133 -115
View File
@@ -1,3 +1,5 @@
// Install dependencies:
// sudo apt install libgl-dev libglew-dev libglfw3-dev libpng-dev libglm-dev
#include <iostream>
#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;
}