gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:18:02 24 4
gpt4 key购买 nike

我刚开始学习 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;
}

类.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

函数.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 左右)并且(显然)离你的视口(viewport)不远:)

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

关于c++ - 我的 C++ OpenGL SDL 程序没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7603484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com