From bf004a04b33e1172c5d9871dc4632ecadf491cd7 Mon Sep 17 00:00:00 2001 From: Matt Turner Date: Fri, 22 Jan 2010 01:21:30 -0500 Subject: Split paddle and ball drawing into separate functions Signed-off-by: Matt Turner --- Makefile | 4 +-- ball.c | 54 ++++++++++++++++++++++++++++++ ball.h | 11 +++++++ glpong3d.c | 110 +++---------------------------------------------------------- glpong3d.h | 1 + paddle.c | 76 ++++++++++++++++++++++++++++++++++++++++++ paddle.h | 12 +++++++ 7 files changed, 161 insertions(+), 107 deletions(-) create mode 100644 ball.c create mode 100644 ball.h create mode 100644 paddle.c create mode 100644 paddle.h diff --git a/Makefile b/Makefile index e9f658d..fe425b8 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC=gcc STRIP=strip -WARN=-Wall -W -ansi -pedantic +WARN=-Wall -W -ansi -pedantic -DDEBUG CFLAGS=-ggdb -g3 -pipe -std=c99 ${WARN} LDFLAGS=-Wl,-O1,-s INCLUDES=`sdl-config --cflags` @@ -9,7 +9,7 @@ EXT=.exe EXE=glpong3d${EXT} -OBJS=glpong3d.o fps.o text.o # ball.o paddle.o +OBJS=glpong3d.o ball.o paddle.o fps.o text.o # ball.o paddle.o all: ${OBJS} ${CC} ${LDFLAGS} ${OBJS} -o ${EXE} ${LIBRARIES} diff --git a/ball.c b/ball.c new file mode 100644 index 0000000..7d60903 --- /dev/null +++ b/ball.c @@ -0,0 +1,54 @@ +#include "SDL.h" +#include "SDL_opengl.h" + +#include "ball.h" + +void +GLPong_BallDraw(const Ball_t * ball) { + 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); + 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(1.0f, 0.0f); glVertex2f(0.0f, 0.0f); /* Upper Right */ + glTexCoord2f(0.0f, 0.0f); glVertex2f(GLPong.Ball.w, 0.0f); /* Upper Left */ + glEnd(); + glDisable(GL_TEXTURE_2D); + +#ifdef DEBUG + /* Lower Left */ + glLoadIdentity(); + glColor3f(1.0f, 0.0f, 0.0f); + glTranslatef(GLPong.Ball.coord.x, GLPong.Ball.coord.y, GLPong.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); + 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); + 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); + glBegin(GL_POINTS); + glVertex3f(0.0f, 0.0f, 0.0f); + glEnd(); +#endif + + glLoadIdentity(); + glTranslatef(0.0f, 0.0f, GLPong.Ball.coord.z); + glCallList(box); +} diff --git a/ball.h b/ball.h new file mode 100644 index 0000000..6ce1deb --- /dev/null +++ b/ball.h @@ -0,0 +1,11 @@ +#ifndef BALL_H +#define BALL_H + +#include "SDL.h" +#include "SDL_opengl.h" + +#include "glpong3d.h" + +void GLPong_BallDraw(const Ball_t * ball); + +#endif diff --git a/glpong3d.c b/glpong3d.c index 477988d..c4c4112 100644 --- a/glpong3d.c +++ b/glpong3d.c @@ -10,6 +10,8 @@ #include "glpong3d.h" #include "text.h" #include "fps.h" +#include "paddle.h" +#include "ball.h" static int GLPong_Init(GLPong_t * GLPong); static int GLPong_HandleEvents(void); @@ -18,8 +20,6 @@ static void GLPong_CleanUp(void); static void GLPong_Move(void); static void SDL_GL_GetMouseState(Paddle_t * paddle); -GLuint box; - int main(int argc, char * argv[]) { unsigned int frames = 0; int done = 0; @@ -293,111 +293,11 @@ void GLPong_Draw(void) { glVertex3f(-1.5f, 1.0f, GLPONG_BACK_Z); glEnd(); - glLoadIdentity(); - glBindTexture(GL_TEXTURE_2D, paddle_texture); - glEnable(GL_TEXTURE_2D); - glColor3f(GLPong.Back.r, GLPong.Back.g, GLPong.Back.b); - glTranslatef(GLPong.Back.coord.x, GLPong.Back.coord.y, GLPong.Back.coord.z); - glBegin(GL_QUADS); - glTexCoord2f(0.832f, 1.0f); glVertex2f(GLPong.Back.w / 2, GLPong.Back.h / 2); /* Upper Right */ - glTexCoord2f(0.168f, 1.0f); glVertex2f(-GLPong.Back.w / 2, GLPong.Back.h / 2); /* Upper Left */ - glTexCoord2f(0.168f, 0.0f); glVertex2f(-GLPong.Back.w / 2, -GLPong.Back.h / 2); /* Lower Left */ - glTexCoord2f(0.832f, 0.0f); glVertex2f(GLPong.Back.w / 2, -GLPong.Back.h / 2); /* Lower Right */ - glEnd(); - glDisable(GL_TEXTURE_2D); - - glLoadIdentity(); - glColor3f(1.0f, 0.0f, 0.0f); - glTranslatef(GLPong.Back.coord.x, GLPong.Back.coord.y, GLPong.Back.coord.z); - glBegin(GL_POINTS); - glVertex3f(0.0f, 0.0f, 0.0f); - glEnd(); + GLPong_PaddleDrawBack(&GLPong.Back); - 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); - 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(1.0f, 0.0f); glVertex2f(0.0f, 0.0f); /* Upper Right */ - glTexCoord2f(0.0f, 0.0f); glVertex2f(GLPong.Ball.w, 0.0f); /* Upper Left */ - glEnd(); - glDisable(GL_TEXTURE_2D); + GLPong_BallDraw(&GLPong.Ball); - /* Lower Left */ - glLoadIdentity(); - glColor3f(1.0f, 0.0f, 0.0f); - glTranslatef(GLPong.Ball.coord.x, GLPong.Ball.coord.y, GLPong.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); - 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); - 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); - glBegin(GL_POINTS); - glVertex3f(0.0f, 0.0f, 0.0f); - glEnd(); - - glLoadIdentity(); - glTranslatef(0.0f, 0.0f, GLPong.Ball.coord.z); - glCallList(box); - - glLoadIdentity(); - glBindTexture(GL_TEXTURE_2D, paddle_texture); - glEnable(GL_TEXTURE_2D); - glColor3f(GLPong.Front.r, GLPong.Front.g, GLPong.Front.b); - glTranslatef(GLPong.Front.coord.x + (GLPong.Front.w / 2), GLPong.Front.coord.y + (GLPong.Front.h / 2), GLPong.Front.coord.z); - glBegin(GL_QUADS); - glTexCoord2f(0.168f, 1.0f); glVertex2f(GLPong.Front.w / 2, GLPong.Front.h / 2); /* Upper Right */ - glTexCoord2f(0.832f, 1.0f); glVertex2f(-GLPong.Front.w / 2, GLPong.Front.h / 2); /* Upper Left */ - glTexCoord2f(0.832f, 0.0f); glVertex2f(-GLPong.Front.w / 2, -GLPong.Front.h / 2); /* Lower Left */ - glTexCoord2f(0.168f, 0.0f); glVertex2f(GLPong.Front.w / 2, -GLPong.Front.h / 2); /* Lower Right */ - glEnd(); - glDisable(GL_TEXTURE_2D); - - /* Lower Left */ - glLoadIdentity(); - glColor3f(1.0f, 0.0f, 0.0f); - glTranslatef(GLPong.Front.coord.x, GLPong.Front.coord.y, GLPong.Front.coord.z); - glBegin(GL_POINTS); - glVertex3f(0.0f, 0.0f, 0.0f); - glEnd(); - - /* Lower Right */ - glLoadIdentity(); - glTranslatef(GLPong.Front.coord.x + GLPong.Front.w, GLPong.Front.coord.y, GLPong.Front.coord.z); - glBegin(GL_POINTS); - glVertex3f(0.0f, 0.0f, 0.0f); - glEnd(); - - /* Top Right */ - glLoadIdentity(); - glTranslatef(GLPong.Front.coord.x + GLPong.Front.w, GLPong.Front.coord.y + GLPong.Front.h, GLPong.Front.coord.z); - glBegin(GL_POINTS); - glVertex3f(0.0f, 0.0f, 0.0f); - glEnd(); - /* Top Left */ - glLoadIdentity(); - glTranslatef(GLPong.Front.coord.x, GLPong.Front.coord.y + GLPong.Front.h, GLPong.Front.coord.z); - glBegin(GL_POINTS); - glVertex3f(0.0f, 0.0f, 0.0f); - glEnd(); + GLPong_PaddleDrawFront(&GLPong.Front); } void GLPong_Collide(Ball_t * ball, const Paddle_t * paddle) { diff --git a/glpong3d.h b/glpong3d.h index 844b629..e032fc7 100644 --- a/glpong3d.h +++ b/glpong3d.h @@ -28,6 +28,7 @@ GLuint paddle_texture; GLuint paddle_texture_hit; GLuint ball_texture; +GLuint box; typedef struct { GLfloat x, y, z; diff --git a/paddle.c b/paddle.c new file mode 100644 index 0000000..2f6bace --- /dev/null +++ b/paddle.c @@ -0,0 +1,76 @@ +#include "SDL.h" +#include "SDL_opengl.h" + +#include "glpong3d.h" +#include "paddle.h" + +void +GLPong_PaddleDrawBack(const Paddle_t * paddle) { + glLoadIdentity(); + glBindTexture(GL_TEXTURE_2D, paddle_texture); + glEnable(GL_TEXTURE_2D); + glColor3f(paddle->r, paddle->g, paddle->b); + glTranslatef(paddle->coord.x, paddle->coord.y, paddle->coord.z); + glBegin(GL_QUADS); + glTexCoord2f(0.832f, 1.0f); glVertex2f(paddle->w / 2, paddle->h / 2); /* Upper Right */ + glTexCoord2f(0.168f, 1.0f); glVertex2f(-paddle->w / 2, paddle->h / 2); /* Upper Left */ + glTexCoord2f(0.168f, 0.0f); glVertex2f(-paddle->w / 2, -paddle->h / 2); /* Lower Left */ + glTexCoord2f(0.832f, 0.0f); glVertex2f(paddle->w / 2, -paddle->h / 2); /* Lower Right */ + glEnd(); + glDisable(GL_TEXTURE_2D); + +#ifdef DEBUG + glLoadIdentity(); + glColor3f(1.0f, 0.0f, 0.0f); + glTranslatef(paddle->coord.x, paddle->coord.y, paddle->coord.z); + glBegin(GL_POINTS); + glVertex3f(0.0f, 0.0f, 0.0f); + glEnd(); +#endif +} + +void +GLPong_PaddleDrawFront(const Paddle_t * paddle) { + glLoadIdentity(); + glBindTexture(GL_TEXTURE_2D, paddle_texture); + glEnable(GL_TEXTURE_2D); + glColor3f(paddle->r, paddle->g, paddle->b); + glTranslatef(paddle->coord.x + (paddle->w / 2), paddle->coord.y + (paddle->h / 2), paddle->coord.z); /* XXX: why does this line differ from above? */ + glBegin(GL_QUADS); + glTexCoord2f(0.168f, 1.0f); glVertex2f(paddle->w / 2, paddle->h / 2); /* Upper Right */ + glTexCoord2f(0.832f, 1.0f); glVertex2f(-paddle->w / 2, paddle->h / 2); /* Upper Left */ + glTexCoord2f(0.832f, 0.0f); glVertex2f(-paddle->w / 2, -paddle->h / 2); /* Lower Left */ + glTexCoord2f(0.168f, 0.0f); glVertex2f(paddle->w / 2, -paddle->h / 2); /* Lower Right */ + glEnd(); + glDisable(GL_TEXTURE_2D); + +#ifdef DEBUG + /* Lower Left */ + glLoadIdentity(); + glColor3f(1.0f, 0.0f, 0.0f); + glTranslatef(paddle->coord.x, paddle->coord.y, paddle->coord.z); + glBegin(GL_POINTS); + glVertex3f(0.0f, 0.0f, 0.0f); + glEnd(); + + /* Lower Right */ + glLoadIdentity(); + glTranslatef(paddle->coord.x + paddle->w, paddle->coord.y, paddle->coord.z); + glBegin(GL_POINTS); + glVertex3f(0.0f, 0.0f, 0.0f); + glEnd(); + + /* Top Right */ + glLoadIdentity(); + glTranslatef(paddle->coord.x + paddle->w, paddle->coord.y + paddle->h, paddle->coord.z); + glBegin(GL_POINTS); + glVertex3f(0.0f, 0.0f, 0.0f); + glEnd(); + /* Top Left */ + glLoadIdentity(); + glTranslatef(paddle->coord.x, paddle->coord.y + paddle->h, paddle->coord.z); + glBegin(GL_POINTS); + glVertex3f(0.0f, 0.0f, 0.0f); + glEnd(); +#endif +} diff --git a/paddle.h b/paddle.h new file mode 100644 index 0000000..5dc4f12 --- /dev/null +++ b/paddle.h @@ -0,0 +1,12 @@ +#ifndef PADDLE_H +#define PADDLE_H + +#include "SDL.h" +#include "SDL_opengl.h" + +#include "glpong3d.h" + +void GLPong_PaddleDrawBack(const Paddle_t * paddle); +void GLPong_PaddleDrawFront(const Paddle_t * paddle); + +#endif /* PADDLE_H */ -- cgit v1.2.3