1
votes

I've been trying to figure out how to make a powder toy style game on the iPhone. My problem is how to draw pixels to the screen. From what I've read, OpenGL is better for games as it is faster/hardware accelerated, but there is no method to draw pixels directly to the screen. Apparently drawing pixels to an off-screen frame buffer is the way to go, but how do I then pass this to OpenGL? Do I use a texture?

(this is assuming I have no previous knowledge of iPhone graphics programming).

Thanks!

1

1 Answers

3
votes

Usually in OpenGL you draw primitives and polygons. If you need to draw a bitmap, then you need to apply texture for your polygon.

Check out cocos2d-iphone engine for 2D games based on OpenGL.

If you still need to draw a pixel, here is a method from cocos2d to draw a line:

void ccDrawLine( CGPoint origin, CGPoint destination )
{
    CGPoint vertices[2];

    vertices[0] = origin;
    vertices[1] = destination;

    // 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, vertices);  
    glDrawArrays(GL_LINES, 0, 2);

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