From ae61cccb701091b0cce414789b681a6bb1eea4aa Mon Sep 17 00:00:00 2001 From: Derek Christ Date: Fri, 9 Jun 2023 14:11:05 +0200 Subject: [PATCH] Refactor logging --- src/CMakeLists.txt | 2 +- src/Controller.cpp | 4 +- src/Window.cpp | 8 ++-- src/camera.cpp | 26 +++++----- src/framebuffer.cpp | 4 +- src/glad.cpp | 28 +++++------ src/gltf_loader.cpp | 112 ++++++++++++++++++++++---------------------- src/image.cpp | 30 +++++++----- src/log.cpp | 17 +++++++ src/log.h | 9 ++++ src/main.cpp | 9 ++-- src/mesh.cpp | 9 ++-- src/scene.cpp | 1 - src/shader.cpp | 28 +++++------ src/util/Log.cpp | 26 ---------- src/util/Log.h | 16 ------- 16 files changed, 158 insertions(+), 171 deletions(-) create mode 100644 src/log.cpp create mode 100644 src/log.h delete mode 100644 src/util/Log.cpp delete mode 100644 src/util/Log.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2ba375c..115322f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -4,7 +4,7 @@ add_library(fever_engine Window.cpp scene.cpp framebuffer.cpp - util/Log.cpp + log.cpp image.cpp mesh.cpp glad.cpp diff --git a/src/Controller.cpp b/src/Controller.cpp index d4575b1..03d1504 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -6,7 +6,6 @@ #include "render.h" #include "shader.h" #include "time.h" -#include "util/Log.h" #include #include @@ -17,6 +16,7 @@ #include #include #include +#include #include #include @@ -51,7 +51,7 @@ void Controller::run() auto standard_material_shader = m_shader_cache.load(shader_hash, Material::SHADER_NAME).first->second; - Log::logger().info("Startup complete. Enter game loop."); + spdlog::info("Startup complete. Enter game loop."); // This is the game loop while (glfwWindowShouldClose(&m_gameWindow->glfw_window()) == GLFW_FALSE) { diff --git a/src/Window.cpp b/src/Window.cpp index 0ffdd0c..b89a3ef 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -1,9 +1,9 @@ #include "Window.h" -#include "util/Log.h" #include "glad.h" -#include #include +#include +#include static constexpr unsigned INIT_WINDOW_WIDTH = 1280; static constexpr unsigned INIT_WINDOW_HEIGHT = 720; @@ -27,7 +27,7 @@ Window::Window() [](GLFWwindow* window) { glfwDestroyWindow(window); }); if (!m_glfw_window) { - Log::logger().critical("Failed to create window"); + spdlog::critical("Failed to create window"); } // Create OpenGL context @@ -66,7 +66,7 @@ void Window::set_catched_cursor(bool value) void Window::glfw_error_callback(int error, char const* description) { - Log::logger().warn("GLFW [{:d}]: {:s}\n", error, description); + spdlog::warn("GLFW [{:d}]: {:s}\n", error, description); } void Window::framebuffer_size_callback(GLFWwindow* glfw_window, int width, int height) diff --git a/src/camera.cpp b/src/camera.cpp index 69ed0b1..bb31c27 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -1,8 +1,7 @@ #include "camera.h" -#include "util/Log.h" +#include "Window.h" #include "input.h" #include "time.h" -#include "Window.h" #include #include @@ -10,7 +9,7 @@ auto Camera::projection_matrix() const -> glm::mat4 { return std::visit( - [](auto &&arg) -> glm::mat4 { + [](auto&& arg) -> glm::mat4 { using T = std::decay_t; if constexpr (std::is_same_v) { return glm::perspective(arg.fov / 2, arg.aspect_ratio, arg.near, arg.far); @@ -21,25 +20,25 @@ auto Camera::projection_matrix() const -> glm::mat4 projection); }; -auto Camera::view_matrix(GlobalTransform const &transform) -> glm::mat4 +auto Camera::view_matrix(GlobalTransform const& transform) -> glm::mat4 { return glm::lookAt( transform.position(), transform.position() + front_vector(transform), UP_VECTOR); } -auto Camera::front_vector(GlobalTransform const &transform) -> glm::vec3 +auto Camera::front_vector(GlobalTransform const& transform) -> glm::vec3 { return glm::normalize(transform.transform * glm::vec4(0.0, 0.0, 1.0, 0.0)); } -void Camera::keyboard_movement(entt::registry ®istry) +void Camera::keyboard_movement(entt::registry& registry) { struct KeyboardMovementContext { bool accelerate{}; }; - auto &movement_context = registry.ctx().emplace(); + auto& movement_context = registry.ctx().emplace(); auto const& key_input = registry.ctx().get(); auto const& delta_time = registry.ctx().get(); @@ -51,10 +50,11 @@ void Camera::keyboard_movement(entt::registry ®istry) front_vec.y = 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); movement_context.accelerate = false; - for (auto const &[key, pressed] : key_input.key_map) { + for (auto const& [key, pressed] : key_input.key_map) { if (key == GLFW_KEY_W && pressed) { delta_pos += delta_factor * glm::normalize(front_vec); } @@ -80,7 +80,7 @@ void Camera::keyboard_movement(entt::registry ®istry) camera_transform.translation += delta_pos; } -void Camera::mouse_orientation(entt::registry ®istry) +void Camera::mouse_orientation(entt::registry& registry) { auto camera_view = registry.view(); auto camera_entity = camera_view.front(); @@ -98,7 +98,7 @@ void Camera::mouse_orientation(entt::registry ®istry) auto yaw = static_cast(deltaX); // Orthographic projection currently unsupported - auto &camera_perspective = std::get(camera.projection); + auto& camera_perspective = std::get(camera.projection); camera_perspective.pitch += glm::radians(pitch); camera_perspective.yaw += glm::radians(yaw); @@ -111,7 +111,7 @@ void Camera::mouse_orientation(entt::registry ®istry) glm::quat(glm::vec3(-camera_perspective.pitch, -camera_perspective.yaw, 0.0)); } -void Camera::aspect_ratio_update(entt::registry ®istry) +void Camera::aspect_ratio_update(entt::registry& registry) { float aspect_ratio = registry.ctx().get().aspect_ratio; @@ -120,6 +120,6 @@ void Camera::aspect_ratio_update(entt::registry ®istry) auto [camera] = camera_view.get(camera_entity); // Orthographic projection currently unsupported - auto &camera_perspective = std::get(camera.projection); + auto& camera_perspective = std::get(camera.projection); camera_perspective.aspect_ratio = aspect_ratio; } diff --git a/src/framebuffer.cpp b/src/framebuffer.cpp index 83048d0..a1d298f 100644 --- a/src/framebuffer.cpp +++ b/src/framebuffer.cpp @@ -1,7 +1,7 @@ #include "framebuffer.h" -#include "util/Log.h" #include +#include Framebuffer::Framebuffer(glm::u32vec2 physical_dimensions) { @@ -35,7 +35,7 @@ Framebuffer::Framebuffer(glm::u32vec2 physical_dimensions) GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depth_stencil_buffer); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { - Log::logger().error("Framebuffer not complete"); + spdlog::error("Framebuffer not complete"); } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); diff --git a/src/glad.cpp b/src/glad.cpp index 9a2d74b..c0ab670 100644 --- a/src/glad.cpp +++ b/src/glad.cpp @@ -1,7 +1,7 @@ #include "glad.h" -#include "util/Log.h" #include +#include static void gl_debug_callback(GLenum source, GLenum type, @@ -109,17 +109,17 @@ static void gl_debug_callback(GLenum source, } if (severity == GL_DEBUG_SEVERITY_HIGH || severity == GL_DEBUG_SEVERITY_MEDIUM) { - Log::logger().debug("[OpenGL Debug Message]\n" - "Message: {}\n" - "Source: {}\n" - "Type: {}\n" - "ID: {}\n" - "Severity: {}\n", - _message, - _source, - _type, - id, - _severity); + spdlog::debug("[OpenGL Debug Message]\n" + "Message: {}\n" + "Source: {}\n" + "Type: {}\n" + "ID: {}\n" + "Severity: {}\n", + _message, + _source, + _type, + id, + _severity); } } @@ -127,7 +127,7 @@ void init_glad() { // Initialize GLAD if (gladLoadGL(glfwGetProcAddress) == 0) { - Log::logger().critical("Failed to initialize GLAD"); + spdlog::critical("Failed to initialize GLAD"); std::quick_exit(-1); } @@ -135,7 +135,7 @@ void init_glad() // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) auto const* gl_version = reinterpret_cast(glGetString(GL_VERSION)); // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) - Log::logger().debug("OpenGL version: {}", gl_version); + spdlog::debug("OpenGL version: {}", gl_version); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); glDebugMessageCallback(&gl_debug_callback, nullptr); #endif diff --git a/src/gltf_loader.cpp b/src/gltf_loader.cpp index 358339b..65095f0 100644 --- a/src/gltf_loader.cpp +++ b/src/gltf_loader.cpp @@ -3,9 +3,9 @@ #include "name.h" #include "relationship.h" #include "scene.h" -#include "util/Log.h" #include +#include struct AttributeLocations { @@ -41,18 +41,18 @@ static auto create_indices(std::span gltf_index_data) -> Indices return Indices{.values = std::move(index_data)}; } -static auto load_texture(fx::gltf::Texture const &texture, - fx::gltf::Document const &gltf, - std::filesystem::path const &document_path, +static auto load_texture(fx::gltf::Texture const& texture, + fx::gltf::Document const& gltf, + std::filesystem::path const& document_path, Image::ColorFormat colorFormat, - entt::resource_cache &image_cache) -> entt::resource + entt::resource_cache& image_cache) -> entt::resource { - auto const &gltf_image = gltf.images.at(texture.source); + auto const& gltf_image = gltf.images.at(texture.source); auto const base_directory = document_path.parent_path(); if (gltf_image.uri.empty()) { - auto const &image_buffer_view = gltf.bufferViews.at(gltf_image.bufferView); - auto const &image_buffer = gltf.buffers.at(image_buffer_view.buffer); + auto const& image_buffer_view = gltf.bufferViews.at(gltf_image.bufferView); + auto const& image_buffer = gltf.buffers.at(image_buffer_view.buffer); std::string const image_name = document_path.string() + ".image." + std::to_string(texture.source); @@ -82,12 +82,12 @@ static auto load_texture(fx::gltf::Texture const &texture, return image_cache.load(image_hash, image_data, colorFormat).first->second; } -static auto load_material(fx::gltf::Material const &material, - fx::gltf::Document const &gltf, - std::filesystem::path const &document_path, - entt::resource_cache &material_cache, - entt::resource_cache &image_cache, - entt::resource_cache &shader_cache) +static auto load_material(fx::gltf::Material const& material, + fx::gltf::Document const& gltf, + std::filesystem::path const& document_path, + entt::resource_cache& material_cache, + entt::resource_cache& image_cache, + entt::resource_cache& shader_cache) -> entt::resource { auto base_color_texture_id = material.pbrMetallicRoughness.baseColorTexture.index; @@ -95,14 +95,14 @@ static auto load_material(fx::gltf::Material const &material, std::optional> base_color_image; if (base_color_texture_id != -1) { - auto const &base_color_texture = gltf.textures.at(base_color_texture_id); + auto const& base_color_texture = gltf.textures.at(base_color_texture_id); base_color_image = load_texture( base_color_texture, gltf, document_path, Image::ColorFormat::SRGB, image_cache); } std::optional> normal_map_image; if (normal_texture_id != -1) { - auto const &normal_texture = gltf.textures.at(normal_texture_id); + auto const& normal_texture = gltf.textures.at(normal_texture_id); normal_map_image = load_texture(normal_texture, gltf, document_path, Image::ColorFormat::RGB, image_cache); } @@ -112,7 +112,7 @@ static auto load_material(fx::gltf::Material const &material, shader_cache.load(shader_hash, Material::SHADER_NAME).first->second; if (material.name.empty()) { - Log::logger().warn("glTF material has no name."); + spdlog::warn("glTF material has no name."); } entt::hashed_string material_hash(material.name.c_str()); @@ -126,13 +126,13 @@ static auto load_material(fx::gltf::Material const &material, static auto load_attribute(std::string_view attribute_name, uint32_t attribute_id, - fx::gltf::Document const &gltf) + fx::gltf::Document const& gltf) -> std::optional> { - auto const &attribute_accessor = gltf.accessors.at(attribute_id); + auto const& attribute_accessor = gltf.accessors.at(attribute_id); if (attribute_accessor.componentType != fx::gltf::Accessor::ComponentType::Float) { - Log::logger().critical("Only float attributes supported!"); + spdlog::critical("Only float attributes supported!"); std::terminate(); } @@ -158,8 +158,8 @@ static auto load_attribute(std::string_view attribute_name, return {}; } - auto const &buffer_view = gltf.bufferViews.at(attribute_accessor.bufferView); - auto const &buffer = gltf.buffers.at(buffer_view.buffer); + auto const& buffer_view = gltf.bufferViews.at(attribute_accessor.bufferView); + auto const& buffer = gltf.buffers.at(buffer_view.buffer); std::span buffer_span(buffer.data); std::span vertex_attribute_data_span = @@ -183,32 +183,32 @@ static auto load_attribute(std::string_view attribute_name, vertex_attribute_data_span); } - Log::logger().critical("Unsupported vertex attribute type!"); + spdlog::critical("Unsupported vertex attribute type!"); std::terminate(); }(); return std::make_pair(vertex_attribute_id.value(), std::move(vertex_attribute_data)); } -auto load_gltf_primitive(fx::gltf::Primitive const &gltf_primitive, - fx::gltf::Document const &gltf, +auto load_gltf_primitive(fx::gltf::Primitive const& gltf_primitive, + fx::gltf::Document const& gltf, std::string_view primitive_identifier, - entt::resource_cache &material_cache, - entt::resource_cache &mesh_cache) -> GltfPrimitive + entt::resource_cache& material_cache, + entt::resource_cache& mesh_cache) -> GltfPrimitive { // Load attributes auto tangent_it = std::find_if(gltf_primitive.attributes.cbegin(), gltf_primitive.attributes.cend(), - [](auto const &attribute) { return attribute.first == "TANGENT"; }); + [](auto const& attribute) { return attribute.first == "TANGENT"; }); if (tangent_it == gltf_primitive.attributes.cend()) { - Log::logger().critical("glTF scene has to include tangent and normal components!"); + spdlog::critical("glTF scene has to include tangent and normal components!"); std::terminate(); } std::map attributes; - for (auto const &attribute : gltf_primitive.attributes) { + for (auto const& attribute : gltf_primitive.attributes) { auto vertex_attribute = load_attribute(attribute.first, attribute.second, gltf); if (!vertex_attribute.has_value()) { @@ -220,9 +220,9 @@ auto load_gltf_primitive(fx::gltf::Primitive const &gltf_primitive, } // Load indices - auto const &indices_accessor = gltf.accessors.at(gltf_primitive.indices); - auto const &indices_buffer_view = gltf.bufferViews.at(indices_accessor.bufferView); - auto const &indices_buffer = gltf.buffers.at(indices_buffer_view.buffer); + auto const& indices_accessor = gltf.accessors.at(gltf_primitive.indices); + auto const& indices_buffer_view = gltf.bufferViews.at(indices_accessor.bufferView); + auto const& indices_buffer = gltf.buffers.at(indices_buffer_view.buffer); std::span indices_buffer_span(indices_buffer.data); std::span indices_data_span = @@ -239,7 +239,7 @@ auto load_gltf_primitive(fx::gltf::Primitive const &gltf_primitive, return create_indices(indices_data_span); } - Log::logger().critical("Unsupported indices type!"); + spdlog::critical("Unsupported indices type!"); std::terminate(); }(); @@ -250,14 +250,14 @@ auto load_gltf_primitive(fx::gltf::Primitive const &gltf_primitive, .first->second; // Get material by hash - auto const &gltf_material = gltf.materials.at(gltf_primitive.material); + auto const& gltf_material = gltf.materials.at(gltf_primitive.material); entt::hashed_string material_hash(gltf_material.name.c_str()); entt::resource material = material_cache[material_hash]; return GltfPrimitive{.mesh = mesh, .material = material}; } -auto GltfLoader::operator()(std::filesystem::path const &document_path) -> result_type +auto GltfLoader::operator()(std::filesystem::path const& document_path) -> result_type { fx::gltf::ReadQuotas const read_quotas{.MaxFileSize = MAX_SIZE, .MaxBufferByteLength = MAX_SIZE}; @@ -275,7 +275,7 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul // Load materials std::vector> materials; - for (auto const &gltf_material : gltf.materials) { + for (auto const& gltf_material : gltf.materials) { entt::resource material = load_material( gltf_material, gltf, document_path, material_cache, image_cache, shader_cache); materials.push_back(material); @@ -285,14 +285,14 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul std::vector> gltf_meshes; gltf_meshes.reserve(gltf.meshes.size()); - for (auto const &gltf_mesh : gltf.meshes) { + for (auto const& gltf_mesh : gltf.meshes) { // Load primitives unsigned primitive_count = 0; std::vector primitives; primitives.reserve(gltf_mesh.primitives.size()); - for (auto const &gltf_primitive : gltf_mesh.primitives) { + for (auto const& gltf_primitive : gltf_mesh.primitives) { std::string const primitive_identifier = document_path.string() + "." + gltf_mesh.name + "." + ".primitive." + std::to_string(primitive_count); @@ -303,7 +303,7 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul } if (gltf_mesh.name.empty()) { - Log::logger().warn("glTF mesh has no name."); + spdlog::warn("glTF mesh has no name."); } entt::hashed_string gltf_mesh_hash(gltf_mesh.name.c_str()); @@ -318,11 +318,11 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul std::unordered_map> nodes_map; nodes_map.reserve(gltf.nodes.size()); for (std::size_t i = 0; i < gltf.nodes.size(); ++i) { - auto const &node = gltf.nodes.at(i); + auto const& node = gltf.nodes.at(i); auto mesh = [this, &node, &gltf_meshes]() -> std::optional> { if (node.mesh != -1) { - auto const &gltf_mesh = gltf_meshes.at(node.mesh); + auto const& gltf_mesh = gltf_meshes.at(node.mesh); return {gltf_mesh}; } @@ -331,10 +331,10 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul auto camera = [this, &node, &gltf]() -> std::optional { if (node.camera != -1) { - auto const &camera = gltf.cameras.at(node.camera); + auto const& camera = gltf.cameras.at(node.camera); if (camera.type != fx::gltf::Camera::Type::Perspective) { - Log::logger().warn("Only perspective projections supported."); + spdlog::warn("Only perspective projections supported."); } // Only perspective supported until now @@ -353,7 +353,7 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul Transform transform{.translation = translation, .orientation = rotation, .scale = scale}; if (node.name.empty()) { - Log::logger().warn("glTF node has no name."); + spdlog::warn("glTF node has no name."); } entt::hashed_string node_hash(node.name.c_str()); @@ -372,45 +372,45 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul // Resolve child hierarchy // TODO WRONG!!! does only work for 1 child generation for (std::size_t i = 0; i < gltf.nodes.size(); ++i) { - auto const &gltf_node = gltf.nodes.at(i); + auto const& gltf_node = gltf.nodes.at(i); std::vector> children; for (int child_node_id : gltf_node.children) { auto child_node = nodes_map.extract(child_node_id); children.push_back(child_node.mapped()); } - auto const &node = nodes_map.at(i); + auto const& node = nodes_map.at(i); node->children = std::move(children); }; std::vector> nodes; nodes.reserve(nodes_map.size()); - for (auto const &node : nodes_map) { + for (auto const& node : nodes_map) { nodes.push_back(node.second); } // Load scenes std::vector> scenes; - for (auto const &gltf_scene : gltf.scenes) { + for (auto const& gltf_scene : gltf.scenes) { // Get nodes by hash std::vector> nodes; nodes.reserve(gltf_scene.nodes.size()); for (auto node_id : gltf_scene.nodes) { - auto const &node = gltf.nodes.at(node_id); + auto const& node = gltf.nodes.at(node_id); entt::hashed_string node_hash(node.name.c_str()); nodes.push_back(gltf_node_cache[node_hash]); } if (gltf_scene.name.empty()) { - Log::logger().warn("glTF scene has no name."); + spdlog::warn("glTF scene has no name."); } entt::registry registry; // Spawn an entity for every node in scene - for (auto const &node : nodes) { - std::function)> spawn_node = - [this, &spawn_node, ®istry](GltfNode const &node, + for (auto const& node : nodes) { + std::function)> spawn_node = + [this, &spawn_node, ®istry](GltfNode const& node, std::optional parent) { auto entity = registry.create(); registry.emplace(entity, node.name); @@ -425,7 +425,7 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul auto mesh = node.mesh; if (mesh.has_value()) { - for (auto const &primitive : mesh.value()->primitives) { + for (auto const& primitive : mesh.value()->primitives) { auto mesh_entity = registry.create(); registry.emplace(mesh_entity, Parent{.parent = entity}); registry.emplace(mesh_entity, Transform{}); @@ -451,7 +451,7 @@ auto GltfLoader::operator()(std::filesystem::path const &document_path) -> resul } // Spawn child nodes - for (auto const &child : node.children) { + for (auto const& child : node.children) { auto child_entity = spawn_node(child, entity); child_entities.push_back(child_entity); } diff --git a/src/image.cpp b/src/image.cpp index 10d457e..fd107e4 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -1,5 +1,6 @@ #include "image.h" -#include "util/Log.h" + +#include #define STB_IMAGE_IMPLEMENTATION #include @@ -10,15 +11,17 @@ Image::Image(std::span bytes, ColorFormat colorFormat) : colorFor int height{}; int components{}; - uint8_t *stbi_image = - stbi_load_from_memory(bytes.data(), static_cast(bytes.size()), &width, &height, &components, 0); + uint8_t* stbi_image = stbi_load_from_memory( + bytes.data(), static_cast(bytes.size()), &width, &height, &components, 0); std::size_t const buffer_length = static_cast(width * height * components); - // Copy the image data into a vector as stbi currently does not support writing into user-defined buffers. - std::copy(stbi_image, - &stbi_image[buffer_length], // NOLINT (cppcoreguidelines-pro-bounds-pointer-arithmetic) - std::back_inserter(data)); + // Copy the image data into a vector as stbi currently does not support writing into + // user-defined buffers. + std::copy( + stbi_image, + &stbi_image[buffer_length], // NOLINT (cppcoreguidelines-pro-bounds-pointer-arithmetic) + std::back_inserter(data)); dataFormat = [components]() { switch (components) { @@ -32,7 +35,7 @@ Image::Image(std::span bytes, ColorFormat colorFormat) : colorFor return DataFormat::RGBA8Uint; default: - Log::logger().warn("Unsupported data format for image."); + spdlog::warn("Unsupported data format for image."); return DataFormat::RGBA8Uint; } }(); @@ -42,7 +45,7 @@ Image::Image(std::span bytes, ColorFormat colorFormat) : colorFor stbi_image_free(stbi_image); } -GpuImage::GpuImage(Image const &image) +GpuImage::GpuImage(Image const& image) { GLenum internalFormat{}; GLenum dataFormat{}; @@ -57,7 +60,8 @@ GpuImage::GpuImage(Image const &image) dataFormat = GL_RGB; break; case Image::DataFormat::RGBA8Uint: - internalFormat = (image.colorFormat == Image::ColorFormat::SRGB) ? GL_SRGB8_ALPHA8 : GL_RGBA8; + internalFormat = + (image.colorFormat == Image::ColorFormat::SRGB) ? GL_SRGB8_ALPHA8 : GL_RGBA8; dataFormat = GL_RGBA; break; } @@ -65,8 +69,10 @@ GpuImage::GpuImage(Image const &image) glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, static_cast(image.sampler.magFilter)); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, static_cast(image.sampler.minFilter)); + glTexParameteri( + GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, static_cast(image.sampler.magFilter)); + glTexParameteri( + GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, static_cast(image.sampler.minFilter)); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, LOD_BIAS); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, static_cast(image.sampler.wrapS)); diff --git a/src/log.cpp b/src/log.cpp new file mode 100644 index 0000000..739da3c --- /dev/null +++ b/src/log.cpp @@ -0,0 +1,17 @@ +#include "log.h" + +#include + +void Log::initialize() +{ + spdlog::set_pattern("[%H:%M:%S.%e] [%^%l%$] %v"); + +#ifndef NDEBUG + spdlog::set_level(spdlog::level::debug); +#else + spdlog::set_level(spdlog::level::warn); +#endif + + // Override level when running with environment variable. + spdlog::cfg::load_env_levels(); +} diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..525d256 --- /dev/null +++ b/src/log.h @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace Log { + +void initialize(); + +} // namespace Log diff --git a/src/main.cpp b/src/main.cpp index b4180f4..fa69b74 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,17 @@ #include "Controller.h" -#include "util/Log.h" +#include "log.h" #include #include +#include auto main() -> int { -#ifndef NDEBUG - std::cout << "[Debug Mode]" << std::endl; -#endif + Log::initialize(); // Initialize GLFW if (glfwInit() == 0) { - Log::logger().critical("Could not initialize GLFW"); + spdlog::critical("Could not initialize GLFW"); return -1; } diff --git a/src/mesh.cpp b/src/mesh.cpp index d39323c..2e6692c 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -1,15 +1,14 @@ #include "mesh.h" -#include "util/Log.h" #include -GpuMesh::GpuMesh(Mesh const &mesh) +GpuMesh::GpuMesh(Mesh const& mesh) { glGenVertexArrays(1, &vao); glBindVertexArray(vao); // Vertex attributes - for (auto const &[attribute_id, attribute_data] : mesh.attributes) { + for (auto const& [attribute_id, attribute_data] : mesh.attributes) { // BUG: https://github.com/llvm/llvm-project/issues/48582 auto attr_id = attribute_id; @@ -18,7 +17,7 @@ GpuMesh::GpuMesh(Mesh const &mesh) glBindBuffer(GL_ARRAY_BUFFER, vbo); std::visit( - [attr_id](auto &&arg) { + [attr_id](auto&& arg) { glBufferData(GL_ARRAY_BUFFER, arg.size(), arg.data(), GL_STATIC_DRAW); int const components = [arg]() -> int { @@ -56,7 +55,7 @@ GpuMesh::GpuMesh(Mesh const &mesh) glGenBuffers(1, &ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); std::visit( - [this](auto &&arg) { + [this](auto&& arg) { glBufferData(GL_ELEMENT_ARRAY_BUFFER, arg.size(), arg.data(), GL_STATIC_DRAW); indices_count = arg.size(); diff --git a/src/scene.cpp b/src/scene.cpp index 339a393..0340b36 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -6,7 +6,6 @@ #include "relationship.h" #include "shader.h" #include "transform.h" -#include "util/Log.h" #include "Window.h" Scene::Scene(entt::registry registry) : m_registry(std::move(registry)) diff --git a/src/shader.cpp b/src/shader.cpp index 5f2b759..2619747 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -1,11 +1,11 @@ #include "shader.h" -#include "util/Log.h" #include #include #include +#include -Shader::Shader(std::string_view name, std::filesystem::path const &directory) +Shader::Shader(std::string_view name, std::filesystem::path const& directory) : program(glCreateProgram()) { std::filesystem::path vertex_shader_path = directory / name; @@ -27,9 +27,9 @@ Shader::Shader(std::string_view name, std::filesystem::path const &directory) GLint linked = 0; glGetProgramiv(program, GL_LINK_STATUS, &linked); - + if (linked == 0) { - Log::logger().warn(R"(Failed to link Shader "{}")", name); + spdlog::warn(R"(Failed to link Shader "{}")", name); } #ifdef NDEBUG @@ -40,7 +40,7 @@ Shader::Shader(std::string_view name, std::filesystem::path const &directory) glDeleteShader(fragment_shader); #endif - Log::logger().trace(R"(Loaded Shader "{}")", name); + spdlog::trace(R"(Loaded Shader "{}")", name); } Shader::~Shader() @@ -58,13 +58,13 @@ void Shader::unbind() glUseProgram(0); } -auto Shader::parse(const std::filesystem::path &path) -> std::string +auto Shader::parse(std::filesystem::path const& path) -> std::string { std::fstream file; file.open(path, std::ios::in); if (!file.is_open()) { - Log::logger().critical(R"(Shader "{}" not found!)", path.string()); + spdlog::critical(R"(Shader "{}" not found!)", path.string()); std::terminate(); } @@ -74,7 +74,7 @@ auto Shader::parse(const std::filesystem::path &path) -> std::string auto Shader::compile(std::string_view source, GLenum type) -> GLuint { GLuint program = glCreateShader(type); - auto const *src = source.data(); + auto const* src = source.data(); glShaderSource(program, 1, &src, nullptr); glCompileShader(program); @@ -89,7 +89,7 @@ auto Shader::compile(std::string_view source, GLenum type) -> GLuint message.reserve(static_cast(length)); glGetShaderInfoLog(program, length, &length, message.data()); - Log::logger().error("Shader compilation failed: {}", message); + spdlog::error("Shader compilation failed: {}", message); return 0; } @@ -108,14 +108,13 @@ auto Shader::retrieveUniformLocation(std::string_view uniform_name) const -> GLi if (location != -1) { uniform_location_cache[uniform_name.data()] = location; } else { - Log::logger().warn(R"(Uniform "{}" not found.)", uniform_name); + spdlog::warn(R"(Uniform "{}" not found.)", uniform_name); } return location; } -template -void Shader::set_uniform(std::string_view name, T value) const +template void Shader::set_uniform(std::string_view name, T value) const { GLint location = retrieveUniformLocation(name); @@ -126,9 +125,10 @@ void Shader::set_uniform(std::string_view name, T value) const } else if constexpr (std::is_same_v) { glUniform1f(location, value); } else if constexpr (std::is_same_v) { - glUniform2f(location, value.x, value.y); //NOLINT(cppcoreguidelines-pro-type-union-access) + glUniform2f(location, value.x, value.y); // NOLINT(cppcoreguidelines-pro-type-union-access) } else if constexpr (std::is_same_v) { - glUniform3f(location, value.x, value.y, value.z); //NOLINT(cppcoreguidelines-pro-type-union-access) + glUniform3f( + location, value.x, value.y, value.z); // NOLINT(cppcoreguidelines-pro-type-union-access) } else if constexpr (std::is_same_v) { glUniformMatrix3fv(location, 1, GL_FALSE, glm::value_ptr(value)); } else if constexpr (std::is_same_v) { diff --git a/src/util/Log.cpp b/src/util/Log.cpp deleted file mode 100644 index 3a16d0c..0000000 --- a/src/util/Log.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "Log.h" - -#include -#include - -Log Log::s_instance; - -Log::Log() noexcept -{ - m_logger = spdlog::stdout_color_mt("Core"); - m_logger->set_pattern("[%H:%M:%S.%e] [%n] [%^%l%$] %v"); - -#ifndef NDEBUG - m_logger->set_level(spdlog::level::debug); -#else - m_logger->set_level(spdlog::level::warn); -#endif - - // Override level when running with environment variable. - spdlog::cfg::load_env_levels(); -} - -spdlog::logger &Log::logger() -{ - return *s_instance.m_logger; -} diff --git a/src/util/Log.h b/src/util/Log.h deleted file mode 100644 index d4044e1..0000000 --- a/src/util/Log.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include - -class Log -{ -public: - static spdlog::logger &logger(); - -private: - Log() noexcept; - - static Log s_instance; - - std::shared_ptr m_logger; -}; \ No newline at end of file