This commit is contained in:
bkaradzic 2013-05-29 23:21:19 -07:00
parent 32b95cace3
commit ed9761e9c9
2 changed files with 54 additions and 63 deletions

View file

@ -8,7 +8,6 @@
#include <limits.h> // INT_MAX #include <limits.h> // INT_MAX
#include <memory.h> // memset #include <memory.h> // memset
#include <vector> #include <vector>
#include "cube_atlas.h" #include "cube_atlas.h"
@ -175,12 +174,10 @@ float RectanglePacker::getUsageRatio()
uint32_t total = m_width * m_height; uint32_t total = m_width * m_height;
if (total > 0) if (total > 0)
{ {
return (float) m_usedSpace / (float) total; return (float)m_usedSpace / (float)total;
}
else
{
return 0.0f;
} }
return 0.0f;
} }
void RectanglePacker::clear() void RectanglePacker::clear()
@ -441,11 +438,11 @@ void Atlas::packUV(const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t
static const int16_t maxVal = INT16_MAX; static const int16_t maxVal = INT16_MAX;
float texMult = (float)(maxVal - minVal) / ( (float)(m_textureSize) ); float texMult = (float)(maxVal - minVal) / ( (float)(m_textureSize) );
int16_t x0 = (int16_t)( ((float)_region.x * texMult) - float(INT16_MAX) ); int16_t x0 = (int16_t)( ( (float)_region.x * texMult) - float(INT16_MAX) );
int16_t y0 = (int16_t)( ((float)_region.y * texMult) - float(INT16_MAX) ); int16_t y0 = (int16_t)( ( (float)_region.y * texMult) - float(INT16_MAX) );
int16_t x1 = (int16_t)( (((float)_region.x + _region.width) * texMult) - float(INT16_MAX) ); int16_t x1 = (int16_t)( ( ( (float)_region.x + _region.width) * texMult) - float(INT16_MAX) );
int16_t y1 = (int16_t)( (((float)_region.y + _region.height) * texMult) - float(INT16_MAX) ); int16_t y1 = (int16_t)( ( ( (float)_region.y + _region.height) * texMult) - float(INT16_MAX) );
int16_t w = (int16_t)( (float(INT16_MAX) / 4.0f) * (float) _region.getComponentIndex() ); int16_t ww = (int16_t)( (float(INT16_MAX) / 4.0f) * (float) _region.getComponentIndex() );
_vertexBuffer += _offset; _vertexBuffer += _offset;
switch (_region.getFaceIndex() ) switch (_region.getFaceIndex() )
@ -455,44 +452,44 @@ void Atlas::packUV(const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t
x1 = -x1; x1 = -x1;
y0 = -y0; y0 = -y0;
y1 = -y1; y1 = -y1;
writeUV(_vertexBuffer, maxVal, y0, x0, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, maxVal, y0, x0, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, maxVal, y1, x0, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, maxVal, y1, x0, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, maxVal, y1, x1, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, maxVal, y1, x1, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, maxVal, y0, x1, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, maxVal, y0, x1, ww); _vertexBuffer += _stride;
break; break;
case 1: // -X case 1: // -X
y0 = -y0; y0 = -y0;
y1 = -y1; y1 = -y1;
writeUV(_vertexBuffer, minVal, y0, x0, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, minVal, y0, x0, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, minVal, y1, x0, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, minVal, y1, x0, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, minVal, y1, x1, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, minVal, y1, x1, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, minVal, y0, x1, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, minVal, y0, x1, ww); _vertexBuffer += _stride;
break; break;
case 2: // +Y case 2: // +Y
writeUV(_vertexBuffer, x0, maxVal, y0, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x0, maxVal, y0, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x0, maxVal, y1, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x0, maxVal, y1, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, maxVal, y1, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, maxVal, y1, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, maxVal, y0, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, maxVal, y0, ww); _vertexBuffer += _stride;
break; break;
case 3: // -Y case 3: // -Y
y0 = -y0; y0 = -y0;
y1 = -y1; y1 = -y1;
writeUV(_vertexBuffer, x0, minVal, y0, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x0, minVal, y0, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x0, minVal, y1, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x0, minVal, y1, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, minVal, y1, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, minVal, y1, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, minVal, y0, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, minVal, y0, ww); _vertexBuffer += _stride;
break; break;
case 4: // +Z case 4: // +Z
y0 = -y0; y0 = -y0;
y1 = -y1; y1 = -y1;
writeUV(_vertexBuffer, x0, y0, maxVal, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x0, y0, maxVal, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x0, y1, maxVal, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x0, y1, maxVal, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, y1, maxVal, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, y1, maxVal, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, y0, maxVal, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, y0, maxVal, ww); _vertexBuffer += _stride;
break; break;
case 5: // -Z case 5: // -Z
@ -500,10 +497,10 @@ void Atlas::packUV(const AtlasRegion& _region, uint8_t* _vertexBuffer, uint32_t
x1 = -x1; x1 = -x1;
y0 = -y0; y0 = -y0;
y1 = -y1; y1 = -y1;
writeUV(_vertexBuffer, x0, y0, minVal, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x0, y0, minVal, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x0, y1, minVal, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x0, y1, minVal, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, y1, minVal, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, y1, minVal, ww); _vertexBuffer += _stride;
writeUV(_vertexBuffer, x1, y0, minVal, w); _vertexBuffer += _stride; writeUV(_vertexBuffer, x1, y0, minVal, ww); _vertexBuffer += _stride;
break; break;
} }
} }

View file

