summaryrefslogtreecommitdiff
path: root/glpong3d.c
diff options
context:
space:
mode:
Diffstat (limited to 'glpong3d.c')
-rw-r--r--glpong3d.c61
1 files changed, 35 insertions, 26 deletions
diff --git a/glpong3d.c b/glpong3d.c
index 60d720b..2fd98f6 100644
--- a/glpong3d.c
+++ b/glpong3d.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
+#include <stdbool.h>
#include "SDL.h"
#include "SDL_opengl.h"
@@ -15,13 +16,15 @@
static int GLPong_Init(GLPong_t * GLPong);
static int GLPong_HandleEvents(void);
-static void GLPong_Draw(void);
+static void GLPong_Draw(const GLPong_t * GLPong);
+static bool GLPong_Collide(const Ball_t * ball, const Paddle_t * paddle);
static void GLPong_CleanUp(void);
-static void GLPong_Move(void);
+static void GLPong_Move(GLPong_t * GLPong);
static void SDL_GL_GetMouseState(GLfloat * x, GLfloat * y);
static GLuint SDL_GL_SurfaceToTexture(SDL_Surface * surface);
int main(int argc, char * argv[]) {
+ GLPong_t GLPong;
unsigned int frames = 0;
int done = 0;
int action;
@@ -45,12 +48,12 @@ int main(int argc, char * argv[]) {
break;
}
- GLPong_Move();
+ GLPong_Move(&GLPong);
/* begin drawing */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- GLPong_Draw();
+ GLPong_Draw(&GLPong);
frames++;
if (frames == 30) {
@@ -137,7 +140,7 @@ GLPong_Init(GLPong_t * GLPong) {
/*temp = IMG_Load("ball.png");*/ /* disabled since it's already loaded for the icon */
texture = SDL_GL_SurfaceToTexture(temp);
- GLPong_BallInit(texture);
+ GLPong_BallInit(&GLPong->ball, texture);
SDL_FreeSurface(temp);
temp = IMG_Load("paddle-skyos.png");
@@ -194,16 +197,28 @@ GLPong_HandleEvents(void) {
}
static void
-GLPong_Move(void) {
+GLPong_Move(GLPong_t * GLPong) {
GLfloat x, y;
SDL_GL_GetMouseState(&x, &y);
- GLPong_PaddleMove(&GLPong.front_paddle, x, y);
+ GLPong_PaddleMove(&GLPong->front_paddle, x, y);
- GLPong_BallMove();
+ GLPong_BallMove(&GLPong->ball);
+
+ if (GLPong_Collide(&GLPong->ball, &GLPong->front_paddle)
+ || 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", GLPong->ball.x, GLPong->ball.y, GLPong->ball.z);
+#endif
+ }
}
-void GLPong_Draw(void) {
+void GLPong_Draw(const GLPong_t * GLPong) {
float count;
glColor3f(0.0f, 1.0f, 0.0f);
@@ -235,29 +250,23 @@ void GLPong_Draw(void) {
glVertex3f(-1.5f, 1.0f, GLPONG_BACK_Z);
glEnd();
- GLPong_PaddleDraw(&GLPong.back_paddle);
+ GLPong_PaddleDraw(&GLPong->back_paddle);
- GLPong_BallDraw();
+ GLPong_BallDraw(&GLPong->ball);
- GLPong_PaddleDraw(&GLPong.front_paddle);
+ GLPong_PaddleDraw(&GLPong->front_paddle);
}
-#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 */
- if (ball->coord.x > paddle->coord.x + paddle->w) return; /* if ball is right of paddle */
- if (ball->coord.y > paddle->coord.y + paddle->h) return; /* if ball is above paddle */
- if (ball->coord.y + ball->h < paddle->coord.y) return; /* if ball is below paddle */
- if (ball->zv < 0) {
- ball->zv -= 0.005f;
- } else {
- ball->zv += 0.005f;
- }
- printf("x: %.3f, y: %.2f, z: %.2f\n", ball->coord.x, ball->coord.y, ball->coord.z);
+bool GLPong_Collide(const Ball_t * ball, const Paddle_t * paddle) {
+ if (ball->z == paddle->z) {
+ if (ball->x + ball->w < paddle->x) return false; /* if ball is left of paddle */
+ if (ball->x > paddle->x + paddle->w) return false; /* if ball is right of paddle */
+ if (ball->y > paddle->y + paddle->h) return false; /* if ball is above paddle */
+ if (ball->y + ball->h < paddle->y) return false; /* if ball is below paddle */
+ return true;
}
+ return false;
}
-#endif
void GLPong_CleanUp(void) {