iOS cocos2d v3将openGL代码转换为CCDrawNode

时间:2014-07-18 08:16:42

标签: ios opengl-es cocos2d-iphone cocos2d-iphone-3

我有一个访问滑动菜单网格的点指示器的方法。

- (void) visit
{
    [super visit];//< Will draw after glPopScene.

    BOOL showPagesIndicator = YES;


    ccColor4B pagesIndicatorNormalColor_ = ccc4([sud integerForKey:key_int_r], [sud integerForKey:key_int_g], [sud integerForKey:key_int_b], 255);
    ccColor4B pagesIndicatorSelectedColor_ = ccc4(255, 255, 255, 255);
    if (showPagesIndicator)
    {
        int totalScreens = iPageCount;

        // Prepare Points Array
        CGFloat n = (CGFloat)totalScreens; //< Total points count in CGFloat.
        CGFloat pY = pageIndicatorPosition.y; //< Points y-coord in parent coord sys.
        CGFloat d = may_double(16.0f); //< Distance between points.
        CGPoint points[totalScreens];
        for (int i=0; i < totalScreens; ++i)
        {
            CGFloat pX = pageIndicatorPosition.x + d * ( (CGFloat)i - 0.5f*(n-1.0f) );
            points[i] = ccp (pX, pY);
        }

        // Set GL Values
#if COCOS2D_VERSION >= 0x00020000
//        ccGLEnable(CC_GL_BLEND);
        ccPointSize( may_double(6.0 * CC_CONTENT_SCALE_FACTOR()) );
#define DRAW_4B_FUNC ccDrawColor4B

#else
        glEnable(GL_POINT_SMOOTH);
        GLboolean blendWasEnabled = glIsEnabled( GL_BLEND );
        glEnable(GL_BLEND);

        // save the old blending functions
        int blend_src = 0;
        int blend_dst = 0;
        glGetIntegerv( GL_BLEND_SRC, &blend_src );
        glGetIntegerv( GL_BLEND_DST, &blend_dst );
        glPointSize( may_double(6.0 * CC_CONTENT_SCALE_FACTOR()) );

#define DRAW_4B_FUNC glColor4ub

#endif
        ccGLBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//        glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );


        // Draw Gray Points
        DRAW_4B_FUNC(pagesIndicatorNormalColor_.r,
                     pagesIndicatorNormalColor_.g,
                     pagesIndicatorNormalColor_.b,
                     pagesIndicatorNormalColor_.a);

        ccDrawPoints( points, totalScreens );

        // Draw White Point for Selected Page
        DRAW_4B_FUNC(pagesIndicatorSelectedColor_.r,
                     pagesIndicatorSelectedColor_.g,
                     pagesIndicatorSelectedColor_.b,
                     pagesIndicatorSelectedColor_.a);
        ccDrawPoint(points[iCurrentPage]);

        // Restore GL Values
#if COCOS2D_VERSION >= 0x00020000
        ccPointSize(1.0f);
#else
        glPointSize(1.0f);
        glDisable(GL_POINT_SMOOTH);
        if (! blendWasEnabled)
            glDisable(GL_BLEND);

        // always restore the blending functions too
        ccGLBlendFunc( blend_src, blend_dst);
//        glBlendFunc( blend_src, blend_dst );
#endif
    }
}

它适用于我之前的项目,但不适用于cocos2d v3。我几乎每个openGL语句都会出错(对C99等无效),例如glPointSize/ccPointSizeglColor4ub。我曾尝试使用CCDrawNode(OOP方式),但它仍然不是一对一的翻译。 (glGetIntegerv等)

以下链接是我创建的测试项目(有错误,请注意屏幕不可滚动,并且未显示点指示符(即visit功能不起作用)

https://drive.google.com/file/d/0BzH5x38rukpYaVlWdUJPWW9EYWc/edit?usp=sharing

编辑: 我尝试使用[renderer enqueueMethod:@selector(drawDotIndicators) target:self];并将访问代码放在drawDotIndicators方法中。它无法正常工作

(本教程结尾:https://www.makegameswith.us/docs/#!/cocos2d/1.1/customrendering

0 个答案:

没有答案