From 5e78c10358fcccd664c9dd80c64cb27eb9ada2a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Thu, 22 Feb 2018 01:37:55 +0800 Subject: [PATCH] Add struct MemSlot for future use, See #1249 (#1342) --- src/bgfx.cpp | 18 ++++++++++-------- src/bgfx_p.h | 41 +++++++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 3ffd800ae..ec661487a 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -561,7 +561,7 @@ namespace bgfx num = bx::vsnprintf(temp, num, _format, argListCopy); uint8_t attr = _attr; - uint8_t* mem = &m_mem[(_y*m_width+_x)*2]; + struct MemSlot* mem = &m_mem[_y*m_width+_x]; for (uint32_t ii = 0, xx = _x; ii < num && xx < m_width; ++ii) { char ch = temp[ii]; @@ -573,9 +573,9 @@ namespace bgfx } else { - mem[0] = ch; - mem[1] = attr; - mem += 2; + mem->character = ch; + mem->attribute = attr; + ++mem; ++xx; } } @@ -697,12 +697,14 @@ namespace bgfx for (; yy < _mem.m_height && numIndices < numBatchIndices; ++yy) { xx = xx < _mem.m_width ? xx : 0; - const uint8_t* line = &_mem.m_mem[(yy*_mem.m_width+xx)*2]; + const struct TextVideoMem::MemSlot* line = &_mem.m_mem[yy*_mem.m_width+xx]; for (; xx < _mem.m_width && numIndices < numBatchIndices; ++xx) { - uint8_t ch = line[0]; - uint8_t attr = line[1]; + uint32_t ch = line->character; + uint8_t attr = line->attribute; + if (ch > 0xff) + ch = 0; // todo: render unicode code point , ch > 255) if (0 != (ch|attr) && (' ' != ch || 0 != (attr&0xf0) ) ) @@ -734,7 +736,7 @@ namespace bgfx numIndices += 6; } - line += 2; + line ++; } if (numIndices >= numBatchIndices) diff --git a/src/bgfx_p.h b/src/bgfx_p.h index df2f0a27d..2e7cc057f 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -493,25 +493,26 @@ namespace bgfx m_height = (uint16_t)height; uint32_t size = m_size; - m_size = m_width * m_height * 2; + m_size = m_width * m_height; - m_mem = (uint8_t*)BX_REALLOC(g_allocator, m_mem, m_size); + m_mem = (struct MemSlot*)BX_REALLOC(g_allocator, m_mem, m_size * sizeof(struct MemSlot)); if (size < m_size) { - bx::memSet(&m_mem[size], 0, m_size-size); + bx::memSet(&m_mem[size], 0, (m_size-size) * sizeof(struct MemSlot)); } } } void clear(uint8_t _attr = 0) { - uint8_t* mem = m_mem; - for (uint32_t ii = 0, num = m_size/2; ii < num; ++ii) - { - mem[0] = 0; - mem[1] = _attr; - mem += 2; + struct MemSlot* mem = m_mem; + bx::memSet(mem, 0, m_size * sizeof(struct MemSlot)); + if (_attr != 0) { + for (uint32_t ii = 0, num = m_size; ii < num; ++ii) + { + mem[ii].attribute = _attr; + } } } @@ -529,16 +530,28 @@ namespace bgfx { if (_x < m_width && _y < m_height) { - uint8_t* dst = &m_mem[(_y*m_width+_x)*2]; + struct MemSlot * dst = &m_mem[_y*m_width+_x]; const uint8_t* src = (const uint8_t*)_data; - const uint32_t width = (bx::min(m_width, _width +_x)-_x)*2; + const uint32_t width = bx::min(m_width, _width +_x)-_x; const uint32_t height = bx::min(m_height, _height+_y)-_y; - const uint32_t dstPitch = m_width*2; - bx::memCopy(dst, src, width, height, _pitch, dstPitch); + const uint32_t dstPitch = m_width; + for (uint32_t ii = 0; ii < height; ++ii) { + for (uint32_t jj = 0; jj < width; ++jj) { + dst[jj].character = src[jj*2]; + dst[jj].attribute = src[jj*2+1]; + } + src += _pitch; + dst += dstPitch; + } } } - uint8_t* m_mem; + struct MemSlot { + uint8_t attribute; + uint8_t character; + }; + + struct MemSlot* m_mem; uint32_t m_size; uint16_t m_width; uint16_t m_height;