Skip to content

Commit

Permalink
Merge pull request #56 from NexoEngine/dev
Browse files Browse the repository at this point in the history
fix: fix sonar issues, suppressed warning, improved coverage
  • Loading branch information
iMeaNz authored Dec 2, 2024
2 parents 1741c6f + 48f5dca commit 3f504f2
Show file tree
Hide file tree
Showing 51 changed files with 867 additions and 395 deletions.
81 changes: 41 additions & 40 deletions common/Exception.hpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
//// Exception.hpp ////////////////////////////////////////////////////////////
//
// zzzzz zzz zzzzzzzzzzzzz zzzz zzzz zzzzzz zzzzz
// zzzzzzz zzz zzzz zzzz zzzz zzzz
// zzz zzz zzz zzzzzzzzzzzzz zzzz zzzz zzz
// zzz zzz zzz z zzzz zzzz zzzz zzzz
// zzz zzz zzzzzzzzzzzzz zzzz zzz zzzzzzz zzzzz
//
// Author: Mehdy MORVAN
// Date: 12/11/2024
// Description: Common header file for the nexo base exception class
//
///////////////////////////////////////////////////////////////////////////////
#pragma once

#include <string>

namespace nexo {
class Exception : public std::exception {
public:
explicit Exception(std::string message, const char *file = __FILE__, const int line = __LINE__)
: message(std::move(message)), file(file), line(line) {}

const char *what() const noexcept override;

const std::string &getMessage() const noexcept { return message; }
const char *getFile() const noexcept { return file; }
int getLine() const noexcept { return line; }

protected:
std::string formatMessage() const;

private:
std::string message;
const char *file;
int line;
mutable std::string formattedMessage;
};
}
//// Exception.hpp ////////////////////////////////////////////////////////////
//
// zzzzz zzz zzzzzzzzzzzzz zzzz zzzz zzzzzz zzzzz
// zzzzzzz zzz zzzz zzzz zzzz zzzz
// zzz zzz zzz zzzzzzzzzzzzz zzzz zzzz zzz
// zzz zzz zzz z zzzz zzzz zzzz zzzz
// zzz zzz zzzzzzzzzzzzz zzzz zzz zzzzzzz zzzzz
//
// Author: Mehdy MORVAN
// Date: 12/11/2024
// Description: Common header file for the nexo base exception class
//
///////////////////////////////////////////////////////////////////////////////
#pragma once

#include <string>
#include <source_location>

namespace nexo {
class Exception : public std::exception {
public:
explicit Exception(std::string message, const std::source_location loc)
: message(std::move(message)), file(loc.file_name()), line(loc.line()) {}

const char *what() const noexcept override;

const std::string &getMessage() const noexcept { return message; }
const char *getFile() const noexcept { return file; }
unsigned int getLine() const noexcept { return line; }

protected:
std::string formatMessage() const;

private:
std::string message;
const char *file;
unsigned int line;
mutable std::string formattedMessage;
};
}

#define THROW_EXCEPTION(ExceptionType, ...) \
throw ExceptionType(__VA_OPT__(__VA_ARGS__,) __FILE__, __LINE__)
throw ExceptionType(__VA_OPT__(__VA_ARGS__,) std::source_location::current())

9 changes: 5 additions & 4 deletions common/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <sstream>
#include <format>
#include <string_view>
#include <source_location>

