Engine core and assets loading refactor
This commit is contained in:
parent
972db16a0c
commit
0576316282
79
src/AssetsLoader.cpp
Normal file
79
src/AssetsLoader.cpp
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
#include "AssetsLoader.h"
|
||||||
|
#include "Assets.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
AssetsLoader::AssetsLoader(Assets* assets) : assets(assets) {
|
||||||
|
}
|
||||||
|
|
||||||
|
void AssetsLoader::addLoader(int tag, aloader_func func) {
|
||||||
|
loaders[tag] = func;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AssetsLoader::add(int tag, const std::string filename, const std::string alias) {
|
||||||
|
entries.push(aloader_entry{ tag, filename, alias });
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AssetsLoader::hasNext() const {
|
||||||
|
return !entries.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AssetsLoader::loadNext() {
|
||||||
|
const aloader_entry& entry = entries.front();
|
||||||
|
std::cout << " loading " << entry.filename << " as " << entry.alias << std::endl;
|
||||||
|
std::cout.flush();
|
||||||
|
auto found = loaders.find(entry.tag);
|
||||||
|
if (found == loaders.end()) {
|
||||||
|
std::cerr << "unknown asset tag " << entry.tag << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
aloader_func loader = found->second;
|
||||||
|
bool status = loader(assets, entry.filename, entry.alias);
|
||||||
|
entries.pop();
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "graphics/Shader.h"
|
||||||
|
#include "graphics/Texture.h"
|
||||||
|
#include "graphics/Font.h"
|
||||||
|
|
||||||
|
bool _load_shader(Assets* assets, const std::string& filename, const std::string& name) {
|
||||||
|
Shader* shader = load_shader(filename + ".glslv", filename + ".glslf");
|
||||||
|
if (shader == nullptr) {
|
||||||
|
std::cerr << "failed to load shader '" << name << "'" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
assets->store(shader, name);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool _load_texture(Assets* assets, const std::string& filename, const 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool _load_font(Assets* assets, const std::string& filename, const std::string& name) {
|
||||||
|
std::vector<Texture*> pages;
|
||||||
|
for (size_t i = 0; i <= 4; i++) {
|
||||||
|
Texture* texture = load_texture(filename + "_" + std::to_string(i) + ".png");
|
||||||
|
if (texture == nullptr) {
|
||||||
|
std::cerr << "failed to load bitmap font '" << name << "' (missing page " << std::to_string(i) << ")" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
pages.push_back(texture);
|
||||||
|
}
|
||||||
|
Font* font = new Font(pages);
|
||||||
|
assets->store(font, name);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AssetsLoader::createDefaults(AssetsLoader& loader) {
|
||||||
|
loader.addLoader(ASSET_SHADER, _load_shader);
|
||||||
|
loader.addLoader(ASSET_TEXTURE, _load_texture);
|
||||||
|
loader.addLoader(ASSET_FONT, _load_font);
|
||||||
|
}
|
||||||
38
src/AssetsLoader.h
Normal file
38
src/AssetsLoader.h
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#ifndef SRC_ASSETS_LOADER_H
|
||||||
|
#define SRC_ASSETS_LOADER_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <functional>
|
||||||
|
#include <map>
|
||||||
|
#include <queue>
|
||||||
|
|
||||||
|
#define ASSET_TEXTURE 1
|
||||||
|
#define ASSET_SHADER 2
|
||||||
|
#define ASSET_FONT 3
|
||||||
|
|
||||||
|
class Assets;
|
||||||
|
|
||||||
|
typedef std::function<bool(Assets*, const std::string&, const std::string&)> aloader_func;
|
||||||
|
|
||||||
|
struct aloader_entry {
|
||||||
|
int tag;
|
||||||
|
const std::string filename;
|
||||||
|
const std::string alias;
|
||||||
|
};
|
||||||
|
|
||||||
|
class AssetsLoader {
|
||||||
|
Assets* assets;
|
||||||
|
std::map<int, aloader_func> loaders;
|
||||||
|
std::queue<aloader_entry> entries;
|
||||||
|
public:
|
||||||
|
AssetsLoader(Assets* assets);
|
||||||
|
void addLoader(int tag, aloader_func func);
|
||||||
|
void add(int tag, const std::string filename, const std::string alias);
|
||||||
|
|
||||||
|
bool hasNext() const;
|
||||||
|
bool loadNext();
|
||||||
|
|
||||||
|
static void createDefaults(AssetsLoader& loader);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SRC_ASSETS_LOADER_H
|
||||||
@ -1,70 +1,21 @@
|
|||||||
#include "declarations.h"
|
#include "declarations.h"
|
||||||
|
|
||||||
#include "Assets.h"
|
#include "AssetsLoader.h"
|
||||||
#include "graphics/Shader.h"
|
|
||||||
#include "graphics/Texture.h"
|
|
||||||
#include "graphics/Font.h"
|
|
||||||
#include "window/Window.h"
|
#include "window/Window.h"
|
||||||
|
|
||||||
#include "voxels/Block.h"
|
#include "voxels/Block.h"
|
||||||
|
|
||||||
// Shaders, textures
|
|
||||||
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 '" << name << "'" << std::endl;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
assets->store(shader, name);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool _load_texture(Assets* assets, std::string filename, std::string name){
|
void initialize_assets(AssetsLoader* loader) {
|
||||||
Texture* texture = load_texture(filename);
|
loader->add(ASSET_SHADER, "res/main", "main");
|
||||||
if (texture == nullptr){
|
loader->add(ASSET_SHADER, "res/crosshair", "crosshair");
|
||||||
std::cerr << "failed to load texture '" << name << "'" << std::endl;
|
loader->add(ASSET_SHADER, "res/lines", "lines");
|
||||||
return false;
|
loader->add(ASSET_SHADER, "res/ui", "ui");
|
||||||
}
|
|
||||||
assets->store(texture, name);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool _load_font(Assets* assets, std::string filename, std::string name){
|
loader->add(ASSET_TEXTURE, "res/block.png", "block");
|
||||||
std::vector<Texture*> pages;
|
loader->add(ASSET_TEXTURE, "res/slot.png", "slot");
|
||||||
for (size_t i = 0; i <= 4; i++){
|
|
||||||
Texture* texture = load_texture(filename+"_"+std::to_string(i)+".png");
|
|
||||||
if (texture == nullptr){
|
|
||||||
std::cerr << "failed to load bitmap font '" << name << "' (missing page " << std::to_string(i) << ")" << std::endl;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
pages.push_back(texture);
|
|
||||||
}
|
|
||||||
Font* font = new Font(pages);
|
|
||||||
assets->store(font, name);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
int initialize_assets(Assets* assets) {
|
loader->add(ASSET_FONT, "res/font", "normal");
|
||||||
#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;
|
|
||||||
#define LOAD_FONT(FILENAME, NAME) \
|
|
||||||
if (!_load_font(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_SHADER("res/ui.glslv", "res/ui.glslf", "ui");
|
|
||||||
|
|
||||||
LOAD_TEXTURE("res/block.png", "block");
|
|
||||||
LOAD_TEXTURE("res/slot.png", "slot");
|
|
||||||
|
|
||||||
LOAD_FONT("res/font", "normal");
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// All in-game definitions (blocks, items, etc..)
|
// All in-game definitions (blocks, items, etc..)
|
||||||
|
|||||||
@ -21,9 +21,9 @@
|
|||||||
#define BLOCK_METAL 15
|
#define BLOCK_METAL 15
|
||||||
#define BLOCK_RUST 16
|
#define BLOCK_RUST 16
|
||||||
|
|
||||||
class Assets;
|
class AssetsLoader;
|
||||||
|
|
||||||
int initialize_assets(Assets* assets);
|
void initialize_assets(AssetsLoader* loader);
|
||||||
void setup_definitions();
|
void setup_definitions();
|
||||||
|
|
||||||
#endif // DECLARATIONS_H
|
#endif // DECLARATIONS_H
|
||||||
|
|||||||
@ -20,6 +20,19 @@
|
|||||||
|
|
||||||
|
|
||||||
HudRenderer::HudRenderer() {
|
HudRenderer::HudRenderer() {
|
||||||
|
// float vertices[] = {
|
||||||
|
// // x y
|
||||||
|
// -0.01f,-0.01f,
|
||||||
|
// 0.01f, 0.01f,
|
||||||
|
|
||||||
|
// -0.01f, 0.01f,
|
||||||
|
// 0.01f,-0.01f,
|
||||||
|
// };
|
||||||
|
// int attrs[] = {
|
||||||
|
// 2, 0 //null terminator
|
||||||
|
// };
|
||||||
|
// crosshair = new Mesh(vertices, 4, attrs);
|
||||||
|
|
||||||
batch = new Batch2D(1024);
|
batch = new Batch2D(1024);
|
||||||
uicamera = new Camera(glm::vec3(), Window::height / 1.0f);
|
uicamera = new Camera(glm::vec3(), Window::height / 1.0f);
|
||||||
uicamera->perspective = false;
|
uicamera->perspective = false;
|
||||||
@ -66,6 +79,7 @@ void HudRenderer::draw(Level* level, Assets* assets){
|
|||||||
|
|
||||||
// Chosen block preview
|
// Chosen block preview
|
||||||
Texture* blocks = assets->getTexture("block");
|
Texture* blocks = assets->getTexture("block");
|
||||||
|
Texture* sprite = assets->getTexture("slot");
|
||||||
|
|
||||||
batch->texture(nullptr);
|
batch->texture(nullptr);
|
||||||
batch->color = vec4(1.0f);
|
batch->color = vec4(1.0f);
|
||||||
@ -76,6 +90,13 @@ void HudRenderer::draw(Level* level, Assets* assets){
|
|||||||
batch->line(Window::width/2-5, Window::height/2-5, Window::width/2+5, Window::height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
|
batch->line(Window::width/2-5, Window::height/2-5, Window::width/2+5, Window::height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
|
||||||
batch->line(Window::width/2+5, Window::height/2-5, Window::width/2-5, Window::height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
|
batch->line(Window::width/2+5, Window::height/2-5, Window::width/2-5, Window::height/2+5, 0.9f, 0.9f, 0.9f, 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// batch->texture(sprite);
|
||||||
|
// batch->sprite(Window::width/2-32, uicamera->fov - 80, 64, 64, 16, 0, vec4(1.0f));
|
||||||
|
// batch->rect(Window::width/2-128-4, Window::height-80-4, 256+8, 64+8,
|
||||||
|
// 0.85f, 0.85f, 0.85f, 0.95f, 0.95f, 0.95f,
|
||||||
|
// 0.55f, 0.55f, 0.55f,
|
||||||
|
// 0.45f, 0.45f, 0.45f, 0.7f, 0.7f, 0.7f, 2);
|
||||||
batch->rect(Window::width/2-128-4, Window::height-80-4, 256+8, 64+8,
|
batch->rect(Window::width/2-128-4, Window::height-80-4, 256+8, 64+8,
|
||||||
0.95f, 0.95f, 0.95f, 0.85f, 0.85f, 0.85f,
|
0.95f, 0.95f, 0.95f, 0.85f, 0.85f, 0.85f,
|
||||||
0.7f, 0.7f, 0.7f,
|
0.7f, 0.7f, 0.7f,
|
||||||
@ -146,6 +167,7 @@ void HudRenderer::draw(Level* level, Assets* assets){
|
|||||||
for (uint i = 1; i < count; i++) {
|
for (uint i = 1; i < count; i++) {
|
||||||
x = xs + step * ((i-1) % (inv_w / step));
|
x = xs + step * ((i-1) % (inv_w / step));
|
||||||
y = ys + step * ((i-1) / (inv_w / step));
|
y = ys + step * ((i-1) / (inv_w / step));
|
||||||
|
// batch->rect(x-2, y-2, size+4, size+4);
|
||||||
batch->rect(x-2, y-2, size+4, size+4,
|
batch->rect(x-2, y-2, size+4, size+4,
|
||||||
0.45f, 0.45f, 0.45f, 0.55f, 0.55f, 0.55f,
|
0.45f, 0.45f, 0.45f, 0.55f, 0.55f, 0.55f,
|
||||||
0.7f, 0.7f, 0.7f,
|
0.7f, 0.7f, 0.7f,
|
||||||
@ -156,6 +178,13 @@ void HudRenderer::draw(Level* level, Assets* assets){
|
|||||||
0.65f, 0.65f, 0.65f, 0.65f, 0.65f, 0.65f, 2);
|
0.65f, 0.65f, 0.65f, 0.65f, 0.65f, 0.65f, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// batch->color = vec4(0.5f, 0.5f, 0.5f, 1.0f);
|
||||||
|
// for (unsigned i = 1; i < count; i++) {
|
||||||
|
// x = xs + step * ((i-1) % (inv_w / step));
|
||||||
|
// y = ys + step * ((i-1) / (inv_w / step));
|
||||||
|
// batch->rect(x, y, size, size);
|
||||||
|
// }
|
||||||
|
|
||||||
//front
|
//front
|
||||||
batch->texture(blocks);
|
batch->texture(blocks);
|
||||||
for (uint i = 1; i < count; i++) {
|
for (uint i = 1; i < count; i++) {
|
||||||
@ -171,8 +200,10 @@ void HudRenderer::draw(Level* level, Assets* assets){
|
|||||||
if (Events::jclicked(GLFW_MOUSE_BUTTON_LEFT)) {
|
if (Events::jclicked(GLFW_MOUSE_BUTTON_LEFT)) {
|
||||||
player->choosenBlock = i;
|
player->choosenBlock = i;
|
||||||
}
|
}
|
||||||
|
// size = 50;
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
|
// size = 48;
|
||||||
tint = vec4(1.0f);
|
tint = vec4(1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,4 +214,15 @@ void HudRenderer::draw(Level* level, Assets* assets){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// batch->render();
|
||||||
|
|
||||||
|
if (Events::_cursor_locked && !level->player->debug){
|
||||||
|
// Shader* crosshairShader = assets->getShader("crosshair");
|
||||||
|
// crosshairShader->use();
|
||||||
|
// crosshairShader->uniform1f("u_ar", (float)Window::height / (float)Window::width);
|
||||||
|
// crosshairShader->uniform1f("u_scale", 1.0f / ((float)Window::height / 1000.0f));
|
||||||
|
// glLineWidth(2.0f);
|
||||||
|
// crosshair->draw(GL_LINES);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
// GLM
|
// GLM
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
@ -18,140 +19,124 @@
|
|||||||
|
|
||||||
using namespace glm;
|
using namespace glm;
|
||||||
|
|
||||||
#include "graphics/Shader.h"
|
|
||||||
#include "graphics/Texture.h"
|
|
||||||
#include "graphics/Mesh.h"
|
|
||||||
#include "graphics/VoxelRenderer.h"
|
|
||||||
#include "graphics/LineBatch.h"
|
|
||||||
#include "graphics/Batch2D.h"
|
|
||||||
#include "graphics/Framebuffer.h"
|
|
||||||
#include "window/Window.h"
|
#include "window/Window.h"
|
||||||
#include "window/Events.h"
|
#include "window/Events.h"
|
||||||
#include "window/Camera.h"
|
#include "window/Camera.h"
|
||||||
#include "loaders/png_loading.h"
|
#include "audio/Audio.h"
|
||||||
#include "voxels/voxel.h"
|
|
||||||
#include "voxels/Chunk.h"
|
#include "voxels/Chunk.h"
|
||||||
#include "voxels/Chunks.h"
|
#include "voxels/Chunks.h"
|
||||||
#include "voxels/Block.h"
|
|
||||||
#include "voxels/WorldGenerator.h"
|
|
||||||
#include "voxels/ChunksController.h"
|
#include "voxels/ChunksController.h"
|
||||||
#include "files/files.h"
|
#include "voxels/ChunksLoader.h"
|
||||||
#include "files/WorldFiles.h"
|
|
||||||
#include "lighting/LightSolver.h"
|
|
||||||
#include "lighting/Lightmap.h"
|
|
||||||
#include "lighting/Lighting.h"
|
|
||||||
#include "physics/Hitbox.h"
|
|
||||||
#include "physics/PhysicsSolver.h"
|
|
||||||
#include "world/World.h"
|
|
||||||
#include "world/Level.h"
|
|
||||||
|
|
||||||
#include "audio/Audio.h"
|
|
||||||
#include "audio/audioutil.h"
|
|
||||||
#include "Assets.h"
|
|
||||||
#include "objects/Player.h"
|
#include "objects/Player.h"
|
||||||
|
#include "world/Level.h"
|
||||||
|
#include "world/World.h"
|
||||||
#include "declarations.h"
|
#include "declarations.h"
|
||||||
|
#include "Assets.h"
|
||||||
|
#include "AssetsLoader.h"
|
||||||
#include "world_render.h"
|
#include "world_render.h"
|
||||||
#include "hud_render.h"
|
#include "hud_render.h"
|
||||||
#include "player_control.h"
|
|
||||||
|
|
||||||
int WIDTH = 1280;
|
|
||||||
int HEIGHT = 720;
|
|
||||||
|
|
||||||
// Save all world data to files
|
class initialize_error : public std::runtime_error {
|
||||||
void write_world(World* world, Level* level){
|
initialize_error(const std::string& message) : std::runtime_error(message) {}
|
||||||
WorldFiles* wfile = world->wfile;
|
};
|
||||||
Chunks* chunks = level->chunks;
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < chunks->volume; i++){
|
struct EngineSettings {
|
||||||
Chunk* chunk = chunks->chunks[i];
|
int displayWidth;
|
||||||
if (chunk == nullptr || !chunk->isUnsaved())
|
int displayHeight;
|
||||||
continue;
|
const char* title;
|
||||||
wfile->put((const char*)chunk->voxels, chunk->x, chunk->z);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
wfile->write();
|
|
||||||
|
|
||||||
world->wfile->writePlayer(level->player);
|
class Engine {
|
||||||
}
|
Assets* assets;
|
||||||
|
Level* level;
|
||||||
|
|
||||||
void update_level(World* world, Level* level, float delta) {
|
uint64_t frame = 0;
|
||||||
level->playerController->update_controls(delta);
|
float lastTime = 0.0f;
|
||||||
if (Events::_cursor_locked){
|
float delta = 0.0f;
|
||||||
level->playerController->update_interaction();
|
bool occlusion = true;
|
||||||
} else
|
public:
|
||||||
{
|
Engine(const EngineSettings& settings);
|
||||||
level->playerController->selectedBlockId = -1;
|
~Engine();
|
||||||
}
|
|
||||||
|
|
||||||
vec3 position = level->player->hitbox->position;
|
void updateTimers();
|
||||||
level->chunks->setCenter(world->wfile, position.x, position.z);
|
void updateHotkeys();
|
||||||
}
|
void mainloop();
|
||||||
|
};
|
||||||
|
|
||||||
Level* load_level(World* world, Player* player) {
|
Engine::Engine(const EngineSettings& settings) {
|
||||||
Level* level = new Level(world, player, new Chunks(56, 56, 0, 0), new PhysicsSolver(vec3(0, -19.6f, 0)));
|
Window::initialize(settings.displayWidth, settings.displayHeight, settings.title);
|
||||||
world->wfile->readPlayer(player);
|
|
||||||
|
|
||||||
Camera* camera = player->camera;
|
|
||||||
camera->rotation = mat4(1.0f);
|
|
||||||
camera->rotate(player->camY, player->camX, 0);
|
|
||||||
return level;
|
|
||||||
}
|
|
||||||
|
|
||||||
int initialize(Assets*& assets) {
|
|
||||||
Window::initialize(WIDTH, HEIGHT, "VoxelEngine-Cpp v12");
|
|
||||||
Events::initialize();
|
Events::initialize();
|
||||||
|
|
||||||
assets = new Assets();
|
assets = new Assets();
|
||||||
std::cout << "-- loading assets" << std::endl;
|
std::cout << "-- loading assets" << std::endl;
|
||||||
int result = initialize_assets(assets);
|
AssetsLoader loader(assets);
|
||||||
if (result){
|
AssetsLoader::createDefaults(loader);
|
||||||
delete assets;
|
initialize_assets(&loader);
|
||||||
Window::terminate();
|
while (loader.hasNext()) {
|
||||||
return result;
|
if (!loader.loadNext()) {
|
||||||
|
delete assets;
|
||||||
|
Window::terminate();
|
||||||
|
throw std::runtime_error("could not to initialize assets");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
std::cout << "-- loading world" << std::endl;
|
||||||
|
vec3 playerPosition = vec3(0, 64, 0);
|
||||||
|
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);
|
||||||
|
|
||||||
|
std::cout << "-- initializing finished" << std::endl;
|
||||||
|
|
||||||
|
Audio::initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
void mainloop(Level* level, Assets* assets) {
|
void Engine::updateTimers() {
|
||||||
|
frame++;
|
||||||
|
float currentTime = glfwGetTime();
|
||||||
|
delta = currentTime - lastTime;
|
||||||
|
lastTime = currentTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::updateHotkeys() {
|
||||||
|
if (Events::jpressed(GLFW_KEY_ESCAPE)) {
|
||||||
|
Window::setShouldClose(true);
|
||||||
|
}
|
||||||
|
if (Events::jpressed(GLFW_KEY_TAB) || Events::jpressed(GLFW_KEY_E)) {
|
||||||
|
Events::toggleCursor();
|
||||||
|
}
|
||||||
|
if (Events::jpressed(GLFW_KEY_O)) {
|
||||||
|
occlusion = !occlusion;
|
||||||
|
}
|
||||||
|
if (Events::jpressed(GLFW_KEY_F3)) {
|
||||||
|
level->player->debug = !level->player->debug;
|
||||||
|
}
|
||||||
|
if (Events::jpressed(GLFW_KEY_F5)) {
|
||||||
|
for (unsigned i = 0; i < level->chunks->volume; i++) {
|
||||||
|
Chunk* chunk = level->chunks->chunks[i];
|
||||||
|
if (chunk != nullptr && chunk->isReady()) {
|
||||||
|
chunk->setModified(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Engine::mainloop() {
|
||||||
Camera* camera = level->player->camera;
|
Camera* camera = level->player->camera;
|
||||||
std::cout << "-- preparing systems" << std::endl;
|
std::cout << "-- preparing systems" << std::endl;
|
||||||
World* world = level->world;
|
World* world = level->world;
|
||||||
WorldRenderer worldRenderer(level, assets);
|
WorldRenderer worldRenderer(level, assets);
|
||||||
HudRenderer hud;
|
HudRenderer hud;
|
||||||
long frame = 0;
|
lastTime = glfwGetTime();
|
||||||
float lastTime = glfwGetTime();
|
|
||||||
float delta = 0.0f;
|
|
||||||
bool occlusion = true;
|
|
||||||
Window::swapInterval(1);
|
Window::swapInterval(1);
|
||||||
while (!Window::isShouldClose()){
|
while (!Window::isShouldClose()){
|
||||||
frame++;
|
updateTimers();
|
||||||
float currentTime = glfwGetTime();
|
updateHotkeys();
|
||||||
delta = currentTime - lastTime;
|
|
||||||
lastTime = currentTime;
|
|
||||||
int fps = 1 / delta;
|
|
||||||
if (Events::jpressed(GLFW_KEY_ESCAPE)){
|
|
||||||
Window::setShouldClose(true);
|
|
||||||
}
|
|
||||||
if (Events::jpressed(GLFW_KEY_TAB) || Events::jpressed(GLFW_KEY_E)){
|
|
||||||
Events::toggleCursor();
|
|
||||||
}
|
|
||||||
if (Events::jpressed(GLFW_KEY_O)){
|
|
||||||
occlusion = !occlusion;
|
|
||||||
}
|
|
||||||
if (Events::jpressed(GLFW_KEY_F3)){
|
|
||||||
level->player->debug = !level->player->debug;
|
|
||||||
}
|
|
||||||
if (Events::jpressed(GLFW_KEY_F5)){
|
|
||||||
for (unsigned i = 0; i < level->chunks->volume; i++) {
|
|
||||||
Chunk* chunk = level->chunks->chunks[i];
|
|
||||||
if (chunk != nullptr && chunk->isReady()){
|
|
||||||
chunk->setModified(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
update_level(world, level, delta);
|
level->update(delta, Events::_cursor_locked);
|
||||||
int freeLoaders = level->chunksController->countFreeLoaders();
|
int freeLoaders = level->chunksController->countFreeLoaders();
|
||||||
for (int i = 0; i < freeLoaders; i++)
|
for (int i = 0; i < freeLoaders; i++)
|
||||||
level->chunksController->_buildMeshes();
|
level->chunksController->_buildMeshes();
|
||||||
@ -165,7 +150,7 @@ void mainloop(Level* level, Assets* assets) {
|
|||||||
worldRenderer.draw(camera, occlusion);
|
worldRenderer.draw(camera, occlusion);
|
||||||
hud.draw(level, assets);
|
hud.draw(level, assets);
|
||||||
if (level->player->debug) {
|
if (level->player->debug) {
|
||||||
hud.drawDebug(level, assets, fps, occlusion);
|
hud.drawDebug(level, assets, 1 / delta, occlusion);
|
||||||
}
|
}
|
||||||
|
|
||||||
Window::swapBuffers();
|
Window::swapBuffers();
|
||||||
@ -173,28 +158,12 @@ void mainloop(Level* level, Assets* assets) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
Engine::~Engine() {
|
||||||
setup_definitions();
|
|
||||||
|
|
||||||
Assets* assets;
|
|
||||||
int status = initialize(assets);
|
|
||||||
if (status) return status;
|
|
||||||
|
|
||||||
std::cout << "-- loading world" << std::endl;
|
|
||||||
vec3 playerPosition = vec3(0,64,0);
|
|
||||||
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* level = load_level(world, player);
|
|
||||||
|
|
||||||
std::cout << "-- initializing finished" << std::endl;
|
|
||||||
|
|
||||||
Audio::initialize();
|
|
||||||
mainloop(level, assets);
|
|
||||||
Audio::finalize();
|
Audio::finalize();
|
||||||
|
|
||||||
|
World* world = level->world;
|
||||||
std::cout << "-- saving world" << std::endl;
|
std::cout << "-- saving world" << std::endl;
|
||||||
write_world(world, level);
|
world->write(level);
|
||||||
|
|
||||||
delete level;
|
delete level;
|
||||||
delete world;
|
delete world;
|
||||||
@ -203,5 +172,19 @@ int main() {
|
|||||||
delete assets;
|
delete assets;
|
||||||
Events::finalize();
|
Events::finalize();
|
||||||
Window::terminate();
|
Window::terminate();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
setup_definitions();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Engine engine(EngineSettings{ 1280, 720, "VoxelEngine-Cpp v13" });
|
||||||
|
engine.mainloop();
|
||||||
|
}
|
||||||
|
catch (const initialize_error& err) {
|
||||||
|
std::cerr << "could not to initialize engine" << std::endl;
|
||||||
|
std::cerr << err.what() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,10 @@
|
|||||||
#include "Level.h"
|
#include "Level.h"
|
||||||
|
#include "World.h"
|
||||||
#include "../lighting/Lighting.h"
|
#include "../lighting/Lighting.h"
|
||||||
#include "../voxels/Chunks.h"
|
#include "../voxels/Chunks.h"
|
||||||
#include "../voxels/ChunksController.h"
|
#include "../voxels/ChunksController.h"
|
||||||
#include "../player_control.h"
|
#include "../player_control.h"
|
||||||
|
#include "../physics/Hitbox.h"
|
||||||
#include "../physics/PhysicsSolver.h"
|
#include "../physics/PhysicsSolver.h"
|
||||||
#include "../objects/Player.h"
|
#include "../objects/Player.h"
|
||||||
|
|
||||||
@ -24,3 +26,16 @@ Level::~Level(){
|
|||||||
delete chunksController;
|
delete chunksController;
|
||||||
delete playerController;
|
delete playerController;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Level::update(float delta, bool interactions) {
|
||||||
|
playerController->update_controls(delta);
|
||||||
|
if (interactions) {
|
||||||
|
playerController->update_interaction();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
playerController->selectedBlockId = -1;
|
||||||
|
}
|
||||||
|
vec3 position = player->hitbox->position;
|
||||||
|
chunks->setCenter(world->wfile, position.x, position.z);
|
||||||
|
}
|
||||||
|
|||||||
@ -20,6 +20,9 @@ public:
|
|||||||
PlayerController* playerController;
|
PlayerController* playerController;
|
||||||
Level(World* world, Player* player, Chunks* chunks, PhysicsSolver* physics);
|
Level(World* world, Player* player, Chunks* chunks, PhysicsSolver* physics);
|
||||||
~Level();
|
~Level();
|
||||||
|
|
||||||
|
void update(float delta, bool interactions);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* WORLD_LEVEL_H_ */
|
#endif /* WORLD_LEVEL_H_ */
|
||||||
|
|||||||
@ -3,6 +3,10 @@
|
|||||||
#include "../files/WorldFiles.h"
|
#include "../files/WorldFiles.h"
|
||||||
#include "../voxels/Chunk.h"
|
#include "../voxels/Chunk.h"
|
||||||
#include "../voxels/Chunks.h"
|
#include "../voxels/Chunks.h"
|
||||||
|
#include "Level.h"
|
||||||
|
#include "../objects/Player.h"
|
||||||
|
#include "../physics/PhysicsSolver.h"
|
||||||
|
#include "../window/Camera.h"
|
||||||
|
|
||||||
World::World(std::string name, std::string directory, int seed) : name(name), seed(seed) {
|
World::World(std::string name, std::string directory, int seed) : name(name), seed(seed) {
|
||||||
wfile = new WorldFiles(directory, REGION_VOL * (CHUNK_VOL * 2 + 8));
|
wfile = new WorldFiles(directory, REGION_VOL * (CHUNK_VOL * 2 + 8));
|
||||||
@ -11,3 +15,27 @@ World::World(std::string name, std::string directory, int seed) : name(name), se
|
|||||||
World::~World(){
|
World::~World(){
|
||||||
delete wfile;
|
delete wfile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void World::write(Level* level) {
|
||||||
|
Chunks* chunks = level->chunks;
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < chunks->volume; i++) {
|
||||||
|
Chunk* chunk = chunks->chunks[i];
|
||||||
|
if (chunk == nullptr || !chunk->isUnsaved())
|
||||||
|
continue;
|
||||||
|
wfile->put((const char*)chunk->voxels, chunk->x, chunk->z);
|
||||||
|
}
|
||||||
|
|
||||||
|
wfile->write();
|
||||||
|
wfile->writePlayer(level->player);
|
||||||
|
}
|
||||||
|
|
||||||
|
Level* World::loadLevel(Player* player) {
|
||||||
|
Level* level = new Level(this, player, new Chunks(56, 56, 0, 0), new PhysicsSolver(vec3(0, -19.6f, 0)));
|
||||||
|
wfile->readPlayer(player);
|
||||||
|
|
||||||
|
Camera* camera = player->camera;
|
||||||
|
camera->rotation = mat4(1.0f);
|
||||||
|
camera->rotate(player->camY, player->camX, 0);
|
||||||
|
return level;
|
||||||
|
}
|
||||||
@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
class WorldFiles;
|
class WorldFiles;
|
||||||
class Chunks;
|
class Chunks;
|
||||||
|
class Level;
|
||||||
|
class Player;
|
||||||
|
|
||||||
class World {
|
class World {
|
||||||
public:
|
public:
|
||||||
@ -14,6 +16,9 @@ public:
|
|||||||
|
|
||||||
World(std::string name, std::string directory, int seed);
|
World(std::string name, std::string directory, int seed);
|
||||||
~World();
|
~World();
|
||||||
|
|
||||||
|
void write(Level* level);
|
||||||
|
Level* loadLevel(Player* player);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* WORLD_WORLD_H_ */
|
#endif /* WORLD_WORLD_H_ */
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user