cocos2d里面使用精灵表单(spritesheet)的好处

来源:互联网 发布:细说php第一版 pdf 编辑:程序博客网 时间:2024/05/17 06:48

免责声明:本博客的所有原创文章,均有time_anime精心写作,供学习交流使用,切勿进行商业传播。同时,转载请不要移除本声明!


SpriteSheet精灵表单:每个游戏开发者都应该知道的基本概念。


看上图的游戏场景,它由不同的图形对象组成,有花、树、云朵和PixelGuy(就是那个小孩了)。所有这些可移动的游戏对象被称作精灵。


让我们看看一个特别的精灵,这个精灵是有着确定的像素宽度和高度的矩形图片。大小为140X140像素。

For eachpixel, some amount of memory is required to store its color. The exact amountdepends on the color depth - which is by default 32bit - and thus consumes 4bytes. So the complete memory usage of PixelGuy is 140x140x4 which is 76kb.


Dependingon your graphics hardware you might only be able to use particular sizes foryour sprites - e.g. a power of 2 or even worse - squared sprites.


For this asprite must be padded with additional unused pixels to match the hardware‘sconstraints. To meet this requirement PixelGuy‘s sprite must be extended to256x256 pixels. This increases the memory usage - now consuming 256kb - whichis more than three times the size of the original sprite.


Not muchfor a single sprite I must admit - but imagine creating a game with manyobjects, characters and animation phases. You might easily end up with hundredsof sprites - each of them wasting precious resources.


But what if youcould use the wasted memory after all? Imagine packing another sprite into thatspace!

The result is calleda sprite sheet!

Now only thecomplete sheet has to meet the hardware constraints. As you see the memoryusage is now much lower than it would be using individual sprites.


Butthere's still more we can do! Let‘s squeeze out the transparent parts of thesprites, packing them even tighter. In our example we have now reduced thememory by another 50%!


But wait!There's even more we can do - if memory is scarce. In a standard 32bit colorformat each color channel and the transparency is represented by 8 bits -totaling 4 bytes for each pixel. We can reduce the color depth to 16bit bysqueezing out some colors. The quality of the reduction can be improved byapplying dithering. The result reduces memory usage by 50%!


In thefirst part, you saw how sprite sheets can help you reduce memory usage for yourgame. In the second part, you are going to learn how the performance of yourgame can be increased with sprite sheets.

0 0
原创粉丝点击