相关几个重要的类的继承关系如下
1.所有文本框必须实现CCLabelProtocol接口
2.CCLableTTF比另外两个文本框渲染慢
TTF(TrueTypeFont)是一种字库名称,是Apple公司和Microsoft公司共同推出的字体文件格式,随着windows的流行,已经变成最常用的一种字体文件表示方式。
OK, 看下它的使用方法
CCLabelTTF *label = CCLabelTTF::labelWithString("the string", "Marker Felt", 21);
3.CCLabelBMFont中每个文字相当于一个CCSprite,可以旋转,移动,改变尺寸等
CCLabelBMFont
CCLabelIBMFont *label = CCLabelIBMFont::labelWithString("the string", "font-hd.png");
this->addChild(label);
label->setPosition(ccp(100, 100));
label->setString("change string");
下面是 font-hd.png文件的截图
font-hd.fnt文件定义了 图像文件的名称,以及每个字符对应的位置信息。
4.CCLabelAtlas
如果你用cocos2d-x项目模板创建过项目,那么你已经看过它的效果了,就是左下角显示帧率的数字。
因为帧率一直在变,使用CCLabelTTF的话效率太低,因为只是数字所以也犯不上使用 CCLabelBMFont 加载那么大的文字图像,所以使用这个比较合适。
CCLabelAtlas *label = CCLabelAtlas::labelWithString("12", "fps_images.png", 12, 18, ".");
this->addChild(label);
label->setPosition(ccp(100, 100));
label->setString("34");
5.下面具体分析一下画字的原理(参考一篇博客)
图字,顾名思义,利用图片做为纹理来显示的文字。当下流行的跨平台2D引擎Cocos2d-x和LibGdx也都有对于图字的应用支持,今天我就来为大家讲一讲图字。
首先要介绍一下,图字是怎么来的?其实这个很早很早了,记得80后在95年开始玩DOS下的仙剑奇侠传的时候,那些令人难忘的中文对话吧!DOS下做游戏,使用的是C语言,不要说写字了,很多复杂的操作甚至涉及驱动。那时候绘图就是利用将图片中的像素取出来后绘制在屏幕上,所以处理游戏中的中文,就只有把这些文字的像素预先写到BMP或二进制文件中,然后读取出来再设置屏幕像素以实现。后来进入DDRAW的时代,可以使用WINDOWS系统中的字库来写字了。把DDRAW的后台表面进行LOCK,取出其DC,然后用GDI将文字写到其DC上,这种做法后面也延续了很久,但GDI进行TextOut的效率非常慢,你要是想像梦幻西游一样满屏写了,那得卡死,解决方案是什么?还是图字。专业的游戏开发者会将所用到的字都预处理生成到一张图片中,通过一个编码与纹理UV对应文件来进行纹理UV的获取后做为顶点的UV值然后进行绘制,有也的在每一帧中实时的将需要的字使用DDRAW写字的方法绘制到相应的纹理上然后使用文字编码与纹理UV对应信息来进行绘制,这样效率就提高很多了。目前比较流行的做法是使用一张png图片来存储用到的文字。一个.fnt文件来存储文字图片说明信息。Cocos2d-x和LibGdx中都集成了相关的图字处理类。在世界范围内,也有很多游戏使用了这个方案。在游戏汉化界,了解和掌握图字的原理和修改方法也是很重要的一项工作
我们以Cocos2d-x的tests工程中的LabelTest中的最后一个Label显示“中国”为例来分析一下。
打开Cocos2d-x所在目录下的tests\Resources\fonts目录,找到bitmapFontChinese.png(文字贴图文件)和bitmapFontChinese.fnt(文字图片说明信息文件)
先打开png,我们可以看到它是512×512大小,上面由12行,14列个文字组成。包括有一些汉字,常用字符,数字和字母。它的每个字都是由青色到蓝色的向下渐变。
再用UEdit或记事本打开bitmapFontChinese.fnt,可以看到它的构成,我在这里讲一下。
第一行是对字体的介绍。
1
|
info face="华康海报体W12(P)" size=32 bold=0italic=0 charset="" unicode=0stretchH=100smooth=1 aa=1 padding=0,0,0,0 spacing=1,1
|
解释:
face=”华康海报体W12(P)”:字体为”华康海报体W12(P)”,
size=32:大小为32像素
bold=0 :不加粗
italic=0:不使用斜体
charset=”": charset是编码字符集,这里没有填写值即使用默认,
unicode=0:不使用Unicode
stretchH=100:纵向缩放百分比
smooth=1 :开启平滑
aa=1:开启抗锯齿
padding=0,0,0,0:内边距,文字与边框的空隙。
spacing=1,1 :外边距,就是相临边缘的距离。
第二行是对应所有字贴图的公共信息
1
|
common lineHeight=37 base=28 scaleW=512 scaleH=512pages=1 packed=0
|
解释:
lineHeight=37:行高,如果遇到换行符时,绘制字的位置坐标的Y值在换行后增加的像素值。
base=28 :字的基本大小
scaleW=512 :图片大小
scaleH=512:图片大小
pages=1 :此种字体共用到几张图。
packed=0:图片不压缩
第三行是对应当前字贴图的信息
//第一页,文件名称是”bitmapFontChinese.png”
page id=0 file=”bitmapFontChinese.png”
第四行是当前贴图中所容纳的文字数量
chars count=204
第五行起把当前贴图中所用到的所有文字的编码以及对应在图片上的矩形位置,偏移等列出来
第一个字符编码为32,也就是空格,位置为0,0,宽高为0,0, 绘制到屏幕的相应位置时,像素偏移(0,28),绘制完后相应位置的x往后移15像素再画下一个字符,字的图块在第1页上
char id=32 x=0 y=0 width=0 height=0 xoffset=0 yoffset=28 xadvance=15 page=0 chnl=0
第一个字符编码为汉字”象”,也就是空格,位置为0,0,宽为33,高为36, 绘制到屏幕的相应位置时,像素偏移(0,-1),绘制完后相应位置的x往后移36像素再画下一个字,字的图块在第1页上
char id=35937 x=0 y=0 width=33 height=36 xoffset=0 yoffset=-1 xadvance=36 page=0 chnl=0
char id=26696 x=33 y=0 width=35 height=36 xoffset=-1 yoffset=-1 xadvance=36 page=0 chnl=0
char id=26071 x=68 y=0 width=35 height=36 xoffset=-1 yoffset=-1 xadvance=36 page=0 chnl=0
…
再后面是描述两个字在进行组合绘制时字距调整的相关信息,这里没有要进行间距调整的字组合所以为设-1。对于字组合间距调整可以看此示例图:
kernings count=-1
这个数字代表参与字组合间距调整的字的数量。
如果kernings count大于零,后面会有类似这样的描述:
kerning first=102 second=41 amount=2
也就是’f’与’)’进行组合显示’f)’时,’)’向右移2像素防止粘在一起。
通过上面这些信息,引擎可以通过编码找到相应的文字并取出对应的纹理块。
//Cocos2d-x中LabelBMFontChinese
下面我们来分析一下Cocos2d-x的tests工程中的LabelTest中的最后一个Label,它的类名为CCLabelBMFont.转到其类定义文件CCLabelBMFont.h
1
|
class CC_DLL CCLabelBMFont
:
public CCSpriteBatchNode,
public CCLabelProtocol,
public CCRGBAProtocol
|
可以看到其直接派生于三个类,分别是
CCSpriteBatchNode :精灵批次管理类,用于将使用一张图的多个精灵在设置一次纹理的批次里进行绘制,提高渲染的效率。
CCLabelProtocol :文字字串类
CCRGBAProtocol:颜色调节接口类
由简入深,我们先来看一下CCRGBAProtocol,打开CCProtocols.h
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class CC_DLL CCRGBAProtocol
{ public : //设置颜色 virtual void setColor ( const ccColor3B & color ) = 0 ; //取得颜色 virtual const ccColor3B & getColor ( void ) = 0 ; //返回透明度 virtual GLubyte getOpacity ( void ) = 0 ; //设置透明度 virtual void setOpacity (GLubyte opacity ) = 0 ; //设置是否使用Alpha值设置RGB,如果 virtual void setIsOpacityModifyRGB ( bool bValue ) = 0 ; //取得是否使用Alpha值设置RGB virtual bool getIsOpacityModifyRGB ( void ) = 0 ; } ; |
可以看到CCRGBAProtocol类是个纯虚类,只是定义了一些设置获取颜色信息的接口函数。
继续看CCLabelProtocal,明显的,它也是纯虚类,做为存储字符串的接口使用:
1
2 3 4 5 6 7 8 9 |
class CC_DLL CCLabelProtocol
{ public : // 设置文字标签显示的字符串 virtual void setString ( const char *label ) = 0 ; // 返回文字标签显示的字符串 virtual const char * getString ( void ) = 0 ; } ; |
最后来分析CCSpriteBatchNode类。它由CCNodet 和 CCTextureProtocal两个类派生而来,CCNode是基础结点类,用于将引擎中所有具有逻辑顺序和父子关系的类组织起来,基于CCNode派生的类均可以互相挂接。CCNode不是本章要详细介绍的内容,就不再详细分析了,看一下CCTextureProtocol,这是一个纹理使用接口类:
1
2 3 4 5 6 7 8 9 |
class CC_DLL CCTextureProtocol
:
public CCBlendProtocol
{ public : // 返回所使用的2D纹理 virtual CCTexture2D * getTexture ( void ) = 0 ; // 设置使用的2D纹理,并为纹理的使用计数器加1操作 virtual void setTexture (CCTexture2D *texture ) = 0 ; } ; |
很简单,只有两个函数对纹理进行设置和获取,它派生于CCBlendProtocal,这是一个Alpha混合系数设置接口类,用于在开启Alpha混合状态后对Alpha混合的系数进行设置。再来看一下CCBlendProtocal,这是一个混合状态设置接口类:
1
2 3 4 5 6 7 8 9 |
class CC_DLL CCBlendProtocol
{ public : // 为纹理设置使用的混合状态 virtual void setBlendFunc (ccBlendFunc blendFunc ) = 0 ; // 返回为纹理设置的混合状态 virtual ccBlendFunc getBlendFunc ( void ) = 0 ; } ; |
返回到类CCSpriteBatchNode的定义。我们再来分析CCSpriteBatchNode。之前说了CCSpriteBatchNode是精灵批次管理类,用于将使用一张图的多个精灵在设置一次纹理的批次里进行绘制,提高渲染的效率。既然多个精灵使用一张图,则需要将多个小图块合并在一张图上,这样只要设置使用大图做为纹理,将使用各小图块做为纹理贴图的精灵设置好顶点与UV等数据,就可以绘制出这些精灵了。Cocos2d-x提供了一个类CCTextureAtlas对使用图块的这些精灵所使用的顶点缓冲区进行管理。为了更好的理解CCSpriteBatchNode,我们看一下它的定义和实现:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
#ifndef __CCTEXTURE_ATLAS_H__
#define __CCTEXTURE_ATLAS_H__ //用到的头文件 #include <string> #include "ccTypes.h" #include "CCObject.h" #include "ccConfig.h" //使用Cocos2d命名空间 namespace cocos2d { class CCTexture2D ; //CCTextureAtlas由CCObject派生而来 class CC_DLL CCTextureAtlas : public CCObject { protected : //使用此大图中的图块的精灵对应的三角形索引数组的指针 GLushort *m_pIndices ; #if CC_USES_VBO //如果使用Vertex Buffer Object(VBO:使用显存而非内存存储顶点缓冲数据,大大提高效率),建立VBO句柄数组,第一个元素存顶点数组的句柄,第二个元素存索引数组句柄 GLuint m_pBuffersVBO [ 2 ] ; //标记是否更新需要更新的图块信息。当你新加入了图块或者修改了图块,需要设置为true。 bool m_bDirty ; #endif // CC_USES_VBO // CC_PROPERTY_READONLY宏为类定义变量及增加相应的get函数。 //当前使用图块的数量 CC_PROPERTY_READONLY ( unsigned int, m_uTotalQuads, TotalQuads ) //存储图块信息的数组容量 CC_PROPERTY_READONLY ( unsigned int, m_uCapacity, Capacity ) //设置所使用的大图纹理 CC_PROPERTY (CCTexture2D *, m_pTexture, Texture ) //使用此大图的图块的所有精灵的顶点缓冲信息数组 CC_PROPERTY (ccV3F_C4B_T2F_Quad *, m_pQuads, Quads ) public : //构造 CCTextureAtlas ( ) ; //析构 virtual ~CCTextureAtlas ( ) ; //描述 char * description ( ) ; //静态函数:从文件中创建纹理,并初始化图块容量 static CCTextureAtlas * textureAtlasWithFile ( const char * file , unsigned int capacity ) ; //同上,只是非静态函数。作者提示不能重复调用,否则会造成内存泄漏。 bool initWithFile ( const char * file, unsigned int capacity ) ; //静态函数:从贴图中创建纹理,并初始化图块容量 static CCTextureAtlas * textureAtlasWithTexture (CCTexture2D *texture, unsigned int capacity ) ; //同上,只是非静态函数。作者提示不能重复调用,否则会造成内存泄漏。 bool initWithTexture (CCTexture2D *texture, unsigned int capacity ) ; //通过索引值找到对应的图块顶点缓冲数据并用新数据修改它,由CCSprite实例对象在变换顶点信息时调用。 void updateQuad (ccV3F_C4B_T2F_Quad * quad, unsigned int index ) ; //通过索引值找到对应的图块顶点缓冲数据,并在其之前插入一个新的图块。 void insertQuad (ccV3F_C4B_T2F_Quad * quad, unsigned int index ) ; //通过索引值找到对应的图块顶点缓冲数据,并把它插入另一个图块之前。 void insertQuadFromIndex ( unsigned int fromIndex, unsigned int newIndex ) ; //移除指定位置的图块顶点缓冲数据. void removeQuadAtIndex ( unsigned int index ) ; //清空所有的图块顶点缓冲数据。 void removeAllQuads ( ) ; //重新设置图块顶点缓冲数组的容量 bool resizeCapacity ( unsigned int n ) ; //绘制指定的图块顶点缓冲 void drawNumberOfQuads ( unsigned int n ) ; //绘制从指定的图块起后面的N个图块 void drawNumberOfQuads ( unsigned int n, unsigned int start ) ; //绘制所有的图块顶点缓冲 void drawQuads ( ) ; private : //初始化索引缓冲数据 void initIndices ( ) ; } ; } //namespace cocos2d #endif //__CCTEXTURE_ATLAS_H__ |
再看CPP:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
#include "CCTextureAtlas.h"
#include "CCTextureCache.h" #include "ccMacros.h" // 纹理头文件 #include "CCTexture2D.h" #include <stdlib.h> //使用Cocos2d命名空间 namespace cocos2d { //构造,初始化成员变量 CCTextureAtlas :: CCTextureAtlas ( ) :m_pIndices ( NULL ) #if CC_USES_VBO , m_bDirty ( false ) #endif ,m_pTexture ( NULL ) ,m_pQuads ( NULL ) { } //析构,释放所用的内存 CCTextureAtlas ::~CCTextureAtlas ( ) { // CCLOGINFO("cocos2d: deallocing CCTextureAtlas."); CC_SAFE_FREE (m_pQuads ) CC_SAFE_FREE (m_pIndices ) #if CC_USES_VBO //释放缓冲区对象 glDeleteBuffers ( 2, m_pBuffersVBO ) ; #endif // CC_USES_VBO CC_SAFE_RELEASE (m_pTexture ) ; } //实现通过宏CC_PROPERTY_READONLY声明的函数 //取得当前使用的图块的数量 unsigned int CCTextureAtlas :: getTotalQuads ( ) { return m_uTotalQuads ; } //取得图块集的容量 unsigned int CCTextureAtlas :: getCapacity ( ) { return m_uCapacity ; } //取得大纹理 CCTexture2D * CCTextureAtlas :: getTexture ( ) { return m_pTexture ; } //设置大纹理 void CCTextureAtlas :: setTexture (CCTexture2D * var ) { CC_SAFE_RETAIN (var ) ; CC_SAFE_RELEASE (m_pTexture ) ; m_pTexture = var ; } //取得使用此大图的图块的所有精灵的顶点缓冲信息数组 ccV3F_C4B_T2F_Quad * CCTextureAtlas :: getQuads ( ) { return m_pQuads ; } //设置使用此大图的图块的所有精灵的顶点缓冲信息数组 void CCTextureAtlas :: setQuads (ccV3F_C4B_T2F_Quad *var ) { m_pQuads = var ; } //静态函数:从文件中创建纹理,并初始化图块容量 CCTextureAtlas * CCTextureAtlas :: textureAtlasWithFile ( const char * file, unsigned int capacity ) { //使用new来实例化一个CCTextureAtlas对象 CCTextureAtlas * pTextureAtlas = new CCTextureAtlas ( ) ; //调用成员函数进行初始化 if (pTextureAtlas && pTextureAtlas - >initWithFile (file, capacity ) ) { pTextureAtlas - >autorelease ( ) ; return pTextureAtlas ; } //如果失败,释放后返回NULL CC_SAFE_DELETE (pTextureAtlas ) ; return NULL ; } //静态函数:从贴图中创建纹理,并初始化图块容量 CCTextureAtlas * CCTextureAtlas :: textureAtlasWithTexture (CCTexture2D *texture, unsigned int capacity ) { //使用new来实例化一个CCTextureAtlas对象 CCTextureAtlas * pTextureAtlas = new CCTextureAtlas ( ) ; //调用成员函数进行初始化 if (pTextureAtlas && pTextureAtlas - >initWithTexture (texture, capacity ) ) { pTextureAtlas - >autorelease ( ) ; return pTextureAtlas ; } //如果失败,释放后返回NULL CC_SAFE_DELETE (pTextureAtlas ) ; return NULL ; } //非静态函数,从文件中创建纹理,并初始化图块容量 bool CCTextureAtlas :: initWithFile ( const char * file, unsigned int capacity ) { // 由纹理管理器加载一个图片文件,返回生成的纹理 CCTexture2D *texture = CCTextureCache :: sharedTextureCache ( ) - >addImage (file ) ; //判断生成纹理是否有效 if (texture ) { //如果成功,使用纹理进行初始化 return initWithTexture (texture, capacity ) ; } else { //不成功打印错误日志并返回NULL CCLOG ( "cocos2d: Could not open file: %s", file ) ; delete this ; return NULL ; } } //非静态函数,设置纹理,并初始化图块容量 bool CCTextureAtlas :: initWithTexture (CCTexture2D *texture, unsigned int capacity ) { //纹理有效性判断,防止重复调用 CCAssert (texture ! = NULL, "texture should not be null" ) ; //设置图块容量 m_uCapacity = capacity ; m_uTotalQuads = 0 ; // 设置纹理 this - >m_pTexture = texture ; CC_SAFE_RETAIN (m_pTexture ) ; // 判断是否重复调用 CCAssert (m_pQuads == NULL && m_pIndices == NULL, "" ) ; //申请容量大小的的顶点缓冲信息数组 m_pQuads = (ccV3F_C4B_T2F_Quad * ) calloc ( sizeof (ccV3F_C4B_T2F_Quad ) * m_uCapacity, 1 ) ; //申请容量大小的索引缓冲数组 m_pIndices = (GLushort * ) calloc ( sizeof (GLushort ) * m_uCapacity * 6, 1 ) ; //如果失败,做相应处理 if ( ! ( m_pQuads && m_pIndices ) && m_uCapacity > 0 ) { //CCLOG("cocos2d: CCTextureAtlas: not enough memory"); CC_SAFE_FREE (m_pQuads ) CC_SAFE_FREE (m_pIndices ) CC_SAFE_RELEASE_NULL (m_pTexture ) ; return false ; } //初始化缓冲区对象 #if CC_USES_VBO glGenBuffers ( 2, &m_pBuffersVBO [ 0 ] ) ; m_bDirty = true ; #endif // CC_USES_VBO //初始化索引缓冲 this - >initIndices ( ) ; return true ; } //取得描述 char * CCTextureAtlas :: description ( ) { char *ret = new char [ 100 ] ; sprintf (ret, "<CCTextureAtlas | totalQuads = %u>", m_uTotalQuads ) ; return ret ; } //初始化索引缓冲 void CCTextureAtlas :: initIndices ( ) { //如果容量为0直接返回 if (m_uCapacity == 0 ) return ; //按照顶点的顺序和使用的三角形渲染排列方式为索引缓冲填充数据。 for ( unsigned int i = 0 ; i < m_uCapacity ; i ++ ) { #if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP m_pIndices [i * 6 + 0 ] = i * 4 + 0 ; m_pIndices [i * 6 + 1 ] = i * 4 + 0 ; m_pIndices [i * 6 + 2 ] = i * 4 + 2 ; m_pIndices [i * 6 + 3 ] = i * 4 + 1 ; m_pIndices [i * 6 + 4 ] = i * 4 + 3 ; m_pIndices [i * 6 + 5 ] = i * 4 + 3 ; #else m_pIndices [i * 6 + 0 ] = (GLushort ) (i * 4 + 0 ) ; m_pIndices [i * 6 + 1 ] = (GLushort ) (i * 4 + 1 ) ; m_pIndices [i * 6 + 2 ] = (GLushort ) (i * 4 + 2 ) ; // inverted index. issue #179 m_pIndices [i * 6 + 3 ] = (GLushort ) (i * 4 + 3 ) ; m_pIndices [i * 6 + 4 ] = (GLushort ) (i * 4 + 2 ) ; m_pIndices [i * 6 + 5 ] = (GLushort ) (i * 4 + 1 ) ; // m_pIndices[i*6+3] = i*4+2; // m_pIndices[i*6+4] = i*4+3; // m_pIndices[i*6+5] = i*4+1; #endif } #if CC_USES_VBO //指定m_pBuffersVBO[0]为顶点缓冲区,并激活缓冲区 glBindBuffer (GL_ARRAY_BUFFER, m_pBuffersVBO [ 0 ] ) ; //绑定完成后为其分配内存 glBufferData (GL_ARRAY_BUFFER, sizeof (m_pQuads [ 0 ] ) * m_uCapacity, m_pQuads, GL_DYNAMIC_DRAW ) ; //指定m_pBuffersVBO[1]为索引缓冲区,并激活缓冲区 glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, m_pBuffersVBO [ 1 ] ) ; //绑定完成后为其分配内存 glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof (m_pIndices [ 0 ] ) * m_uCapacity * 6, m_pIndices, GL_STATIC_DRAW ) ; //取消绑定 glBindBuffer (GL_ARRAY_BUFFER, 0 ) ; glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0 ) ; #endif // CC_USES_VBO } //通过索引值找到对应图块的顶点缓冲并用新数据修改它 void CCTextureAtlas :: updateQuad (ccV3F_C4B_T2F_Quad *quad, unsigned int index ) { //有效性判断 CCAssert ( index >= 0 && index < m_uCapacity, "updateQuadWithTexture: Invalid index" ) ; //如果index大于现有的数量则更新数量 m_uTotalQuads = max ( index + 1, m_uTotalQuads ) ; //修改对应的数据值 m_pQuads [index ] = *quad ; //需要更新 #if CC_USES_VBO m_bDirty = true ; #endif } //通过索引值找到对应图块的顶点缓冲,并在其位置之前插入一个新的图块。 void CCTextureAtlas :: insertQuad (ccV3F_C4B_T2F_Quad *quad, unsigned int index ) { //有效性判断 CCAssert ( index < m_uCapacity, "insertQuadWithTexture: Invalid index" ) ; //数量增加 m_uTotalQuads ++ ; CCAssert ( m_uTotalQuads <= m_uCapacity, "invalid totalQuads" ) ; //先将索引位置之后的数据整体后移一个位置,再用新数据填充索引位置的数据。实现插入操作。 unsigned int remaining = (m_uTotalQuads - 1 ) - index ; if ( remaining > 0 ) { // texture coordinates memmove ( &m_pQuads [index + 1 ], &m_pQuads [index ], sizeof (m_pQuads [ 0 ] ) * remaining ) ; } m_pQuads [index ] = *quad ; //设置需要更新m_pBuffersVBO中的VBO数组数据。 #if CC_USES_VBO m_bDirty = true ; #endif } //通过索引值找到对应图块的顶点缓冲,并把它插入另一个图块之前。 void CCTextureAtlas :: insertQuadFromIndex ( unsigned int oldIndex, unsigned int newIndex ) { //有效性判断 CCAssert ( newIndex >= 0 && newIndex < m_uTotalQuads, "insertQuadFromIndex:atIndex: Invalid index" ) ; CCAssert ( oldIndex >= 0 && oldIndex < m_uTotalQuads, "insertQuadFromIndex:atIndex: Invalid index" ) ; //两个索引相同直接返回即可 if ( oldIndex == newIndex ) return ; //计算要移动的图块数量 unsigned int howMany = (oldIndex - newIndex ) > 0 ? (oldIndex - newIndex ) : (newIndex - oldIndex ) ; unsigned int dst = oldIndex ; unsigned int src = oldIndex + 1 ; if ( oldIndex > newIndex ) { dst = newIndex + 1 ; src = newIndex ; } // 开始进行移动 ccV3F_C4B_T2F_Quad quadsBackup = m_pQuads [oldIndex ] ; memmove ( |