From f9aa198eef7b85eeba3bac4c4fe2d4578b836bbf Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 13 Oct 2024 20:52:08 +0200 Subject: WIP behavior script (problems) --- src/crepe/util/log.cpp | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) (limited to 'src/crepe/util/log.cpp') diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp index f91d52c..b46620d 100644 --- a/src/crepe/util/log.cpp +++ b/src/crepe/util/log.cpp @@ -4,6 +4,7 @@ #include #include "log.h" +#include "fmt.h" using namespace crepe::util; @@ -14,37 +15,27 @@ static const char * const LOG_PREFIX[] = { [log_level::ERROR] = "[ERR] ", }; -static void va_logf(enum log_level level, va_list args, const std::string fmt) { - va_list args_copy; - va_copy(args_copy, args); - - // prepend log level and ensure newline - std::string format_fixed = LOG_PREFIX[level] + fmt; - if (!format_fixed.ends_with("\n")) format_fixed += "\n"; - - size_t sz = vsnprintf(NULL, 0, format_fixed.c_str(), args_copy) + 1; - char * msg = (char *) malloc(sz); - va_end(args_copy); - - vsnprintf(msg, sz, format_fixed.c_str(), args); +static void log(enum log_level level, const std::string msg) { + using namespace std; + string final = string(LOG_PREFIX[level]) + msg; + if (!final.ends_with("\n")) final += "\n"; // TODO: also log to file or smth - printf("%s", msg); + printf("%s", final.c_str()); fflush(stdout); - - free(msg); } void crepe::util::logf(const char * fmt, ...) { va_list args; va_start(args, fmt); - va_logf(crepe::util::log_level::DEBUG, args, fmt); + log(log_level::DEBUG, va_stringf(args, fmt)); va_end(args); } void crepe::util::logf(log_level level, const char * fmt, ...) { va_list args; va_start(args, fmt); - va_logf(level, args, fmt); + log(level, va_stringf(args, fmt)); va_end(args); } + -- cgit v1.2.3 From a2c719948ff8af9abe267c4d1c9b16cd1e2fb317 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Thu, 17 Oct 2024 17:15:53 +0200 Subject: `make format` --- src/crepe/Component.cpp | 1 - src/crepe/Component.h | 1 + src/crepe/ComponentManager.cpp | 11 +++-------- src/crepe/ComponentManager.hpp | 9 ++++++--- src/crepe/ScriptSystem.cpp | 17 ++++++----------- src/crepe/ScriptSystem.h | 4 +--- src/crepe/System.h | 7 +++---- src/crepe/api/BehaviorScript.cpp | 7 ++----- src/crepe/api/BehaviorScript.h | 8 ++++---- src/crepe/api/BehaviorScript.hpp | 4 ++-- src/crepe/api/Script.cpp | 2 -- src/crepe/api/Script.h | 4 ++-- src/crepe/util/fmt.cpp | 5 ++--- src/crepe/util/fmt.h | 2 +- src/crepe/util/log.cpp | 3 +-- src/example/script.cpp | 11 ++++------- 16 files changed, 38 insertions(+), 58 deletions(-) (limited to 'src/crepe/util/log.cpp') diff --git a/src/crepe/Component.cpp b/src/crepe/Component.cpp index 737f30a..bce90f1 100644 --- a/src/crepe/Component.cpp +++ b/src/crepe/Component.cpp @@ -1,4 +1,3 @@ #include "Component.h" using namespace crepe; - diff --git a/src/crepe/Component.h b/src/crepe/Component.h index 00b2164..d9a01ac 100644 --- a/src/crepe/Component.h +++ b/src/crepe/Component.h @@ -5,6 +5,7 @@ namespace crepe { class Component { protected: Component() = default; + public: virtual ~Component() = default; // TODO: shouldn't this constructor be deleted because this class will never diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index 8aa9d4e..8bde33a 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -1,5 +1,5 @@ -#include "util/log.h" #include "ComponentManager.h" +#include "util/log.h" using namespace crepe; @@ -24,11 +24,6 @@ void ComponentManager::delete_all_components() { this->components.clear(); } -ComponentManager::ComponentManager() { - dbg_trace(); -} - -ComponentManager::~ComponentManager() { - dbg_trace(); -} +ComponentManager::ComponentManager() { dbg_trace(); } +ComponentManager::~ComponentManager() { dbg_trace(); } diff --git a/src/crepe/ComponentManager.hpp b/src/crepe/ComponentManager.hpp index e0242a2..2377a94 100644 --- a/src/crepe/ComponentManager.hpp +++ b/src/crepe/ComponentManager.hpp @@ -10,7 +10,8 @@ template T & ComponentManager::add_component(uint32_t id, Args &&... args) { using namespace std; - static_assert(is_base_of::value, "add_component must recieve a derivative class of Component"); + static_assert(is_base_of::value, + "add_component must recieve a derivative class of Component"); // Determine the type of T (this is used as the key of the unordered_map<>) type_index type = typeid(T); @@ -81,7 +82,8 @@ ComponentManager::get_components_by_id(uint32_t id) const { if (components.find(type) == components.end()) return component_vector; // Get the correct vector<> - const vector>> & component_array = components.at(type); + const vector>> & component_array + = components.at(type); // Make sure that the id (that we are looking for) is within the boundaries of the vector<> if (id >= component_array.size()) return component_vector; @@ -117,7 +119,8 @@ ComponentManager::get_components_by_type() const { if (components.find(type) == components.end()) return component_vector; // Get the correct vector<> - const vector>> & component_array = components.at(type); + const vector>> & component_array + = components.at(type); // Loop through the whole vector<> for (const vector> & component : component_array) { diff --git a/src/crepe/ScriptSystem.cpp b/src/crepe/ScriptSystem.cpp index 1a7bff4..5d882be 100644 --- a/src/crepe/ScriptSystem.cpp +++ b/src/crepe/ScriptSystem.cpp @@ -2,8 +2,8 @@ #include #include -#include "ScriptSystem.h" #include "ComponentManager.h" +#include "ScriptSystem.h" #include "api/BehaviorScript.h" #include "api/Script.h" #include "util/log.h" @@ -12,12 +12,8 @@ using namespace std; using namespace crepe; using namespace crepe::api; -ScriptSystem::ScriptSystem() { - dbg_trace(); -} -ScriptSystem::~ScriptSystem() { - dbg_trace(); -} +ScriptSystem::ScriptSystem() { dbg_trace(); } +ScriptSystem::~ScriptSystem() { dbg_trace(); } ScriptSystem & ScriptSystem::get_instance() { static ScriptSystem instance; @@ -29,14 +25,14 @@ void ScriptSystem::update() { dbg_trace(); forward_list