diff options
author | Matt Turner <mattst88@gmail.com> | 2010-01-21 21:13:03 -0500 |
---|---|---|
committer | Matt Turner <mattst88@gmail.com> | 2011-04-04 17:14:35 -0400 |
commit | 4e9c7eec3078775c7bf2ed79169fc384e6d17d63 (patch) | |
tree | 76120089a46ab5c91a935ce4eaedaf6ecef9d8b8 | |
parent | 89c1f661870d210cf4c0971f97a67a1a7c5b7a39 (diff) |
Rework Mouse, clean GLPong_t struct.
Signed-off-by: Matt Turner <mattst88@gmail.com>
-rw-r--r-- | glpong3d.c | 158 | ||||
-rw-r--r-- | glpong3d.h | 16 |
2 files changed, 89 insertions, 85 deletions
@@ -16,6 +16,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); GLuint box; @@ -43,6 +44,7 @@ int main(int argc, char * argv[]) { break; } + SDL_GL_GetMouseState(&GLPong.Front); GLPong_Move(); /* begin drawing */ @@ -73,9 +75,9 @@ GLPong_Init(GLPong_t * GLPong) { 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.coord.x = -(GLPong->Ball.w / 2); + GLPong->Ball.coord.y = -(GLPong->Ball.h / 2); + GLPong->Ball.coord.z = -3.0f; GLPong->Ball.r = 1.0f; GLPong->Ball.g = 0.0f; GLPong->Ball.b = 0.0f; @@ -85,18 +87,18 @@ GLPong_Init(GLPong_t * GLPong) { GLPong->Front.w = 0.66f; GLPong->Front.h = 0.5f; - GLPong->Front.x = -1.0f; - GLPong->Front.y = -1.0f; - GLPong->Front.z = -3.0f; + GLPong->Front.coord.x = -1.0f; + GLPong->Front.coord.y = -1.0f; + GLPong->Front.coord.z = -3.0f; 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.x = -(GLPong->Front.w / 2); - GLPong->Back.y = -(GLPong->Front.h / 2); - GLPong->Back.z = -9.0f; + GLPong->Back.coord.x = -(GLPong->Front.w / 2); + GLPong->Back.coord.y = -(GLPong->Front.h / 2); + GLPong->Back.coord.z = -9.0f; GLPong->Back.r = 1.0f; GLPong->Back.g = 1.0f; GLPong->Back.b = 1.0f; @@ -108,7 +110,7 @@ GLPong_Init(GLPong_t * GLPong) { atexit(SDL_Quit); SDL_WM_SetCaption("glpong3d", "glpong3d"); - SDL_ShowCursor(SDL_DISABLE); +/* SDL_ShowCursor(SDL_DISABLE);*/ temp = IMG_Load("ball.png"); if (!temp) { @@ -186,8 +188,6 @@ GLPong_Init(GLPong_t * GLPong) { static int GLPong_HandleEvents(void) { SDL_Event Event; - Uint8 * key; - int x = 0, y = 0; if (SDL_PollEvent(&Event) != 0) { if (Event.type == SDL_QUIT) { @@ -215,59 +215,38 @@ GLPong_HandleEvents(void) { } } - key = SDL_GetKeyState(NULL); - SDL_GetMouseState(&x, &y); - - GLPong.Mouse.x = ((float) x / GLPONG_WIDTH) * 3.3f - 1.65f; - - if (GLPong.Mouse.x <= -1.5 + (GLPong.Front.w / 2)) { - GLPong.Mouse.x = -1.5 + (GLPong.Front.w / 2); - } else if (GLPong.Mouse.x >= 1.5 - (GLPong.Front.w / 2)) { - GLPong.Mouse.x = 1.5 - (GLPong.Front.w / 2); - } - - GLPong.Mouse.y = ((float) y / GLPONG_HEIGHT) * 2.5f - 1.25f; - - if (GLPong.Mouse.y <= -1.0 + (GLPong.Front.h / 2)) { - GLPong.Mouse.y = -1.0 + (GLPong.Front.h / 2); - } else if (GLPong.Mouse.y >= 1.0 - (GLPong.Front.h / 2)) { - GLPong.Mouse.y = 1.0 - (GLPong.Front.h / 2); - } - GLPong.Front.x = GLPong.Mouse.x - GLPong.Front.w / 2; - GLPong.Front.y = -(GLPong.Mouse.y + GLPong.Front.h / 2); - /*printf("SDL x,y: %d,%d; OpenGL x,y: %f,%f\n", x, y, GLPong.Mouse.x, GLPong.Mouse.y);*/ return GLPONG_NOACTION; } void GLPong_Move(void) { - GLPong.Ball.x += GLPong.Ball.xv; - if (GLPong.Ball.x > 1.0f) { - GLPong.Ball.x = 1.0f; + GLPong.Ball.coord.x += GLPong.Ball.xv; + if (GLPong.Ball.coord.x > 1.0f) { + GLPong.Ball.coord.x = 1.0f; GLPong.Ball.xv = -GLPong.Ball.xv; - } else if (GLPong.Ball.x + GLPong.Ball.w < -1.0f) { - GLPong.Ball.x = -1.0f - GLPong.Ball.w; + } else if (GLPong.Ball.coord.x + GLPong.Ball.w < -1.0f) { + GLPong.Ball.coord.x = -1.0f - GLPong.Ball.w; GLPong.Ball.xv = -GLPong.Ball.xv; } - GLPong.Ball.y += GLPong.Ball.yv; - if (GLPong.Ball.y > 0.5f) { - GLPong.Ball.y = 0.5f; + GLPong.Ball.coord.y += GLPong.Ball.yv; + if (GLPong.Ball.coord.y > 0.5f) { + GLPong.Ball.coord.y = 0.5f; GLPong.Ball.yv = -GLPong.Ball.yv; - } else if (GLPong.Ball.y + GLPong.Ball.h < -0.5f) { - GLPong.Ball.y = -0.5f - GLPong.Ball.h; + } else if (GLPong.Ball.coord.y + GLPong.Ball.h < -0.5f) { + GLPong.Ball.coord.y = -0.5f - GLPong.Ball.h; GLPong.Ball.yv = -GLPong.Ball.yv; } - GLPong.Ball.z += GLPong.Ball.zv; - if (GLPong.Ball.z < -9.0f) { - GLPong.Ball.z = -9.0f; + GLPong.Ball.coord.z += GLPong.Ball.zv; + if (GLPong.Ball.coord.z < -9.0f) { + GLPong.Ball.coord.z = -9.0f; GLPong.Ball.zv = -GLPong.Ball.zv; - } else if (GLPong.Ball.z > -3.0f) { - GLPong.Ball.z = -3.0f; + } else if (GLPong.Ball.coord.z > -3.0f) { + GLPong.Ball.coord.z = -3.0f; GLPong.Ball.zv = -GLPong.Ball.zv; } - GLPong.Back.x = GLPong.Ball.x; - GLPong.Back.y = GLPong.Ball.y; + GLPong.Back.coord.x = GLPong.Ball.coord.x; + GLPong.Back.coord.y = GLPong.Ball.coord.y; GLPong_Collide(&GLPong.Ball, &GLPong.Front); } @@ -339,7 +318,7 @@ void GLPong_Draw(void) { glBindTexture(GL_TEXTURE_2D, paddle_texture); glEnable(GL_TEXTURE_2D); glColor3f(GLPong.Back.r, GLPong.Back.g, GLPong.Back.b); - glTranslatef(GLPong.Back.x, GLPong.Back.y, GLPong.Back.z); + 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 */ @@ -350,7 +329,7 @@ void GLPong_Draw(void) { glLoadIdentity(); glColor3f(1.0f, 0.0f, 0.0f); - glTranslatef(GLPong.Back.x, GLPong.Back.y, GLPong.Back.z); + glTranslatef(GLPong.Back.coord.x, GLPong.Back.coord.y, GLPong.Back.coord.z); glBegin(GL_POINTS); glVertex3f(0.0f, 0.0f, 0.0f); glEnd(); @@ -359,7 +338,7 @@ void GLPong_Draw(void) { glBindTexture(GL_TEXTURE_2D, ball_texture); glEnable(GL_TEXTURE_2D); glColor3f(GLPong.Ball.r, GLPong.Ball.g, GLPong.Ball.b); - glTranslatef(GLPong.Ball.x, GLPong.Ball.y, GLPong.Ball.z); + 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 */ @@ -371,40 +350,40 @@ void GLPong_Draw(void) { /* Lower Left */ glLoadIdentity(); glColor3f(1.0f, 0.0f, 0.0f); - glTranslatef(GLPong.Ball.x, GLPong.Ball.y, GLPong.Ball.z); + 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.x + GLPong.Ball.w, GLPong.Ball.y, GLPong.Ball.z); + 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.x + GLPong.Ball.w, GLPong.Ball.y + GLPong.Ball.h, GLPong.Ball.z); + 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.x, GLPong.Ball.y + GLPong.Ball.h, GLPong.Ball.z); + 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.z); + 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.x + (GLPong.Front.w / 2), GLPong.Front.y + (GLPong.Front.h / 2), GLPong.Front.z); + 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 */ @@ -413,55 +392,47 @@ void GLPong_Draw(void) { glEnd(); glDisable(GL_TEXTURE_2D); - /* Mouse Pointer */ - glLoadIdentity(); - glColor3f(1.0f, 1.0f, 0.0f); - glTranslatef(GLPong.Mouse.x, -GLPong.Mouse.y, GLPong.Front.z); - glBegin(GL_POINTS); - glVertex3f(0.0f, 0.0f, 0.0f); - glEnd(); - /* Lower Left */ glLoadIdentity(); glColor3f(1.0f, 0.0f, 0.0f); - glTranslatef(GLPong.Front.x, GLPong.Front.y, GLPong.Front.z); + 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.x + GLPong.Front.w, GLPong.Front.y, GLPong.Front.z); + 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.x + GLPong.Front.w, GLPong.Front.y + GLPong.Front.h, GLPong.Front.z); + 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.x, GLPong.Front.y + GLPong.Front.h, GLPong.Front.z); + 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(); } void GLPong_Collide(Ball_t * ball, const Paddle_t * paddle) { - if (ball->z == paddle->z) { - if (ball->x + ball->w < paddle->x) return; /* if ball is left of paddle */ - if (ball->x > paddle->x + paddle->w) return; /* if ball is right of paddle */ - if (ball->y > paddle->y + paddle->h) return; /* if ball is above paddle */ - if (ball->y + ball->h < paddle->y) return; /* if ball is below 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->x, ball->y, ball->z); + printf("x: %.3f, y: %.2f, z: %.2f\n", ball->coord.x, ball->coord.y, ball->coord.z); } } @@ -535,3 +506,36 @@ GLuint SDL_GL_SurfaceToTexture(SDL_Surface * surface) { return texture; } + +static void +SDL_GL_GetMouseState(Paddle_t * paddle) { + int x, y; + float temp_x, temp_y; + Uint8 * key; + + key = SDL_GetKeyState(NULL); + 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); + } + + paddle->coord.x = temp_x - paddle->w / 2; + paddle->coord.y = -(temp_y + paddle->h / 2); + +#ifdef DEBUG + printf("SDL (x, y): (%d, %d)\tGL (x, y, z): (%.3f, %.3f)\n", x, y, paddle->coord.x, paddle->coord.y); +#endif +} @@ -27,28 +27,28 @@ GLuint paddle_texture_hit; GLuint ball_texture; typedef struct { - GLfloat w, h, x, y, z; + GLfloat x, y, z; +} Coord_t; + +typedef struct { + Coord_t coord; + GLfloat w, h; GLfloat r, g, b; } Paddle_t; typedef struct { - GLfloat w, h, x, y, z; + Coord_t coord; + GLfloat w, h; GLfloat r, g, b; GLfloat xv, yv, zv; GLfloat rotate; } Ball_t; typedef struct { - GLfloat x, y; -} Mouse_t; - -typedef struct { SDL_Surface * Surface; Ball_t Ball; Paddle_t Front; Paddle_t Back; - Mouse_t Mouse; - GLfloat fps; } GLPong_t; GLPong_t GLPong; |