summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ball.c46
-rw-r--r--ball.h4
-rw-r--r--glpong.c19
3 files changed, 49 insertions, 20 deletions
diff --git a/ball.c b/ball.c
index ce962a7..81c3ad0 100644
--- a/ball.c
+++ b/ball.c
@@ -6,6 +6,10 @@
#include "glpong.h"
#include "ball.h"
+static GLuint ball;
+static GLfloat ball_width;
+static GLfloat ball_height;
+
#ifdef DEBUG
int
length(Ball_t * balls) {
@@ -18,6 +22,27 @@ length(Ball_t * balls) {
#endif
void
+GLPong_BallInit() {
+ ball_width = GLPONG_WIDTH / 32.0f; /* FIXME: magic numbers */
+ ball_height = GLPONG_HEIGHT / 24.0f;
+
+ ball = glGenLists(1);
+ glNewList(ball, GL_COMPILE);
+ glBegin(GL_QUADS);
+ glTexCoord2f(0.0f, 0.0f); glVertex2f( 0.0f, 0.0f); /* Lower Left */
+ glTexCoord2f(1.0f, 0.0f); glVertex2f(ball_width, 0.0f); /* Lower Right */
+ glTexCoord2f(1.0f, 1.0f); glVertex2f(ball_width, ball_height); /* Upper Right */
+ glTexCoord2f(0.0f, 1.0f); glVertex2f( 0.0f, ball_height); /* Upper Left */
+ glEnd();
+ glEndList();
+}
+
+void
+GLPong_BallDeinit() {
+ glDeleteLists(ball, 1);
+}
+
+void
GLPong_BallAdd(Ball_t ** balls) {
Ball_t * newball = NULL;
@@ -28,8 +53,6 @@ GLPong_BallAdd(Ball_t ** balls) {
#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 */
@@ -72,10 +95,10 @@ GLPong_BallDeleteAll(Ball_t * 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;
+ if ( b->x + ball_width < a->x ) return 0;
+ if ( b->x > a->x + ball_width ) return 0;
+ if ( b->y + ball_height < a->y ) return 0;
+ if ( b->y > a->y + ball_height ) return 0;
return 1;
}
@@ -91,10 +114,10 @@ GLPong_BallMoveAll(Ball_t ** balls) {
current->x += current->xv;
current->y += current->yv;
- if (((current->y + current->h) >= GLPONG_HEIGHT) || (current->y <= 0.0f)) {
+ if (((current->y + ball_height) >= GLPONG_HEIGHT) || (current->y <= 0.0f)) {
current->yv = -current->yv;
}
- if (((current->x + current->w) >= GLPONG_WIDTH) || (current->x <= 0.0f)) {
+ if (((current->x + ball_width) >= GLPONG_WIDTH) || (current->x <= 0.0f)) {
current->xv = -current->xv;
}
@@ -113,12 +136,7 @@ GLPong_BallDrawAll(Ball_t * 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();
+ glCallList(ball);
balls = balls->next;
}
glDisable(GL_TEXTURE_2D);
diff --git a/ball.h b/ball.h
index ad371b3..4618b46 100644
--- a/ball.h
+++ b/ball.h
@@ -8,11 +8,13 @@ GLuint ball_texture;
typedef struct Ball_t {
struct Ball_t * next; /* next ball; 4 or 8 bytes */
- GLfloat w, h, x, y; /* width, height, x, y coordinates; 4 bytes ea. */
+ GLfloat x, y; /* x, y coordinates; 4 bytes ea. */
GLfloat xv, yv; /* x and y velocity components; 4 bytes ea. */
GLubyte r, g, b, a; /* red, green, blue, alpha components; 1 byte ea. */
} Ball_t;
+void GLPong_BallInit();
+void GLPong_BallDeinit();
void GLPong_BallAdd(Ball_t ** balls);
void GLPong_BallDelete(Ball_t * from, Ball_t * ball);
void GLPong_BallDeleteAll(Ball_t * list);
diff --git a/glpong.c b/glpong.c
index 883d926..b5ad25c 100644
--- a/glpong.c
+++ b/glpong.c
@@ -69,6 +69,8 @@ int main(int argc, char * argv[]) {
/* clean up */
GLPong_BallDeleteAll(GLPong.balls);
+ GLPong_BallDeinit();
+
return 0;
}
@@ -122,9 +124,11 @@ GLPong_Init(GLPong_t * GLPong) {
printf("OpenGL Version: %s\n", glGetString(GL_VERSION));
printf("OpenGL Extensions: %s\n", glGetString(GL_EXTENSIONS));
- ball_texture = SDL_GL_SurfaceToTexture(temp);
+ ball_texture = SDL_GL_NPOTSurfaceToTexture(temp, NULL, NULL);
SDL_FreeSurface(temp);
+ GLPong_BallInit();
+
return 0;
}
@@ -214,8 +218,10 @@ SDL_GL_SurfaceToTexture(SDL_Surface * surface) {
static GLuint
SDL_GL_NPOTSurfaceToTexture(SDL_Surface * surface, GLfloat * wratio, GLfloat * hratio) {
SDL_Surface * pow2 = NULL;
+
unsigned int w = NextPow2(surface->w);
unsigned int h = NextPow2(surface->h);
+
if (wratio) {
*wratio = (GLfloat)surface->w / (GLfloat)h;
}
@@ -223,9 +229,12 @@ SDL_GL_NPOTSurfaceToTexture(SDL_Surface * surface, GLfloat * wratio, GLfloat * h
*hratio = (GLfloat)surface->h/(GLfloat)h;
}
- pow2 = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surface->format->BitsPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask);
- SDL_BlitSurface(surface, NULL, pow2, NULL);
-
- return SDL_GL_SurfaceToTexture(pow2);
+ if ((w != surface->w) || (h != surface->h)) {
+ pow2 = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, surface->format->BitsPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask);
+ SDL_BlitSurface(surface, NULL, pow2, NULL);
+ return SDL_GL_SurfaceToTexture(pow2);
+ } else {
+ return SDL_GL_SurfaceToTexture(surface);
+ }
}