[SDL] OpenGL in SDL

Mario de Sousa mcd at jhb.ucs.co.za
Fri Feb 13 00:49:20 PST 2004


Hey, guys.

Can anyone see anything wrong with the following code:

#include <SDL/SDL.h>
#include <GL/gl.h>
#include <stdio.h>

int main() {
        if ( SDL_Init( SDL_INIT_VIDEO ) != 0 ) {
                printf( "%s\n", SDL_GetError() );
                return 1;
        }
        atexit( SDL_Quit );
        SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
        SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
        SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 6 );
        SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
        if ( SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ) == NULL ) {
                printf( "%s\n", SDL_GetError() );
                return 1;
        }
        SDL_WM_SetCaption( "OpenGL Cube", "OpenGL Cube" );
        glEnableClientState( GL_VERTEX_ARRAY );
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        glClearColor( 0, 0, 0, 0 );
        glClear( GL_COLOR_BUFFER_BIT );
        glColor3f( 1.0, 0.0, 0.0 );
        GLfloat all_vertices[] = { 0.0, 0.0, 10.0,
                                   10.0, 0.0, 10.0,
                                   10.0, 10.0, 10.0,
                                   0.0, 10.0, 10.0,
                                   0.0, 0.0, 0.0,
                                   10.0, 0.0, 0.0,
                                   10.0, 10.0, 0.0,
                                   0.0, 10.0, 0.0 };
        glVertexPointer( 3, GL_FLOAT, 0, all_vertices );
        GLubyte all_indices[] = { 4, 5, 6, 7,
                                1, 2, 6, 5,
                                0, 1, 5, 4,
                                0, 3, 2, 1,
                                0, 4, 7, 3,
                                2, 3, 7, 6 };
        glDrawElements( GL_QUADS, 24, GL_UNSIGNED_BYTE, all_indices );
        glFlush();
        SDL_GL_SwapBuffers;
        SDL_Delay( 5000 );
        return 0;
}

It compiles but doesn't display anything on screen.

Thanks.

Mario de Sousa





More information about the SDL mailing list