namespace nexo {

Expand Down Expand Up @@ -82,7 +83,7 @@ namespace nexo {
}

template<typename... Args>
static void logWithFormat(const LogLevel level, const char *file, const int line, const std::string_view fmt, Args &&... args)
static void logWithFormat(const LogLevel level, const std::source_location loc, const std::string_view fmt, Args &&... args)
{
auto transformed = std::tuple{toFormatFriendly(std::forward<Args>(args))...};

Expand All @@ -97,7 +98,7 @@ namespace nexo {
else
{
std::stringstream ss;
ss << getFileName(file) << ":" << line << " - " << message;
ss << getFileName(loc.file_name()) << ":" << loc.line() << " - " << message;
logString(level, ss.str());
}
}
Expand All @@ -115,10 +116,10 @@ namespace nexo {
}

#define LOG(level, fmt, ...) \
nexo::Logger::logWithFormat(level, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
nexo::Logger::logWithFormat(level, std::source_location::current(), fmt, ##__VA_ARGS__)

#define LOG_EXCEPTION(exception) \
LOG(NEXO_ERROR, "{}:{} - Exception: {}", exception.getFile(), exception.getLine(), exception.getMessage());
LOG(NEXO_ERROR, "{}:{} - Exception: {}", exception.getFile(), exception.getLine(), exception.getMessage())

#define NEXO_FATAL nexo::LogLevel::FATAL
#define NEXO_ERROR nexo::LogLevel::ERROR
Expand Down
13 changes: 8 additions & 5 deletions common/math/Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
#include <glm/gtc/matrix_transform.hpp>

namespace nexo::math {
void decomposeTransformEuler(const glm::mat4 &mat, glm::vec3 &outTranslation, glm::vec3 &outRotation, glm::vec3 &outScale)
void decomposeTransformEuler(const glm::mat4 &mat, glm::vec3 &outTranslation, glm::vec3 &outRotation,
glm::vec3 &outScale)
{
// Extract translation
outTranslation = glm::vec3(mat[3][0], mat[3][1], mat[3][2]);
Expand All @@ -33,16 +34,18 @@ namespace nexo::math {
if (outScale.z != 0) rotationMatrix[2] /= outScale.z;

// Check for negative scale using the cross product of the first two rows
glm::vec3 crossProduct = glm::cross(glm::vec3(rotationMatrix[0]), glm::vec3(rotationMatrix[1]));
if (glm::dot(crossProduct, glm::vec3(rotationMatrix[2])) < 0) {
if (const glm::vec3 crossProduct = glm::cross(glm::vec3(rotationMatrix[0]), glm::vec3(rotationMatrix[1]));
glm::dot(crossProduct, glm::vec3(rotationMatrix[2])) < 0)
{
outScale.x *= -1;
rotationMatrix[0] = -rotationMatrix[0];
}

// Extract Euler angles (rotation)
outRotation.x = static_cast<float>(atan2(rotationMatrix[1][2], rotationMatrix[2][2])); // Rotation around X-axis
const auto c2 = static_cast<float>(sqrt(rotationMatrix[0][0] * rotationMatrix[0][0] + rotationMatrix[0][1] * rotationMatrix[0][1]));
outRotation.y = static_cast<float>(atan2(-rotationMatrix[0][2], c2)); // Rotation around Y-axis
const auto c2 = static_cast<float>(sqrt(
rotationMatrix[0][0] * rotationMatrix[0][0] + rotationMatrix[0][1] * rotationMatrix[0][1]));
outRotation.y = static_cast<float>(atan2(-rotationMatrix[0][2], c2)); // Rotation around Y-axis
outRotation.z = static_cast<float>(atan2(rotationMatrix[0][1], rotationMatrix[0][0])); // Rotation around Z-axis
}
}
25 changes: 14 additions & 11 deletions editor/src/DocumentWindows/ConsoleWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
///////////////////////////////////////////////////////////////////////////////

#include <imgui.h>
#include <format>
#include "ConsoleWindow.hpp"

namespace nexo::editor {
Expand Down Expand Up @@ -69,24 +70,26 @@ namespace nexo::editor {
items.clear();
}

void ConsoleWindow::addLog(const char *fmt, ...)
template <typename... Args>
void ConsoleWindow::addLog(const char* fmt, Args&&... args)
{
char buf[1024];
va_list args;
va_start(args, fmt);
vsnprintf(buf, IM_ARRAYSIZE(buf), fmt, args);
buf[IM_ARRAYSIZE(buf) - 1] = 0;
va_end(args);
items.emplace_back(buf);
try
{
std::string formattedMessage = std::vformat(fmt, std::make_format_args(std::forward<Args>(args)...));
items.emplace_back(formattedMessage);
}
catch (const std::format_error& e)
{
items.emplace_back(std::format("[Error formatting log message]: {}", e.what()));
}

scrollToBottom = true;
}

void ConsoleWindow::executeCommand(const char *command_line)
void ConsoleWindow::executeCommand(const char* command_line)
{
commands.emplace_back(command_line);

addLog("# %s\n", command_line);
addLog("# {}\n", command_line);
}

void ConsoleWindow::show()
Expand Down
6 changes: 2 additions & 4 deletions editor/src/DocumentWindows/ConsoleWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ namespace nexo::editor {
void show() override;
void update() override;

void addLog(const char* fmt, ...);
template <typename... Args>
void addLog(const char* fmt, Args&&... args);
void executeCommand(const char* command_line);

private:
Expand All @@ -47,9 +48,6 @@ namespace nexo::editor {
loguru::Verbosity_INFO,
};

// Command and variables handling (optional)
// std::map<std::string, std::function<void(const std::string&)>> commandsMap;

const Editor& _editor;

void clearLog();
Expand Down
27 changes: 14 additions & 13 deletions editor/src/DocumentWindows/MainScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ namespace nexo::editor {
// const ecs::Entity basicQuad = EntityFactory2D::createQuad({0.0f, 0.0f, 0.0f}, {1.0f, 1.0f}, 45.0f);
// app.addEntityToScene(basicQuad, _sceneID, static_cast<int>(defaultLayerId));
// app.setAmbientLightValue(_sceneID, 1.0f);
const ecs::Entity basicCube = EntityFactory3D::createCube({0.0f, 0.0f, -2.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f, 0.0f});
app.addEntityToScene(basicCube, _sceneID, static_cast<int>(defaultLayerId));
const ecs::Entity basicCube = EntityFactory3D::createCube({0.0f, 0.0f, -2.0f}, {1.0f, 1.0f, 1.0f},
{0.0f, 0.0f, 0.0f});
app.addEntityToScene(basicCube, _sceneID, static_cast<int>(defaultLayerId));

// const ecs::Entity gunModel = EntityFactory3D::createModel(Path::resolvePathRelativeToExe("../assets/models/9mn/scene.gltf").string(), {0.0f, 0.0f, -2.0f}, {0.01f, 0.01f, 0.01f}, {0.0f, 0.0f, 0.0f});
// app.addEntityToScene(gunModel, _sceneID, static_cast<int>(defaultLayerId));
Expand Down Expand Up @@ -116,7 +117,9 @@ namespace nexo::editor {
void MainScene::setupFramebuffer()
{
renderer::FramebufferSpecs framebufferSpecs;
framebufferSpecs.attachments = {renderer::FrameBufferTextureFormats::RGBA8, renderer::FrameBufferTextureFormats::Depth};
framebufferSpecs.attachments = {
renderer::FrameBufferTextureFormats::RGBA8, renderer::FrameBufferTextureFormats::Depth
};
framebufferSpecs.width = static_cast<unsigned int>(_viewSize.x);
framebufferSpecs.height = static_cast<unsigned int>(_viewSize.y);
m_framebuffer = renderer::Framebuffer::create(framebufferSpecs);
Expand Down Expand Up @@ -312,17 +315,15 @@ namespace nexo::editor {
const auto transformComponent = coord->tryGetComponent<components::TransformComponent>(entity);
const auto renderComponent = coord->tryGetComponent<components::RenderComponent>(entity);

if (renderComponent && transformComponent)
if (renderComponent && transformComponent && renderComponent->get().renderable->isClicked(
transformComponent->get(), mouseWorldPosition))
{
if (renderComponent->get().renderable->isClicked(transformComponent->get(), mouseWorldPosition))
{
m_sceneManagerBridge->setSelectedEntity(-1);
EntityProperties props{};
props.entity = entity;
m_sceneManagerBridge->setData(props);
m_sceneManagerBridge->setSelectionType(SelectionType::ENTITY);
return;
}
m_sceneManagerBridge->setSelectedEntity(-1);
EntityProperties props{};
props.entity = entity;
m_sceneManagerBridge->setData(props);
m_sceneManagerBridge->setSelectionType(SelectionType::ENTITY);
return;
}
}
m_sceneManagerBridge->unselectEntity();
Expand Down
3 changes: 1 addition & 2 deletions editor/src/DocumentWindows/MainScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "ADocumentWindow.hpp"
#include "core/scene/SceneManager.hpp"
#include "renderer/Framebuffer.hpp"
//#include "core/camera/PerspectiveCameraController.hpp"
#include "core/camera/CameraController.hpp"
#include <imgui.h>
#include <ImGuizmo.h>
Expand All @@ -33,7 +32,7 @@ namespace nexo::editor {
void update() override;

[[nodiscard]] const std::string &getName() const {return m_sceneName;};
void setName(const std::string &name) {m_sceneName = name; };
void setName(const std::string_view name) { m_sceneName = name; };

void setupFramebuffer();

Expand Down
4 changes: 2 additions & 2 deletions editor/src/DocumentWindows/PopupManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ namespace nexo::editor {
m_popups[popupName] = true;
}

void PopupManager::closePopup()
void PopupManager::closePopup() const
{
ImGui::EndPopup();
}

void PopupManager::closePopupInContext()
void PopupManager::closePopupInContext() const
{
ImGui::CloseCurrentPopup();
}
Expand Down
15 changes: 11 additions & 4 deletions editor/src/DocumentWindows/PopupManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@ namespace nexo::editor {
void openPopup(const std::string &popupName);
bool showPopupModal(const std::string &popupModalName);
bool showPopup(const std::string &popupName);
void closePopup();
void closePopupInContext();
void closePopup() const;
void closePopupInContext() const;

VariantData &getUserData(const std::string &popupName);
void setUserData(const std::string &popupName, const VariantData &data);

private:
std::unordered_map<std::string, bool> m_popups;
std::unordered_map<std::string, VariantData> m_userData;
struct TransparentHasher {
using is_transparent = void; // Required for heterogeneous lookup
std::size_t operator()(std::string_view key) const noexcept {
return std::hash<std::string_view>{}(key);
}
};

std::unordered_map<std::string, bool, TransparentHasher, std::equal_to<>> m_popups;
std::unordered_map<std::string, VariantData, TransparentHasher, std::equal_to<>> m_userData;
};
}
Loading

0 comments on commit 3f504f2

Please sign in to comment.