Minor refactorings
This commit is contained in:
16
.clang-tidy
16
.clang-tidy
@@ -1,7 +1,17 @@
|
|||||||
---
|
Checks: >
|
||||||
Checks: 'clang-diagnostic-*,clang-analyzer-*,modernize-*,bugprone-*,concurrency-*,cppcoreguidelines-*,performance-*,portability-*,readability-*,-readability-identifier-length,-readability-function-cognitive-complexity,-bugprone-easily-swappable-parameters'
|
clang-diagnostic-*,
|
||||||
|
clang-analyzer-*,
|
||||||
|
modernize-*,
|
||||||
|
bugprone-*,
|
||||||
|
concurrency-*,
|
||||||
|
cppcoreguidelines-*,
|
||||||
|
performance-*,
|
||||||
|
portability-*,
|
||||||
|
readability-*,
|
||||||
|
-readability-identifier-length,
|
||||||
|
-readability-function-cognitive-complexity,
|
||||||
|
-cppcoreguidelines-pro-type-union-access
|
||||||
WarningsAsErrors: ''
|
WarningsAsErrors: ''
|
||||||
HeaderFilterRegex: ''
|
HeaderFilterRegex: ''
|
||||||
AnalyzeTemporaryDtors: false
|
AnalyzeTemporaryDtors: false
|
||||||
FormatStyle: file
|
FormatStyle: file
|
||||||
...
|
|
||||||
|
|||||||
@@ -26,9 +26,7 @@ using namespace entt::literals;
|
|||||||
|
|
||||||
Controller::Controller()
|
Controller::Controller()
|
||||||
: m_gameWindow(std::make_shared<Window>()),
|
: m_gameWindow(std::make_shared<Window>()),
|
||||||
m_postProcessFrameBuffer(m_gameWindow->dimensions().first,
|
m_postProcessFrameBuffer(m_gameWindow->physical_dimensions(), post_processing_shader),
|
||||||
m_gameWindow->dimensions().second,
|
|
||||||
post_processing_shader),
|
|
||||||
m_gltf_loader{.image_cache = m_image_cache,
|
m_gltf_loader{.image_cache = m_image_cache,
|
||||||
.material_cache = m_material_cache,
|
.material_cache = m_material_cache,
|
||||||
.mesh_cache = m_mesh_cache,
|
.mesh_cache = m_mesh_cache,
|
||||||
@@ -38,7 +36,6 @@ Controller::Controller()
|
|||||||
.gltf_node_cache = m_gltf_node_cache},
|
.gltf_node_cache = m_gltf_node_cache},
|
||||||
m_gltf_cache(m_gltf_loader)
|
m_gltf_cache(m_gltf_loader)
|
||||||
{
|
{
|
||||||
// std::filesystem::path document_path("Lantern/glTF-Binary/Lantern.glb");
|
|
||||||
std::filesystem::path document_path("ABeautifulGame.glb");
|
std::filesystem::path document_path("ABeautifulGame.glb");
|
||||||
entt::hashed_string document_hash(document_path.c_str());
|
entt::hashed_string document_hash(document_path.c_str());
|
||||||
|
|
||||||
@@ -93,7 +90,6 @@ void Controller::run()
|
|||||||
|
|
||||||
// Update window size
|
// Update window size
|
||||||
if (m_gameWindow->dimensions_changed()) {
|
if (m_gameWindow->dimensions_changed()) {
|
||||||
m_gameWindow->update_dimensions();
|
|
||||||
update_window_dimensions();
|
update_window_dimensions();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -123,21 +119,22 @@ void Controller::update_window_dimensions()
|
|||||||
// m_camera->updateAspectRatio(m_gameWindow->aspectRatio());
|
// m_camera->updateAspectRatio(m_gameWindow->aspectRatio());
|
||||||
// m_gameEventHandler->setFirstMouseInput(1);
|
// m_gameEventHandler->setFirstMouseInput(1);
|
||||||
|
|
||||||
auto dimensions = m_gameWindow->dimensions();
|
auto dimensions = m_gameWindow->physical_dimensions();
|
||||||
m_postProcessFrameBuffer.changeDimensions(dimensions.first, dimensions.second);
|
m_postProcessFrameBuffer.updateDimensions(dimensions);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Controller::updateExposure(Shader &shader) const
|
void Controller::updateExposure(Shader& shader) const
|
||||||
{
|
{
|
||||||
shader.bind();
|
shader.bind();
|
||||||
shader.set_uniform("u_exposure", m_exposure);
|
shader.set_uniform("u_exposure", m_exposure);
|
||||||
Shader::unbind();
|
Shader::unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Controller::update_delta_time(entt::registry ®istry) const
|
void Controller::update_delta_time(entt::registry& registry) const
|
||||||
{
|
{
|
||||||
static constexpr auto MICROSECONDS_PER_SECOND = 1'000'000;
|
static constexpr auto MICROSECONDS_PER_SECOND = 1'000'000;
|
||||||
|
|
||||||
registry.ctx().erase<Time::Delta>();
|
registry.ctx().erase<Time::Delta>();
|
||||||
registry.ctx().emplace<Time::Delta>(std::chrono::microseconds(static_cast<unsigned>(m_deltaTime * MICROSECONDS_PER_SECOND)));
|
registry.ctx().emplace<Time::Delta>(
|
||||||
|
std::chrono::microseconds(static_cast<unsigned>(m_deltaTime * MICROSECONDS_PER_SECOND)));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include "util/Log.h"
|
#include "util/Log.h"
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <glm/fwd.hpp>
|
||||||
|
|
||||||
void Framebuffer::bind() const
|
void Framebuffer::bind() const
|
||||||
{
|
{
|
||||||
@@ -19,11 +20,11 @@ GLuint Framebuffer::getFBO() const
|
|||||||
return m_FBO;
|
return m_FBO;
|
||||||
}
|
}
|
||||||
|
|
||||||
Framebuffer::Framebuffer(uint32_t width, uint32_t height, Shader &shader) : m_shader(shader)
|
Framebuffer::Framebuffer(glm::u32vec2 physical_dimensions, Shader &shader) : m_shader(shader)
|
||||||
{
|
{
|
||||||
glGenFramebuffers(1, &m_FBO);
|
glGenFramebuffers(1, &m_FBO);
|
||||||
|
|
||||||
generateTextures(width, height);
|
generateTextures(physical_dimensions.x, physical_dimensions.y);
|
||||||
setExposureCorrection(true);
|
setExposureCorrection(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,13 +64,13 @@ void Framebuffer::drawOnEntireScreen() const
|
|||||||
m_shader.unbind();
|
m_shader.unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Framebuffer::changeDimensions(uint32_t width, uint32_t height)
|
void Framebuffer::updateDimensions(glm::u32vec2 physical_dimensions)
|
||||||
{
|
{
|
||||||
// Delete old textures
|
// Delete old textures
|
||||||
glDeleteTextures(1, &m_colorBuffer);
|
glDeleteTextures(1, &m_colorBuffer);
|
||||||
glDeleteRenderbuffers(1, &m_depthStencilBuffer);
|
glDeleteRenderbuffers(1, &m_depthStencilBuffer);
|
||||||
|
|
||||||
generateTextures(width, height);
|
generateTextures(physical_dimensions.x, physical_dimensions.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Framebuffer::generateTextures(uint32_t width, uint32_t height)
|
void Framebuffer::generateTextures(uint32_t width, uint32_t height)
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <glad/gl.h>
|
#include <glad/gl.h>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
class Shader;
|
class Shader;
|
||||||
|
|
||||||
class Framebuffer
|
class Framebuffer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Framebuffer(uint32_t width, uint32_t height, Shader &shader);
|
Framebuffer(glm::u32vec2 physical_dimensions, Shader &shader);
|
||||||
~Framebuffer();
|
~Framebuffer();
|
||||||
|
|
||||||
void bind() const;
|
void bind() const;
|
||||||
@@ -16,8 +17,7 @@ public:
|
|||||||
|
|
||||||
void drawOnEntireScreen() const;
|
void drawOnEntireScreen() const;
|
||||||
|
|
||||||
void changeDimensions(uint32_t width, uint32_t height);
|
void updateDimensions(glm::u32vec2 physical_dimensions);
|
||||||
|
|
||||||
void setExposureCorrection(bool exposureCorrection) const;
|
void setExposureCorrection(bool exposureCorrection) const;
|
||||||
|
|
||||||
GLuint getTextureId() const;
|
GLuint getTextureId() const;
|
||||||
|
|||||||
150
src/Window.cpp
150
src/Window.cpp
@@ -8,17 +8,6 @@
|
|||||||
|
|
||||||
Window::Window()
|
Window::Window()
|
||||||
{
|
{
|
||||||
// Hint Wayland preference to GLFW
|
|
||||||
// glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND);
|
|
||||||
|
|
||||||
// Initialize GLFW
|
|
||||||
if (glfwInit() == 0) {
|
|
||||||
std::terminate();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_width = INIT_WINDOW_WIDTH;
|
|
||||||
m_height = INIT_WINDOW_HEIGHT;
|
|
||||||
|
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
@@ -32,44 +21,29 @@ Window::Window()
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
m_glfw_window = std::shared_ptr<GLFWwindow>(
|
m_glfw_window = std::shared_ptr<GLFWwindow>(
|
||||||
glfwCreateWindow(
|
glfwCreateWindow(INIT_WINDOW_WIDTH, INIT_WINDOW_HEIGHT, "OpenGL", nullptr, nullptr),
|
||||||
static_cast<int>(m_width), static_cast<int>(m_height), "OpenGL", nullptr, nullptr),
|
[](GLFWwindow* window) { glfwDestroyWindow(window); });
|
||||||
[](GLFWwindow *window) { glfwDestroyWindow(window); });
|
|
||||||
|
|
||||||
if (!m_glfw_window) {
|
if (!m_glfw_window) {
|
||||||
Log::logger().critical("Failed to create window");
|
Log::logger().critical("Failed to create window");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for window to maximize (in case)
|
|
||||||
// Helper::sleep(1000);
|
|
||||||
|
|
||||||
int width{};
|
|
||||||
int height{};
|
|
||||||
|
|
||||||
glfwGetWindowSize(m_glfw_window.get(), &width, &height);
|
|
||||||
|
|
||||||
m_width = static_cast<uint32_t>(width);
|
|
||||||
m_height = static_cast<uint32_t>(height);
|
|
||||||
|
|
||||||
// Create OpenGL context
|
// Create OpenGL context
|
||||||
glfwMakeContextCurrent(m_glfw_window.get());
|
glfwMakeContextCurrent(m_glfw_window.get());
|
||||||
|
|
||||||
// Initialize GLAD
|
// Initialize GLAD
|
||||||
if (gladLoadGL(glfwGetProcAddress) == 0) {
|
if (gladLoadGL(glfwGetProcAddress) == 0) {
|
||||||
Log::logger().critical("Failed to initialize GLAD");
|
Log::logger().critical("Failed to initialize GLAD");
|
||||||
std::terminate();
|
std::quick_exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
|
// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||||
Log::logger().debug("OpenGL version: {}",
|
auto const* gl_version = reinterpret_cast<char const*>(glGetString(GL_VERSION));
|
||||||
reinterpret_cast<const char *>(glGetString(GL_VERSION)));
|
|
||||||
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
|
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
|
||||||
|
Log::logger().debug("OpenGL version: {}", gl_version);
|
||||||
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
||||||
glDebugMessageCallback(&Helper::gl_debug_callback, nullptr);
|
glDebugMessageCallback(&Helper::gl_debug_callback, nullptr);
|
||||||
|
|
||||||
// Disable mouse cursor
|
|
||||||
m_mouse_catched.catched = false;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Enable z buffer
|
// Enable z buffer
|
||||||
@@ -83,73 +57,64 @@ Window::Window()
|
|||||||
// Enable multisampling (a bit redundant because most graphics drivers do this automatically)
|
// Enable multisampling (a bit redundant because most graphics drivers do this automatically)
|
||||||
glEnable(GL_MULTISAMPLE);
|
glEnable(GL_MULTISAMPLE);
|
||||||
|
|
||||||
// Disable VSync since my sleep function handles this
|
#ifndef NDEBUG
|
||||||
|
// Disable mouse cursor
|
||||||
|
m_mouse_catched.catched = false;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Disable VSync
|
||||||
glfwSwapInterval(0);
|
glfwSwapInterval(0);
|
||||||
|
|
||||||
set_catched_cursor(m_mouse_catched.catched);
|
set_catched_cursor(m_mouse_catched.catched);
|
||||||
|
|
||||||
glViewport(0, 0, static_cast<GLsizei>(m_width), static_cast<GLsizei>(m_height));
|
{
|
||||||
|
int width{};
|
||||||
|
int height{};
|
||||||
|
glfwGetFramebufferSize(m_glfw_window.get(), &width, &height);
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Callbacks
|
||||||
glfwSetWindowUserPointer(m_glfw_window.get(), this);
|
glfwSetWindowUserPointer(m_glfw_window.get(), this);
|
||||||
glfwSetKeyCallback(m_glfw_window.get(), key_callback);
|
glfwSetKeyCallback(m_glfw_window.get(), key_callback);
|
||||||
glfwSetMouseButtonCallback(m_glfw_window.get(), mouse_button_callback);
|
|
||||||
glfwSetCursorPosCallback(m_glfw_window.get(), mouse_cursor_callback);
|
glfwSetCursorPosCallback(m_glfw_window.get(), mouse_cursor_callback);
|
||||||
|
glfwSetFramebufferSizeCallback(m_glfw_window.get(), framebuffer_size_callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Window::dimensions_changed() const -> bool
|
auto Window::dimensions_changed() -> bool
|
||||||
{
|
{
|
||||||
int new_width{};
|
bool temp = m_dimensions_changed;
|
||||||
int new_height{};
|
m_dimensions_changed = false;
|
||||||
|
return temp;
|
||||||
glfwGetFramebufferSize(m_glfw_window.get(), &new_width, &new_height);
|
|
||||||
|
|
||||||
return !(static_cast<uint32_t>(new_width) == m_width &&
|
|
||||||
static_cast<uint32_t>(new_height) == m_height);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Window::update_dimensions()
|
|
||||||
{
|
|
||||||
int width{};
|
|
||||||
int height{};
|
|
||||||
|
|
||||||
glfwGetFramebufferSize(m_glfw_window.get(), &width, &height);
|
|
||||||
|
|
||||||
m_width = static_cast<uint32_t>(width);
|
|
||||||
m_height = static_cast<uint32_t>(height);
|
|
||||||
|
|
||||||
glViewport(0, 0, width, height);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::set_catched_cursor(bool value)
|
void Window::set_catched_cursor(bool value)
|
||||||
{
|
{
|
||||||
|
|
||||||
glfwSetInputMode(
|
glfwSetInputMode(
|
||||||
m_glfw_window.get(), GLFW_CURSOR, value ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
|
m_glfw_window.get(), GLFW_CURSOR, value ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
|
||||||
|
|
||||||
m_mouse_catched.catched = value;
|
m_mouse_catched.catched = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// GLFW error function
|
void Window::glfw_error_callback(int error, char const* description)
|
||||||
void Window::glfw_error_callback(int error, const char *description)
|
|
||||||
{
|
{
|
||||||
Log::logger().warn("GLFW [{:d}]: {:s}\n", error, description);
|
Log::logger().warn("GLFW [{:d}]: {:s}\n", error, description);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function is called when the window gets resized (currently not used)
|
void Window::framebuffer_size_callback(GLFWwindow* glfw_window, int width, int height)
|
||||||
void Window::framebuffer_size_callback(GLFWwindow *window, int width, int height)
|
|
||||||
{
|
{
|
||||||
(void)window;
|
auto& window = *static_cast<Window*>(glfwGetWindowUserPointer(glfw_window));
|
||||||
|
window.m_dimensions_changed = true;
|
||||||
glViewport(0, 0, width, height);
|
glViewport(0, 0, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOLINTBEGIN(bugprone-easily-swappable-parameters)
|
void Window::key_callback(GLFWwindow* glfw_window,
|
||||||
void Window::key_callback(GLFWwindow *glfw_window, int key, int scancode, int action, int mods)
|
int key,
|
||||||
// NOLINTEND(bugprone-easily-swappable-parameters)
|
[[maybe_unused]] int scancode,
|
||||||
|
int action,
|
||||||
|
[[maybe_unused]] int mods)
|
||||||
{
|
{
|
||||||
(void)mods;
|
auto& window = *static_cast<Window*>(glfwGetWindowUserPointer(glfw_window));
|
||||||
(void)scancode;
|
|
||||||
|
|
||||||
auto &window = *static_cast<Window *>(glfwGetWindowUserPointer(glfw_window));
|
|
||||||
|
|
||||||
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
|
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
|
||||||
glfwSetWindowShouldClose(glfw_window, GLFW_TRUE);
|
glfwSetWindowShouldClose(glfw_window, GLFW_TRUE);
|
||||||
@@ -162,24 +127,10 @@ void Window::key_callback(GLFWwindow *glfw_window, int key, int scancode, int ac
|
|||||||
window.m_key_input.key_map[key] = (action == GLFW_PRESS);
|
window.m_key_input.key_map[key] = (action == GLFW_PRESS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// NOLINTBEGIN(bugprone-easily-swappable-parameters)
|
|
||||||
void Window::mouse_button_callback(GLFWwindow *glfw_window,
|
|
||||||
int button,
|
|
||||||
int action,
|
|
||||||
int mods) // NOLINT(bugprone-easily-swappable-parameters)
|
|
||||||
// NOLINTEND(bugprone-easily-swappable-parameters)
|
|
||||||
{
|
|
||||||
(void)mods;
|
|
||||||
(void)button;
|
|
||||||
(void)action;
|
|
||||||
|
|
||||||
auto &window = *static_cast<Window *>(glfwGetWindowUserPointer(glfw_window));
|
void Window::mouse_cursor_callback(GLFWwindow* glfw_window, double xpos, double ypos)
|
||||||
(void)window;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Window::mouse_cursor_callback(GLFWwindow *glfw_window, double xpos, double ypos)
|
|
||||||
{
|
{
|
||||||
auto &window = *static_cast<Window *>(glfwGetWindowUserPointer(glfw_window));
|
auto& window = *static_cast<Window*>(glfwGetWindowUserPointer(glfw_window));
|
||||||
|
|
||||||
double deltaCursorPosX = xpos - window.m_last_cursor_pos_x;
|
double deltaCursorPosX = xpos - window.m_last_cursor_pos_x;
|
||||||
double deltaCursorPosY = -(ypos - window.m_last_cursor_pos_y);
|
double deltaCursorPosY = -(ypos - window.m_last_cursor_pos_y);
|
||||||
@@ -189,8 +140,8 @@ void Window::mouse_cursor_callback(GLFWwindow *glfw_window, double xpos, double
|
|||||||
|
|
||||||
// Check if this is the first VALID mouse event after window being resized
|
// Check if this is the first VALID mouse event after window being resized
|
||||||
if (window.m_first_mouse_input &&
|
if (window.m_first_mouse_input &&
|
||||||
!(std::abs(deltaCursorPosX) < std::numeric_limits<double>::epsilon() &&
|
(std::abs(deltaCursorPosX) >= std::numeric_limits<double>::epsilon() ||
|
||||||
std::abs(deltaCursorPosY) < std::numeric_limits<double>::epsilon())) {
|
std::abs(deltaCursorPosY) >= std::numeric_limits<double>::epsilon())) {
|
||||||
window.m_first_mouse_input = false;
|
window.m_first_mouse_input = false;
|
||||||
deltaCursorPosX = 0.0;
|
deltaCursorPosX = 0.0;
|
||||||
deltaCursorPosY = 0.0;
|
deltaCursorPosY = 0.0;
|
||||||
@@ -199,19 +150,25 @@ void Window::mouse_cursor_callback(GLFWwindow *glfw_window, double xpos, double
|
|||||||
deltaCursorPosX *= MOUSE_SENSITIVITY;
|
deltaCursorPosX *= MOUSE_SENSITIVITY;
|
||||||
deltaCursorPosY *= MOUSE_SENSITIVITY;
|
deltaCursorPosY *= MOUSE_SENSITIVITY;
|
||||||
|
|
||||||
auto &[deltaX, deltaY] = window.m_mouse_cursor_input.cursor_movement;
|
auto& [deltaX, deltaY] = window.m_mouse_cursor_input.cursor_movement;
|
||||||
deltaX += deltaCursorPosX;
|
deltaX += deltaCursorPosX;
|
||||||
deltaY += deltaCursorPosY;
|
deltaY += deltaCursorPosY;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Window::dimensions() const -> std::pair<unsigned, unsigned>
|
auto Window::logical_dimensions() const -> glm::u32vec2
|
||||||
{
|
{
|
||||||
return {m_width, m_height};
|
int width{};
|
||||||
|
int height{};
|
||||||
|
glfwGetWindowSize(m_glfw_window.get(), &width, &height);
|
||||||
|
return {width, height};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto Window::aspectRatio() const -> float
|
auto Window::physical_dimensions() const -> glm::u32vec2
|
||||||
{
|
{
|
||||||
return (float)m_width / (float)m_height;
|
int width{};
|
||||||
|
int height{};
|
||||||
|
glfwGetFramebufferSize(m_glfw_window.get(), &width, &height);
|
||||||
|
return {width, height};
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::clear_mouse_cursor_input()
|
void Window::clear_mouse_cursor_input()
|
||||||
@@ -219,15 +176,18 @@ void Window::clear_mouse_cursor_input()
|
|||||||
m_mouse_cursor_input = {};
|
m_mouse_cursor_input = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
void Window::update_catched_mouse(entt::registry ®istry) const
|
void Window::update_catched_mouse(entt::registry& registry) const
|
||||||
{
|
{
|
||||||
registry.ctx().erase<MouseCatched>();
|
registry.ctx().erase<MouseCatched>();
|
||||||
registry.ctx().emplace<MouseCatched>(m_mouse_catched);
|
registry.ctx().emplace<MouseCatched>(m_mouse_catched);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::update_descriptor(entt::registry ®istry) const
|
void Window::update_descriptor(entt::registry& registry) const
|
||||||
{
|
{
|
||||||
|
auto dimensions = logical_dimensions();
|
||||||
|
|
||||||
registry.ctx().erase<Descriptor>();
|
registry.ctx().erase<Descriptor>();
|
||||||
registry.ctx().emplace<Descriptor>(
|
registry.ctx().emplace<Descriptor>(Descriptor{
|
||||||
Descriptor{.width = m_width, .height = m_height, .aspect_ratio = aspectRatio()});
|
.logical_dimensions = dimensions,
|
||||||
|
.aspect_ratio = static_cast<float>(dimensions.x) / static_cast<float>(dimensions.y)});
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/Window.h
19
src/Window.h
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
@@ -18,34 +20,30 @@ public:
|
|||||||
|
|
||||||
struct Descriptor
|
struct Descriptor
|
||||||
{
|
{
|
||||||
uint32_t width{};
|
glm::u32vec2 logical_dimensions;
|
||||||
uint32_t height{};
|
|
||||||
float aspect_ratio{};
|
float aspect_ratio{};
|
||||||
};
|
};
|
||||||
|
|
||||||
Window();
|
Window();
|
||||||
|
|
||||||
[[nodiscard]] auto glfw_window() -> GLFWwindow & { return *m_glfw_window; }
|
[[nodiscard]] auto glfw_window() -> GLFWwindow & { return *m_glfw_window; }
|
||||||
[[nodiscard]] auto dimensions() const -> std::pair<unsigned, unsigned>;
|
[[nodiscard]] auto physical_dimensions() const -> glm::u32vec2;
|
||||||
[[nodiscard]] auto dimensions_changed() const -> bool;
|
[[nodiscard]] auto logical_dimensions() const -> glm::u32vec2;
|
||||||
[[nodiscard]] auto aspectRatio() const -> float;
|
[[nodiscard]] auto dimensions_changed() -> bool;
|
||||||
[[nodiscard]] auto key_input() const -> auto const & { return m_key_input; }
|
[[nodiscard]] auto key_input() const -> auto const & { return m_key_input; }
|
||||||
[[nodiscard]] auto mouse_cursor_input() const -> auto const & { return m_mouse_cursor_input; }
|
[[nodiscard]] auto mouse_cursor_input() const -> auto const & { return m_mouse_cursor_input; }
|
||||||
[[nodiscard]] auto mouse_button_input() const -> auto const & { return m_mouse_button_input; }
|
[[nodiscard]] auto mouse_button_input() const -> auto const & { return m_mouse_button_input; }
|
||||||
[[nodiscard]] auto cursor_catched() const -> auto { return m_mouse_catched; }
|
[[nodiscard]] auto cursor_catched() const -> auto { return m_mouse_catched; }
|
||||||
|
|
||||||
void clear_mouse_cursor_input();
|
void clear_mouse_cursor_input();
|
||||||
void update_dimensions();
|
|
||||||
void update_catched_mouse(entt::registry ®istry) const;
|
void update_catched_mouse(entt::registry ®istry) const;
|
||||||
void update_descriptor(entt::registry ®istry) const;
|
void update_descriptor(entt::registry ®istry) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void key_callback(GLFWwindow *glfw_window, int key, int scancode, int action, int mods);
|
static void key_callback(GLFWwindow *glfw_window, int key, int scancode, int action, int mods);
|
||||||
static void mouse_button_callback(GLFWwindow *glfw_window, int button, int action, int mods);
|
|
||||||
static void mouse_cursor_callback(GLFWwindow *glfw_window, double xpos, double ypos);
|
static void mouse_cursor_callback(GLFWwindow *glfw_window, double xpos, double ypos);
|
||||||
|
|
||||||
static void glfw_error_callback(int error, const char *description);
|
|
||||||
static void framebuffer_size_callback(GLFWwindow *glfw_window, int width, int height);
|
static void framebuffer_size_callback(GLFWwindow *glfw_window, int width, int height);
|
||||||
|
static void glfw_error_callback(int error, const char *description);
|
||||||
|
|
||||||
static constexpr float MOUSE_SENSITIVITY = 0.15F;
|
static constexpr float MOUSE_SENSITIVITY = 0.15F;
|
||||||
|
|
||||||
@@ -58,8 +56,7 @@ private:
|
|||||||
Input::MouseButton m_mouse_button_input;
|
Input::MouseButton m_mouse_button_input;
|
||||||
Input::MouseCursor m_mouse_cursor_input;
|
Input::MouseCursor m_mouse_cursor_input;
|
||||||
|
|
||||||
uint32_t m_width{};
|
bool m_dimensions_changed = false;
|
||||||
uint32_t m_height{};
|
|
||||||
double m_last_cursor_pos_x = 0.0;
|
double m_last_cursor_pos_x = 0.0;
|
||||||
double m_last_cursor_pos_y = 0.0;
|
double m_last_cursor_pos_y = 0.0;
|
||||||
|
|
||||||
|
|||||||
@@ -40,15 +40,15 @@ void Camera::keyboard_movement(entt::registry ®istry)
|
|||||||
};
|
};
|
||||||
|
|
||||||
auto &movement_context = registry.ctx().emplace<KeyboardMovementContext>();
|
auto &movement_context = registry.ctx().emplace<KeyboardMovementContext>();
|
||||||
auto const& key_input = registry.ctx().at<Input::Key>();
|
auto const& key_input = registry.ctx().get<Input::Key>();
|
||||||
auto const& delta_time = registry.ctx().at<Time::Delta>();
|
auto const& delta_time = registry.ctx().get<Time::Delta>();
|
||||||
|
|
||||||
auto camera_view = registry.view<Camera const, Transform, GlobalTransform const>();
|
auto camera_view = registry.view<Camera const, Transform, GlobalTransform const>();
|
||||||
auto camera_entity = camera_view.front();
|
auto camera_entity = camera_view.front();
|
||||||
auto [camera, camera_transform, camera_global_transform] = camera_view.get(camera_entity);
|
auto [camera, camera_transform, camera_global_transform] = camera_view.get(camera_entity);
|
||||||
|
|
||||||
glm::vec3 front_vec = front_vector(camera_global_transform);
|
glm::vec3 front_vec = front_vector(camera_global_transform);
|
||||||
front_vec.y = 0; // NOLINT (cppcoreguidelines-pro-type-union-access)
|
front_vec.y = 0;
|
||||||
|
|
||||||
glm::vec3 delta_pos = glm::vec3(0., 0., 0.);
|
glm::vec3 delta_pos = glm::vec3(0., 0., 0.);
|
||||||
float delta_factor = SPEED * delta_time.delta.count() * (movement_context.accelerate ? ACCELERATION : 1.0F);
|
float delta_factor = SPEED * delta_time.delta.count() * (movement_context.accelerate ? ACCELERATION : 1.0F);
|
||||||
@@ -86,7 +86,7 @@ void Camera::mouse_orientation(entt::registry ®istry)
|
|||||||
auto camera_entity = camera_view.front();
|
auto camera_entity = camera_view.front();
|
||||||
auto [camera, camera_transform] = camera_view.get(camera_entity);
|
auto [camera, camera_transform] = camera_view.get(camera_entity);
|
||||||
|
|
||||||
auto const& mouse_cursor_input = registry.ctx().at<Input::MouseCursor>();
|
auto const& mouse_cursor_input = registry.ctx().get<Input::MouseCursor>();
|
||||||
auto [deltaX, deltaY] = mouse_cursor_input.cursor_movement;
|
auto [deltaX, deltaY] = mouse_cursor_input.cursor_movement;
|
||||||
|
|
||||||
if (std::abs(deltaX) < std::numeric_limits<double>::epsilon() &&
|
if (std::abs(deltaX) < std::numeric_limits<double>::epsilon() &&
|
||||||
@@ -96,7 +96,6 @@ void Camera::mouse_orientation(entt::registry ®istry)
|
|||||||
|
|
||||||
auto pitch = static_cast<float>(deltaY);
|
auto pitch = static_cast<float>(deltaY);
|
||||||
auto yaw = static_cast<float>(deltaX);
|
auto yaw = static_cast<float>(deltaX);
|
||||||
auto roll = 0.0F;
|
|
||||||
|
|
||||||
// Orthographic projection currently unsupported
|
// Orthographic projection currently unsupported
|
||||||
auto &camera_perspective = std::get<Perspective>(camera.projection);
|
auto &camera_perspective = std::get<Perspective>(camera.projection);
|
||||||
@@ -114,7 +113,7 @@ void Camera::mouse_orientation(entt::registry ®istry)
|
|||||||
|
|
||||||
void Camera::aspect_ratio_update(entt::registry ®istry)
|
void Camera::aspect_ratio_update(entt::registry ®istry)
|
||||||
{
|
{
|
||||||
float aspect_ratio = registry.ctx().at<Window::Descriptor>().aspect_ratio;
|
float aspect_ratio = registry.ctx().get<Window::Descriptor>().aspect_ratio;
|
||||||
|
|
||||||
auto camera_view = registry.view<Camera>();
|
auto camera_view = registry.view<Camera>();
|
||||||
auto camera_entity = camera_view.front();
|
auto camera_entity = camera_view.front();
|
||||||
|
|||||||
14
src/main.cpp
14
src/main.cpp
@@ -1,18 +1,22 @@
|
|||||||
#include "Controller.h"
|
#include "Controller.h"
|
||||||
|
#include "Helper.h"
|
||||||
|
#include "util/Log.h"
|
||||||
|
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
auto main(int argc, char **argv) -> int
|
auto main() -> int
|
||||||
{
|
{
|
||||||
// Suppress warning about unused variable
|
|
||||||
(void)argc;
|
|
||||||
(void)argv;
|
|
||||||
|
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
std::cout << "[Debug Mode]" << std::endl;
|
std::cout << "[Debug Mode]" << std::endl;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Initialize GLFW
|
||||||
|
if (glfwInit() == 0) {
|
||||||
|
Log::logger().critical("Could not initialize GLFW");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
// Create controller
|
// Create controller
|
||||||
Controller controller;
|
Controller controller;
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ void Scene::update()
|
|||||||
Camera::aspect_ratio_update(m_registry);
|
Camera::aspect_ratio_update(m_registry);
|
||||||
Camera::keyboard_movement(m_registry);
|
Camera::keyboard_movement(m_registry);
|
||||||
|
|
||||||
if (m_registry.ctx().at<Window::MouseCatched>().catched) {
|
if (m_registry.ctx().get<Window::MouseCatched>().catched) {
|
||||||
Camera::mouse_orientation(m_registry);
|
Camera::mouse_orientation(m_registry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,6 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <entt/entt.hpp>
|
#include <entt/entt.hpp>
|
||||||
|
|
||||||
class Shader;
|
|
||||||
|
|
||||||
class Scene
|
class Scene
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace Time {
|
|||||||
|
|
||||||
struct Delta
|
struct Delta
|
||||||
{
|
{
|
||||||
std::chrono::duration<float> delta;
|
std::chrono::duration<double> delta;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Time
|
} // namespace Time
|
||||||
Reference in New Issue
Block a user