Fixed resource leak in font examples.

This commit is contained in:
bkaradzic 2013-08-08 22:18:19 -07:00
Родитель c9de4a1f19
Коммит 3e6f682c30
5 изменённых файлов: 73 добавлений и 117 удалений

Просмотреть файл

@ -16,42 +16,29 @@
#include <stdio.h>
#include <wchar.h>
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 };

Просмотреть файл

@ -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);

Просмотреть файл

@ -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
};

Просмотреть файл

@ -9,7 +9,6 @@
#include "dbg.h"
#include <bx/bx.h>
#include <bx/debug.h>
namespace entry
{

Просмотреть файл

@ -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() );