From 0e933091edf2deced2f19941b6579e4a6481bffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Mon, 28 Nov 2016 21:00:57 -0800 Subject: [PATCH] Added ANSI escape codes for debug text. Added info about backbuffer and debug text width/height. --- examples/00-helloworld/helloworld.cpp | 10 ++++ include/bgfx/bgfx.h | 5 ++ include/bgfx/bgfxdefines.h | 2 +- include/bgfx/c99/bgfx.h | 6 +++ src/bgfx.cpp | 72 +++++++++++++++++++++++++++ src/bgfx_p.h | 27 ++++------ 6 files changed, 103 insertions(+), 19 deletions(-) diff --git a/examples/00-helloworld/helloworld.cpp b/examples/00-helloworld/helloworld.cpp index 987ec881b..ff67d3138 100644 --- a/examples/00-helloworld/helloworld.cpp +++ b/examples/00-helloworld/helloworld.cpp @@ -65,6 +65,16 @@ class ExampleHelloWorld : public entry::AppI bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/00-helloworld"); bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Initialization and debug text."); + bgfx::dbgTextPrintf(0, 4, 0x0f, "Color can be changed with ANSI \e[9;me\e[10;ms\e[11;mc\e[12;ma\e[13;mp\e[14;me\e[0m code too."); + + const bgfx::Stats* stats = bgfx::getStats(); + bgfx::dbgTextPrintf(0, 6, 0x0f, "Backbuffer %dW x %dH in pixels, debug text %dW x %dH in characters." + , stats->width + , stats->height + , stats->textWidth + , stats->textHeight + ); + // Advance to next frame. Rendering thread will be kicked to // process submitted rendering primitives. bgfx::frame(); diff --git a/include/bgfx/bgfx.h b/include/bgfx/bgfx.h index 01d6383ec..1d3d0ae0f 100644 --- a/include/bgfx/bgfx.h +++ b/include/bgfx/bgfx.h @@ -720,6 +720,11 @@ namespace bgfx int64_t waitRender; //!< Time spent waiting for render backend thread to finish issuing //! draw commands to underlying graphics API. int64_t waitSubmit; //!< Time spent waiting for submit thread to advance to next frame. + + uint16_t width; + uint16_t height; + uint16_t textWidth; + uint16_t textHeight; }; /// Vertex declaration. diff --git a/include/bgfx/bgfxdefines.h b/include/bgfx/bgfxdefines.h index 290211386..89d05781b 100644 --- a/include/bgfx/bgfxdefines.h +++ b/include/bgfx/bgfxdefines.h @@ -6,7 +6,7 @@ #ifndef BGFX_DEFINES_H_HEADER_GUARD #define BGFX_DEFINES_H_HEADER_GUARD -#define BGFX_API_VERSION UINT32_C(32) +#define BGFX_API_VERSION UINT32_C(33) /// #define BGFX_STATE_RGB_WRITE UINT64_C(0x0000000000000001) //!< Enable RGB write. diff --git a/include/bgfx/c99/bgfx.h b/include/bgfx/c99/bgfx.h index 2a5683145..203de8734 100644 --- a/include/bgfx/c99/bgfx.h +++ b/include/bgfx/c99/bgfx.h @@ -329,6 +329,12 @@ typedef struct bgfx_stats int64_t waitRender; int64_t waitSubmit; + + uint16_t width; + uint16_t height; + uint16_t textWidth; + uint16_t textHeight; + } bgfx_stats_t; /**/ diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 1f53cad90..969d2c0af 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -529,6 +529,78 @@ namespace bgfx } } + static uint8_t parseAttrTo(char*& _ptr, char _to, uint8_t _default) + { + const char* str = strchr(_ptr, _to); + if (NULL != str + && 3 > str-_ptr) + { + char tmp[4]; + uint32_t len = uint32_t(str-_ptr); + + len = bx::uint32_min(BX_COUNTOF(tmp), len); + strncpy(tmp, _ptr, len); + tmp[len] = '\0'; + + uint8_t attr = uint8_t(atoi(tmp) ); + _ptr += len+1; + return attr; + } + + return _default; + } + + static uint8_t parseAttr(char*& _ptr, uint8_t _default) + { + char* ptr = _ptr; + if (*ptr++ != '[') + { + return _default; + } + + if (0 == strncmp(ptr, "0m", 2) ) + { + _ptr = ptr + 2; + return _default; + } + + uint8_t fg = parseAttrTo(ptr, ';', _default & 0xf); + uint8_t bg = parseAttrTo(ptr, 'm', _default >> 4); + + uint8_t attr = (bg<<4) | fg; + _ptr = ptr; + return attr; + } + + void TextVideoMem::printfVargs(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, va_list _argList) + { + if (_x < m_width && _y < m_height) + { + char* temp = (char*)alloca(m_width); + + uint32_t num = bx::vsnprintf(temp, m_width, _format, _argList); + + uint8_t attr = _attr; + uint8_t* mem = &m_mem[(_y*m_width+_x)*2]; + for (uint32_t ii = 0, xx = _x; ii < num && xx < m_width; ++ii, ++xx) + { + char ch = temp[ii]; + if (BX_UNLIKELY(ch == '\e') ) + { + char* ptr = &temp[ii+1]; + attr = parseAttr(ptr, _attr); + ii += uint32_t(ptr - &temp[ii+1]); + } + else + { + mem[0] = ch; + mem[1] = attr; + mem += 2; + } + } + } + } + static const uint32_t numCharsPerBatch = 1024; static const uint32_t numBatchVertices = numCharsPerBatch*4; static const uint32_t numBatchIndices = numCharsPerBatch*6; diff --git a/src/bgfx_p.h b/src/bgfx_p.h index 4f71b2db3..1e83aefeb 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -433,23 +433,7 @@ namespace bgfx } } - void printfVargs(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, va_list _argList) - { - if (_x < m_width && _y < m_height) - { - char* temp = (char*)alloca(m_width); - - uint32_t num = bx::vsnprintf(temp, m_width, _format, _argList); - - uint8_t* mem = &m_mem[(_y*m_width+_x)*2]; - for (uint32_t ii = 0, xx = _x; ii < num && xx < m_width; ++ii, ++xx) - { - mem[0] = temp[ii]; - mem[1] = _attr; - mem += 2; - } - } - } + void printfVargs(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, va_list _argList); void printf(uint16_t _x, uint16_t _y, uint8_t _attr, const char* _format, ...) { @@ -2288,7 +2272,14 @@ namespace bgfx BGFX_API_FUNC(const Stats* getPerfStats() ) { - return &m_submit->m_perfStats; + Stats& stats = m_submit->m_perfStats; + const Resolution& resolution = m_submit->m_resolution; + stats.width = uint16_t(resolution.m_width); + stats.height = uint16_t(resolution.m_height); + const TextVideoMem* tvm = m_submit->m_textVideoMem; + stats.textWidth = tvm->m_width; + stats.textHeight = tvm->m_height; + return &stats; } BGFX_API_FUNC(IndexBufferHandle createIndexBuffer(const Memory* _mem, uint16_t _flags) )