我的C ++ OpenGL SDL程序没有按预期工作

时间:2011-09-29 21:16:10

标签: c++ opengl sdl

我刚开始学习OpenGL,我正在尝试制作乒乓球游戏。 我无法让屏幕出现在屏幕上,我无法理解为什么:我想这个代码它应该出现在左上角,当按下向上和向上键时移动。

main.cpp -

#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include "classes.h"
#include "functions.h"

int main (int argc, char *argv[])
{
    init_everything();

    PlayerPaddle paddle;

    bool quit = false;

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

            if( event.type == SDL_QUIT )
            {
                quit = true;
            }
        }

        glClear( GL_COLOR_BUFFER_BIT );
        paddle.show();
        SDL_GL_SwapBuffers();
    }

    SDL_Quit();
    return 0;
}

classes.h -

#ifndef CLASSES_H
#define CLASSES_H

SDL_Event event;

// ******************* Beginning of PlayerPaddle class *******************

class PlayerPaddle
{
private:
    int xloc;
    int yloc;
    int paddle_height;
    int paddle_width;

public:
    PlayerPaddle();
    void show();
    void handle_input();
};

PlayerPaddle::PlayerPaddle()
{
    int xloc = 0;
    int yloc = 0;
    int paddle_height = 50;
    int paddle_width = 15;
}

void PlayerPaddle::show()
{
    glBegin( GL_QUADS );

    glColor4f( 1.0, 1.0, 1.0, 1.0 );

    glVertex3f( 0,            yloc,             0 );
    glVertex3f( paddle_width, yloc,             0 );
    glVertex3f( paddle_width, paddle_height, 0 );
    glVertex3f( 0,            paddle_height, 0 );

    glEnd();
    glLoadIdentity();
}

void PlayerPaddle::handle_input()
{
    if( event.type == SDL_KEYDOWN )
    {
        switch( event.key.keysym.sym )
        {
        case SDLK_UP:
            yloc -= 10;
            paddle_height -= 10;
            break;
        case SDLK_DOWN:
            yloc += 10;
            paddle_height += 10;
            break;
        }
    }

    if (yloc < 0)
    {
        yloc += 10;
        paddle_height += 10;
    }

    if (yloc > 640)
    {
        yloc -= 10;
        paddle_height -= 10;
    }
}

// ******************* End of the PlayerPaddle class *******************

#endif

functions.h -

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

void init_GL()
{
    glClearColor( 0, 0, 0, 0 );

    glViewport(0, 0, 640, 480);

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( 0, 640, 480, 0, 1, -1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
}

void init_everything()
{
    SDL_Init( SDL_INIT_EVERYTHING );
    SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL );
    init_GL();
    SDL_WM_SetCaption( "Pong by Michael Clover", NULL );
}

#endif

1 个答案:

答案 0 :(得分:4)

试一试:

#include <SDL.h>
#include <SDL_opengl.h>


class PlayerPaddle
{
private:
    int xloc;
    int yloc;
    int paddle_height;
    int paddle_width;

public:
    PlayerPaddle()
    {
        xloc = 0;
        yloc = 0;
        paddle_height = 50;
        paddle_width = 15;
    }

    void show()
    {
        glPushMatrix();
        glTranslatef( xloc, yloc, 0 );

        glBegin( GL_QUADS );
        glColor4f( 1.0, 1.0, 1.0, 1.0 );
        glVertex2f( 0,            0             );
        glVertex2f( paddle_width, 0             );
        glVertex2f( paddle_width, paddle_height );
        glVertex2f( 0,            paddle_height );
        glEnd();

        glPopMatrix();
    }

    void handle_input( const SDL_Event& event )
    {
        if( event.type == SDL_KEYDOWN )
        {
            switch( event.key.keysym.sym )
            {
            case SDLK_UP: yloc += 10; break;
            case SDLK_DOWN: yloc -= 10; break;
            }
        }

        if (yloc < 0)
        {
            yloc = 0;
        }

        if (yloc > 640)
        {
            yloc = 640;
        }
    }
};


void init_GL()
{
    glClearColor( 0, 0, 0, 0 );

    glViewport(0, 0, 640, 480);

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( 0, 640, 0, 480, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
}


void init_everything()
{
    SDL_Init( SDL_INIT_EVERYTHING );
    SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL );
    init_GL();
    SDL_WM_SetCaption( "Pong by Michael Clover", NULL );
}


int main (int argc, char *argv[])
{ 
    init_everything();

    PlayerPaddle paddle;

    bool quit = false;

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

            if( event.type == SDL_QUIT )
            {
                quit = true;
            }
        }

        glClear( GL_COLOR_BUFFER_BIT );

        paddle.show();

        SDL_GL_SwapBuffers();
    }

    SDL_Quit();

    return 0;
}

最值得注意的是,你的构造函数被剔除了。您正在声明并设置局部变量而不是您的成员变量。这导致了未初始化的成员变量,在la-la land(我的机器上约为8万左右)和(显然)远离你的视口的地方:)

我切换glOrtho()调用以产生标准的笛卡尔坐标系,左下角为(0,0),右上角为(640,480)。