From d1937d4f261f7cf5670fb56864125571a10be8cb Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Tue, 15 Oct 2024 19:55:38 +0200 Subject: [PATCH] Updated logger and minors tweaks --- CMakeLists.txt | 1 - Engine/Graphics/3DRenderer.cpp | 4 +-- Engine/Misc/Console.cpp | 0 Engine/Misc/Console.hpp | 0 Engine/Misc/Logger.cpp | 54 ++++++++++++++++++++++++++-------- Engine/Misc/Logger.hpp | 10 ++++--- Game.cpp | 3 +- Game.hpp | 1 + ProtoTank.cpp | 24 ++++++++++----- srcs.list | 2 -- 10 files changed, 69 insertions(+), 30 deletions(-) delete mode 100644 Engine/Misc/Console.cpp delete mode 100644 Engine/Misc/Console.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 83fbc9e..f141cc0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,7 +46,6 @@ endif() # targets declarations add_executable(${PROJECT_NAME}) target_precompile_headers(${PROJECT_NAME} PRIVATE - "$<$:>" "$<$:>" "$<$:>" "$<$:>" diff --git a/Engine/Graphics/3DRenderer.cpp b/Engine/Graphics/3DRenderer.cpp index 979449a..80b77a2 100644 --- a/Engine/Graphics/3DRenderer.cpp +++ b/Engine/Graphics/3DRenderer.cpp @@ -104,7 +104,7 @@ void Graphic3DRenderer::Draw(sf::RenderTexture& context) { auto& oMesh = obj->GetObjectMesh(); M3D_V3TransformPersDiv( projVertices + processedVerticesCnt, sizeof(M3D_F3), - (M3D_F3*)oMesh.vertices.data(), sizeof(Vertex), + reinterpret_cast(oMesh.vertices.data()), sizeof(M3D_F4), oMesh.vertices.size(), obj->GetTransform() * viewProjMat ); @@ -112,7 +112,7 @@ void Graphic3DRenderer::Draw(sf::RenderTexture& context) { //TODO: Fill a z-depth buffer... for (auto& objPt : obj->GetObjectMesh().parts) { - auto indicePtr = (uint32_t*)objPt.indices.data(); + auto indicePtr = static_cast(objPt.indices.data()); for (uint32_t i = 0; i < objPt.GetIndicesCount(); i += 3) { // Misscontructed indices tree failsafe if (i+2 > objPt.GetIndicesCount()) diff --git a/Engine/Misc/Console.cpp b/Engine/Misc/Console.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/Engine/Misc/Console.hpp b/Engine/Misc/Console.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/Engine/Misc/Logger.cpp b/Engine/Misc/Logger.cpp index 9a1594a..c64c072 100644 --- a/Engine/Misc/Logger.cpp +++ b/Engine/Misc/Logger.cpp @@ -1,6 +1,7 @@ #include "Logger.hpp" #include +#include enum LOG_TYPE{ @@ -15,44 +16,73 @@ Logger* Logger::smInstance = nullptr; /* ------------------------------------------------------------------------------------------------------------------------------------------------- */ -static void LogConsolePrint(const LOG_TYPE logType, const std::string& message, const double time) { - std::cout << std::fixed << std::setprecision(6) << '[' << time; - +inline static std::string GetLabelType(const LOG_TYPE logType) { switch (logType) { case LOG_TYPE_DBG: - std::cout << "][DEBUG] "; + return "][DEBUG] "; break; case LOG_TYPE_CRIT: - std::cout << "][CRIT] "; + return "][CRIT] "; break; case LOG_TYPE_WARN: - std::cout << "][WARN] "; + return "][WARN] "; break; case LOG_TYPE_INFO: default: - std::cout << "][INFO] "; + return "][INFO] "; break; } +} +static void LogConsolePrint(const LOG_TYPE logType, const std::string& message, const double time) { + std::cout << std::fixed << std::setprecision(6) << '[' << time; + std::cout << GetLabelType(logType); std::cout << message << std::endl; } +static void LogFilePrint(const LOG_TYPE logType, const std::string& message, const double time) { + std::ofstream outLogFile; + outLogFile.open("log.txt", std::ios::app); + + if (outLogFile.is_open()) { + outLogFile << std::fixed << std::setprecision(6) << '[' << time; + outLogFile << GetLabelType(logType); + outLogFile << message << std::endl; + + outLogFile.close(); + } +} + /* ------------------------------------------------------------------------------------------------------------------------------------------------- */ Logger::Logger() { mLogTimer = std::make_unique(); - PrintInfo("Log module ready"); + LogConsolePrint(LOG_TYPE_INFO, "Log module ready", mLogTimer->GetElapsedTimeS()); } -void Logger::PrintInfo(std::string _in) { LogConsolePrint(LOG_TYPE_INFO, _in, mLogTimer->GetElapsedTimeS()); } +void Logger::PrintInfo(std::string _in, bool writeToFile) { + LogConsolePrint(LOG_TYPE_INFO, _in, mLogTimer->GetElapsedTimeS()); + if (writeToFile) + LogFilePrint(LOG_TYPE_INFO, _in, mLogTimer->GetElapsedTimeS()); +} -void Logger::PrintWarning(std::string _in) { LogConsolePrint(LOG_TYPE_WARN, _in, mLogTimer->GetElapsedTimeS()); } +void Logger::PrintWarning(std::string _in, bool writeToFile) { + LogConsolePrint(LOG_TYPE_WARN, _in, mLogTimer->GetElapsedTimeS()); + if (writeToFile) + LogFilePrint(LOG_TYPE_WARN, _in, mLogTimer->GetElapsedTimeS()); +} -void Logger::PrintCritical(std::string _in) { LogConsolePrint(LOG_TYPE_CRIT, _in, mLogTimer->GetElapsedTimeS()); } +void Logger::PrintCritical(std::string _in, bool writeToFile) { + LogConsolePrint(LOG_TYPE_CRIT, _in, mLogTimer->GetElapsedTimeS()); + if (writeToFile) + LogFilePrint(LOG_TYPE_CRIT, _in, mLogTimer->GetElapsedTimeS()); +} -void Logger::PrintDebug(std::string _in) { +void Logger::PrintDebug(std::string _in, bool writeToFile) { #ifdef _DEBUG LogConsolePrint(LOG_TYPE_DBG, _in, mLogTimer->GetElapsedTimeS()); + if (writeToFile) + LogFilePrint(LOG_TYPE_DBG, _in, mLogTimer->GetElapsedTimeS()); #endif } \ No newline at end of file diff --git a/Engine/Misc/Logger.hpp b/Engine/Misc/Logger.hpp index cb3589e..a5af55a 100644 --- a/Engine/Misc/Logger.hpp +++ b/Engine/Misc/Logger.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include "../Utils/Timers.hpp" @@ -19,10 +21,10 @@ public: Logger(Logger const&) = delete; Logger& operator= (Logger const&) = delete; - void PrintInfo(std::string inPrt); - void PrintWarning(std::string inPrt); - void PrintCritical(std::string inPrt); - void PrintDebug(std::string inPrt); + void PrintInfo(std::string inPrt, bool writeToFile = false); + void PrintWarning(std::string inPrt, bool writeToFile = false); + void PrintCritical(std::string inPrt, bool writeToFile = false); + void PrintDebug(std::string inPrt, bool writeToFile = false); private: Logger() noexcept(false); diff --git a/Game.cpp b/Game.cpp index 1b6b8a5..3712928 100644 --- a/Game.cpp +++ b/Game.cpp @@ -5,6 +5,7 @@ #include #include +#include "Engine/Misc/Logger.hpp" #include "Engine/Utils/Perfs.hpp" using std::make_shared; @@ -39,7 +40,7 @@ GAME_STATUS Game::Tick() { msTicksMonitoring.push_back(((1000000.f / TARGET_FPS) - mLastCycleSleepDelta) * TARGET_FPS / 1000000.f); if (mPerfsTimer->GetDeltaTime() >= 1000) { // Every 1s // This average CPU/cycle_time method can monitor when CPU is in overload state (>0%) or in underload one (<0%) - double cpuUsage = 200 * std::accumulate(msTicksMonitoring.begin(), msTicksMonitoring.end(), (double)(0)) / msTicksMonitoring.size() - 100; + double cpuUsage = 200 * std::accumulate(msTicksMonitoring.begin(), msTicksMonitoring.end(), static_cast(0)) / msTicksMonitoring.size() - 100; mDbgUI->UpdateDebugData(cpuUsage, (((1000000.f / TARGET_FPS) - mLastCycleSleepDelta) / 1000), ((-mPrevdelta + mSysTimer.GetDeltaTimeUs()) / 1000), mFrameCnt, (PerfsGetVirtMem() / 1000), (PerfsGetPhysMem() / 1000)); diff --git a/Game.hpp b/Game.hpp index d8b096b..af17f9a 100644 --- a/Game.hpp +++ b/Game.hpp @@ -14,6 +14,7 @@ typedef enum eGameStatus { GAME_QUIT } GAME_STATUS; +//TODO: Use thread for Update/Render instance class Game final { public: static Game& getInstance(std::shared_ptr mainWnd, bool dbgFlag) { diff --git a/ProtoTank.cpp b/ProtoTank.cpp index 3612500..a0a1c6c 100644 --- a/ProtoTank.cpp +++ b/ProtoTank.cpp @@ -9,7 +9,7 @@ #include "Game.hpp" -static std::shared_ptr InitWindow(); +static std::shared_ptr InitWindow(unsigned int width, unsigned int height, bool fullscreen); int main(int argc, char** argv) { Logger& log = Logger::getInstance(); @@ -17,7 +17,7 @@ int main(int argc, char** argv) { log.PrintInfo(LOGGER_MSG_FT("Create main window")); std::shared_ptr mainWindow = nullptr; try { - mainWindow = InitWindow(); + mainWindow = InitWindow(1280, 720, false); } catch (const std::exception& ex) { log.PrintCritical(std::string(ex.what())); return EXIT_FAILURE; @@ -30,7 +30,6 @@ int main(int argc, char** argv) { for ( ;; ) { sf::Event event; while (mainWindow->pollEvent(event)) { - if (event.type == sf::Event::Closed) switch (event.type) { case sf::Event::Closed: mainWindow->close(); @@ -43,11 +42,11 @@ int main(int argc, char** argv) { break; //case sf::Event::LostFocus: - // pause(); + // EventSuspending(); // break; //case sf::Event::GainedFocus: - // resume(); + // EventResuming(); // break; default: @@ -67,6 +66,11 @@ int main(int argc, char** argv) { } } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { + mainWindow->close(); + status = GAME_QUIT; + } + status = arcadeGame.Tick(); if (status == GAME_QUIT) @@ -77,7 +81,7 @@ int main(int argc, char** argv) { return EXIT_SUCCESS; } -static std::shared_ptr InitWindow() { +static std::shared_ptr InitWindow(unsigned int width, unsigned int height, bool fullscreen) { // Create default game window sf::ContextSettings sWindowSettings; sWindowSettings.depthBits = 0; @@ -85,9 +89,13 @@ static std::shared_ptr InitWindow() { sWindowSettings.antialiasingLevel = 8; sWindowSettings.sRgbCapable = true; + sf::Uint32 wndStyle = sf::Style::Close | sf::Style::Titlebar; + if(fullscreen) + wndStyle = wndStyle | sf::Style::Fullscreen; + auto wnd = std::make_shared( - sf::VideoMode(1280, 720), "ProtoTank", - (sf::Style::Close | sf::Style::Titlebar), + sf::VideoMode(width, height), "ProtoTank", + wndStyle, sWindowSettings ); diff --git a/srcs.list b/srcs.list index 149e2d8..009603b 100644 --- a/srcs.list +++ b/srcs.list @@ -17,8 +17,6 @@ set(UTILS_SCRS Engine/Utils/Perfs.hpp ) set(MISC_SCRS - Engine/Misc/Console.cpp - Engine/Misc/Console.hpp Engine/Misc/Logger.cpp Engine/Misc/Logger.hpp Engine/Misc/Fonts.hpp