diff options
Diffstat (limited to 'ball.c')
-rw-r--r-- | ball.c | 46 |
1 files changed, 32 insertions, 14 deletions
@@ -6,6 +6,10 @@ #include "glpong.h" #include "ball.h" +static GLuint ball; +static GLfloat ball_width; +static GLfloat ball_height; + #ifdef DEBUG int length(Ball_t * balls) { @@ -18,6 +22,27 @@ length(Ball_t * balls) { #endif void +GLPong_BallInit() { + ball_width = GLPONG_WIDTH / 32.0f; /* FIXME: magic numbers */ + ball_height = GLPONG_HEIGHT / 24.0f; + + ball = glGenLists(1);
+ glNewList(ball, GL_COMPILE);
+ glBegin(GL_QUADS); + glTexCoord2f(0.0f, 0.0f); glVertex2f( 0.0f, 0.0f); /* Lower Left */ + glTexCoord2f(1.0f, 0.0f); glVertex2f(ball_width, 0.0f); /* Lower Right */ + glTexCoord2f(1.0f, 1.0f); glVertex2f(ball_width, ball_height); /* Upper Right */ + glTexCoord2f(0.0f, 1.0f); glVertex2f( 0.0f, ball_height); /* Upper Left */ + glEnd();
+ glEndList(); +} + +void +GLPong_BallDeinit() { + glDeleteLists(ball, 1); +} + +void GLPong_BallAdd(Ball_t ** balls) { Ball_t * newball = NULL; @@ -28,8 +53,6 @@ GLPong_BallAdd(Ball_t ** balls) { #endif newball->next = *balls; - newball->w = GLPONG_WIDTH / 32.0f; /* FIXME: magic numbers */ - newball->h = GLPONG_HEIGHT / 24.0f; newball->x = 100.0f; newball->y = 100.0f; newball->xv = GLPONG_WIDTH / (GLfloat)320; /* FIXME: magic numbers */ @@ -72,10 +95,10 @@ GLPong_BallDeleteAll(Ball_t * list) { int GLPong_BallCollide(const Ball_t * a, const Ball_t * b) { - if ( b->x + b->w < a->x ) return 0; - if ( b->x > a->x + a->w ) return 0; - if ( b->y + b->h < a->y ) return 0; - if ( b->y > a->y + a->h ) return 0; + if ( b->x + ball_width < a->x ) return 0; + if ( b->x > a->x + ball_width ) return 0; + if ( b->y + ball_height < a->y ) return 0; + if ( b->y > a->y + ball_height ) return 0; return 1; } @@ -91,10 +114,10 @@ GLPong_BallMoveAll(Ball_t ** balls) { current->x += current->xv; current->y += current->yv; - if (((current->y + current->h) >= GLPONG_HEIGHT) || (current->y <= 0.0f)) { + if (((current->y + ball_height) >= GLPONG_HEIGHT) || (current->y <= 0.0f)) { current->yv = -current->yv; } - if (((current->x + current->w) >= GLPONG_WIDTH) || (current->x <= 0.0f)) { + if (((current->x + ball_width) >= GLPONG_WIDTH) || (current->x <= 0.0f)) { current->xv = -current->xv; } @@ -113,12 +136,7 @@ GLPong_BallDrawAll(Ball_t * balls) { glLoadIdentity(); glTranslatef(balls->x, balls->y, 0); glColor4ub(balls->r, balls->g, balls->b, balls->a); - glBegin(GL_QUADS); - glTexCoord2f(0.0f, 0.0f); glVertex2f( 0.0f, 0.0f ); /* Lower Left */ - glTexCoord2f(1.0f, 0.0f); glVertex2f(balls->w, 0.0f ); /* Lower Right */ - glTexCoord2f(1.0f, 1.0f); glVertex2f(balls->w, balls->h); /* Upper Right */ - glTexCoord2f(0.0f, 1.0f); glVertex2f( 0.0f, balls->h); /* Upper Left */ - glEnd(); + glCallList(ball); balls = balls->next; } glDisable(GL_TEXTURE_2D); |