diff options
Diffstat (limited to 'ball.c')
-rw-r--r-- | ball.c | 80 |
1 files changed, 69 insertions, 11 deletions
@@ -3,18 +3,29 @@ #include "ball.h" +typedef struct { + Coord_t coord; + GLfloat w, h; + GLfloat r, g, b; + GLfloat xv, yv, zv; + GLfloat rotate; +} Ball_t; + +static Ball_t ball; +static GLuint ball_texture; + void -GLPong_BallDraw(const Ball_t * ball) { +GLPong_BallDraw(void) { glLoadIdentity(); glBindTexture(GL_TEXTURE_2D, ball_texture); glEnable(GL_TEXTURE_2D); - glColor3f(GLPong.Ball.r, GLPong.Ball.g, GLPong.Ball.b); - glTranslatef(GLPong.Ball.coord.x, GLPong.Ball.coord.y, GLPong.Ball.coord.z); + glColor3f(ball.r, ball.g, ball.b); + glTranslatef(ball.coord.x, ball.coord.y, ball.coord.z); glBegin(GL_QUADS); - glTexCoord2f(0.0f, 1.0f); glVertex2f(GLPong.Ball.w, GLPong.Ball.h); /* Lower Left */ - glTexCoord2f(1.0f, 1.0f); glVertex2f(0.0f, GLPong.Ball.h); /* Lower Right */ + glTexCoord2f(0.0f, 1.0f); glVertex2f(ball.w, ball.h); /* Lower Left */ + glTexCoord2f(1.0f, 1.0f); glVertex2f(0.0f, ball.h); /* Lower Right */ glTexCoord2f(1.0f, 0.0f); glVertex2f(0.0f, 0.0f); /* Upper Right */ - glTexCoord2f(0.0f, 0.0f); glVertex2f(GLPong.Ball.w, 0.0f); /* Upper Left */ + glTexCoord2f(0.0f, 0.0f); glVertex2f(ball.w, 0.0f); /* Upper Left */ glEnd(); glDisable(GL_TEXTURE_2D); @@ -22,33 +33,80 @@ GLPong_BallDraw(const Ball_t * ball) { /* Lower Left */ glLoadIdentity(); glColor3f(1.0f, 0.0f, 0.0f); - glTranslatef(GLPong.Ball.coord.x, GLPong.Ball.coord.y, GLPong.Ball.coord.z); + glTranslatef(ball.coord.x, ball.coord.y, ball.coord.z); glBegin(GL_POINTS); glVertex3f(0.0f, 0.0f, 0.0f); glEnd(); /* Lower Right */ glLoadIdentity(); - glTranslatef(GLPong.Ball.coord.x + GLPong.Ball.w, GLPong.Ball.coord.y, GLPong.Ball.coord.z); + glTranslatef(ball.coord.x + ball.w, ball.coord.y, ball.coord.z); glBegin(GL_POINTS); glVertex3f(0.0f, 0.0f, 0.0f); glEnd(); /* Top Right */ glLoadIdentity(); - glTranslatef(GLPong.Ball.coord.x + GLPong.Ball.w, GLPong.Ball.coord.y + GLPong.Ball.h, GLPong.Ball.coord.z); + glTranslatef(ball.coord.x + ball.w, ball.coord.y + ball.h, ball.coord.z); glBegin(GL_POINTS); glVertex3f(0.0f, 0.0f, 0.0f); glEnd(); /* Top Left */ glLoadIdentity(); - glTranslatef(GLPong.Ball.coord.x, GLPong.Ball.coord.y + GLPong.Ball.h, GLPong.Ball.coord.z); + glTranslatef(ball.coord.x, ball.coord.y + ball.h, ball.coord.z); glBegin(GL_POINTS); glVertex3f(0.0f, 0.0f, 0.0f); glEnd(); #endif glLoadIdentity(); - glTranslatef(0.0f, 0.0f, GLPong.Ball.coord.z); + glTranslatef(0.0f, 0.0f, ball.coord.z); glCallList(box); } + +void +GLPong_BallInit(GLuint texture) { + ball.w = 0.5f; + ball.h = 0.5f; + ball.coord.x = -(ball.w / 2); + ball.coord.y = -(ball.h / 2); + ball.coord.z = GLPONG_FRONT_Z; + ball.r = 1.0f; + ball.g = 0.0f; + ball.b = 0.0f; + ball.xv = 0.02f; + ball.yv = 0.01f; + ball.zv = -0.05f; + + ball_texture = texture; +} + +void +GLPong_BallMove(void) { + ball.coord.x += ball.xv; + if (ball.coord.x > 1.0f) { + ball.coord.x = 1.0f; + ball.xv = -ball.xv; + } else if (ball.coord.x + ball.w < -1.0f) { + ball.coord.x = -1.0f - ball.w; + ball.xv = -ball.xv; + } + + ball.coord.y += ball.yv; + if (ball.coord.y > 0.5f) { + ball.coord.y = 0.5f; + ball.yv = -ball.yv; + } else if (ball.coord.y + ball.h < -0.5f) { + ball.coord.y = -0.5f - ball.h; + ball.yv = -ball.yv; + } + + ball.coord.z += ball.zv; + if (ball.coord.z < GLPONG_BACK_Z) { + ball.coord.z = GLPONG_BACK_Z; + ball.zv = -ball.zv; + } else if (ball.coord.z > GLPONG_FRONT_Z) { + ball.coord.z = GLPONG_FRONT_Z; + ball.zv = -ball.zv; + } +} |