教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 教育文库 >

深入分析Cocos2d-x 2.0中的“纹理”(3)

来源:网络收集 时间:2026-10-03
导读: } // 由 CCImage 对象来初始化纹理生成 OpenGL 贴图。 bool CCTexture2D::initWithImage(CCImage *uiImage) { //参数有效性判断。 if (uiImage == NULL) { CCLOG(cocos2d: CCTexture2D. Can't create Texture. UIIm

}

// 由 CCImage 对象来初始化纹理生成 OpenGL 贴图。 bool CCTexture2D::initWithImage(CCImage *uiImage) { //参数有效性判断。 if (uiImage == NULL) { CCLOG("cocos2d: CCTexture2D. Can't create Texture. UIImage is nil"); this->release(); return false; } //取得图片的宽高 unsigned int imageWidth = uiImage->getWidth(); unsigned int imageHeight = uiImage->getHeight(); //取得引擎的配置信息 CCConfiguration *conf = CCConfiguration::sharedConfiguration(); //取得配置信息中指定的最大纹理大小 unsigned maxTextureSize = conf->getMaxTextureSize(); //如果当前图片大于指定的最大纹理大小,提示错误警告交释放当前纹理返回 NULL。 if (imageWidth > maxTextureSize || imageHeight > maxTextureSize) { CCLOG("cocos2d: WARNING: Image (%u x %u) is bigger than the supported %u x %u", imageWidth, imageHeight, maxTextureSi ze, maxTextureSize);

246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256. 257. 258. 259. 260. 261. 262. 263. 264. 265. 266. } }

this->release(); return NULL;

//总是按加载 ALPHA 渐变的图片方式来生成 OpenGL 贴图 return initPremultipliedATextureWithImage(uiImage, imageWidth, imageHeight);

//加载一个带 ALPHA 渐变的图片生成 OpenGL 贴图 bool CCTexture2D::initPremultipliedATextureWithImage(CCImage *image, unsigned int width, unsigned int height) { //取得图片的相关信息 //定义指针变量指向图片像素数据。 unsigned char* tempData = image->getData();

//定义无符号 int 指针变量,也是为了指向 32 位色深的图片像素数据,以便使指针直接对应指定的一个像素数据位置。 unsigned int* inPixel32 = NULL;

//定义无符号 char 指针变量,也是为了指向 8 位色深的图片像素数据,以便使指针直接对应指定的一个像素数据位置。 unsigned char* inPixel8 = NULL;

//定义无符号 short 指针变量,指向 16 位色深的贴图像素数据,以便使指针直接对应指定的一个像素数据位置。 unsigned short* outPixel16 = NULL;

//定义 bool 变量 hasAlpha 取得图片是否有 Alpha 通道。 bool hasAlpha = image->hasAlpha();

267. 268. 269. 270. 271. 272. 273. 274. 275. 276. 277. 278. 279. 280. 281.

//定义变量 imageSize 保存图片大小。 CCSize imageSize = CCSizeMake((float)(image->getWidth()), (float)(image->getHeight()));

//定义变量 pixelFormat 用来保存贴图的像素格式。 CCTexture2DPixelFormat pixelFormat;

//定义变量 bpp 保存图片的色深。 size_t bpp = image->g

etBitsPerComponent();

// 如果有 ALPHA 通道,使用默认的 RGBA8888 格式。 if(hasAlpha) { pixelFormat = g_defaultAlphaPixelFormat; } else { //如果没有 ALPHA 通道 //如果色深大于等于 8,则转为 RGB888 格式,否则转为 RGB565 格式。这里有点问题,感觉应该按色深大于 16 来进行判断。即 24 和 32 位都转为 RGB888,而 16 位及以下转为 RGB565。

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. { }

if (bpp >= 8) { pixelFormat = kCCTexture2DPixelFormat_RGB888; } else { pixelFormat = kCCTexture2DPixelFormat_RGB565; }

