1
0
Fork 0
Univerxel/src/main.cpp

158 lines
5.6 KiB
C++
Raw Normal View History

2020-07-06 19:18:29 +00:00
/**
* \file main.cpp
* \brief Univerxel game
* \author Maelys Bois
* \version 0.0.1
*
* Univerxel main program.
*/
#include <stdexcept>
#include <iostream>
#include <chrono>
#include <thread>
#include "render/window.hpp"
#include "render/UI.hpp"
#include "control/InputMap.hpp"
#include "control/Camera.hpp"
2020-07-10 17:49:16 +00:00
#include "render/Renderer.hpp"
#include "world/World.hpp"
#include "contouring/FlatBox.hpp"
2020-07-06 19:18:29 +00:00
#include "data/state.h"
/// Entry point
int main(int, char *[]){
options options;
state state;
reports reports;
GLFWwindow *window = createWindow(options.samples);
if(window == NULL)
return 1;
2020-07-12 13:46:51 +00:00
glClearColor(options.renderer.clear_color.x, options.renderer.clear_color.y, options.renderer.clear_color.z, options.renderer.clear_color.w);
2020-07-06 19:18:29 +00:00
glfwSwapInterval(options.target_fps < MIN_FPS);
InputMap inputs(window);
2020-07-10 19:37:49 +00:00
Camera camera(window, inputs, options.camera);
2020-07-06 19:18:29 +00:00
2020-07-10 17:49:16 +00:00
Renderer *renderer = new Renderer(options.renderer);
2020-07-06 19:18:29 +00:00
UI::setup(window);
GLuint aimTexture = Program::loadTexture("ui/Aim", false);
2020-07-10 17:49:16 +00:00
renderer->LightInvDir = glm::vec3(-0.5f, 2, -2);
World world = World(options.world);
2020-07-06 19:18:29 +00:00
do {
const double startTime = glfwGetTime();
{ // Update
const double partTime = glfwGetTime();
inputs.toggle(state.capture_mouse, Input::Mouse);
inputs.toggle(options.show_debug_menu, Input::Debug);
camera.update(state.capture_mouse, !UI::isFocus());
2020-07-10 17:49:16 +00:00
renderer->lookFrom(camera);
2020-07-10 19:37:49 +00:00
state.position = camera.getPosition();
2020-07-10 17:49:16 +00:00
/*look_at = world.raycast(camera.getViewRay() / scale);
2020-07-06 19:18:29 +00:00
if (capture_mouse && look_at.has_value()) {
if (inputs.isPressing(Mouse::Left))
world.setCube(look_at.value().first, 2, Voxel{0, 0});
else if (inputs.isPressing(Mouse::Right))
world.setCube(look_at.value().first, 2, Voxel{2, 1});
2020-07-10 17:49:16 +00:00
}*/
2020-07-10 19:37:49 +00:00
world.update(state.position / options.voxel_size, reports.world);
2020-07-06 19:18:29 +00:00
inputs.saveKeys();
reports.main.update.push((glfwGetTime() - partTime) * 1000);
}
{
const auto actions = UI::draw(options, state, reports, aimTexture);
if (actions && UI::Actions::FPS) {
glfwSwapInterval(options.target_fps < MIN_FPS);
}
if (actions && UI::Actions::FullScreen) {
// MAYBE: real fullscreen
if(options.fullscreen) {
GLFWmonitor *monitor = glfwGetPrimaryMonitor();
const GLFWvidmode *mode = glfwGetVideoMode(monitor);
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
} else {
glfwSetWindowMonitor(window, NULL, 0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT, GLFW_DONT_CARE);
}
}
if(actions && UI::Actions::ClearColor) {
2020-07-12 13:46:51 +00:00
renderer->FogColor = glm::vec3(options.renderer.clear_color.x, options.renderer.clear_color.y, options.renderer.clear_color.z);
glClearColor(options.renderer.clear_color.x, options.renderer.clear_color.y, options.renderer.clear_color.z, options.renderer.clear_color.w);
2020-07-06 19:18:29 +00:00
}
2020-07-10 17:49:16 +00:00
if(actions && UI::Actions::RendererSharders) {
renderer->reloadShaders(options.renderer.main);
}
if(actions && UI::Actions::RendererTextures) {
renderer->reloadTextures(options.renderer.textures, options.renderer.mipMapLOD);
}
if(actions && UI::Actions::World) {
world.setOptions(options.world);
}
2020-07-10 19:37:49 +00:00
if(actions && UI::Actions::Camera) {
camera.setOptions(options.camera);
}
2020-07-06 19:18:29 +00:00
}
{ // Rendering
const double partTime = glfwGetTime();
2020-07-10 17:49:16 +00:00
auto pass = renderer->getPass();
pass.start();
2020-07-06 19:18:29 +00:00
2020-07-10 17:49:16 +00:00
std::vector<std::pair<glm::mat4, Buffer *>> models;
2020-07-10 19:37:49 +00:00
if(options.culling) {
world.getModels(models, options.voxel_size, camera.getFrustum());
} else {
world.getModels(models, options.voxel_size);
}
2020-07-06 19:18:29 +00:00
reports.main.models_count = 0;
2020-07-10 17:49:16 +00:00
reports.main.tris_count = 0;
for (auto [model, buffer] : models) {
reports.main.models_count++;
reports.main.tris_count += buffer->draw(pass.setup(model));
}
2020-07-06 19:18:29 +00:00
UI::render();
reports.main.render.push((glfwGetTime() - partTime) * 1000);
}
{ // Swap buffers
const double partTime = glfwGetTime();
glfwSwapBuffers(window);
glfwPollEvents();
reports.main.swap.push((glfwGetTime() - partTime) * 1000);
}
{ // Wait target fps
const double partTime = glfwGetTime();
if(options.target_fps >= MIN_FPS && options.target_fps <= MAX_FPS) {
while (glfwGetTime() < startTime + 1.0 / options.target_fps) {
std::this_thread::sleep_for(std::chrono::microseconds(100));
}
}
reports.main.wait.push((glfwGetTime() - partTime) * 1000);
}
reports.main.fps.push(1.0 / (glfwGetTime() - startTime));
} // Check if the ESC key was pressed or the window was closed
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
UI::unload();
2020-07-10 17:49:16 +00:00
delete renderer;
2020-07-06 19:18:29 +00:00
// Close OpenGL window and terminate GLFW
glfwTerminate();
2020-07-12 13:46:51 +00:00
options.save();
2020-07-06 19:18:29 +00:00
return 0;
}