diff --git a/NothinFancy/CMakeLists.txt b/NothinFancy/CMakeLists.txt index 1b1e8fe..ab4e473 100644 --- a/NothinFancy/CMakeLists.txt +++ b/NothinFancy/CMakeLists.txt @@ -1,5 +1,5 @@ # NF library CMakeLists.txt -add_library(NothinFancy STATIC "src/Engine.cpp" "src/include/nf.h" "src/pch.h" "src/util.h" "src/util/log.h" "src/util/log.cpp" "src/include/nf/EngineConfig.h") +add_library(NothinFancy STATIC "src/Engine.cpp" "src/include/nf.h" "src/pch.h" "src/util.h" "src/util/log.h" "src/util/log.cpp" "src/include/nf/EngineConfig.h" "src/util/util.cpp") # Use C++20 set_property(TARGET NothinFancy PROPERTY CXX_STANDARD 20) diff --git a/NothinFancy/src/Engine.cpp b/NothinFancy/src/Engine.cpp index cb123f6..cb74e67 100644 --- a/NothinFancy/src/Engine.cpp +++ b/NothinFancy/src/Engine.cpp @@ -16,9 +16,11 @@ namespace nf { SetThreadDescription(GetCurrentThread(), L"NF Main Thread"); SetConsoleTitle(std::format("{} Debug Console - {}", engineStr, gameStr).c_str()); #endif - - std::this_thread::sleep_for(std::chrono::seconds(1)); - NFLog("Test!"); + { + NFTime(); + std::this_thread::sleep_for(std::chrono::seconds(1)); + NFLog("Test!"); + } std::cin.get(); } diff --git a/NothinFancy/src/util.h b/NothinFancy/src/util.h index ce1c3e0..38ec996 100644 --- a/NothinFancy/src/util.h +++ b/NothinFancy/src/util.h @@ -2,3 +2,20 @@ #pragma once #include "util/log.h" + +#ifdef _DEBUG +#define NFTime() ::nf::util::ScopedTimer __scopeTimer(__FUNCSIG__) +#else +#define NFTime() +#endif + +namespace nf::util { + class ScopedTimer { + public: + ScopedTimer(const char* funcName); + ~ScopedTimer(); + private: + const char* m_funcName; + std::chrono::time_point m_startTime; + }; +} diff --git a/NothinFancy/src/util/log.cpp b/NothinFancy/src/util/log.cpp index b1184e0..6e740d8 100644 --- a/NothinFancy/src/util/log.cpp +++ b/NothinFancy/src/util/log.cpp @@ -16,6 +16,7 @@ namespace nf::util { // Mapping of type strings static std::map s_logTypesMap = { {LogType::Log, "\x1b[93mLog\x1b[0m"}, + {LogType::Timing, "\x1b[92mTiming\x1b[0m"}, {LogType::Error, "\x1b[91mError\x1b[0m"} }; diff --git a/NothinFancy/src/util/log.h b/NothinFancy/src/util/log.h index cac49a4..a1c3e92 100644 --- a/NothinFancy/src/util/log.h +++ b/NothinFancy/src/util/log.h @@ -14,6 +14,7 @@ namespace nf::util { enum class LogType { Log, + Timing, Error }; diff --git a/NothinFancy/src/util/util.cpp b/NothinFancy/src/util/util.cpp new file mode 100644 index 0000000..ef34527 --- /dev/null +++ b/NothinFancy/src/util/util.cpp @@ -0,0 +1,16 @@ +// Miscellaneous utilities implementation +#include "pch.h" + +#include "util.h" + +namespace nf::util { + ScopedTimer::ScopedTimer(const char* funcName) + : m_funcName(funcName) + , m_startTime(std::chrono::high_resolution_clock::now()) + {} + + ScopedTimer::~ScopedTimer() { + auto duration = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - m_startTime); + ::nf::util::log(std::format("Scope in {} took {:.3f} ms", m_funcName, duration.count() * 1e-3), ::nf::util::LogType::Timing); + } +}