当前位置: 首页 > 编程日记 > 正文

Improve Performance and Reduce Memory with PVRTC Textures and Cocos2d

转载自:http://www.uchidacoonga.com/2011/07/pvrtc-textures-and-cocos2d/

Hello everyone! A few months have passed since I posted Simple Platformer Using Cocos2d and Box2d with Collision Detection. I have received many positive feedbacks and interest on the tutorial. I am glad that it has been helpful for so many in the community!

Also, I would like to share the good news that the game we have been working on has been picked up by a major publisher. We hope to release it around September, so please stay tuned! The game has been built using the very same articles and tutorials found on this blog.

What Is It?

PVRTC-I and -II are a family of lossy, fixed-rate texture compression formats used in PowerVR’s MBX and SGX technologies. PVRTC is the compressed texture format used in all generations of the iPhone, iPod Touch and iPad (source). You will see PVR and PVRTC being used interchangeably. They are not the same. They key thing to keep in mind is that a PVR is uncompressed, and PVRTC is compressed. Also, a PVR is a container which may contain various formats (a PVRTC is contained within a PVR file, for example). Some uncompressed PVR types are:

  • RGBA8888: 32-bit texture with alpha channel, best image quality
  • RGBA4444: 16-bit texture with alpha channel, good image quality
  • RGB565: 16-bit texture without alpha channel, good image quality but no alpha (transparency)

Here are the PVRTC types, which are compressed and lossy (meaning, quality is degraded):

  • PVRTC4BPP: Compressed format, 4 bits per pixel, ok image quality
  • PVRTC2BPP: Compressed format, 2 bits per pixel, poor image quality

Pros and Cons

Why would you want to use a PVRTC texture? Because, it is the native texture format and is hardware accelerated. This translates to faster loading time than PNGs. Have you ever been plagued by long load times? Then PVRTC may be the answer. But even better, it will use less memory on the device and give you a performance boost.

It all sounds great! But wait, there is a downside. The downside is that because it is a lossy compression, the quality of your textures will suffer. Given a PNG of the same dimension, a PVRTC4BPP texture will take up more disk space and it will look worse (you will see artifacts, especially if you rely heavily on transparency or have a lot of gradients).

Why Size Does Matter

As you may have already guessed, the amount of space used increases as the quality increases. This means RGBA8888 uses the most amount of space (in memory AND on disk). Here is a handy formula to calculate how much space will be used.

numBytes = width * height * bitsPerPixel / 8

What this means is that given a 1024×1024 image, a RGBA8888 texture will require 4 MB of space. And a PVRTC4BPP will take 512 KB space. Why does size on disk matter at all? There is a very good reason for keeping the amount of space taken by your textures as low as possible.

This is due to the 20 MB size limit imposed by AT&T (I do not know about Verizon or other international providers). If your application is over 20 MB, users will not be able to download your application over the air from their devices. They will have to go to a computer that has iTunes installed and download it from their Mac or PC. If you are working on a game, then you have to start rationing out disk space as textures aren’t the only assets that will require disk space (the other big one being audio).

So which one should you pick? A PNG will take less space on disk, but the loading time will be longer. The amount of space a PNG will take once loaded to memory is dependent on the pixel format that you set in Cocos2d, like so:

[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA4444]// Available textures // kCCTexture2DPixelFormat_RGBA8888 - 32-bit texture with Alpha channel // kCCTexture2DPixelFormat_RGB565 - 16-bit texture without Alpha channel // kCCTexture2DPixelFormat_A8 - 8-bit textures used as masks // kCCTexture2DPixelFormat_I8 - 8-bit intensity texture // kCCTexture2DPixelFormat_AI88 - 16-bit textures used as masks // kCCTexture2DPixelFormat_RGBA4444 - 16-bit textures: RGBA4444 // kCCTexture2DPixelFormat_RGB5A1 - 16-bit textures: RGB5A1 // kCCTexture2DPixelFormat_PVRTC4 - 4-bit PVRTC-compressed texture: PVRTC4 // kCCTexture2DPixelFormat_PVRTC2 - 2-bit PVRTC-compressed texture: PVRTC2

Using a PVRTC will give you better performance and faster loading times, but worse quality and more disk space. Well, I guess you’ll have to pick the lesser of the two evils :) You can always mix PNGs and PVRTCs, so that is another option. It is definitely a balancing act.

