我的C ++ OpenGL SDL程序无法正常运行 [英] My C++ OpenGL SDL program is not working as I expected

查看:89
本文介绍了我的C ++ OpenGL SDL程序无法正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习OpenGL,并且正在尝试制作乒乓球游戏. 我无法使操纵杆出现在屏幕上,我也无法弄清楚为什么:我想通过这段代码,它应该出现在左上角并在按下上下键时移动.

I have just started learning OpenGL and I am attempting to make a game of pong. I'm having trouble getting the paddle to appear on the screen and I can't fathom out why: I thought with this code it should appear in the top left corner and move when down and up keys are pressed.

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-

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-

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

推荐答案

试一下:

#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(在我的计算机上为-80 000左右)和(显然)在您的视口附近都没有出现:)

Most notably your constructor was borked. You were declaring and setting local variables instead of your member variables. Which resulted in uninitialized member variables, way off in la-la land (-80 thousand or so on my machine) and (obviously) nowhere near your viewport :)

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

I switched the glOrtho() call around to produce a standard Cartesian coordinate system, with (0,0) in the lower-left and (640,480) in the upper-right.

这篇关于我的C ++ OpenGL SDL程序无法正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