diff --git a/examples/10-font/font.cpp b/examples/10-font/font.cpp index 3b91fd542..1cba4a52b 100644 --- a/examples/10-font/font.cpp +++ b/examples/10-font/font.cpp @@ -16,42 +16,29 @@ #include #include -TrueTypeHandle loadTtf(FontManager* _fm, const char* _fontPath) +long int fsize(FILE* _file) { - FILE* pFile; - pFile = fopen(_fontPath, "rb"); - if (NULL != pFile) + long int pos = ftell(_file); + fseek(_file, 0L, SEEK_END); + long int size = ftell(_file); + fseek(_file, pos, SEEK_SET); + return size; +} + +TrueTypeHandle loadTtf(FontManager* _fm, const char* _filePath) +{ + FILE* file = fopen(_filePath, "rb"); + if (NULL != file) { - if (0 == fseek(pFile, 0L, SEEK_END) ) - { - // Get the size of the file. - long bufsize = ftell(pFile); - if (bufsize == -1) - { - fclose(pFile); - TrueTypeHandle invalid = BGFX_INVALID_HANDLE; - return invalid; - } - - uint8_t* buffer = new uint8_t[bufsize]; - - // Go back to the start of the file. - fseek(pFile, 0L, SEEK_SET); - - // Read the entire file into memory. - uint32_t newLen = fread( (void*)buffer, sizeof(char), bufsize, pFile); - if (newLen == 0) - { - fclose(pFile); - delete [] buffer; - TrueTypeHandle invalid = BGFX_INVALID_HANDLE; - return invalid; - } - - fclose(pFile); - - return _fm->createTtf(buffer, bufsize); - } + uint32_t size = (uint32_t)fsize(file); + uint8_t* mem = (uint8_t*)malloc(size+1); + size_t ignore = fread(mem, 1, size, file); + BX_UNUSED(ignore); + fclose(file); + mem[size-1] = '\0'; + TrueTypeHandle handle = _fm->createTtf(mem, size); + free(mem); + return handle; } TrueTypeHandle invalid = BGFX_INVALID_HANDLE; @@ -183,14 +170,20 @@ int _main_(int /*_argc*/, char** /*_argv*/) const double freq = double(bx::getHPFrequency() ); const double toMs = 1000.0 / freq; + // Use debug font to print information about this example. + bgfx::dbgTextClear(); + bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/10-font"); + bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Use the font system to display text and styled text."); + bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs); + // Use transient text to display debug information. wchar_t fpsText[64]; bx::swnprintf(fpsText, BX_COUNTOF(fpsText), L"Frame: % 7.3f[ms]", double(frameTime) * toMs); textBufferManager->clearTextBuffer(transientText); - textBufferManager->setPenPosition(transientText, 20.0, 4.0f); - textBufferManager->appendText(transientText, consola_16, L"bgfx/examples/10-font\n"); - textBufferManager->appendText(transientText, consola_16, L"Description: Use the font system to display text and styled text.\n"); + textBufferManager->setPenPosition(transientText, width - 150.0f, 10.0f); + textBufferManager->appendText(transientText, consola_16, L"Transient\n"); + textBufferManager->appendText(transientText, consola_16, L"text buffer\n"); textBufferManager->appendText(transientText, consola_16, fpsText); float at[3] = { 0, 0, 0.0f }; diff --git a/examples/11-fontsdf/fontsdf.cpp b/examples/11-fontsdf/fontsdf.cpp index 4187ac5ab..8113595ca 100644 --- a/examples/11-fontsdf/fontsdf.cpp +++ b/examples/11-fontsdf/fontsdf.cpp @@ -26,83 +26,37 @@ long int fsize(FILE* _file) return size; } -char* loadText(const char* _textFile) -{ - FILE* pFile; - pFile = fopen(_textFile, "rb"); - if (pFile == NULL) - { - return NULL; - } - - // Go to the end of the file. - if (fseek(pFile, 0L, SEEK_END) == 0) +static char* loadText(const char* _filePath) +{ + FILE* file = fopen(_filePath, "rb"); + if (NULL != file) { - // Get the size of the file. - long bufsize = ftell(pFile); - if (bufsize == -1) - { - fclose(pFile); - return NULL; - } - - char* buffer = new char[bufsize]; - - // Go back to the start of the file. - fseek(pFile, 0L, SEEK_SET); - - // Read the entire file into memory. - size_t newLen = fread( (void*)buffer, sizeof(char), bufsize, pFile); - if (newLen == 0) - { - fclose(pFile); - delete [] buffer; - return NULL; - } - - fclose(pFile); - - return buffer; + uint32_t size = (uint32_t)fsize(file); + char* mem = (char*)malloc(size+1); + size_t ignore = fread(mem, 1, size, file); + BX_UNUSED(ignore); + fclose(file); + mem[size-1] = '\0'; + return mem; } + return NULL; } -TrueTypeHandle loadTtf(FontManager* _fm, const char* _fontPath) +TrueTypeHandle loadTtf(FontManager* _fm, const char* _filePath) { - FILE* pFile; - pFile = fopen(_fontPath, "rb"); - if (NULL != pFile) + FILE* file = fopen(_filePath, "rb"); + if (NULL != file) { - if (0 == fseek(pFile, 0L, SEEK_END) ) - { - // Get the size of the file. - long bufsize = ftell(pFile); - if (bufsize == -1) - { - fclose(pFile); - TrueTypeHandle invalid = BGFX_INVALID_HANDLE; - return invalid; - } - - uint8_t* buffer = new uint8_t[bufsize]; - - // Go back to the start of the file. - fseek(pFile, 0L, SEEK_SET); - - // Read the entire file into memory. - uint32_t newLen = fread( (void*)buffer, sizeof(char), bufsize, pFile); - if (newLen == 0) - { - fclose(pFile); - delete [] buffer; - TrueTypeHandle invalid = BGFX_INVALID_HANDLE; - return invalid; - } - - fclose(pFile); - - return _fm->createTtf(buffer, bufsize); - } + uint32_t size = (uint32_t)fsize(file); + uint8_t* mem = (uint8_t*)malloc(size+1); + size_t ignore = fread(mem, 1, size, file); + BX_UNUSED(ignore); + fclose(file); + mem[size-1] = '\0'; + TrueTypeHandle handle = _fm->createTtf(mem, size); + free(mem); + return handle; } TrueTypeHandle invalid = BGFX_INVALID_HANDLE; @@ -286,6 +240,8 @@ int _main_(int /*_argc*/, char** /*_argv*/) bgfx::frame(); } + free(bigText); + fontManager->destroyTtf(font); // Destroy the fonts. fontManager->destroyFont(fontSdf); diff --git a/examples/common/entry/entry.cpp b/examples/common/entry/entry.cpp index 2908f5fa5..99f41050b 100644 --- a/examples/common/entry/entry.cpp +++ b/examples/common/entry/entry.cpp @@ -59,14 +59,15 @@ namespace entry { if (_argc > 1) { - if (setOrToggle(s_reset, "vsync", BGFX_RESET_VSYNC, 1, _argc, _argv) - || setOrToggle(s_reset, "msaa", BGFX_RESET_MSAA_X16, 1, _argc, _argv) ) + if (setOrToggle(s_reset, "vsync", BGFX_RESET_VSYNC, 1, _argc, _argv) + || setOrToggle(s_reset, "msaa", BGFX_RESET_MSAA_X16, 1, _argc, _argv) ) { return 0; } - else if (setOrToggle(s_debug, "stats", BGFX_DEBUG_STATS, 1, _argc, _argv) - || setOrToggle(s_debug, "ifh", BGFX_DEBUG_IFH, 1, _argc, _argv) - || setOrToggle(s_debug, "text", BGFX_DEBUG_TEXT, 1, _argc, _argv) ) + else if (setOrToggle(s_debug, "stats", BGFX_DEBUG_STATS, 1, _argc, _argv) + || setOrToggle(s_debug, "ifh", BGFX_DEBUG_IFH, 1, _argc, _argv) + || setOrToggle(s_debug, "text", BGFX_DEBUG_TEXT, 1, _argc, _argv) + || setOrToggle(s_debug, "wireframe", BGFX_DEBUG_WIREFRAME, 1, _argc, _argv) ) { bgfx::setDebug(s_debug); return 0; @@ -84,10 +85,11 @@ namespace entry static const InputBinding s_bindings[] = { - { entry::Key::KeyQ, entry::Modifier::LeftCtrl, 1, cmd, "exit" }, - { entry::Key::F1, entry::Modifier::None, 1, cmd, "graphics stats" }, - { entry::Key::F7, entry::Modifier::None, 1, cmd, "graphics vsync" }, - { entry::Key::F8, entry::Modifier::None, 1, cmd, "graphics msaa" }, + { entry::Key::KeyQ, entry::Modifier::LeftCtrl, 1, cmd, "exit" }, + { entry::Key::F1, entry::Modifier::None, 1, cmd, "graphics stats" }, + { entry::Key::F3, entry::Modifier::None, 1, cmd, "graphics wireframe" }, + { entry::Key::F7, entry::Modifier::None, 1, cmd, "graphics vsync" }, + { entry::Key::F8, entry::Modifier::None, 1, cmd, "graphics msaa" }, INPUT_BINDING_END }; diff --git a/examples/common/entry/entry.h b/examples/common/entry/entry.h index 49ba1193b..bfdbbb7b4 100644 --- a/examples/common/entry/entry.h +++ b/examples/common/entry/entry.h @@ -9,7 +9,6 @@ #include "dbg.h" #include -#include namespace entry { diff --git a/examples/common/font/text_buffer_manager.cpp b/examples/common/font/text_buffer_manager.cpp index 2505a4f2e..e56c24182 100644 --- a/examples/common/font/text_buffer_manager.cpp +++ b/examples/common/font/text_buffer_manager.cpp @@ -695,6 +695,12 @@ void TextBufferManager::submitTextBuffer(TextBufferHandle _handle, uint8_t _id, uint32_t indexSize = bc.textBuffer->getIndexCount() * bc.textBuffer->getIndexSize(); uint32_t vertexSize = bc.textBuffer->getVertexCount() * bc.textBuffer->getVertexSize(); + + if (0 == indexSize || 0 == vertexSize) + { + return; + } + const bgfx::Memory* mem; bgfx::setTexture(0, u_texColor, m_fontManager->getAtlas()->getTextureHandle() );