Tools of the Trade

So, how exactly do you create a PVRTC texture from a PNG? There are a few tools available.

  • TexturePacker allows you create sprite sheets with an option for saving the textures as a PVRTC.
  • TextureTool is a tool in the iOS SDK that allows you to compress your textures into a PVR texture format.
  • PVRTexTool is the one I use. It is a free tool which comes with both a GUI and a command line utility.

I can’t speak for TexturePacker because I haven’t used it before, but with the other two, you will have to tinker with it to have it generate the correct textures. Let me save you the frustration and give you a simple PVRTexTool command-line command that I use to convert my Zwoptex created texture atlas from a PNG to a PVR.

PVRTexTool -fOGLPVRTC4 -iMyAtlas.png -yflip0 -premultalpha -pvrtiterations8

The command above will create a PVRTC4BPP texture which will be saved as MyAtlas.pvr. If you are curious about what all the options mean, then take a look at the manual that comes with PVRTexTool, which has detailed information about them.

How to Load it Using Cocos2d

This part is pretty trivial. Here is the code to load the PVRTC texture in Cocos2d. I’m assuming that you are loading a sprite atlas.

CCTexture2D *tex = [[CCTextureCache sharedTextureCache] addImage:@"MyAtlas.pvr"][[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"MyAtlas.plist" texture:tex];

I hope this has been helpful. And for those in the United States, happy 4th of July! I live near Washington D.C., but I am not courageous enough to brave the traffic to go see the fireworks :) I-66 will do that to you (if you live around here, you’ll know what I mean!).

If you enjoyed this post, make sure you subscribe to my RSS feed!

转载于:https://www.cnblogs.com/yang3wei/archive/2012/03/20/2739861.html

相关文章:

VDI序曲二十三 制作OFFICE 2003应用程序虚拟化序列

APP-V平台由三个重要组件构成:APP-V排序器、用于虚拟应用程序交付和管理的APP-V管理和流式处理服务器以及APP-V客户端。并且在虚拟环境中不会包含不必要的文件和设置,让IT管理员按需交付应用程序软件。 我们之前在一台干净的PC上做好了“捕获鸟笼”&…

零基础学Java程序有什么好的建议

​ java技术的快速发展,引起了很多人的关注,尤其是一些零基础人群,都想通过学习java技术来改善自己的职业,那么零基础学Java程序有什么好的建议呢?来看看下面的详细介绍。 ​  零基础学Java程序有什么好的建议? 1.选择对的书籍…

Numpy入门教程:12. 线性代数

背景 什么是 NumPy 呢? NumPy 这个词来源于两个单词 – Numerical和Python。其是一个功能强大的 Python 库,可以帮助程序员轻松地进行数值计算,通常应用于以下场景: 执行各种数学任务,如:数值积分、微分、…

Exchange 2010 移动邮箱时提示超过了目标配额

在使用 new-moveRequest 移动邮箱时,提示超过了目标配额。解决过程如下: 经查看,此邮箱配额为 3.826G ,由于邮箱实际比配额大,因此移动时出现下面的错误 找到该用户,邮箱设置-存储配额,最下面的…

什么是URL?协议头,路径和端口是什么意思?

URL(Uniform Resource Locator,统一资源定位符)是互联网上标准资源的地址,互联网上每个文件(即资源)都有一个唯一的URL,它包含了文件的位置以及浏览器处理方式等信息。 URL地址由协议头、服务器地址、文件路径三部分组成。比如,一…

技术图文:Matlab VS. Numpy 常见矩阵

背景 前段时间在知识星球上立了一个Flag,至少写10篇关于 Python,Matlab 和 C# 对比的总结。 这是第 4 篇,对比 Matlab 与 Numpy 中经常用到的各种矩阵,比如零矩阵、单位矩阵、全1矩阵、对角矩阵、均匀分布的随机矩阵、标准正态分…

CssGaga 快速上手指南

CssGaga是ytzong(涛哥)基于我们在朋友网日常的重构工作流程,为了提高工作效率而开发并不断完善起来的一个重构辅助工具。运行于Windows .NET的环境,提供了重构相关的一系列解决方案。最近越来越多的朋友通过腾讯微博、QQ和Email咨…

学习web前端开发要注意什么

