From adae2767b68a64e90f48577b05124b806c96061c Mon Sep 17 00:00:00 2001 From: Graham Pentheny Date: Wed, 4 Jun 2025 00:50:56 -0400 Subject: [PATCH] Cleanup SampleInterfaces and use std::vector and std::string to retain log messages. --- RecastDemo/Include/SampleInterfaces.h | 39 ++++---- RecastDemo/Source/SampleInterfaces.cpp | 123 +++++++++++++++---------- 2 files changed, 88 insertions(+), 74 deletions(-) diff --git a/RecastDemo/Include/SampleInterfaces.h b/RecastDemo/Include/SampleInterfaces.h index d9c9a13b..d14afd51 100644 --- a/RecastDemo/Include/SampleInterfaces.h +++ b/RecastDemo/Include/SampleInterfaces.h @@ -18,29 +18,25 @@ #pragma once -#include -#include - #include "DebugDraw.h" #include "PerfTimer.h" #include "Recast.h" #include "RecastDump.h" +#include +#include +#include +#include + // These are example implementations of various interfaces used in Recast and Detour. /// Recast build context. class BuildContext : public rcContext { - std::array m_startTime; - std::array m_accTime; + std::array startTime; + std::array accTime; - static const int MAX_MESSAGES = 1000; - const char* m_messages[MAX_MESSAGES]; - int m_messageCount = 0; - - static const int TEXT_POOL_SIZE = 8000; - char m_textPool[TEXT_POOL_SIZE]; - int m_textPoolSize = 0; + std::vector logMessages; public: BuildContext(); @@ -53,15 +49,12 @@ public: const char* getLogText(const int i) const; protected: - /// Virtual functions for custom implementations. - ///@{ - virtual void doResetLog(); - virtual void doLog(rcLogCategory category, const char* msg, const int len); - virtual void doResetTimers(); - virtual void doStartTimer(rcTimerLabel label); - virtual void doStopTimer(rcTimerLabel label); - virtual int doGetAccumulatedTime(rcTimerLabel label) const; - ///@} + void doResetLog() override; + void doLog(rcLogCategory category, const char* msg, const int len) override; + void doResetTimers() override; + void doStartTimer(rcTimerLabel label) override; + void doStopTimer(rcTimerLabel label) override; + int doGetAccumulatedTime(rcTimerLabel label) const override; }; /// OpenGL debug draw implementation. @@ -97,6 +90,6 @@ public: virtual bool read(void* ptr, const size_t size); private: - FILE* m_fp = nullptr; - int m_mode = -1; + FILE* fp = nullptr; + int mode = -1; }; diff --git a/RecastDemo/Source/SampleInterfaces.cpp b/RecastDemo/Source/SampleInterfaces.cpp index 7836236a..91b25ac8 100644 --- a/RecastDemo/Source/SampleInterfaces.cpp +++ b/RecastDemo/Source/SampleInterfaces.cpp @@ -1,10 +1,7 @@ #include "SampleInterfaces.h" -#include "DetourDebugDraw.h" #include "PerfTimer.h" #include "Recast.h" -#include "RecastDebugDraw.h" -#include "SDL.h" #include "SDL_opengl.h" #include @@ -19,57 +16,57 @@ BuildContext::BuildContext() { - memset(m_messages, 0, sizeof(char*) * MAX_MESSAGES); resetTimers(); } void BuildContext::doResetLog() { - m_messageCount = 0; - m_textPoolSize = 0; + logMessages.clear(); } void BuildContext::doLog(const rcLogCategory category, const char* msg, const int len) { - if (!len) { return; } - if (m_messageCount >= MAX_MESSAGES) { return; } - char* dst = &m_textPool[m_textPoolSize]; - int n = TEXT_POOL_SIZE - m_textPoolSize; - if (n < 2) { return; } - char* cat = dst; - char* text = dst + 1; - const int maxtext = n - 1; - // Store category - *cat = (char)category; - // Store message - const int count = rcMin(len + 1, maxtext); - memcpy(text, msg, count); - text[count - 1] = '\0'; - m_textPoolSize += 1 + count; - m_messages[m_messageCount++] = dst; + if (len == 0) + { + return; + } + + logMessages.emplace_back(); + std::string& message = logMessages[logMessages.size() - 1]; + message.push_back((char)category); + message.append(msg); } void BuildContext::doResetTimers() { - for (int i = 0; i < RC_MAX_TIMERS; ++i) { m_accTime[i] = -1; } + for (int i = 0; i < RC_MAX_TIMERS; ++i) + { + accTime[i] = -1; + } } void BuildContext::doStartTimer(const rcTimerLabel label) { - m_startTime[label] = getPerfTime(); + startTime[label] = getPerfTime(); } void BuildContext::doStopTimer(const rcTimerLabel label) { const TimeVal endTime = getPerfTime(); - const TimeVal deltaTime = endTime - m_startTime[label]; - if (m_accTime[label] == -1) { m_accTime[label] = deltaTime; } - else { m_accTime[label] += deltaTime; } + const TimeVal deltaTime = endTime - startTime[label]; + if (accTime[label] == -1) + { + accTime[label] = deltaTime; + } + else + { + accTime[label] += deltaTime; + } } int BuildContext::doGetAccumulatedTime(const rcTimerLabel label) const { - return getPerfTimeUsec(m_accTime[label]); + return getPerfTimeUsec(accTime[label]); } void BuildContext::dumpLog(const char* format, ...) @@ -83,9 +80,10 @@ void BuildContext::dumpLog(const char* format, ...) // Print messages const int TAB_STOPS[4] = {28, 36, 44, 52}; - for (int i = 0; i < m_messageCount; ++i) + for (int i = 0; i < logMessages.size(); ++i) { - const char* msg = m_messages[i] + 1; + std::string& message = logMessages[i]; + const char* msg = message.c_str() + 1; int n = 0; while (*msg) { @@ -119,12 +117,12 @@ void BuildContext::dumpLog(const char* format, ...) int BuildContext::getLogCount() const { - return m_messageCount; + return static_cast(logMessages.size()); } const char* BuildContext::getLogText(const int i) const { - return m_messages[i] + 1; + return logMessages[i].c_str() + 1; } //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -136,7 +134,8 @@ class GLCheckerTexture public: ~GLCheckerTexture() { - if (m_texId != 0) { + if (m_texId != 0) + { glDeleteTextures(1, &m_texId); } } @@ -211,8 +210,12 @@ void DebugDrawGL::begin(duDebugDrawPrimitives prim, float size) glLineWidth(size); glBegin(GL_LINES); break; - case DU_DRAW_TRIS: glBegin(GL_TRIANGLES); break; - case DU_DRAW_QUADS: glBegin(GL_QUADS); break; + case DU_DRAW_TRIS: + glBegin(GL_TRIANGLES); + break; + case DU_DRAW_QUADS: + glBegin(GL_QUADS); + break; }; } @@ -253,50 +256,68 @@ void DebugDrawGL::end() FileIO::~FileIO() { - if (m_fp) + if (fp) { - fclose(m_fp); + fclose(fp); } } bool FileIO::openForWrite(const char* path) { - if (m_fp) { return false; } - m_fp = fopen(path, "wb"); - if (!m_fp) { return false; } - m_mode = 1; + if (fp) + { + return false; + } + fp = fopen(path, "wb"); + if (!fp) + { + return false; + } + mode = 1; return true; } bool FileIO::openForRead(const char* path) { - if (m_fp) { return false; } - m_fp = fopen(path, "rb"); - if (!m_fp) { return false; } - m_mode = 2; + if (fp) + { + return false; + } + fp = fopen(path, "rb"); + if (!fp) + { + return false; + } + mode = 2; return true; } bool FileIO::isWriting() const { - return m_mode == 1; + return mode == 1; } bool FileIO::isReading() const { - return m_mode == 2; + return mode == 2; } bool FileIO::write(const void* ptr, const size_t size) { - if (!m_fp || m_mode != 1) { return false; } - fwrite(ptr, size, 1, m_fp); + if (!fp || mode != 1) + { + return false; + } + fwrite(ptr, size, 1, fp); return true; } bool FileIO::read(void* ptr, const size_t size) { - if (!m_fp || m_mode != 2) { return false; } - size_t readLen = fread(ptr, size, 1, m_fp); + if (!fp || mode != 2) + { + return false; + } + size_t readLen = fread(ptr, size, 1, fp); return readLen == 1; }