closed #4569:Texture2D support for update partial texture

This commit is contained in:
Dhilan007 2014-03-27 15:11:04 +08:00
parent 91ca76515b
commit cac4e0bbbc
3 changed files with 35 additions and 12 deletions

View File

@ -69,6 +69,11 @@ FontAtlas::FontAtlas(Font &theFont)
_currentPageData = new unsigned char[_currentPageDataSize]; _currentPageData = new unsigned char[_currentPageDataSize];
memset(_currentPageData, 0, _currentPageDataSize); memset(_currentPageData, 0, _currentPageDataSize);
auto pixelFormat = fontTTf->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8;
texture->initWithData(_currentPageData, _currentPageDataSize,
pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) );
addTexture(texture,0); addTexture(texture,0);
texture->release(); texture->release();
#if CC_ENABLE_CACHE_TEXTURE_DATA #if CC_ENABLE_CACHE_TEXTURE_DATA
@ -182,12 +187,10 @@ void FontAtlas::listenToForeground(EventCustom *event)
} }
else else
{ {
auto contentSize = Size(CacheTextureWidth,CacheTextureHeight);
auto pixelFormat = fontTTf->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8; auto pixelFormat = fontTTf->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8;
// this is a memory leak as the texture previously in _atlasTextures[_currentPage] is not deleted from OpenGL _atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize,
// see CCTexture2D::initWithData for the temporary fix pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) );
_atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, contentSize );
} }
} }
#endif #endif
@ -228,12 +231,13 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String)
Rect tempRect; Rect tempRect;
FontLetterDefinition tempDef; FontLetterDefinition tempDef;
auto contentSize = Size(CacheTextureWidth,CacheTextureHeight);
auto scaleFactor = CC_CONTENT_SCALE_FACTOR(); auto scaleFactor = CC_CONTENT_SCALE_FACTOR();
auto pixelFormat = fontTTf->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8; auto pixelFormat = fontTTf->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8;
bool existNewLetter = false; bool existNewLetter = false;
int bottomHeight = _commonLineHeight - _fontAscender; int bottomHeight = _commonLineHeight - _fontAscender;
float startX = _currentPageOrigX;
float startY = _currentPageOrigY;
for (int i = 0; i < length; ++i) for (int i = 0; i < length; ++i)
{ {
@ -260,13 +264,18 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String)
_currentPageOrigX = 0; _currentPageOrigX = 0;
if(_currentPageOrigY + _commonLineHeight >= CacheTextureHeight) if(_currentPageOrigY + _commonLineHeight >= CacheTextureHeight)
{ {
// this is a memory leak as the texture previously in _atlasTextures[_currentPage] is not deleted from OpenGL auto data = _currentPageData + CacheTextureWidth * (int)startY;
// see CCTexture2D::initWithData for the temporary fix _atlasTextures[_currentPage]->updateWithData(data, 0, startY,
_atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, contentSize ); CacheTextureWidth, CacheTextureHeight - startY);
startX = 0.0f;
startY = 0.0f;
_currentPageOrigY = 0; _currentPageOrigY = 0;
memset(_currentPageData, 0, _currentPageDataSize); memset(_currentPageData, 0, _currentPageDataSize);
_currentPage++; _currentPage++;
auto tex = new Texture2D; auto tex = new Texture2D;
tex->initWithData(_currentPageData, _currentPageDataSize,
pixelFormat, CacheTextureWidth, CacheTextureHeight, Size(CacheTextureWidth,CacheTextureHeight) );
addTexture(tex,_currentPage); addTexture(tex,_currentPage);
tex->release(); tex->release();
} }
@ -307,9 +316,9 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String)
if(existNewLetter) if(existNewLetter)
{ {
// this is a memory leak as the texture previously in _atlasTextures[_currentPage] is not deleted from OpenGL auto data = _currentPageData + CacheTextureWidth * (int)startY;
// see CCTexture2D::initWithData for the temporary fix _atlasTextures[_currentPage]->updateWithData(data, 0, startY,
_atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, contentSize ); CacheTextureWidth, _currentPageOrigY - startY + _commonLineHeight);
} }
return true; return true;
} }

View File

@ -682,6 +682,18 @@ bool Texture2D::initWithMipmaps(MipmapInfo* mipmaps, int mipmapsNum, PixelFormat
return true; return true;
} }
bool Texture2D::updateWithData(const void *data,int offsetX,int offsetY,int width,int height)
{
if (_name)
{
GL::bindTexture2D(_name);
const PixelFormatInfo& info = _pixelFormatInfoTables.at(_pixelFormat);
glTexSubImage2D(GL_TEXTURE_2D,0,offsetX,offsetY,width,height,info.format, info.type,data);
return true;
}
return false;
}
std::string Texture2D::getDescription() const std::string Texture2D::getDescription() const
{ {

View File

@ -223,6 +223,8 @@ public:
/** Initializes with mipmaps */ /** Initializes with mipmaps */
bool initWithMipmaps(MipmapInfo* mipmaps, int mipmapsNum, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh); bool initWithMipmaps(MipmapInfo* mipmaps, int mipmapsNum, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh);
/** Update with texture data*/
bool updateWithData(const void *data,int offsetX,int offsetY,int width,int height);
/** /**
Drawing extensions to make it easy to draw basic quads using a Texture2D object. Drawing extensions to make it easy to draw basic quads using a Texture2D object.
These functions require GL_TEXTURE_2D and both GL_VERTEX_ARRAY and GL_TEXTURE_COORD_ARRAY client states to be enabled. These functions require GL_TEXTURE_2D and both GL_VERTEX_ARRAY and GL_TEXTURE_COORD_ARRAY client states to be enabled.