diff options
Diffstat (limited to 'ball.c')
-rw-r--r-- | ball.c | 177 |
1 files changed, 126 insertions, 51 deletions
@@ -1,51 +1,126 @@ -#include <stdlib.h>
-
-#include "SDL.h"
-#include "SDL_opengl.h"
-
-#include "glpong.h"
-#include "ball.h"
-
-void GLPong_BallAdd() {
- Uint8 i;
- int added = 0;
- for (i = 0; i < sizeof(GLPong.Balls) / sizeof(void *); i++) {
- if (GLPong.Balls[i] == NULL) {
- GLPong.Balls[i] = malloc(sizeof(Ball_t));
- added = 1;
- break;
- }
- }
-
- if (added) {
- GLPong.Balls[i]->w = GLPong.w / (GLfloat)40;
- GLPong.Balls[i]->h = GLPong.h / (GLfloat)30;
- GLPong.Balls[i]->x = 40.0f;
- GLPong.Balls[i]->y = 40.0f;
- GLPong.Balls[i]->r = (float)rand() / RAND_MAX;
- GLPong.Balls[i]->g = (float)rand() / RAND_MAX;
- GLPong.Balls[i]->b = (float)rand() / RAND_MAX;
- GLPong.Balls[i]->a = SDL_ALPHA_OPAQUE;
- GLPong.Balls[i]->xv = GLPong.w / (GLfloat)320;
- GLPong.Balls[i]->yv = GLPong.h / (GLfloat)240;
- }
-}
-
-void GLPong_BallDelete() {
- Uint8 i;
- for (i = 0; i < sizeof(GLPong.Balls) / sizeof(void *); i--) {
- if (GLPong.Balls[i] != NULL) {
- free(GLPong.Balls[i]);
- GLPong.Balls[i] = NULL;
- break;
- }
- }
-}
-
-int GLPong_BallCollide(const Ball_t * a, const Ball_t * b) {
- if ( b->x + b->w < a->x ) return 0;
- if ( b->x > a->x + a->w ) return 0;
- if ( b->y + b->h < a->y ) return 0;
- if ( b->y > a->y + a->h ) return 0;
- return 1;
-}
+#include <stdlib.h> + +#include "SDL.h" +#include "SDL_opengl.h" + +#include "glpong.h" +#include "ball.h" + +#ifdef DEBUG +int +length(Ball_t * balls) { + if (balls == NULL) { + return 0; + } else { + return 1 + length(balls->next); + } +} +#endif + +void +GLPong_BallAdd(Ball_t ** balls) { + Ball_t * newball = NULL; + + newball = malloc(sizeof(Ball_t)); + if (newball) { +#ifdef DEBUG + puts("Ball created.\n"); +#endif + newball->next = *balls; + + newball->w = GLPONG_WIDTH / 32.0f; /* FIXME: magic numbers */ + newball->h = GLPONG_HEIGHT / 24.0f; + newball->x = 100.0f; + newball->y = 100.0f; + newball->xv = GLPONG_WIDTH / (GLfloat)320; /* FIXME: magic numbers */ + newball->yv = GLPONG_HEIGHT / (GLfloat)240; + newball->r = rand() % 255; + newball->g = rand() % 255; + newball->b = rand() % 255; + newball->a = SDL_ALPHA_OPAQUE; + + *balls = newball; + } else { +#ifdef DEBUG + fputs("Cannot create ball: malloc failure.\n", stderr); +#endif + } +} + +void +GLPong_BallDelete(Ball_t * from, Ball_t * ball) { + Ball_t * current = from; + Ball_t * temp = NULL; + while (current) { + if (current == ball) { + temp = ball; + current->next = ball->next; + free(ball); + break; + } + current = current->next; + } +} + +void +GLPong_BallDeleteAll(Ball_t * list) { + if (list) { + GLPong_BallDeleteAll(list->next); + free(list); + } +} + +int +GLPong_BallCollide(const Ball_t * a, const Ball_t * b) { + if ( b->x + b->w < a->x ) return 0; + if ( b->x > a->x + a->w ) return 0; + if ( b->y + b->h < a->y ) return 0; + if ( b->y > a->y + a->h ) return 0; + return 1; +} + +void +GLPong_BallMoveAll(Ball_t ** balls) { + Ball_t * current = *balls; + + while (current) { +#ifdef DEBUG + printf("current->x = %0.2f\ncurrent->y = %0.2f\n", current->x, current->y); + printf("current->xv = %0.2f\ncurrent->yv = %0.2f\n", current->xv, current->yv); +#endif + current->x += current->xv; + current->y += current->yv; + + if (((current->y + current->h) >= GLPONG_HEIGHT) || (current->y <= 0.0f)) { + current->yv = -current->yv; + } + if (((current->x + current->w) >= GLPONG_WIDTH) || (current->x <= 0.0f)) { + current->xv = -current->xv; + } + + current = current->next; + } +} +void +GLPong_BallDrawAll(Ball_t * balls) { +#ifdef DEBUG + printf("%d balls, draw all\n", length(balls)); +#endif + + glBindTexture(GL_TEXTURE_2D, ball_texture); + glEnable(GL_TEXTURE_2D); + while (balls) { + glLoadIdentity(); + glTranslatef(balls->x, balls->y, 0); + glColor4ub(balls->r, balls->g, balls->b, balls->a); + glBegin(GL_QUADS); + glTexCoord2f(0.0f, 0.0f); glVertex2f( 0.0f, 0.0f ); /* Lower Left */ + glTexCoord2f(1.0f, 0.0f); glVertex2f(balls->w, 0.0f ); /* Lower Right */ + glTexCoord2f(1.0f, 1.0f); glVertex2f(balls->w, balls->h); /* Upper Right */ + glTexCoord2f(0.0f, 1.0f); glVertex2f( 0.0f, balls->h); /* Upper Left */ + glEnd(); + balls = balls->next; + } + glDisable(GL_TEXTURE_2D); +} + |