diff --git a/engine/asset/src/asset.cpp b/engine/asset/src/asset.cpp index 1f4ca80..3619fc9 100644 --- a/engine/asset/src/asset.cpp +++ b/engine/asset/src/asset.cpp @@ -20,7 +20,7 @@ std::unique_ptr load_mesh(const file::Path path) { auto file = file::open(path, true); if(!file.has_value()) { - console::error(System::Renderer, "Failed to load mesh from {}!", path); + prism::log::error(System::Renderer, "Failed to load mesh from {}!", path); return nullptr; } @@ -29,7 +29,7 @@ std::unique_ptr load_mesh(const file::Path path) { if(version == 5 || version == 6) { } else { - console::error(System::Renderer, "{} failed the mesh version check! reported version = {}", path, std::to_string(version)); + prism::log::error(System::Renderer, "{} failed the mesh version check! reported version = {}", path, std::to_string(version)); return nullptr; } @@ -190,7 +190,7 @@ std::unique_ptr load_texture(const file::Path path) { auto file = file::open(path, true); if(!file.has_value()) { - console::error(System::Renderer, "Failed to load texture from {}!", path); + prism::log::error(System::Renderer, "Failed to load texture from {}!", path); return nullptr; } @@ -202,7 +202,7 @@ std::unique_ptr load_texture(const file::Path path) { int width, height, channels; unsigned char* data = stbi_load_from_memory(file->cast_data(), file->size(), &width, &height, &channels, 4); if(!data) { - console::error(System::Renderer, "Failed to load texture from {}!", path); + prism::log::error(System::Renderer, "Failed to load texture from {}!", path); return nullptr; } @@ -247,7 +247,7 @@ std::unique_ptr load_material(const file::Path path) { auto file = file::open(path); if(!file.has_value()) { - console::error(System::Core, "Failed to load material from {}!", path); + prism::log::error(System::Core, "Failed to load material from {}!", path); return {}; } @@ -258,7 +258,7 @@ std::unique_ptr load_material(const file::Path path) { mat->path = path.string(); if(!j.count("version") || j["version"] != 2) { - console::error(System::Core, "Material {} failed the version check!", path); + prism::log::error(System::Core, "Material {} failed the version check!", path); return mat; } diff --git a/engine/core/include/console.hpp b/engine/core/include/console.hpp index a8fd8b3..4fe6642 100755 --- a/engine/core/include/console.hpp +++ b/engine/core/include/console.hpp @@ -6,46 +6,47 @@ #include #include -namespace console { - enum class ArgType { +namespace prism::console { + enum class argument_type { String, Integer, Boolean }; - - struct ConsoleArgument { - ConsoleArgument(bool data) : data(data) {} - ConsoleArgument(int data) : data(data) {} - - ArgType query_type() const { - if(std::holds_alternative(data)) - return ArgType::String; - else if(std::holds_alternative(data)) - return ArgType::Integer; - else if(std::holds_alternative(data)) - return ArgType::Boolean; + + struct console_argument { + explicit console_argument(bool data) : data(data) {} + + explicit console_argument(int data) : data(data) {} + + [[nodiscard]] argument_type query_type() const { + if (std::holds_alternative(data)) + return argument_type::String; + else if (std::holds_alternative(data)) + return argument_type::Integer; + else if (std::holds_alternative(data)) + return argument_type::Boolean; } - + std::variant data; }; - - struct Arguments { - std::vector arguments; - }; - - using FunctionPtr = std::function; - struct ArgumentFormat { - ArgumentFormat(const int num_arguments) : num_arguments(num_arguments) {} - + using arguments = std::vector; + + using function_ptr = std::function; + + struct argument_format { + explicit argument_format(const int num_arguments) : num_arguments(num_arguments) {} + int num_arguments = 0; - std::vector argument_types; + std::vector argument_types; }; - - void register_command(const std::string_view name, const ArgumentFormat expected_format, const FunctionPtr function); - - void invoke_command(const std::string_view name, const Arguments arguments); - void parse_and_invoke_command(const std::string_view command); - void register_variable(const std::string_view name, bool& variable); -} + void + register_command(std::string_view name, argument_format expected_format, function_ptr function); + + void invoke_command(std::string_view name, arguments arguments); + + void parse_and_invoke_command(std::string_view command); + + void register_variable(std::string_view name, bool &variable); +} \ No newline at end of file diff --git a/engine/core/src/console.cpp b/engine/core/src/console.cpp index 35875a8..d93f507 100644 --- a/engine/core/src/console.cpp +++ b/engine/core/src/console.cpp @@ -7,41 +7,41 @@ #include "log.hpp" #include "string_utils.hpp" -struct RegisteredCommand { - RegisteredCommand(const console::ArgumentFormat format, const console::FunctionPtr function) : expected_format(format), function(function) {} - - console::ArgumentFormat expected_format; - std::function function; +struct registered_command { + registered_command(const prism::console::argument_format format, const prism::console::function_ptr function) : expected_format(format), function(function) {} + + prism::console::argument_format expected_format; + std::function function; }; -static std::unordered_map registered_commands; +static std::unordered_map registered_commands; struct RegisteredVariable { - RegisteredVariable(bool& data) : type(console::ArgType::Boolean), data(&data) {} - - console::ArgType type; + RegisteredVariable(bool& data) : type(prism::console::argument_type::Boolean), data(&data) {} + + prism::console::argument_type type; std::variant data; }; static std::unordered_map registered_variables; -void console::register_command(const std::string_view name, const ArgumentFormat expected_format, const console::FunctionPtr function) { - registered_commands.try_emplace(name.data(), RegisteredCommand(expected_format, function)); +void prism::console::register_command(const std::string_view name, const prism::console::argument_format expected_format, const prism::console::function_ptr function) { + registered_commands.try_emplace(name.data(), registered_command(expected_format, function)); } -void console::invoke_command(const std::string_view name, const Arguments arguments) { +void prism::console::invoke_command(const std::string_view name, const prism::console::arguments arguments) { for(auto& [command_name, command_data] : registered_commands) { if(command_name == name) { bool invalid_format = false; - if(arguments.arguments.size() != command_data.expected_format.num_arguments) + if(arguments.size() != command_data.expected_format.num_arguments) invalid_format = true; if(invalid_format) { - console::info(System::Core, "Invalid command format!"); + prism::log::info(System::Core, "Invalid command format!"); } else { - command_data.function(console::Arguments()); + command_data.function(console::arguments()); } return; @@ -52,18 +52,18 @@ void console::invoke_command(const std::string_view name, const Arguments argume if(variable_name == name) { bool invalid_format = false; - if(arguments.arguments.empty()) + if(arguments.empty()) invalid_format = true; - if(arguments.arguments[0].query_type() != variable_data.type) + if(arguments[0].query_type() != variable_data.type) invalid_format = true; if(invalid_format) { - console::info(System::Core, "Wrong or empty variable type!"); + prism::log::info(System::Core, "Wrong or empty variable type!"); } else { - auto argument = arguments.arguments[0]; + auto argument = arguments[0]; switch(argument.query_type()) { - case console::ArgType::Boolean: + case console::argument_type::Boolean: *std::get(variable_data.data) = std::get(argument.data); break; } @@ -73,39 +73,36 @@ void console::invoke_command(const std::string_view name, const Arguments argume } } - console::info(System::Core, "{} is not the name of a valid command or variable!", name.data()); + prism::log::info(System::Core, "{} is not the name of a valid command or variable!", name.data()); } -void console::parse_and_invoke_command(const std::string_view command) { +void prism::console::parse_and_invoke_command(const std::string_view command) { const auto tokens = tokenize(command, " "); if(tokens.empty()) return; - const auto try_parse_argument = [](std::string_view arg) -> std::optional { + const auto try_parse_argument = [](std::string_view arg) -> std::optional { if(arg == "true") { - return ConsoleArgument(true); + return console_argument(true); } else if(arg == "false") { - return ConsoleArgument(false); + return console_argument(false); } else if(is_numeric(arg)) { - return ConsoleArgument(std::stoi(arg.data())); + return console_argument(std::stoi(arg.data())); } return std::nullopt; }; - std::vector arguments; + std::vector arguments; for(int i = 1; i < tokens.size(); i++) { if(auto arg = try_parse_argument(tokens[i]); arg) arguments.push_back(*arg); } - - console::Arguments args; - args.arguments = arguments; - - console::invoke_command(tokens[0], args); + + console::invoke_command(tokens[0], arguments); } -void console::register_variable(const std::string_view name, bool& variable) { +void prism::console::register_variable(const std::string_view name, bool& variable) { registered_variables.try_emplace(name.data(), RegisteredVariable(variable)); } diff --git a/engine/core/src/engine.cpp b/engine/core/src/engine.cpp index feff6a2..373336d 100755 --- a/engine/core/src/engine.cpp +++ b/engine/core/src/engine.cpp @@ -27,15 +27,15 @@ using prism::engine; engine::engine(const int argc, char* argv[]) { - console::info(System::Core, "Prism Engine loading..."); + log::info(System::Core, "Prism Engine loading..."); - console::register_command("test_cmd", console::ArgumentFormat(0), [](const console::Arguments&) { - console::info(System::Core, "Test cmd!"); + console::register_command("test_cmd", console::argument_format(0), [](const console::arguments&) { + log::info(System::Core, "Test cmd!"); }); console::register_variable("rs_dynamic_resolution", render_options.dynamic_resolution); - console::register_command("quit", console::ArgumentFormat(0), [this](const console::Arguments&) { + console::register_command("quit", console::argument_format(0), [this](const console::arguments&) { quit(); }); @@ -134,7 +134,7 @@ Scene* engine::load_scene(const file::Path& path) { auto file = file::open(path); if(!file.has_value()) { - console::error(System::Core, "Failed to load scene from {}!", path); + prism::log::error(System::Core, "Failed to load scene from {}!", path); return nullptr; } @@ -227,7 +227,7 @@ Animation engine::load_animation(const file::Path& path) { auto file = file::open(path, true); if(!file.has_value()) { - console::error(System::Core, "Failed to load animation from {}!", path); + prism::log::error(System::Core, "Failed to load animation from {}!", path); return {}; } @@ -287,7 +287,7 @@ void engine::load_cutscene(const file::Path& path) { auto file = file::open(path); if(!file.has_value()) { - console::error(System::Core, "Failed to load cutscene from {}!", path); + prism::log::error(System::Core, "Failed to load cutscene from {}!", path); return; } @@ -362,7 +362,7 @@ Object engine::add_prefab(Scene& scene, const file::Path& path, const std::strin auto file = file::open(path); if(!file.has_value()) { - console::error(System::Core, "Failed to load prefab from {}!", path); + prism::log::error(System::Core, "Failed to load prefab from {}!", path); return NullObject; } diff --git a/engine/core/src/file.cpp b/engine/core/src/file.cpp index 8bfb351..0fcb88e 100755 --- a/engine/core/src/file.cpp +++ b/engine/core/src/file.cpp @@ -21,7 +21,7 @@ std::optional file::open(const file::Path path, const bool binary_mo auto str = get_file_path(path).string(); FILE* file = fopen(str.c_str(), binary_mode ? "rb" : "r"); if(file == nullptr) { - console::error(System::File, "Failed to open file handle from {}!", str); + prism::log::error(System::File, "Failed to open file handle from {}!", str); return {}; } diff --git a/engine/core/src/screen.cpp b/engine/core/src/screen.cpp index 8354805..b5516b6 100755 --- a/engine/core/src/screen.cpp +++ b/engine/core/src/screen.cpp @@ -97,7 +97,7 @@ UIElement* ui::Screen::find_element(const std::string& id) { ui::Screen::Screen(const file::Path path) { auto file = file::open(path); if(!file.has_value()) { - console::error(System::Core, "Failed to load UI from {}!", path); + prism::log::error(System::Core, "Failed to load UI from {}!", path); return; } diff --git a/engine/gfx/vulkan/src/gfx_vulkan.cpp b/engine/gfx/vulkan/src/gfx_vulkan.cpp index fa2dfdc..a4fa7cd 100755 --- a/engine/gfx/vulkan/src/gfx_vulkan.cpp +++ b/engine/gfx/vulkan/src/gfx_vulkan.cpp @@ -165,14 +165,14 @@ VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback( const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData, void *pUserData) { - console::debug(System::GFX, pCallbackData->pMessage); + prism::log::debug(System::GFX, pCallbackData->pMessage); return VK_FALSE; } VkResult name_object(VkDevice device, VkObjectType type, uint64_t object, std::string_view name) { if(object == 0x0) { - console::error(System::GFX, "Failed to name object {}", name); + prism::log::error(System::GFX, "Failed to name object {}", name); return VK_ERROR_DEVICE_LOST; } @@ -537,7 +537,7 @@ void GFXVulkan::copy_texture(GFXTexture* texture, void* data, GFXSize size) { } void GFXVulkan::copy_texture(GFXTexture* from, GFXTexture* to) { - console::error(System::GFX, "Copy Texture->Texture unimplemented!"); + prism::log::error(System::GFX, "Copy Texture->Texture unimplemented!"); } void GFXVulkan::copy_texture(GFXTexture* from, GFXBuffer* to) { @@ -1688,7 +1688,7 @@ void GFXVulkan::resetDescriptorState() { void GFXVulkan::cacheDescriptorState(GFXVulkanPipeline* pipeline, VkDescriptorSetLayout layout) { uint64_t hash = getDescriptorHash(pipeline); - console::debug(System::GFX, "Caching descriptor hash {}", std::to_string(hash)); + prism::log::debug(System::GFX, "Caching descriptor hash {}", std::to_string(hash)); vkDeviceWaitIdle(device); @@ -1703,7 +1703,7 @@ void GFXVulkan::cacheDescriptorState(GFXVulkanPipeline* pipeline, VkDescriptorSe VkResult error = vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet); if(error == VK_ERROR_OUT_OF_POOL_MEMORY) { - console::error(System::GFX, "ERROR: COULD NOT CACHE BECAUSE OUT OF DESCRIPTOR SETS."); + prism::log::error(System::GFX, "ERROR: COULD NOT CACHE BECAUSE OUT OF DESCRIPTOR SETS."); } if(error != VK_SUCCESS) diff --git a/engine/log/include/log.hpp b/engine/log/include/log.hpp index 7c53350..9839e9e 100755 --- a/engine/log/include/log.hpp +++ b/engine/log/include/log.hpp @@ -20,45 +20,45 @@ enum class Level { Debug }; -namespace console { - inline void internal_format(std::string& msg, const std::string& arg) { +namespace prism::log { + inline void internal_format(std::string &msg, const std::string &arg) { auto pos = msg.find_first_of("{}"); msg.replace(pos, 2, arg); } - inline void internal_format(std::string& msg, const char*& arg) { + inline void internal_format(std::string &msg, const char *&arg) { auto pos = msg.find_first_of("{}"); msg.replace(pos, 2, arg); } - + void process_message(const Level level, const System system, const std::string_view message); - + template - void internal_print(const Level level, const System system, const std::string_view format, Args&&... args) { + void internal_print(const Level level, const System system, const std::string_view format, Args &&... args) { auto msg = std::string(format); - + ((internal_format(msg, args)), ...); - + process_message(level, system, msg); } - + template - void info(const System system, const std::string_view format, Args&&... args) { + void info(const System system, const std::string_view format, Args &&... args) { internal_print(Level::Info, system, format, args...); } - + template - void warning(const System system, const std::string_view format, Args&&... args) { + void warning(const System system, const std::string_view format, Args &&... args) { internal_print(Level::Warning, system, format, args...); } - + template - void error(const System system, const std::string_view format, Args&&... args) { + void error(const System system, const std::string_view format, Args &&... args) { internal_print(Level::Error, system, format, args...); } - + template - void debug(const System system, const std::string_view format, Args&&... args) { + void debug(const System system, const std::string_view format, Args &&... args) { internal_print(Level::Debug, system, format, args...); } diff --git a/engine/log/src/log.cpp b/engine/log/src/log.cpp index 1c3608d..490d183 100755 --- a/engine/log/src/log.cpp +++ b/engine/log/src/log.cpp @@ -6,7 +6,7 @@ #include "string_utils.hpp" #include "utility.hpp" -void console::process_message(const Level level, const System system, const std::string_view message) { +void prism::log::process_message(const Level level, const System system, const std::string_view message) { auto now = std::chrono::system_clock::now(); std::time_t t_c = std::chrono::system_clock::to_time_t(now); diff --git a/engine/renderer/src/imguipass.cpp b/engine/renderer/src/imguipass.cpp index 7f10bc3..b828ff9 100755 --- a/engine/renderer/src/imguipass.cpp +++ b/engine/renderer/src/imguipass.cpp @@ -162,7 +162,7 @@ void ImGuiPass::load_font(const std::string_view filename) { io.Fonts->AddFontFromMemoryTTF(font_file->cast_data(), font_file->size(), 15.0 * platform::get_window_dpi(0)); ImGui::GetIO().FontGlobalScale = 1.0 / platform::get_window_dpi(0); } else { - console::error(System::Renderer, "Failed to load font file for imgui!"); + prism::log::error(System::Renderer, "Failed to load font file for imgui!"); return; } } diff --git a/engine/renderer/src/materialcompiler.cpp b/engine/renderer/src/materialcompiler.cpp index e421b71..aa83ce3 100755 --- a/engine/renderer/src/materialcompiler.cpp +++ b/engine/renderer/src/materialcompiler.cpp @@ -13,7 +13,7 @@ ShaderSource get_shader(std::string filename, bool skinned, bool cubemap) { auto shader_file = file::open(file::internal_domain / filename); if(!shader_file.has_value()) { - console::error(System::Renderer, "Failed to open shader file {}!", filename); + prism::log::error(System::Renderer, "Failed to open shader file {}!", filename); return {}; } diff --git a/engine/renderer/src/renderer.cpp b/engine/renderer/src/renderer.cpp index b290894..d886ea1 100755 --- a/engine/renderer/src/renderer.cpp +++ b/engine/renderer/src/renderer.cpp @@ -887,7 +887,7 @@ void Renderer::createPostPipelines() { void Renderer::createFontTexture() { auto file = file::open(file::app_domain / "font.fp", true); if(file == std::nullopt) { - console::error(System::Renderer, "Failed to load font file!"); + prism::log::error(System::Renderer, "Failed to load font file!"); return; } diff --git a/engine/shadercompiler/src/shadercompiler.cpp b/engine/shadercompiler/src/shadercompiler.cpp index b2a451d..cac9a3d 100755 --- a/engine/shadercompiler/src/shadercompiler.cpp +++ b/engine/shadercompiler/src/shadercompiler.cpp @@ -54,7 +54,7 @@ const std::vector compile_glsl_to_spv(const std::string_view source_st includer.pushExternalLocalDirectory(path); if (!Shader.parse(&Resources, 100, false, messages, includer)) { - console::error(System::Renderer, "{}", Shader.getInfoLog()); + prism::log::error(System::Renderer, "{}", Shader.getInfoLog()); return {}; } @@ -63,7 +63,7 @@ const std::vector compile_glsl_to_spv(const std::string_view source_st Program.addShader(&Shader); if(!Program.link(messages)) { - console::error(System::None, "Failed to link shader: {} {} {}", source_string.data(), Shader.getInfoLog(), Shader.getInfoDebugLog()); + prism::log::error(System::None, "Failed to link shader: {} {} {}", source_string.data(), Shader.getInfoLog(), Shader.getInfoDebugLog()); return {}; } @@ -78,7 +78,7 @@ const std::vector compile_glsl_to_spv(const std::string_view source_st std::optional ShaderCompiler::compile(const ShaderLanguage from_language, const ShaderStage shader_stage, const ShaderSource& shader_source, const ShaderLanguage to_language, const CompileOptions& options) { if(from_language != ShaderLanguage::GLSL) { - console::error(System::Renderer, "Non-supported input language!"); + prism::log::error(System::Renderer, "Non-supported input language!"); return std::nullopt; } @@ -97,7 +97,7 @@ std::optional ShaderCompiler::compile(const ShaderLanguage from_la auto spirv = compile_glsl_to_spv(shader_source.as_string(), lang, options); if(spirv.empty()) { - console::error(System::Renderer, "SPIRV generation failed!"); + prism::log::error(System::Renderer, "SPIRV generation failed!"); return std::nullopt; } diff --git a/engine/utility/include/file_utils.hpp b/engine/utility/include/file_utils.hpp index d199933..7483f56 100644 --- a/engine/utility/include/file_utils.hpp +++ b/engine/utility/include/file_utils.hpp @@ -7,7 +7,7 @@ namespace file { using Path = std::filesystem::path; } -namespace console { +namespace prism::log { inline void internal_format(std::string& msg, const file::Path& arg) { auto pos = msg.find_first_of("{}"); msg.replace(pos, 2, arg.string()); diff --git a/tools/common/src/commoneditor.cpp b/tools/common/src/commoneditor.cpp index 0ef9c05..5f79457 100755 --- a/tools/common/src/commoneditor.cpp +++ b/tools/common/src/commoneditor.cpp @@ -751,7 +751,7 @@ void CommonEditor::set_undo_stack(UndoStack *stack) { bool mesh_readable(const file::Path path) { auto file = file::open(path); if(!file.has_value()) { - console::error(System::Renderer, "Failed to load mesh from {}!", path); + prism::log::error(System::Renderer, "Failed to load mesh from {}!", path); return false; } @@ -764,7 +764,7 @@ bool mesh_readable(const file::Path path) { bool material_readable(const file::Path path) { auto file = file::open(path); if(!file.has_value()) { - console::error(System::Core, "Failed to load material from {}!", path); + prism::log::error(System::Core, "Failed to load material from {}!", path); return false; } @@ -1057,13 +1057,13 @@ void CommonEditor::drawConsole() { ImGui::SameLine(); if(ImGui::Button("Run")) { - console::parse_and_invoke_command(command_buffer); + prism::console::parse_and_invoke_command(command_buffer); command_buffer.clear(); } ImGui::BeginChild("console_output", ImVec2(-1, -1), true); - for(const auto& message : console::stored_output) + for(const auto& message : prism::log::stored_output) ImGui::TextWrapped("%s", message.c_str()); ImGui::EndChild(); @@ -1140,7 +1140,7 @@ void CommonEditor::save_thumbnail_cache() { FILE* file = fopen("thumbnail-cache", "wb"); if(file == nullptr) { - console::error(System::Core, "Failed to write thumbnail cache!"); + prism::log::error(System::Core, "Failed to write thumbnail cache!"); return; } diff --git a/tools/shadercompiler/main.cpp b/tools/shadercompiler/main.cpp index 5a04605..ab1ad10 100755 --- a/tools/shadercompiler/main.cpp +++ b/tools/shadercompiler/main.cpp @@ -13,7 +13,7 @@ bool has_extension(const std::filesystem::path path, const std::string_view exte int main(int argc, char* argv[]) { if(argc < 2) { - console::error(System::Core, "Not enough arguments!"); + prism::log::error(System::Core, "Not enough arguments!"); return -1; } @@ -33,8 +33,8 @@ int main(int argc, char* argv[]) { std::ofstream out(outname + ".glsl"); out << buffer.rdbuf(); - - console::info(System::Core, "Successfully written {} to {}.", source_path, destination_path); + + prism::log::info(System::Core, "Successfully written {} to {}.", source_path, destination_path); return 0; } else { @@ -60,7 +60,7 @@ int main(int argc, char* argv[]) { const auto compiled_source = shader_compiler.compile(ShaderLanguage::GLSL, stage, buffer.str(), language, options); if(!compiled_source.has_value()) { - console::error(System::Core, "Error when compiling {}!", source_path); + prism::log::error(System::Core, "Error when compiling {}!", source_path); return -1; } @@ -82,8 +82,8 @@ int main(int argc, char* argv[]) { default: break; } - - console::info(System::Core, "Successfully written shader from {} to {}.", source_path, destination_path); + + prism::log::info(System::Core, "Successfully written shader from {} to {}.", source_path, destination_path); return 0; }