mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-10 20:42:21 +02:00
SDL/OGL platform improvements
This commit is contained in:
parent
213aea8c82
commit
258a6653f9
16 changed files with 711 additions and 1141 deletions
|
@ -56,7 +56,7 @@ void FTFont::Dispose(bool cacheRetain)
|
|||
}
|
||||
}
|
||||
}
|
||||
mFaceSize = NULL;
|
||||
mFaceSize = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -78,14 +78,14 @@ FTFontManager::FTFontManager()
|
|||
{
|
||||
uint8 whiteVal = (uint8)(pow(i / 255.0f, 0.75) * 255.0f);
|
||||
uint8 blackVal = (uint8)(pow(i / 255.0f, 0.9) * 255.0f);
|
||||
|
||||
|
||||
mWhiteTab[i] = whiteVal;
|
||||
mBlackTab[i] = blackVal;
|
||||
}
|
||||
}
|
||||
|
||||
FTFontManager::~FTFontManager()
|
||||
{
|
||||
{
|
||||
for (auto page : mPages)
|
||||
delete page;
|
||||
mPages.Clear();
|
||||
|
@ -110,7 +110,7 @@ void FTFontManager::DoClearCache()
|
|||
}
|
||||
else
|
||||
++itr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FTFontManager::ClearCache()
|
||||
|
@ -133,11 +133,11 @@ bool FTFont::Load(const StringImpl& fileName, float pointSize)
|
|||
{
|
||||
if (gFTLibrary == NULL)
|
||||
FT_Init_FreeType(&gFTLibrary);
|
||||
|
||||
|
||||
FTFontManager::Face* face = NULL;
|
||||
|
||||
FTFontManager::Face** facePtr = NULL;
|
||||
if (gFTFontManager.mFaces.TryAdd(fileName, NULL, &facePtr))
|
||||
if (gFTFontManager.mFaces.TryAdd(fileName, NULL, &facePtr))
|
||||
{
|
||||
face = new FTFontManager::Face();
|
||||
*facePtr = face;
|
||||
|
@ -153,7 +153,7 @@ bool FTFont::Load(const StringImpl& fileName, float pointSize)
|
|||
faceIdx = atoi(useFileName.c_str() + atPos + 1);
|
||||
useFileName.RemoveToEnd(atPos);
|
||||
}
|
||||
auto error = FT_New_Face(gFTLibrary, useFileName.c_str(), faceIdx, &ftFace);
|
||||
auto error = FT_New_Face(gFTLibrary, useFileName.c_str(), faceIdx, &ftFace);
|
||||
face->mFTFace = ftFace;
|
||||
}
|
||||
else
|
||||
|
@ -164,7 +164,7 @@ bool FTFont::Load(const StringImpl& fileName, float pointSize)
|
|||
return false;
|
||||
|
||||
mFace = face;
|
||||
|
||||
|
||||
FTFontManager::FaceSize** faceSizePtr = NULL;
|
||||
if (face->mFaceSizes.TryAdd(pointSize, NULL, &faceSizePtr))
|
||||
{
|
||||
|
@ -177,17 +177,17 @@ bool FTFont::Load(const StringImpl& fileName, float pointSize)
|
|||
FT_New_Size(mFace->mFTFace, &ftSize);
|
||||
FT_Activate_Size(ftSize);
|
||||
auto error = FT_Set_Char_Size(mFace->mFTFace, 0, (int)(pointSize * 64), 72, 72);
|
||||
|
||||
|
||||
mFaceSize->mFace = mFace;
|
||||
mFaceSize->mFTSize = ftSize;
|
||||
mFaceSize->mPointSize = pointSize;
|
||||
mFaceSize->mPointSize = pointSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
mFaceSize = *faceSizePtr;
|
||||
}
|
||||
mFaceSize->mRefCount++;
|
||||
|
||||
|
||||
mHeight = mFaceSize->mFTSize->metrics.height / 64;
|
||||
mAscent = mFaceSize->mFTSize->metrics.ascender / 64;
|
||||
mDescent = mFaceSize->mFTSize->metrics.descender / 64;
|
||||
|
@ -199,7 +199,7 @@ bool FTFont::Load(const StringImpl& fileName, float pointSize)
|
|||
TextureSegment* BF_CALLTYPE Gfx_CreateTextureSegment(TextureSegment* textureSegment, int srcX, int srcY, int srcWidth, int srcHeight);
|
||||
|
||||
FTFontManager::Glyph* FTFont::AllocGlyph(int charCode, bool allowDefault)
|
||||
{
|
||||
{
|
||||
FT_Activate_Size(mFaceSize->mFTSize);
|
||||
|
||||
int glyph_index = FT_Get_Char_Index(mFace->mFTFace, charCode);
|
||||
|
@ -209,20 +209,20 @@ FTFontManager::Glyph* FTFont::AllocGlyph(int charCode, bool allowDefault)
|
|||
auto error = FT_Load_Glyph(mFace->mFTFace, glyph_index, FT_LOAD_NO_BITMAP);
|
||||
if (error != FT_Err_Ok)
|
||||
return NULL;
|
||||
|
||||
|
||||
error = FT_Render_Glyph(mFace->mFTFace->glyph, FT_RENDER_MODE_NORMAL);
|
||||
if (error != FT_Err_Ok)
|
||||
return NULL;
|
||||
|
||||
|
||||
auto& bitmap = mFace->mFTFace->glyph->bitmap;
|
||||
|
||||
|
||||
if ((bitmap.rows > FT_PAGE_HEIGHT) || (bitmap.width > FT_PAGE_WIDTH))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
FTFontManager::Page* page = NULL;
|
||||
|
||||
|
||||
if (!gFTFontManager.mPages.empty())
|
||||
{
|
||||
page = gFTFontManager.mPages.back();
|
||||
|
@ -231,14 +231,14 @@ FTFontManager::Glyph* FTFont::AllocGlyph(int charCode, bool allowDefault)
|
|||
// Move down to next row
|
||||
page->mCurX = 0;
|
||||
page->mCurY += page->mMaxRowHeight;
|
||||
page->mMaxRowHeight = 0;
|
||||
page->mMaxRowHeight = 0;
|
||||
}
|
||||
|
||||
if (page->mCurY + (int)bitmap.rows > page->mTexture->mHeight)
|
||||
{
|
||||
// Doesn't fit
|
||||
page = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (page == NULL)
|
||||
|
@ -261,28 +261,32 @@ FTFontManager::Glyph* FTFont::AllocGlyph(int charCode, bool allowDefault)
|
|||
glyph->mX = page->mCurX;
|
||||
glyph->mY = page->mCurY;
|
||||
glyph->mWidth = bitmap.width;
|
||||
glyph->mHeight = bitmap.rows;
|
||||
glyph->mHeight = bitmap.rows;
|
||||
|
||||
if (page->mTexture == NULL)
|
||||
{
|
||||
ImageData img;
|
||||
img.CreateNew(FT_PAGE_WIDTH, FT_PAGE_HEIGHT);
|
||||
ImageData* img = new ImageData();
|
||||
img->CreateNew(FT_PAGE_WIDTH, FT_PAGE_HEIGHT);
|
||||
auto* bits = img->mBits;
|
||||
for (int i = 0; i < FT_PAGE_HEIGHT * FT_PAGE_WIDTH; i++)
|
||||
{
|
||||
if (i % 3 == 0)
|
||||
img.mBits[i] = 0xFFFF0000;
|
||||
bits[i] = 0xFFFF0000;
|
||||
else if (i % 3 == 1)
|
||||
img.mBits[i] = 0xFF00FF00;
|
||||
bits[i] = 0xFF00FF00;
|
||||
else
|
||||
img.mBits[i] = 0xFF0000FF;
|
||||
bits[i] = 0xFF0000FF;
|
||||
}
|
||||
page->mTexture = gBFApp->mRenderDevice->LoadTexture(&img, TextureFlag_NoPremult);
|
||||
page->mTexture = gBFApp->mRenderDevice->LoadTexture(img, TextureFlag_NoPremult);
|
||||
img->Deref();
|
||||
}
|
||||
|
||||
if (bitmap.width > 0)
|
||||
{
|
||||
ImageData img;
|
||||
img.CreateNew(bitmap.width, bitmap.rows);
|
||||
ImageData* img = new ImageData();
|
||||
img->CreateNew(bitmap.width, bitmap.rows);
|
||||
auto* bits = img->mBits;
|
||||
int width = img->mWidth;
|
||||
for (int y = 0; y < (int)bitmap.rows; y++)
|
||||
{
|
||||
for (int x = 0; x < (int)bitmap.width; x++)
|
||||
|
@ -292,17 +296,18 @@ FTFontManager::Glyph* FTFont::AllocGlyph(int charCode, bool allowDefault)
|
|||
uint8 whiteVal = gFTFontManager.mWhiteTab[val];
|
||||
uint8 blackVal = gFTFontManager.mBlackTab[val];
|
||||
|
||||
img.mBits[y * img.mWidth + x] = ((int32)whiteVal << 24) |
|
||||
bits[y * width + x] = ((int32)whiteVal << 24) |
|
||||
((int32)blackVal) | ((int32)0xFF << 8) | ((int32)0xFF << 16);
|
||||
}
|
||||
}
|
||||
page->mTexture->Blt(&img, page->mCurX, page->mCurY);
|
||||
page->mTexture->Blt(img, page->mCurX, page->mCurY);
|
||||
img->Deref();
|
||||
}
|
||||
|
||||
page->mCurX += bitmap.width;
|
||||
page->mMaxRowHeight = std::max(page->mMaxRowHeight, (int)bitmap.rows);
|
||||
|
||||
auto texture = page->mTexture;
|
||||
auto texture = page->mTexture;
|
||||
texture->AddRef();
|
||||
|
||||
TextureSegment* textureSegment = new TextureSegment();
|
||||
|
@ -330,7 +335,7 @@ int FTFont::GetKerning(int charA, int charB)
|
|||
|
||||
void FTFont::Release(bool cacheRetain)
|
||||
{
|
||||
Dispose(cacheRetain);
|
||||
Dispose(cacheRetain);
|
||||
delete this;
|
||||
}
|
||||
|
||||
|
@ -338,7 +343,7 @@ void FTFont::Release(bool cacheRetain)
|
|||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
BF_EXPORT FTFont* BF_CALLTYPE FTFont_Load(const char* fileName, float pointSize)
|
||||
{
|
||||
{
|
||||
auto ftFont = new FTFont();
|
||||
if (!ftFont->Load(fileName, pointSize))
|
||||
{
|
||||
|
@ -351,11 +356,11 @@ BF_EXPORT FTFont* BF_CALLTYPE FTFont_Load(const char* fileName, float pointSize)
|
|||
|
||||
BF_EXPORT void BF_CALLTYPE FTFont_ClearCache()
|
||||
{
|
||||
FTFontManager::ClearCache();
|
||||
FTFontManager::ClearCache();
|
||||
}
|
||||
|
||||
BF_EXPORT void BF_CALLTYPE FTFont_Delete(FTFont* ftFont, bool cacheRetain)
|
||||
{
|
||||
{
|
||||
ftFont->Release(cacheRetain);
|
||||
}
|
||||
|
||||
|
@ -366,6 +371,6 @@ BF_EXPORT FTFontManager::Glyph* BF_CALLTYPE FTFont_AllocGlyph(FTFont* ftFont, in
|
|||
|
||||
BF_EXPORT int BF_CALLTYPE FTFont_GetKerning(FTFont* ftFont, int charCodeA, int charCodeB)
|
||||
{
|
||||
auto kerning = ftFont->GetKerning(charCodeA, charCodeB);
|
||||
auto kerning = ftFont->GetKerning(charCodeA, charCodeB);
|
||||
return kerning;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue