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();
Player开发者_JAVA技巧Paddle 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
Give this a shot:
#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;
}
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 :)
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.
精彩评论