From 2702663dd712037f01d9dd97663d8f63603ea930 Mon Sep 17 00:00:00 2001 From: Matt Turner Date: Fri, 4 Aug 2006 04:23:58 +0000 Subject: Bouncing ball fixes/improvements git-svn-id: svn://mattst88.com/svn/glpong3d/trunk@5 4dd1920e-271a-0410-bca0-81b404a81564 --- ball.h | 1 + glpong3d.c | 104 +++++++++++++++++++++++++++++++++++-------------------------- 2 files changed, 60 insertions(+), 45 deletions(-) diff --git a/ball.h b/ball.h index 3ee3e2e..48c8f14 100644 --- a/ball.h +++ b/ball.h @@ -7,6 +7,7 @@ typedef struct { GLfloat w, h, x, y, z; GLfloat r, g, b, a; GLfloat xv, yv, zv; + GLfloat rotate; } Ball_t; #endif diff --git a/glpong3d.c b/glpong3d.c index b9a5d29..a41537c 100644 --- a/glpong3d.c +++ b/glpong3d.c @@ -8,6 +8,8 @@ #include "glpong3d.h" +GLuint box; + int main(int argc, char * argv[]) { if (argc) { if (argv) {} @@ -32,16 +34,16 @@ void GLPong_Init() { GLPong.done = 0; GLPong.w = 800; - GLPong.h = 800; + GLPong.h = 600; - GLPong.Ball.w = 0.2f; - GLPong.Ball.h = 0.2f; - GLPong.Ball.x = -0.1f; - GLPong.Ball.y = -0.1f; + GLPong.Ball.w = 0.5f; + GLPong.Ball.h = 0.5f; + GLPong.Ball.x = -(GLPong.Ball.w / 2); + GLPong.Ball.y = -(GLPong.Ball.h / 2); GLPong.Ball.z = -3.0f; GLPong.Ball.xv = 0.02f; GLPong.Ball.yv = 0.01f; - GLPong.Ball.zv = -0.05f; + GLPong.Ball.zv = -0.01f; SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE); atexit(SDL_Quit); @@ -86,7 +88,17 @@ void GLPong_Init() { temp = IMG_Load("128x128.png"); ball_texture = SDL_GL_SurfaceToTexture(temp); SDL_FreeSurface(temp); -} + + box = glGenLists(1); + glNewList(box, GL_COMPILE); + glBegin(GL_LINE_LOOP); + glVertex3f(-1.5f, -1.0f, 0.0f); /* Lower Left */ + glVertex3f( 1.5f, -1.0f, 0.0f); /* Lower Right */ + glVertex3f( 1.5f, 1.0f, 0.0f); /* Upper Right */ + glVertex3f(-1.5f, 1.0f, 0.0f); /* Upper Left */ + glEnd(); + glEndList(); + } void GLPong_HandleEvents() { if (SDL_PollEvent(&GLPong.Event) != 0) { @@ -125,49 +137,54 @@ void GLPong_Draw() { glLoadIdentity(); glColor3f(0.0f, 1.0f, 0.0f); - glTranslatef(0.0f, 0.0f, 0.0f); - glBegin(GL_LINE_LOOP); - glVertex3f(-1.0f, -1.0f, -3.0f); /* Lower Left */ - glVertex3f( 1.0f, -1.0f, -3.0f); /* Lower Right */ - glVertex3f( 1.0f, 1.0f, -3.0f); /* Upper Right */ - glVertex3f(-1.0f, 1.0f, -3.0f); /* Upper Left */ - glEnd(); + + glTranslatef(0.0f, 0.0f, -3.0f); + glCallList(box); glLoadIdentity(); - glTranslatef(0.0f, 0.0f, 0.0f); - glBegin(GL_LINE_LOOP); - glVertex3f(-1.0f, -1.0f, -9.0f); /* Lower Left */ - glVertex3f( 1.0f, -1.0f, -9.0f); /* Lower Right */ - glVertex3f( 1.0f, 1.0f, -9.0f); /* Upper Right */ - glVertex3f(-1.0f, 1.0f, -9.0f); /* Upper Left */ - glEnd(); + glTranslatef(0.0f, 0.0f, -4.0f); + glCallList(box); glLoadIdentity(); - glTranslatef(0.0f, 0.0f, 0.0f); - glBegin(GL_LINES); - glVertex3f(-1.0f, -1.0f, -3.0f); /* Lower Left */ - glVertex3f(-1.0f, -1.0f, -9.0f); - glEnd(); + glTranslatef(0.0f, 0.0f, -5.0f); + glCallList(box); glLoadIdentity(); - glTranslatef(0.0f, 0.0f, 0.0f); - glBegin(GL_LINES); - glVertex3f( 1.0f, -1.0f, -3.0f); /* Lower Right */ - glVertex3f( 1.0f, -1.0f, -9.0f); - glEnd(); + glTranslatef(0.0f, 0.0f, -6.0f); + glCallList(box); + + glLoadIdentity(); + glTranslatef(0.0f, 0.0f, -7.0f); + glCallList(box); + + glLoadIdentity(); + glTranslatef(0.0f, 0.0f, -8.0f); + glCallList(box); + + glLoadIdentity(); + glTranslatef(0.0f, 0.0f, -9.0f); + glCallList(box); glLoadIdentity(); glTranslatef(0.0f, 0.0f, 0.0f); glBegin(GL_LINES); - glVertex3f( 1.0f, 1.0f, -3.0f); /* Upper Right */ - glVertex3f( 1.0f, 1.0f, -9.0f); + glVertex3f(-1.5f, -1.0f, -3.0f); /* Lower Left */ + glVertex3f(-1.5f, -1.0f, -9.0f); + glEnd(); + + glBegin(GL_LINES); + glVertex3f( 1.5f, -1.0f, -3.0f); /* Lower Right */ + glVertex3f( 1.5f, -1.0f, -9.0f); + glEnd(); + + glBegin(GL_LINES); + glVertex3f( 1.5f, 1.0f, -3.0f); /* Upper Right */ + glVertex3f( 1.5f, 1.0f, -9.0f); glEnd(); - glLoadIdentity(); - glTranslatef(0.0f, 0.0f, 0.0f); glBegin(GL_LINES); - glVertex3f(-1.0f, 1.0f, -3.0f); /* Upper Left */ - glVertex3f(-1.0f, 1.0f, -9.0f); + glVertex3f(-1.5f, 1.0f, -3.0f); /* Upper Left */ + glVertex3f(-1.5f, 1.0f, -9.0f); glEnd(); glLoadIdentity(); @@ -175,6 +192,7 @@ void GLPong_Draw() { glEnable(GL_TEXTURE_2D); glColor3f(1.0f, 1.0f, 1.0f); glTranslatef(GLPong.Ball.x, GLPong.Ball.y, GLPong.Ball.z); + glRotatef(GLPong.Ball.rotate, 0.0f, 0.0f, 1.0f); 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 */ @@ -185,12 +203,7 @@ void GLPong_Draw() { glLoadIdentity(); glTranslatef(0.0f, 0.0f, GLPong.Ball.z); - glBegin(GL_LINE_LOOP); - glVertex3f(-1.0f, -1.0f, 0.0f); /* Lower Left */ - glVertex3f( 1.0f, -1.0f, 0.0f); /* Lower Right */ - glVertex3f( 1.0f, 1.0f, 0.0f); /* Upper Right */ - glVertex3f(-1.0f, 1.0f, 0.0f); /* Upper Left */ - glEnd(); + glCallList(box); SDL_GL_SwapBuffers(); } @@ -203,15 +216,16 @@ void GLPong_Move() { GLPong.Ball.x += GLPong.Ball.xv; GLPong.Ball.y += GLPong.Ball.yv; GLPong.Ball.z += GLPong.Ball.zv; - if ((GLPong.Ball.x > 1.0f) || (GLPong.Ball.x < -1.0f)) { + if ((GLPong.Ball.x > 1.0f) || (GLPong.Ball.x + GLPong.Ball.w < -1.0f)) { GLPong.Ball.xv = -GLPong.Ball.xv; } - if ((GLPong.Ball.y > 1.0f) || (GLPong.Ball.y < -1.0f)) { + if ((GLPong.Ball.y > 0.5f) || (GLPong.Ball.y + GLPong.Ball.h < -0.5f)) { GLPong.Ball.yv = -GLPong.Ball.yv; } if ((GLPong.Ball.z < -9.0f) || (GLPong.Ball.z > -3.0f)) { GLPong.Ball.zv = -GLPong.Ball.zv; } + GLPong.Ball.rotate += 0.0f; } __inline__ unsigned int NextPow2(unsigned int value) { -- cgit v1.2.3