diff options
-rw-r--r-- | glpong3d.c | 68 | ||||
-rw-r--r-- | glpong3d.h | 10 | ||||
-rw-r--r-- | paddle.c | 76 | ||||
-rw-r--r-- | paddle.h | 5 |
4 files changed, 71 insertions, 88 deletions
@@ -18,7 +18,7 @@ static int GLPong_HandleEvents(void); static void GLPong_Draw(void); static void GLPong_CleanUp(void); static void GLPong_Move(void); -static void SDL_GL_GetMouseState(Paddle_t * paddle); +static void SDL_GL_GetMouseState(GLfloat * x, GLfloat * y); static GLuint SDL_GL_SurfaceToTexture(SDL_Surface * surface); int main(int argc, char * argv[]) { @@ -45,7 +45,6 @@ int main(int argc, char * argv[]) { break; } - SDL_GL_GetMouseState(&GLPong.Front); GLPong_Move(); /* begin drawing */ @@ -76,24 +75,6 @@ GLPong_Init(GLPong_t * GLPong) { SDL_Surface * temp = NULL; GLuint texture; - GLPong->Front.w = 0.66f; - GLPong->Front.h = 0.5f; - GLPong->Front.coord.x = -1.0f; - GLPong->Front.coord.y = -1.0f; - GLPong->Front.coord.z = GLPONG_FRONT_Z; - GLPong->Front.r = 1.0f; - GLPong->Front.g = 1.0f; - GLPong->Front.b = 1.0f; - - GLPong->Back.w = 0.66f; - GLPong->Back.h = 0.5f; - GLPong->Back.coord.x = -(GLPong->Front.w / 2); - GLPong->Back.coord.y = -(GLPong->Front.h / 2); - GLPong->Back.coord.z = GLPONG_BACK_Z; - GLPong->Back.r = 1.0f; - GLPong->Back.g = 1.0f; - GLPong->Back.b = 1.0f; - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) { fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); return -1; @@ -135,7 +116,7 @@ GLPong_Init(GLPong_t * GLPong) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective(45.0f, GLPONG_WIDTH / GLPONG_HEIGHT, 3.0f, 9.0f); + gluPerspective(45.0f, GLPONG_WIDTH / GLPONG_HEIGHT, -GLPONG_FRONT_Z, -GLPONG_BACK_Z); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); @@ -158,10 +139,9 @@ GLPong_Init(GLPong_t * GLPong) { SDL_FreeSurface(temp); temp = IMG_Load("paddle-skyos.png"); - paddle_texture = SDL_GL_SurfaceToTexture(temp); - SDL_FreeSurface(temp); - temp = IMG_Load("paddle-skyos.png"); - paddle_texture_hit = SDL_GL_SurfaceToTexture(temp); + texture = SDL_GL_SurfaceToTexture(temp); + GLPong_PaddleInit(&GLPong->front_paddle, GLPONG_FRONT_Z, texture); + GLPong_PaddleInit(&GLPong->back_paddle, GLPONG_BACK_Z, texture); SDL_FreeSurface(temp); box = glGenLists(1); @@ -213,6 +193,11 @@ GLPong_HandleEvents(void) { static void GLPong_Move(void) { + GLfloat x, y; + + SDL_GL_GetMouseState(&x, &y); + GLPong_PaddleMove(&GLPong.front_paddle, x, y); + GLPong_BallMove(); } @@ -248,11 +233,11 @@ void GLPong_Draw(void) { glVertex3f(-1.5f, 1.0f, GLPONG_BACK_Z); glEnd(); - GLPong_PaddleDrawBack(&GLPong.Back); + GLPong_PaddleDraw(&GLPong.back_paddle); GLPong_BallDraw(); - GLPong_PaddleDrawFront(&GLPong.Front); + GLPong_PaddleDraw(&GLPong.front_paddle); } #if 0 @@ -332,33 +317,16 @@ SDL_GL_SurfaceToTexture(SDL_Surface * surface) { } static void -SDL_GL_GetMouseState(Paddle_t * paddle) { - int x, y; - float temp_x, temp_y; +SDL_GL_GetMouseState(GLfloat * x, GLfloat * y) { + int SDL_x, SDL_y; SDL_PumpEvents(); - SDL_GetMouseState(&x, &y); - - temp_x = ((float) x / GLPONG_WIDTH) * 3.3f - 1.65f; - - if (temp_x <= -1.5 + (paddle->w / 2)) { - temp_x = -1.5 + (paddle->w / 2); - } else if (temp_x >= 1.5 - (paddle->w / 2)) { - temp_x = 1.5 - (paddle->w / 2); - } - - temp_y = ((float) y / GLPONG_HEIGHT) * 2.5f - 1.25f; - - if (temp_y <= -1.0 + (paddle->h / 2)) { - temp_y = -1.0 + (paddle->h / 2); - } else if (temp_y >= 1.0 - (paddle->h / 2)) { - temp_y = 1.0 - (paddle->h / 2); - } + SDL_GetMouseState(&SDL_x, &SDL_y); - paddle->coord.x = temp_x - paddle->w / 2; - paddle->coord.y = -(temp_y + paddle->h / 2); + *x = ((float)SDL_x / GLPONG_WIDTH) * 3.3f - 1.65f; + *y = ((float)SDL_y / GLPONG_HEIGHT) * 2.25f - 1.25f; #ifdef DEBUG - printf("SDL (x, y): (%d, %d)\tGL (x, y, z): (%.3f, %.3f)\n", x, y, paddle->coord.x, paddle->coord.y); + printf("SDL (x, y): (%d, %d)\tGL (x, y): (%.3f, %.3f)\n", SDL_x, SDL_y, *x, *y); #endif } @@ -25,10 +25,6 @@ #define AMASK 0xff000000 #endif -GLuint paddle_texture; -GLuint paddle_texture_hit; -GLuint box; - typedef struct { GLfloat x, y, z; } Coord_t; @@ -40,10 +36,12 @@ typedef struct { } Paddle_t; typedef struct { - Paddle_t Front; - Paddle_t Back; + Paddle_t front_paddle; + Paddle_t back_paddle; } GLPong_t; GLPong_t GLPong; +GLuint box; + #endif @@ -4,62 +4,38 @@ #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 -} +static GLuint paddle_texture; void -GLPong_PaddleDrawFront(const Paddle_t * paddle) { +GLPong_PaddleDraw(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? */ + glTranslatef(paddle->coord.x + (paddle->w / 2), paddle->coord.y + (paddle->h / 2), paddle->coord.z); 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(); + glEnd(); glDisable(GL_TEXTURE_2D); #ifdef DEBUG /* Lower Left */ glLoadIdentity(); - glColor3f(1.0f, 0.0f, 0.0f); + glColor3f(0.0f, 0.0f, 1.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(); + glColor3f(1.0f, 0.0f, 0.0f); 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); @@ -72,5 +48,45 @@ GLPong_PaddleDrawFront(const Paddle_t * paddle) { glBegin(GL_POINTS); glVertex3f(0.0f, 0.0f, 0.0f); glEnd(); + /* Center */ + glLoadIdentity(); + glColor3f(0.0f, 1.0f, 0.0f); + glTranslatef(paddle->coord.x + paddle->w / 2.0f, paddle->coord.y + paddle->h / 2.0f, paddle->coord.z); + glBegin(GL_POINTS); + glVertex3f(0.0f, 0.0f, 0.0f); + glEnd(); #endif } + +void +GLPong_PaddleInit(Paddle_t * paddle, GLfloat z, GLuint texture) { + paddle->w = 0.66f; + paddle->h = 0.5f; + paddle->coord.x = paddle->w / 2.0f; + paddle->coord.y = paddle->h / 2.0f; + paddle->coord.z = z; + paddle->r = 1.0f; + paddle->g = 1.0f; + paddle->b = 1.0f; + + paddle_texture = texture; +} + +void +GLPong_PaddleMove(Paddle_t * paddle, GLfloat x, GLfloat y) { + /* clamp paddle to border */ + if (x <= -1.5f + (paddle->w / 2.0f)) { + x = -1.5f + (paddle->w / 2.0f); + } else if (x >= 1.5f - (paddle->w / 2.0f)) { + x = 1.5f - (paddle->w / 2.0f); + } + + if (y <= -1.0f + (paddle->h / 2.0f)) { + y = -1.0f + (paddle->h / 2.0f); + } else if (y >= 1.0f - (paddle->h / 2.0f)) { + y = 1.0f - (paddle->h / 2.0f); + } + + paddle->coord.x = x - (paddle->w / 2.0f); + paddle->coord.y = -(y + (paddle->h / 2.0f)); +} @@ -6,7 +6,8 @@ #include "glpong3d.h" -void GLPong_PaddleDrawBack(const Paddle_t * paddle); -void GLPong_PaddleDrawFront(const Paddle_t * paddle); +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); #endif /* PADDLE_H */ |