web前端的发展前景有目共睹,越来越多的人都对web前端这门编程语言非常感兴趣,想要学习web前端技术,那么学习web前端开发要注意什么呢?大家在学习之前是否有做好这些功课呢?来看看下面的详细介绍吧。 学习web前端开发要注意什么? 1、任何技…

Nagios之事件处理

目标:实现在监控3306端口服务时,出现1次critical软状态时或者在上一次执行后没有成功后出现的第一次硬状态critical情况下,远程执行mysql重启服务,并且每次执行远程重启服务前把报告事件记录到DB中 牵涉技术: (1)Nagios事件处理原理 (2)Ssh无…

张孝祥javascript学习笔记1---HTMLCSS

本节摘要及说明&#xff1a;本节只是对学习过程中知识点的一个简单的记录。 <body>标签的属性 Text属性:用于设定整个网页中的文字颜色 link属性:用于设定一般超链接文本的显示颜色 alink属性&#xff1a;用于设定鼠标移动到超链接上时&#xff0c;超链接文本的显示颜色 …

技术图文:C# VS. Python 读取CSV文件指南

背景 CSV 是一种以逗号进行特征分隔的文本文件类型&#xff0c;在数据库或电子表格中是一种非常常见的导入导出格式。本篇图文就以泰坦尼克号船员获救预测&#xff08; Kaggle&#xff09;中使用的数据集为例来说明 C#、Python和Pandas 如何读取 CSV 数据的。 CSV原始文件如下…

零基础学软件测试去哪家培训机构好

移动互联网的时代&#xff0c;几乎每个人都是机不离手&#xff0c;手机软件的应用频率越来越高&#xff0c;软件测试这个岗位的发展前景更是非常可观&#xff0c;很多人都开始学习软测技术&#xff0c;想要知道零基础学软件测试去哪家培训机构好?来看看下面的详细介绍。 零基础…

技术图文:Numpy 一维数组 VS. Pandas Series

背景 Numpy 提供的最重要的数据结构是 ndarray&#xff0c;它是 Python 中 list 的扩展。 Pandas 提供了两种非常重要的数据结构 Series和DataFrame。 Numpy 中的一维数组与 Series 相似&#xff0c;一维数组只是提供了从0开始与位置有关的索引&#xff0c;而Series除了位置…

【Python】向函数传递任意数量的实参

传递任意数量的实参 有时候&#xff0c;你预先不知道函数需要接受多少个实参&#xff0c;好在Python允许函数从调用语句中收集任意数量的实参 def get_letter(*letters):for i in letters:print(i) get_letter(A,B,C,D,E)形参名*letters中的星号让Python创建一个名为letters的空…

word中插入下标

Word2007中为数字加上下标的几种方法&#xff1a; 一&#xff1a;通过插入>公式>>选择&#xff0c;通过此上下标。 二&#xff1a;写下数字&#xff0c;例如5&#xff0c;然后按ctrlshift号三个键&#xff0c;就可添加上标&#xff0c;按ctrl号两键&#xff0c;就可标…

手机应用软件测试的思路与要点

软件测试主要针对于移动互联网行业&#xff0c;那么APP等相关软件的测试工作是非常多的&#xff0c;尤其对于产品的手机项目(应用软件)&#xff0c;主要是进行系统测试。针对手机应用软件的系统测试&#xff0c;通常从如下几个角度开展&#xff1a;功能测试&#xff0c;兼容性测…

数据结构与算法:01 绪论

绪论 知识结构&#xff1a; 一、什么是数据结构 例1&#xff1a;电话号码薄的查询问题。 (a1,b1),(a2,b2),…,(an,bn)(a_1,b_1),(a_2,b_2),\dots,(a_n,b_n) (a1​,b1​),(a2​,b2​),…,(an​,bn​) aia_iai​&#xff1a;表示姓名&#xff0c;bib_ibi​&#xff1a;表示电话…

rar for linux缺少GLIBC_2.7

今天安装rar4.0 for linux&#xff0c;遇到了一个缺少GLIBC_2.7的问题&#xff0c;弄了好久才成功&#xff0c;记录一下&#xff0c;以备不时之需。 系统版本为CentOS 5.5。下载了rar4.0 for linux源码包&#xff0c;解压后&#xff0c;按照makfile文件的提示&#xff0c;进行安…

硅谷产学研的创新循环

