原文请猛戳:
http://galoisplusplus.coding....
之前曾经在cocos2d-x V3.x不规则按钮探讨过在cocos2d-x 3.x版本实现不规则按钮的方法,后来本渣又琢磨了下仿照RenderTexture类调用OpenGL ES API来获取图片像素信息的方式。这种方式由于按钮图片的Texture已在内存中,且不需要解析图片文件格式,因此相比之前用Image::initWithImagefile还是要快一些的。
重写的loadnormaltransparentInfo
函数如下:
voID Irregularbutton::loadnormaltransparentInfo(){#ifdef DEBUG auto start = std::chrono::steady_clock::Now();#endif Sprite* normalRenderer = static_cast<Sprite*>(_buttonnormalRenderer); auto normalTexture = normalRenderer->getTexture(); const Size& s = normalTexture->getContentSizeInPixels(); int savedBufferWIDth = (int)s.wIDth; int savedBufferHeight = (int)s.height; glubyte *buffer = nullptr; // the FBO which cocos2dx used is not window-system-provIDed (non-zero ID) Glint oldFBO; glGetIntegerv(GL_FRAMEBUFFER_BINDING,&oldFBO); gluint framebuffer; glGenFramebuffers(1,&framebuffer); glBindFramebuffer(GL_FRAMEBUFFER,framebuffer); glBindTexture(GL_TEXTURE_2D,normalTexture->getname()); glTexImage2D(GL_TEXTURE_2D,GL_RGBA4,savedBufferWIDth,savedBufferHeight,GL_RGBA,GL_UNSIGNED_BYTE,0); glFramebufferTexture2D(GL_FRAMEBUFFER,GL_color_ATTACHMENT0,GL_TEXTURE_2D,normalTexture->getname(),0); CCASSERT(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE,"Could not attach texture to framebuffer"); buffer = new (std::nothrow) glubyte[savedBufferWIDth * savedBufferHeight * 4]; glPixelStorei(GL_PACK_AlignmENT,1); glreadPixels(0,buffer); glBindFramebuffer(GL_FRAMEBUFFER,oldFBO); auto dataLen = savedBufferWIDth * savedBufferHeight * 4; if (normaltransparent_ != nullptr) { delete[] normaltransparent_; } normalimageWIDth_ = savedBufferWIDth; normalimageHeight_ = savedBufferHeight; normaltransparent_ = new bool[dataLen / (sizeof(unsigned char) * 4)]; for (auto i = 0; i < normalimageHeight_; i++) { for (auto j = 0; j < normalimageWIDth_; j++) { normaltransparent_[i * normalimageWIDth_ + j] = (buffer[(i * normalimageWIDth_ + j) * 4 + 3] == 0); } } CC_SAFE_DELETE_ARRAY(buffer); #ifdef DEBUG auto end = std::chrono::steady_clock::Now(); auto totalTime = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); printf("load from memory: %lld ms\n",totalTime.count());#endif}
完整代码请参考:
cocos2d-x-irregular-button
以上是内存溢出为你收集整理的cocos2d-x V3.x不规则按钮-续篇全部内容,希望文章能够帮你解决cocos2d-x V3.x不规则按钮-续篇所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)