【cocos2dx】ttf文本纹理内存优化,加上打印对比工具

参照:https://blog.csdn.net/ghost0620/article/details/52574020

 

1:添加打印接口

参照图片内存打印的方法,添加ttf文本纹理打印,方便和策划沟通如何在文本这一块进行优化

std::string FontAtlasCache::getCachedTextureInfo()
{	
	std::string buffer;
	char buftmp[4096];

	unsigned int count = 0;
	unsigned int totalBytes = 0;
	auto atlasMapCopy = _atlasMap;
	for (auto&& atlas : atlasMapCopy) {
		std::unordered_map<ssize_t, Texture2D*> _textures = atlas.second->getTextures();
		for (auto& texture : _textures) {
			memset(buftmp, 0, sizeof(buftmp));
			Texture2D* tex = texture.second;
			unsigned int bpp = tex->getBitsPerPixelForFormat();
			// Each texture takes up width * height * bytesPerPixel bytes.
			auto bytes = tex->getPixelsWide() * tex->getPixelsHigh() * bpp / 8;
			totalBytes += bytes;
			count++;
			snprintf(buftmp, sizeof(buftmp) - 1, "\"%s\" page = %lu rc=%lu id=%lu %lu x %lu @ %ld bpp => %lu KB\n",
				atlas.first.c_str(),
				(long)texture.first,
				(long)tex->getReferenceCount(),
				(long)tex->getName(),
				(long)tex->getPixelsWide(),
				(long)tex->getPixelsHigh(),
				(long)bpp,
				(long)bytes / 1024);

			buffer += buftmp;
		}
	}

	snprintf(buftmp, sizeof(buftmp) - 1, "FontAtlasCache dumpDebugInfo: %ld textures, for %lu KB (%.2f MB)\n", (long)count, (long)totalBytes / 1024, totalBytes / (1024.0f*1024.0f));
	buffer += buftmp;

	return buffer;
}

 

2:对比优化效果

纹路默认大小改之前:

改之后, 可以看到占用内存确实是少了一大截,虽然某个字体可能因为内容过多,申请了5张纹理图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值