// 取得数据的长度 unsigned int length = width * height; //根据图片的不同格式和要创建的纹理格式,将数据填充到纹理中。 if (pixelFormat == kCCTexture2DPixelFormat_RGB565)

//根据是否有 ALPHA 通道来分别进行填充处理 if (hasAlpha) { // 转换 RGBA8888 到 RGB565 // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB" //一个像素占 2 个字节,所以图像中所有像素占用的字节数为 width*height*2。由此大小申请内存作为贴图的像素数据。 tempData = new unsigned char[width * height * 2]; //将贴图像素数据的地址返回给 unsigned short 指针。这样 outPixel16 就指向了贴图中的第一个像素的数据位置。 outPixel16 = (unsigned short*)tempData; //因为有 alpha,则图片是 32 位 RGBA8888 格式。取得图像的像素数据地址返回给 unsigned int 指针,则 inPixel32 指向了贴图中第一个 像素的数据位置。

308. 309.

inPixel32 = (unsigned int*)image->getData(); //遍历图片中所有的像素,逐像素处理。

310. 311. 数据中。 312. 313. 314. 315. 316. 317. 318. 319. 320. 321. 322. 323. 324. 325. 326. 327. 328. 329. } else {

for(unsigned int i = 0; i < length; ++i, ++inPixel32) { //将 inPixel32 指向的 unsigned int 数据通过取出 R,G,B 各 8 位数据值, 然后组成 RGB565 值。 放入 outPixel16 指向的 unsigned short

*outPixel16++ = ((((*inPixel32 >> ((((*inPixel32 >> 0) & 0xFF) >> 3) << 11) | 8) & 0xFF) >> 2) << 5) | // R // G // B

((((*inPixel32 >> 16) & 0xFF) >> 3) << 0); }

// 转换 RGB888 到 RGB565 // Convert "RRRRRRRRRGGGGGGGGBBBBBBBB" to "RRRRRGGGGGGBBBBB" //一个像素占 2 个字节,所以图像中所有像素占用的字节数为 width*height*2。由此大小申请内存作为贴图的像素数据。 tempData = new unsigned char[width * height * 2]; //将贴图像素数据的地址返回给 unsigned short 指针。这样 outPixel16 就指向了贴图中的第一个像素的数据位置。 outPixel16 = (unsigned short*)tempData; //如果图像的格式为 RGB888。取得图像的像素数据地址返回给 unsigned char 指针,则 inPixel8 指向了贴图中第一个像素的 R 值位置。 inPixel8 = (unsigned char*)image->getData(); //遍历图片中所有的像素,逐像素处理。 for(unsigned int i = 0; i < le

ngth; ++i) { //inPixel8 指向的是 unsigned char 值,通过++操作来取出 R,G,B 数据值,然后组成 RGB565 值。放入 outPixel16 指向的

unsigned short 数据中。 330. 331. 332. 333. 334. 335. 336. 337. 338. 339. 340. 341. 342. 343. 344. 345. 346. 347. 348. 349. 数据中。 350. 351. *outPixel16++ = ((((*inPixel32 >> 0) & 0xFF) >> 4) << 12) | // R } else if (pixelFormat == kCCTexture2DPixelFormat_RGBA4444) { // 转换 RGBA8888 到 RGBA4444 // Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRGGGGBBBBAAAA" //取得图像的像素数据地址返回给 unsigned int 指针,则 inPixel32 指向了贴图中第一个像素的数据位置。 inPixel32 = (unsigned int*)image->getData(); //一个像素占 2 个字节,所以图像中所有像素占用的字节数为 width*height*2。由此大小申请内存作为贴图的像素数据。 tempData = new unsigned char[width * height * 2]; //将贴图像素数据的地址返回给 unsigned short 指针。这样 outPixel16 就指向了贴图中的第一个像素的 …… 此处隐藏:4645字,全部文档内容请下载后查看。喜欢就下载吧 ……

深入分析Cocos2d-x 2.0中的“纹理”(3).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/111599.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150份
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150份
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)