summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ball.c80
-rw-r--r--ball.h4
-rw-r--r--glpong3d.c60
-rw-r--r--glpong3d.h15
4 files changed, 84 insertions, 75 deletions
diff --git a/ball.c b/ball.c
index 7d60903..e5a0c4e 100644
--- a/ball.c
+++ b/ball.c
@@ -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;
+ }
+}
diff --git a/ball.h b/ball.h
index 6ce1deb..bf3e849 100644
--- a/ball.h
+++ b/ball.h
@@ -6,6 +6,8 @@
#include "glpong3d.h"
-void GLPong_BallDraw(const Ball_t * ball);
+void GLPong_BallDraw(void);
+void GLPong_BallInit(GLuint texture);
+void GLPong_BallMove(void);
#endif
diff --git a/glpong3d.c b/glpong3d.c
index ff1d064..dbd67a2 100644
--- a/glpong3d.c
+++ b/glpong3d.c
@@ -19,6 +19,7 @@ static void GLPong_Draw(void);
static void GLPong_CleanUp(void);
static void GLPong_Move(void);
static void SDL_GL_GetMouseState(Paddle_t * paddle);
+static GLuint SDL_GL_SurfaceToTexture(SDL_Surface * surface);
int main(int argc, char * argv[]) {
unsigned int frames = 0;
@@ -73,18 +74,7 @@ static int
GLPong_Init(GLPong_t * GLPong) {
SDL_Surface * Surface = NULL;
SDL_Surface * temp = NULL;
-
- GLPong->Ball.w = 0.5f;
- GLPong->Ball.h = 0.5f;
- GLPong->Ball.coord.x = -(GLPong->Ball.w / 2);
- GLPong->Ball.coord.y = -(GLPong->Ball.h / 2);
- GLPong->Ball.coord.z = GLPONG_FRONT_Z;
- GLPong->Ball.r = 1.0f;
- GLPong->Ball.g = 0.0f;
- GLPong->Ball.b = 0.0f;
- GLPong->Ball.xv = 0.02f;
- GLPong->Ball.yv = 0.01f;
- GLPong->Ball.zv = -0.05f;
+ GLuint texture;
GLPong->Front.w = 0.66f;
GLPong->Front.h = 0.5f;
@@ -163,8 +153,10 @@ GLPong_Init(GLPong_t * GLPong) {
printf("OpenGL Extensions: %s\n", glGetString(GL_EXTENSIONS));
/*temp = IMG_Load("ball.png");*/ /* disabled since it's already loaded for the icon */
- ball_texture = SDL_GL_SurfaceToTexture(temp);
- SDL_FreeSurface(temp);
+ texture = SDL_GL_SurfaceToTexture(temp);
+ GLPong_BallInit(texture);
+ SDL_FreeSurface(temp);
+
temp = IMG_Load("paddle-skyos.png");
paddle_texture = SDL_GL_SurfaceToTexture(temp);
SDL_FreeSurface(temp);
@@ -221,38 +213,7 @@ GLPong_HandleEvents(void) {
static void
GLPong_Move(void) {
- GLPong.Ball.coord.x += GLPong.Ball.xv;
- if (GLPong.Ball.coord.x > 1.0f) {
- GLPong.Ball.coord.x = 1.0f;
- GLPong.Ball.xv = -GLPong.Ball.xv;
- } else if (GLPong.Ball.coord.x + GLPong.Ball.w < -1.0f) {
- GLPong.Ball.coord.x = -1.0f - GLPong.Ball.w;
- GLPong.Ball.xv = -GLPong.Ball.xv;
- }
-
- GLPong.Ball.coord.y += GLPong.Ball.yv;
- if (GLPong.Ball.coord.y > 0.5f) {
- GLPong.Ball.coord.y = 0.5f;
- GLPong.Ball.yv = -GLPong.Ball.yv;
- } else if (GLPong.Ball.coord.y + GLPong.Ball.h < -0.5f) {
- GLPong.Ball.coord.y = -0.5f - GLPong.Ball.h;
- GLPong.Ball.yv = -GLPong.Ball.yv;
- }
-
- GLPong.Ball.coord.z += GLPong.Ball.zv;
- if (GLPong.Ball.coord.z < GLPONG_BACK_Z) {
- GLPong.Ball.coord.z = GLPONG_BACK_Z;
- GLPong.Ball.zv = -GLPong.Ball.zv;
- } else if (GLPong.Ball.coord.z > GLPONG_FRONT_Z) {
- GLPong.Ball.coord.z = GLPONG_FRONT_Z;
- GLPong.Ball.zv = -GLPong.Ball.zv;
- }
-
-#ifdef DEBUG
- GLPong.Back.coord.x = GLPong.Ball.coord.x;
- GLPong.Back.coord.y = GLPong.Ball.coord.y;
-#endif
- GLPong_Collide(&GLPong.Ball, &GLPong.Front);
+ GLPong_BallMove();
}
void GLPong_Draw(void) {
@@ -289,11 +250,12 @@ void GLPong_Draw(void) {
GLPong_PaddleDrawBack(&GLPong.Back);
- GLPong_BallDraw(&GLPong.Ball);
+ GLPong_BallDraw();
GLPong_PaddleDrawFront(&GLPong.Front);
}
+#if 0
void GLPong_Collide(Ball_t * ball, const Paddle_t * paddle) {
if (ball->coord.z == paddle->coord.z) {
if (ball->coord.x + ball->w < paddle->coord.x) return; /* if ball is left of paddle */
@@ -308,6 +270,7 @@ void GLPong_Collide(Ball_t * ball, const Paddle_t * paddle) {
printf("x: %.3f, y: %.2f, z: %.2f\n", ball->coord.x, ball->coord.y, ball->coord.z);
}
}
+#endif
void GLPong_CleanUp(void) {
@@ -337,7 +300,8 @@ NextPow2(unsigned int value) {
return x;
}
-GLuint SDL_GL_SurfaceToTexture(SDL_Surface * surface) {
+static GLuint
+SDL_GL_SurfaceToTexture(SDL_Surface * surface) {
GLuint texture;
GLenum format = GL_RGB;
diff --git a/glpong3d.h b/glpong3d.h
index 318dcf0..894de0d 100644
--- a/glpong3d.h
+++ b/glpong3d.h
@@ -27,7 +27,6 @@
GLuint paddle_texture;
GLuint paddle_texture_hit;
-GLuint ball_texture;
GLuint box;
typedef struct {
@@ -41,24 +40,10 @@ typedef struct {
} Paddle_t;
typedef struct {
- Coord_t coord;
- GLfloat w, h;
- GLfloat r, g, b;
- GLfloat xv, yv, zv;
- GLfloat rotate;
-} Ball_t;
-
-typedef struct {
- Ball_t Ball;
Paddle_t Front;
Paddle_t Back;
} GLPong_t;
GLPong_t GLPong;
-void GLPong_Collide(Ball_t * ball, const Paddle_t * paddle);
-
-GLuint SDL_GL_SurfaceToTexture(SDL_Surface * surface);
-static __inline__ unsigned int NextPow2(unsigned int value);
-
#endif