147 lines
5.0 KiB
C++
147 lines
5.0 KiB
C++
|
/**
|
||
|
* \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"
|
||
|
|
||
|
#include "render/pass/SimpleProgram.hpp"
|
||
|
#include "render/buffer/VertexBuffer.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.clear_color.x, options.clear_color.y, options.clear_color.z, options.clear_color.w);
|
||
|
glfwSwapInterval(options.target_fps < MIN_FPS);
|
||
|
|
||
|
InputMap inputs(window);
|
||
|
Camera camera(window, inputs);
|
||
|
|
||
|
SimpleProgram *testPass = new SimpleProgram();
|
||
|
UI::setup(window);
|
||
|
|
||
|
static const GLfloat g_vertex_buffer_data[] = {
|
||
|
-1.0f, -1.0f, 0.0f,
|
||
|
1.0f, -1.0f, 0.0f,
|
||
|
1.0f, 1.0f, 0.0f,
|
||
|
-1.0f, 1.0f, 0.0f,
|
||
|
-1.0f, -1.0f, 0.0f,
|
||
|
1.0f, 1.0f, 0.0f,
|
||
|
};
|
||
|
Buffer *quadBuffer = new VertexBuffer(GL_TRIANGLES, 6, sizeof(g_vertex_buffer_data), g_vertex_buffer_data);
|
||
|
|
||
|
GLuint aimTexture = Program::loadTexture("ui/Aim", false);
|
||
|
|
||
|
GLuint VertexArrayID;
|
||
|
glGenVertexArrays(1, &VertexArrayID);
|
||
|
glBindVertexArray(VertexArrayID);
|
||
|
|
||
|
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.update(camera);
|
||
|
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});
|
||
|
}
|
||
|
|
||
|
const auto report = world.update(camera.getPosition() / scale);
|
||
|
chunk_count_tracker.push(report.count);
|
||
|
chunk_load_tracker.push(report.load);
|
||
|
chunk_unload_tracker.push(report.unload);*/
|
||
|
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) {
|
||
|
glClearColor(options.clear_color.x, options.clear_color.y, options.clear_color.z, options.clear_color.w);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
{ // Rendering
|
||
|
const double partTime = glfwGetTime();
|
||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||
|
testPass->useIt();
|
||
|
const auto mvp = camera.getProjectionMatrix() * camera.getViewMatrix() * glm::mat4(1.0f);
|
||
|
testPass->setMVP(&mvp[0][0]);
|
||
|
quadBuffer->draw(Buffer::params{.vertexOnly = false});
|
||
|
|
||
|
reports.main.tris_count = 0;
|
||
|
reports.main.models_count = 0;
|
||
|
|
||
|
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();
|
||
|
|
||
|
// Close OpenGL window and terminate GLFW
|
||
|
glfwTerminate();
|
||
|
|
||
|
return 0;
|
||
|
}
|