1

I seem to be having the same trouble over and over again and cannot get my thick head around it. This program will not display a paddle as I want (and expected it) to do on screen :( -

#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;

}

I just get a clear black screen until I quit the program which returns 0 as expected. I know the code:

    ppaddle.MovePaddle();

    glClear(GL_COLOR_BUFFER_BIT);

    ppaddle.ShowPaddle();

    SDL_GL_SwapBuffers();

iterates at least once as if I inserted a return 1; statement in there the program would start up and return 1 immediately.

genpfault
  • 51,148
  • 11
  • 85
  • 139

1 Answers1

1

At a minimum you screwed up your constructor. Again.

It should probably be this:

PlayerPaddle()
{
    xloc = 20;
    yloc = 200;
    yvel = 0;
}

You may also want to toss in a SDL_Delay(100) at the end of your main loop to slow things down until you get some more sophisticated physics handling going.

Community
  • 1
  • 1
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    Talk about not learning from your mistakes. Thank you genpfault for repeatedly taking your time to correct my stupid mistakes, the program works now. –  Oct 08 '11 at 16:05
  • No problem :) Keep on learning and asking questions and you'll get better! – genpfault Oct 08 '11 at 16:12