我似乎一遍又一遍地遇到同样的麻烦,并且不能让我的头围得那么厚。这个程序不会显示我想要(并期望它)在屏幕上执行的桨:( -
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
SDL_Event event;
const int paddle_width = 20;
const int paddle_height = 80;
class PlayerPaddle
{
private:
int xloc;
int yloc;
int yvel;
public:
PlayerPaddle()
{
int xloc = 20;
int yloc = 200;
int yvel = 0;
}
void ShowPaddle()
{
glTranslatef(xloc,yloc,0);
glBegin(GL_QUADS);
glColor4f(1.0,1.0,1.0,1.0);
glVertex3f(0,0,0);
glVertex3f(20,0,0);
glVertex3f(20,80,0);
glVertex3f(0,80,0);
glEnd();
glLoadIdentity();
}
void MovePaddle()
{
yloc += yvel;
}
void Handle_Input()
{
if( event.type == SDL_KEYDOWN )
{
switch( event.key.keysym.sym )
{
case SDLK_UP: yvel -= paddle_height / 2; break;
case SDLK_DOWN: yvel += paddle_height / 2; break;
}
}
else if( event.type == SDL_KEYUP )
{
switch( event.key.keysym.sym )
{
case SDLK_UP: yvel += paddle_height / 2; break;
case SDLK_DOWN: yvel -= paddle_height / 2; break;
}
}
}
};
void init ()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(640,480,32,SDL_OPENGL);
SDL_WM_SetCaption( "Pong", NULL );
glClearColor( 0, 0, 0, 0 );
glViewport(0, 0, 640, 480);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, 640 , 480 , 0, 0, 1 );
glMatrixMode( GL_MODELVIEW );
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
}
int main (int argc, char **argv)
{
init();
PlayerPaddle ppaddle;
bool quit = false;
while (quit == false)
{
while (SDL_PollEvent (&event))
{
ppaddle.Handle_Input();
if (event.type == SDL_QUIT)
quit = true;
}
ppaddle.MovePaddle();
glClear(GL_COLOR_BUFFER_BIT);
ppaddle.ShowPaddle();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
我只是得到一个清晰的黑屏,直到我退出程序,按预期返回0。我知道代码:
ppaddle.MovePaddle();
glClear(GL_COLOR_BUFFER_BIT);
ppaddle.ShowPaddle();
SDL_GL_SwapBuffers();
至少迭代一次,好像我插入了一个返回1;在那里的声明程序将启动并立即返回1.
答案 0 :(得分:1)
至少你搞砸了你的构造函数。 Again.
应该是这样的:
PlayerPaddle()
{
xloc = 20;
yloc = 200;
yvel = 0;
}
您可能还想在主循环结束时放入SDL_Delay(100)
以减慢速度,直到您进行更复杂的物理处理为止。