在现代社会形态形成的几百年历史中&#xff0c;大学与产业界在分化的体制轨道中形成了各自不同的目标、结构和文化&#xff0c;有关大学与产业合作的种种争议无不缘自于此。今天当知识和技术逐步取代了自然资源和简单劳动力资源而成为首要的创造财富的源泉时&#xff0c;产业界…

java技术培训之File类中常用的构造方法

File类用于封装一个路径&#xff0c;这个路径可以是从系统盘符开始的绝对路径&#xff0c;如&#xff1a;“D:\file\a.txt”&#xff0c;也可以是相对于当前目录而言的相对路径&#xff0c;如&#xff1a;“src\Hello.java”。File类内部封装的路径可以指向一个文件&#xff0c…

数据结构与算法:02 C#语言基本语法结构

02 C#语言基本语法结构 知识结构&#xff1a; 1、数据类型 第一种分类&#xff1a; 简单数据类型&#xff1a;byte、short、int、long、float、double、char、bool组合数据类型&#xff1a;struct、enum、class、interface 类型描述byte无符号8位整型(ushort) short&#x…

积少成多 Flash(ActionScript 3.0 Flex 3.0) 系列文章索引

[源码下载]积少成多 Flash(ActionScript 3.0 & Flex 3.0) 系列文章索引作者&#xff1a;webabcdFlash 之 ActionScript 3.0 1、积少成多Flash(1) - ActionScript 3.0 基础之数据类型、操作符和流程控制语句介绍Flash ActionScript 3.0 中所有的数据类型都是对象&#xff0c…

WPF Snoop 2.7 源码研究

转载于:https://www.cnblogs.com/puncha/archive/2012/04/01/3877001.html

java培训基础知识都学哪些

很多人都开始学习java技术&#xff0c;觉得java语言在未来的发展前景空间非常大&#xff0c;事实却是如此&#xff0c;那么针对于零基础的同学&#xff0c; 学习java技术需要学哪些呢?下面我们就来看看java培训基础知识都学哪些? java培训基础知识都学哪些? 1.JavaWeb Linux…

数据结构与算法:03 C#面向对象设计 I

03 C#面向对象设计 I 知识结构&#xff1a; 1、类与对象 类&#xff1a;用高级程序语言实现的一个ADT描述。对象&#xff1a;通过类声明的变量。 2、封装 2.1 什么是封装 把类的内部隐藏起来以防止外部看到内部的实现过程。 2.2 怎样封装 通过限制修饰符private、protect…

Centos7安装编译安装zabbix2.219及mariadb-5.5.46

mariadb-5.5.46的安装&#xff1a; 首先下载mariadb-5.5.46-linux-x86_64.tar.gz&#xff0c;然后使用tar -xf mariadb-5.5.46-linux-x86_64.tar.gz -C /usr/local目录下 添加数据库组 # groupadd mysql 添加数据库用户 # useradd -g mysql mysql cd /usr/local ln -sv…

软件测试开发:常见测试类型概念

软件测试是软件开发中非常重要的一个环节&#xff0c;软件测试工程师需要对每个环节进行严格把控&#xff0c;才能保证系统在每个阶段得以控制。下面小编就为大家详细介绍一下软件测试开发:常见测试类型概念的相关内容。 软件测试开发:常见测试类型概念&#xff1a; (1)边界测试…

技术图文:C#语言中的泛型 I

C#语言中的泛型 I 知识结构&#xff1a; 1. 泛型概述 泛型广泛应用于容器&#xff08;Collections&#xff09;和对容器操作的方法中。 从 .NET Framework2.0 开始&#xff0c;微软提供了一个新的命名空间System.Collections.Generic&#xff0c;其中包含了一些新的基于泛型…

ubuntu搭建svn、git遇到的问题及解决办法

不错的git笔记博客&#xff1a; http://www.cnblogs.com/wanqieddy/category/406859.html http://blog.csdn.net/zxncvb/article/details/22153019 Git学习教程&#xff08;六&#xff09;Git日志 http://fsjoy.blog.51cto.com/318484/245261/ 图解git http://my.oschina.net/x…

webstorm同时打开多个project方法

曾经多次碰到过想要打开多个project的时候&#xff0c;可每次打开其他项目时&#xff0c;必须选择新窗口还是替换次窗口&#xff0c;如果新窗口的话就无法跟现在的项目在同一个webstorm中同时进行编辑&#xff0c;需要来回切换窗口&#xff0c;很是不方便&#xff0c;今天无意中…