#include "SDL.h" #include "SDL_opengl.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) { 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); 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 */ glEnd(); glDisable(GL_TEXTURE_2D); #ifdef DEBUG /* Lower Left */ glLoadIdentity(); glColor3f(1.0f, 0.0f, 0.0f); 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(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(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(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, 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; } }