Search code examples
iosiphoneipadopengl-escocos2d-iphone

Can't draw a filled rectangle in iPhone 5S and iPad Air


This code draws a filled rectangle:

void ccDrawFilledRect( CGPoint v1, CGPoint v2 )
{
    CGPoint poli[]={v1,CGPointMake(v1.x,v2.y),v2,CGPointMake(v2.x,v1.y)};

    // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
    // Needed states: GL_VERTEX_ARRAY,
    // Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    glVertexPointer(2, GL_FLOAT, 0, poli);
    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

    // restore default state
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnable(GL_TEXTURE_2D);
}

It has always worked, until I begun using an iPhone 5S and iPad Air. I don't understand why doesn't it work with those devices - it was fine with all others. Always using the latest iOS version.

What might it be?

I doubt it matters, but I've been using cocos2d-iphone 1.0.1.


Solution

  • Fixed.

    void ccDrawFilledRect( CGPoint v1, CGPoint v2 ) {
    
        float vertices[] = {
            v1.x,  v1.y,      // Bottom left
            v2.x,  v1.y,  // Bottom right
            v2.x,  v2.y, // Top right
            v1.x, v2.y // Top left
        };
    
        glDisable(GL_TEXTURE_2D);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);
    
        glVertexPointer(2, GL_FLOAT, 0, vertices);
        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
    
        glEnableClientState(GL_COLOR_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glEnable(GL_TEXTURE_2D);
    }
    

    No, I have absolutely no idea why did the old code fail in the iPhone 5S and iPad Air, but this new code seems to work on all devices now.