summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--glpong3d.c18
-rw-r--r--paddle.c18
-rw-r--r--paddle.h11
4 files changed, 37 insertions, 12 deletions
diff --git a/Makefile b/Makefile
index fe425b8..21580a8 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
CC=gcc
STRIP=strip
-WARN=-Wall -W -ansi -pedantic -DDEBUG
+WARN=-Wall -W -pedantic -DDEBUG
CFLAGS=-ggdb -g3 -pipe -std=c99 ${WARN}
LDFLAGS=-Wl,-O1,-s
INCLUDES=`sdl-config --cflags`
diff --git a/glpong3d.c b/glpong3d.c
index 2fd98f6..c8cf85f 100644
--- a/glpong3d.c
+++ b/glpong3d.c
@@ -205,15 +205,27 @@ GLPong_Move(GLPong_t * GLPong) {
GLPong_BallMove(&GLPong->ball);
- if (GLPong_Collide(&GLPong->ball, &GLPong->front_paddle)
- || GLPong_Collide(&GLPong->ball, &GLPong->back_paddle)) {
+ if (GLPong_Collide(&GLPong->ball, &GLPong->front_paddle)) {
if (GLPong->ball.zv < 0) {
GLPong->ball.zv -= 0.005f;
} else {
GLPong->ball.zv += 0.005f;
}
#ifdef DEBUG
- printf("Collision: x: %.3f, y: %.2f, z: %.2f\n", GLPong->ball.x, GLPong->ball.y, GLPong->ball.z);
+ printf("Collision: x: %.3f, y: %.2f, z: %.2f\n\tPaddle: xv: %.3f, yv: %.3f\n",
+ GLPong->ball.x, GLPong->ball.y, GLPong->ball.z,
+ GLPong_PaddleXV(&GLPong->front_paddle), GLPong_PaddleYV(&GLPong->front_paddle));
+#endif
+ } else if (GLPong_Collide(&GLPong->ball, &GLPong->back_paddle)) {
+ if (GLPong->ball.zv < 0) {
+ GLPong->ball.zv -= 0.005f;
+ } else {
+ GLPong->ball.zv += 0.005f;
+ }
+#ifdef DEBUG
+ printf("Collision: x: %.3f, y: %.2f, z: %.2f\n\tPaddle: xv: %.3f, yv: %.3f\n",
+ GLPong->ball.x, GLPong->ball.y, GLPong->ball.z,
+ GLPong_PaddleXV(&GLPong->back_paddle), GLPong_PaddleYV(&GLPong->back_paddle));
#endif
}
}
diff --git a/paddle.c b/paddle.c
index 4c4c9f0..78bc189 100644
--- a/paddle.c
+++ b/paddle.c
@@ -67,6 +67,8 @@ GLPong_PaddleInit(Paddle_t * paddle, GLfloat z, GLuint texture) {
paddle->x = paddle->w / 2.0f;
paddle->y = paddle->h / 2.0f;
paddle->z = z;
+ paddle->prev_x = paddle->x;
+ paddle->prev_y = paddle->y;
paddle->r = 1.0f;
paddle->g = 1.0f;
paddle->b = 1.0f;
@@ -76,10 +78,10 @@ GLPong_PaddleInit(Paddle_t * paddle, GLfloat z, GLuint texture) {
void
GLPong_PaddleMove(Paddle_t * paddle, GLfloat x, GLfloat y) {
- GLfloat old_x = paddle->x;
- GLfloat old_y = paddle->y;
GLfloat mouse_x;
GLfloat mouse_y;
+ paddle->prev_x = paddle->x;
+ paddle->prev_y = paddle->y;
/* clamp paddle to border */
if (x <= -1.5f + (paddle->w / 2.0f)) {
@@ -99,12 +101,12 @@ GLPong_PaddleMove(Paddle_t * paddle, GLfloat x, GLfloat y) {
#ifdef DEBUG
printf("(mouse_x, mouse_y) = (%.3f, %.3f)\n", mouse_x, mouse_y);
- printf("x diff is %f\n", fabs(mouse_x - old_x));
- printf("y diff is %f\n", fabs(mouse_y - old_y));
+ printf("x diff is %f\n", fabs(mouse_x - paddle->prev_x));
+ printf("y diff is %f\n", fabs(mouse_y - paddle->prev_y));
#endif
- if (fabs(mouse_x - old_x) > 0.33f / 2.0f) {
- if (mouse_x > old_x) {
+ if (fabs(mouse_x - paddle->prev_x) > 0.33f / 2.0f) {
+ if (mouse_x > paddle->prev_x) {
paddle->x += 0.33f / 2.0f;
} else {
paddle->x -= 0.33f / 2.0f;
@@ -113,8 +115,8 @@ GLPong_PaddleMove(Paddle_t * paddle, GLfloat x, GLfloat y) {
paddle->x = mouse_x;
}
- if (fabs(mouse_y - old_y) > 0.25f / 2.0f) {
- if (mouse_y > old_y) {
+ if (fabs(mouse_y - paddle->prev_y) > 0.25f / 2.0f) {
+ if (mouse_y > paddle->prev_y) {
paddle->y += 0.25f / 2.0f;
} else {
paddle->y -= 0.25f / 2.0f;
diff --git a/paddle.h b/paddle.h
index 61962e7..9570717 100644
--- a/paddle.h
+++ b/paddle.h
@@ -6,6 +6,7 @@
typedef struct {
GLfloat x, y, z;
+ GLfloat prev_x, prev_y;
GLfloat w, h;
GLfloat r, g, b;
} Paddle_t;
@@ -14,4 +15,14 @@ void GLPong_PaddleDraw(const Paddle_t * paddle);
void GLPong_PaddleInit(Paddle_t * paddle, GLfloat z, GLuint texture);
void GLPong_PaddleMove(Paddle_t * paddle, GLfloat x, GLfloat y);
+static inline GLfloat
+GLPong_PaddleXV(const Paddle_t * paddle) {
+ return paddle->x - paddle->prev_x;
+}
+
+static inline GLfloat
+GLPong_PaddleYV(const Paddle_t * paddle) {
+ return paddle->y - paddle->prev_y;
+}
+
#endif /* PADDLE_H */