zoukankan      html  css  js  c++  java
  • opengles 2.0 移植之路

    mvp相关链接:
    http://www.raywenderlich.com/forums//viewtopic.php?f=20&t=512&start=40#p23610

    相关代码:

    void TestDbgDraw::rtWithColor(ccColor4F bgColor, float textureSize) {
        // 1: Create new CCRenderTexture
        m_pRt = CCRenderTexture::create(textureSize, textureSize);
    
        // 2: Call CCRenderTexture:begin
        m_pRt->beginWithClear(bgColor.r, bgColor.g, bgColor.b, bgColor.a);
    
        // 3: Draw into the texture
        CCSprite* t_pSpNoise = CCSprite::create("blocks.png");
        t_pSpNoise->setBlendFunc((ccBlendFunc){GL_DST_COLOR, GL_ZERO});
        t_pSpNoise->setPosition(ccp(textureSize/2, textureSize/2));
        t_pSpNoise->visit();
    
        //openGL gradient
        float gradientAlpha = 1.0f;
    
        /**
         * Added by Bruce Yang on 2012.12.18.13.45~
         * 症结所在,耗费我 n 多个小时(将 CCPoint 换成 CGPoint,也可以换成 b2Vec2)~
         */
        CGPoint vertices[4];
        ccColor4F colors[4];
        int nVertices = 0;
    
        vertices[nVertices] = CGPointMake(0, 0);
        colors[nVertices ++] = (ccColor4F){1, 0, 0, 0};
        vertices[nVertices] = CGPointMake(textureSize, 0);
        colors[nVertices ++] = (ccColor4F){0, 1, 0, 0};
        vertices[nVertices] = CGPointMake(0, textureSize);
        colors[nVertices ++] = (ccColor4F){0, 0, 1, gradientAlpha};
        vertices[nVertices] = CGPointMake(textureSize, textureSize);
        colors[nVertices ++] = (ccColor4F){1, 1, 0, gradientAlpha};
    
        CC_NODE_DRAW_SETUP();
        glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);
        glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_FLOAT, GL_FALSE, 0, colors);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
    
        // 4: Call CCRenderTexture:end
        m_pRt->end();
        m_pRt->setPosition(64.0f, 64.0f);
    
        ccTexParams tp = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
        m_pRt->getSprite()->getTexture()->setTexParameters(&tp);
    
        // 6: Create a new Sprite from the texture
        addChild(m_pRt);
    }

    注意点:
    1.m_pRt 是 CCRenderTexture* 类型的成员变量~
    2.在 cocos2d-x v2.0 里面,不能随意将 CGPoint 换成 CCPoint,
    此次就是因为没有注意到这点让我吃足了苦头。
    如果使用 cocos2d-x v2.0 的话,要继续使用 CGPoint 可以引入头文件 <CoreGraphics/CGGeometry.h>
    当然,CoreGraphics 是 iOS sdk 的专属,是不适用于 andriod 平台的。
    不过也不要紧,如果是想移植到 andriod 平台,也可以用 b2Vec2 来代替 CGPoint 类型,
    总之,就是不要用 CCPoint!!(CCPoint 本身就是一个 class 类型而非简单的结构体,这就是症结所在!)~
    3.CCRenderTexture 的 clear 和 beginWithClear 方法有很大的区别,要注意区分~

  • 相关阅读:
    预处理命令
    函数
    结构体
    字符数组
    数组
    文件
    用 typedef 定义类型
    枚举类型
    联合
    位运算
  • 原文地址:https://www.cnblogs.com/java20130723/p/3212076.html
Copyright © 2011-2022 走看看