diff options
Diffstat (limited to 'ball.c')
-rw-r--r-- | ball.c | 102 |
1 files changed, 47 insertions, 55 deletions
@@ -1,31 +1,23 @@ #include "SDL.h" #include "SDL_opengl.h" +#include "glpong3d.h" #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(void) { +GLPong_BallDraw(const Ball_t * ball) { glLoadIdentity(); glBindTexture(GL_TEXTURE_2D, ball_texture); glEnable(GL_TEXTURE_2D); - glColor3f(ball.r, ball.g, ball.b); - glTranslatef(ball.coord.x, ball.coord.y, ball.coord.z); + glColor3f(ball->r, ball->g, ball->b); + glTranslatef(ball->x, ball->y, ball->z); glBegin(GL_QUADS); - 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(ball.w, 0.0f); /* Upper Left */ + 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(ball->w, 0.0f); /* Upper Left */ glEnd(); glDisable(GL_TEXTURE_2D); @@ -33,80 +25,80 @@ GLPong_BallDraw(void) { /* Lower Left */ glLoadIdentity(); glColor3f(1.0f, 0.0f, 0.0f); - glTranslatef(ball.coord.x, ball.coord.y, ball.coord.z); + glTranslatef(ball->x, ball->y, ball->z); glBegin(GL_POINTS); glVertex3f(0.0f, 0.0f, 0.0f); glEnd(); /* Lower Right */ glLoadIdentity(); - glTranslatef(ball.coord.x + ball.w, ball.coord.y, ball.coord.z); + glTranslatef(ball->x + ball->w, ball->y, ball->z); glBegin(GL_POINTS); glVertex3f(0.0f, 0.0f, 0.0f); glEnd(); /* Top Right */ glLoadIdentity(); - glTranslatef(ball.coord.x + ball.w, ball.coord.y + ball.h, ball.coord.z); + glTranslatef(ball->x + ball->w, ball->y + ball->h, ball->z); glBegin(GL_POINTS); glVertex3f(0.0f, 0.0f, 0.0f); glEnd(); /* Top Left */ glLoadIdentity(); - glTranslatef(ball.coord.x, ball.coord.y + ball.h, ball.coord.z); + glTranslatef(ball->x, ball->y + ball->h, ball->z); glBegin(GL_POINTS); glVertex3f(0.0f, 0.0f, 0.0f); glEnd(); #endif glLoadIdentity(); - glTranslatef(0.0f, 0.0f, ball.coord.z); + glTranslatef(0.0f, 0.0f, ball->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; +GLPong_BallInit(Ball_t * ball, GLuint texture) { + ball->w = 0.5f; + ball->h = 0.5f; + ball->x = -(ball->w / 2); + ball->y = -(ball->h / 2); + ball->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; +GLPong_BallMove(Ball_t * ball) { + ball->x += ball->xv; + if (ball->x > 1.0f) { + ball->x = 1.0f; + ball->xv = -ball->xv; + } else if (ball->x + ball->w < -1.0f) { + ball->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->y += ball->yv; + if (ball->y > 0.5f) { + ball->y = 0.5f; + ball->yv = -ball->yv; + } else if (ball->y + ball->h < -0.5f) { + ball->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; + ball->z += ball->zv; + if (ball->z < GLPONG_BACK_Z) { + ball->z = GLPONG_BACK_Z; + ball->zv = -ball->zv; + } else if (ball->z > GLPONG_FRONT_Z) { + ball->z = GLPONG_FRONT_Z; + ball->zv = -ball->zv; } } |