@ -16,22 +16,12 @@
#if BGFX_CONFIG_USE_TINYSTL #if BGFX_CONFIG_USE_TINYSTL
namespace tinystl namespace tinystl
{ {
//struct bgfx_allocator
//{
//static void* static_allocate(size_t _bytes);
//static void static_deallocate(void* _ptr, size_t /*_bytes*/);
//};
} // namespace tinystl } // namespace tinystl
//# define TINYSTL_ALLOCATOR tinystl::bgfx_allocator # include <TINYSTL/unordered_map.hh>
# include <TINYSTL/unordered_map.h>
//# include <TINYSTL/unordered_set.h>
namespace stl = tinystl; namespace stl = tinystl;
#else #else
# include <unordered_map> # include <unordered_map>
namespace std namespace std { namespace tr1 {} }
{ namespace tr1
{}
}
namespace stl namespace stl
{ {
using namespace std; using namespace std;
@ -201,25 +191,25 @@ bool TrueTypeFont::bakeGlyphAlpha(CodePoint _codePoint, GlyphInfo& _glyphInfo, u
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyph; FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyph;
int32_t x = bitmap->left; int32_t xx = bitmap->left;
int32_t y = -bitmap->top; int32_t yy = -bitmap->top;
int32_t w = bitmap->bitmap.width; int32_t ww = bitmap->bitmap.width;
int32_t h = bitmap->bitmap.rows; int32_t hh = bitmap->bitmap.rows;
_glyphInfo.offset_x = (float) x; _glyphInfo.offset_x = (float)xx;
_glyphInfo.offset_y = (float) y; _glyphInfo.offset_y = (float)yy;
_glyphInfo.width = (float) w; _glyphInfo.width = (float)ww;
_glyphInfo.height = (float) h; _glyphInfo.height = (float)hh;
_glyphInfo.advance_x = (float)slot->advance.x / 64.0f; _glyphInfo.advance_x = (float)slot->advance.x / 64.0f;
_glyphInfo.advance_y = (float)slot->advance.y / 64.0f; _glyphInfo.advance_y = (float)slot->advance.y / 64.0f;
int32_t charsize = 1; int32_t charsize = 1;
int32_t depth = 1; int32_t depth = 1;
int32_t stride = bitmap->bitmap.pitch; int32_t stride = bitmap->bitmap.pitch;
for (int32_t ii = 0; ii < h; ++ii) for (int32_t ii = 0; ii < hh; ++ii)
{ {
memcpy(_outBuffer + (ii * w) * charsize * depth, memcpy(_outBuffer + (ii * ww) * charsize * depth,
bitmap->bitmap.buffer + (ii * stride) * charsize, w * charsize * depth); bitmap->bitmap.buffer + (ii * stride) * charsize, ww * charsize * depth);
} }
FT_Done_Glyph(glyph); FT_Done_Glyph(glyph);
@ -270,8 +260,10 @@ bool TrueTypeFont::bakeGlyphSubpixel(CodePoint _codePoint, GlyphInfo& _glyphInfo
int32_t stride = bitmap->bitmap.pitch; int32_t stride = bitmap->bitmap.pitch;
for (int32_t ii = 0; ii < h; ++ii) for (int32_t ii = 0; ii < h; ++ii)
{ {
memcpy(_outBuffer + (ii * w) * charsize * depth, memcpy(_outBuffer + (ii * w) * charsize * depth
bitmap->bitmap.buffer + (ii * stride) * charsize, w * charsize * depth); , bitmap->bitmap.buffer + (ii * stride) * charsize
, w * charsize * depth
);
} }
FT_Done_Glyph(glyph); FT_Done_Glyph(glyph);
@ -472,9 +464,11 @@ typedef stl::unordered_map<CodePoint, GlyphInfo> GlyphHashMap;
struct FontManager::CachedFont struct FontManager::CachedFont
{ {
CachedFont() CachedFont()
: trueTypeFont(NULL)
{ {
trueTypeFont = NULL; masterFontHandle.idx = -1; masterFontHandle.idx = bx::HandleAlloc::invalid;
} }
FontInfo fontInfo; FontInfo fontInfo;
GlyphHashMap cachedGlyphs; GlyphHashMap cachedGlyphs;
TrueTypeFont* trueTypeFont; TrueTypeFont* trueTypeFont;
@ -568,7 +562,7 @@ FontHandle FontManager::createFontByPixelSize(TrueTypeHandle _tt_handle, uint32_
if (!ttf->init(m_cachedFiles[_tt_handle.idx].buffer, m_cachedFiles[_tt_handle.idx].bufferSize, _typefaceIndex, _pixelSize) ) if (!ttf->init(m_cachedFiles[_tt_handle.idx].buffer, m_cachedFiles[_tt_handle.idx].bufferSize, _typefaceIndex, _pixelSize) )
{ {
delete ttf; delete ttf;
FontHandle invalid = BGFX_INVALID_HANDLE; FontHandle invalid = { bx::HandleAlloc::invalid };
return invalid; return invalid;
} }
@ -580,7 +574,7 @@ FontHandle FontManager::createFontByPixelSize(TrueTypeHandle _tt_handle, uint32_
m_cachedFonts[fontIdx].fontInfo.fontType = _fontType; m_cachedFonts[fontIdx].fontInfo.fontType = _fontType;
m_cachedFonts[fontIdx].fontInfo.pixelSize = _pixelSize; m_cachedFonts[fontIdx].fontInfo.pixelSize = _pixelSize;
m_cachedFonts[fontIdx].cachedGlyphs.clear(); m_cachedFonts[fontIdx].cachedGlyphs.clear();
m_cachedFonts[fontIdx].masterFontHandle.idx = -1; m_cachedFonts[fontIdx].masterFontHandle.idx = bx::HandleAlloc::invalid;
FontHandle ret = {fontIdx}; FontHandle ret = {fontIdx};
return ret; return ret;
} }