开发者

SDL OpenGL C++ problem

开发者 https://www.devze.com 2023-04-08 03:40 出处:网络
I\'m trying to make a simple game of pong using SDL and OpenGL in C++ and I\'m having trouble displaying any sort of OpenGL image onto the screen and was wondering if anybody could help:

I'm trying to make a simple game of pong using SDL and OpenGL in C++ and I'm having trouble displaying any sort of OpenGL image onto the screen and was wondering if anybody could help:

Initialization:

void init_everything()
{
    SDL_Init( SDL_INIT_EVERYTHING );
    SDL_SetVideoMode( width_of_screen, height_of_screen, bpp_of_screen, SDL_OPENGL );

    glClearColor( 0, 0, 0, 0 );

    // Sets the projection
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( 0, width_of_screen, height_of_screen, 0, 1, -1 );

    // initalises the modelview matrix
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    SDL_WM_SetCaption( "Pong - By Michael Clover", NULL );
}

After this I call the class function:

void paddle::show()
{
    //Move to offset
    glTranslatef( x, y, 0 );

    //Start quad
    glBegin( GL_QUADS );

        //Set color to white
        glColor4f( 1.0, 1.0, 1.0, 1.0 );

        //Draw square
        glVertex3f( 0,            0,             0 );
        glVertex3f( paddle_width, 0,             0 );
        glVertex3f( paddle_width, paddle_height, 0 );
        glVertex3f( 0,            paddle_height, 0 );

    //End quad
    glEnd();

    //Reset
    glLoadIdentity();

}

I then put this in my main function:

int main( int argc, char **argv )
{
    bool quit = false;

    init_everything();

    paddle playe开发者_StackOverflowrpaddle;

    while (quit == false )
    {
        while( SDL_PollEvent( &event ) )
        {
            playerpaddle.handle_input();

            if (event.type == SDL_QUIT)
            {
                quit = true;
            }
        }
        playerpaddle.move();
        glClear( GL_COLOR_BUFFER_BIT );
        playerpaddle.show();
        SDL_GL_SwapBuffers();

    }

    clean_up();
    return 0;
}

All I get is a constant black background screen that I set within a 640 by 480px SDL screen.

Sorry for the huge amount of text and I would be extremely grateful for any insight on what the problem could be here, I'm guessing I'm making a silly mistake somewhere.


I think glOrtho might be set up wrong. The last 2 parameters are near and far clipping planes. Your near plane should be less than zero if you don't want to clip your paddle. And your far plane should be greater than zero. So try this:

glOrtho( 0, width_of_screen, height_of_screen, 0, -1, 1 );


Just 2 things to check now that you've ruled out errors:

  1. Try removing the glTranslatef call. It may be too far off for you to see the scene.

  2. Are you creating your vertices in a clockwise order? I can't tell by the variables you used, but if paddle_height is > 0 or paddle_width < 0, it's not going to be clockwise (which it needs to be).


Try this for your projection instead (if you want 0,0,0 at your center).

double range = 4.0; // Set this to whatever you want for the range of the display.
double ratio = double(displayWidth) / double(displayHeight);
glOrtho(-range* ratio, range* ratio, -range, range, -1.0, 1.0);

Temporarily try to render a square from -1.0,-1.0 to 1.0,1.0 to see if your display is working.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号