/** * \file main.cpp * \brief Univerxel game * \author Maelys Bois * \version 0.0.1 * * Univerxel main program. */ #include #include #include #include #include "render/window.hpp" #include "render/UI.hpp" #include "control/InputMap.hpp" #include "control/Camera.hpp" #include "render/Renderer.hpp" #include "world/World.hpp" #include "contouring/FlatBox.hpp" #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; glClearColor(options.renderer.clear_color.x, options.renderer.clear_color.y, options.renderer.clear_color.z, options.renderer.clear_color.w); glfwSwapInterval(options.target_fps < MIN_FPS); InputMap inputs(window); Camera camera(window, inputs, options.camera); Renderer *renderer = new Renderer(options.renderer); UI::setup(window); GLuint aimTexture = Program::loadTexture("ui/Aim", false); renderer->LightInvDir = glm::vec3(-0.5f, 2, -2); World world = World(options.world); 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()); renderer->lookFrom(camera); state.position = camera.getPosition(); /*look_at = world.raycast(camera.getViewRay() / scale); 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}); }*/ world.update(state.position / options.voxel_size, reports.world); 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) { 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); } 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); } if(actions && UI::Actions::Camera) { camera.setOptions(options.camera); } renderer->SkyEnable = options.renderer.skybox; } { // Rendering const double partTime = glfwGetTime(); auto pass = renderer->getPass(); pass.start(); std::vector> models; if(options.culling) { world.getModels(models, options.voxel_size, camera.getFrustum()); } else { world.getModels(models, options.voxel_size); } reports.main.models_count = 0; reports.main.tris_count = 0; for (auto [model, buffer] : models) { reports.main.models_count++; reports.main.tris_count += buffer->draw(pass.setup(model)); } renderer->postProcess(); 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(); delete renderer; // Close OpenGL window and terminate GLFW glfwTerminate(); options.save(); return 0; }