summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile53
-rw-r--r--ball.c177
-rw-r--r--ball.h43
-rw-r--r--glpong.c507
-rw-r--r--glpong.h67
-rw-r--r--paddle.c23
-rw-r--r--paddle.h14
7 files changed, 445 insertions, 439 deletions
diff --git a/Makefile b/Makefile
index 8333d28..69f36fb 100644
--- a/Makefile
+++ b/Makefile
@@ -1,26 +1,27 @@
-CC=gcc
-STRIP=strip
-WARN=-Wall -W -ansi -pedantic
-CFLAGS=-Os -pipe -std=c99 ${WARN} -g
-LDFLAGS=-Wl,-O1
-INCLUDES=`sdl-config --cflags`
-LIBRARIES=`sdl-config --libs` -lSDL_image -lGL
-EXT=.exe
-
-EXE=glpong${EXT}
-
-OBJS=glpong.o ball.o paddle.o
-
-all: ${OBJS}
- ${CC} ${LDFLAGS} ${OBJS} -o ${EXE} ${LIBRARIES}
-
-clean:
- rm -f ${OBJS}
- rm -f ${EXE}
-
-rebuild: clean all
-remake: rebuild
-
-%.o: %.c
- ${CC} ${CFLAGS} ${INCLUDES} -c -o $@ $<
-
+CC=gcc
+STRIP=strip
+WARN=-Wall -W -ansi -pedantic
+DEFINES=-DINLINE=inline
+CFLAGS=-Os -pipe -std=c99 -g ${DEFINES} -Wall #${WARN}
+LDFLAGS=-Wl,-O1
+INCLUDES=`sdl-config --cflags`
+LIBRARIES=`sdl-config --libs` -lSDL_image -lGL
+EXT=.exe
+
+EXE=glpong${EXT}
+
+OBJS=glpong.o ball.o #paddle.o
+
+all: ${OBJS}
+ ${CC} ${LDFLAGS} ${OBJS} -o ${EXE} ${LIBRARIES}
+
+clean:
+ rm -f ${OBJS}
+ rm -f ${EXE}
+
+rebuild: clean all
+remake: rebuild
+
+%.o: %.c
+ ${CC} ${CFLAGS} ${INCLUDES} -c -o $@ $<
+
diff --git a/ball.c b/ball.c
index 5728e9f..ce962a7 100644
--- a/ball.c
+++ b/ball.c
@@ -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);
+}
+
diff --git a/ball.h b/ball.h
index 6707ebb..ad371b3 100644
--- a/ball.h
+++ b/ball.h
@@ -1,19 +1,24 @@
-#ifndef BALL_H
-#define BALL_H
-
-#include "SDL.h"
-#include "SDL_opengl.h"
-
-GLuint ball_texture;
-
-typedef struct {
- GLfloat w, h, x, y;
- GLfloat r, g, b, a;
- GLfloat xv, yv;
-} Ball_t;
-
-void GLPong_BallAdd();
-void GLPong_BallDelete();
-int GLPong_BallCollide(const Ball_t * a, const Ball_t * b);
-
-#endif
+#ifndef BALL_H
+#define BALL_H
+
+#include "SDL.h"
+#include "SDL_opengl.h"
+
+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 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_BallAdd(Ball_t ** balls);
+void GLPong_BallDelete(Ball_t * from, Ball_t * ball);
+void GLPong_BallDeleteAll(Ball_t * list);
+int GLPong_BallCollide(const Ball_t * a, const Ball_t * b);
+void GLPong_BallMoveAll(Ball_t ** balls);
+void GLPong_BallDrawAll(Ball_t * balls);
+
+#endif
+
diff --git a/glpong.c b/glpong.c
index 6779a79..883d926 100644
--- a/glpong.c
+++ b/glpong.c
@@ -1,276 +1,231 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <time.h>
-
-#include "SDL.h"
-#include "SDL_opengl.h"
-#include "SDL_image.h"
-
-#include "glpong.h"
-
-int main(int argc, char * argv[]) {
- if (argc) {
- if (argv) {}
- }
-
- GLPong_Init();
-
- GLPong_PaddlesCreate();
-
- GLPong_BallAdd();
-
- while ( !GLPong.done ) {
- GLPong_HandleEvents();
- GLPong_Move();
- GLPong_Draw();
- SDL_Delay(10);
- }
- GLPong_CleanUp();
-
- return 0;
-}
-
-void GLPong_Init() {
- SDL_Surface * temp = NULL;
- Uint8 i;
- GLPong.done = 0;
-
- GLPong.w = 800;
- GLPong.h = 600;
-
- for (i = 0; i < sizeof(GLPong.Balls) / sizeof(void *); i++) {
- GLPong.Balls[i] = NULL;
- }
-
- srand(time(NULL));
-
- SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
- atexit(SDL_Quit);
-
- SDL_WM_SetCaption("glPong", "glPong");
- temp = IMG_Load("ball.png");
- SDL_WM_SetIcon(temp, NULL);
- SDL_ShowCursor(SDL_DISABLE);
-
- SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8);
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
-
- GLPong.Surface = SDL_SetVideoMode(GLPong.w, GLPong.h, 32, SDL_OPENGL);
- glViewport(0, 0, GLPong.w, GLPong.h);
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0f, GLPong.w, GLPong.h, 0.0f, -1.0f, 1.0f);
-
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
-
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- glShadeModel(GL_SMOOTH);
- glClearDepth(2.0f);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LEQUAL);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
-
- GLPong.Event.type = SDL_NOEVENT;
-
- printf("OpenGL Vendor: %s\n", glGetString(GL_VENDOR));
- printf("OpenGL Renderer: %s\n", glGetString(GL_RENDERER));
- printf("OpenGL Version: %s\n", glGetString(GL_VERSION));
- printf("OpenGL Extensions: %s\n", glGetString(GL_EXTENSIONS));
-
- ball_texture = SDL_GL_SurfaceToTexture(temp);
- SDL_FreeSurface(temp);
-}
-
-void GLPong_HandleEvents() {
- if (SDL_PollEvent(&GLPong.Event) != 0) {
- if (GLPong.Event.type == SDL_QUIT) {
- GLPong.done = 1;
- return;
- }
- if (GLPong.Event.type == SDL_KEYDOWN) {
- if (GLPong.Event.key.keysym.sym == SDLK_ESCAPE) {
- GLPong.done = 1;
- return;
- } else if (GLPong.Event.key.keysym.sym == SDLK_SPACE) {
- GLPong_BallAdd();
- } else if (GLPong.Event.key.keysym.sym == SDLK_p) {
- for (;;) {
- SDL_Delay(10);
- if (SDL_PollEvent(&GLPong.Event) != 0) {
- if (GLPong.Event.type == SDL_QUIT) {
- GLPong.done = 1;
- return;
- } else if (GLPong.Event.type == SDL_KEYDOWN){
- if (GLPong.Event.key.keysym.sym == SDLK_ESCAPE) {
- GLPong.done = 1;
- return;
- } else if (GLPong.Event.key.keysym.sym == SDLK_p) {
- break;
- }
- }
- }
- }
- }
- }
- }
-}
-
-void GLPong_Draw() {
- Uint8 i;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- glBindTexture(GL_TEXTURE_2D, ball_texture);
- glEnable(GL_TEXTURE_2D);
- for (i = 0; i < sizeof(GLPong.Balls) / sizeof(void *); i++) {
- if (GLPong.Balls[i] != NULL) {
- glLoadIdentity();
- glTranslatef(GLPong.Balls[i]->x, GLPong.Balls[i]->y, 0);
- glColor4f(GLPong.Balls[i]->r, GLPong.Balls[i]->g, GLPong.Balls[i]->b, GLPong.Balls[i]->a);
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f, 1.0f); glVertex2f(GLPong.Balls[i]->w, GLPong.Balls[i]->h); /* Lower Left */
- glTexCoord2f(1.0f, 1.0f); glVertex2f(0.0f, GLPong.Balls[i]->h); /* Lower Right */
- glTexCoord2f(1.0f, 0.0f); glVertex2f(0.0f, 0.0f); /* Upper Right */
- glTexCoord2f(0.0f, 0.0f); glVertex2f(GLPong.Balls[i]->w, 0.0f); /* Upper Left */
- glEnd();
- }
- }
- glDisable(GL_TEXTURE_2D);
-
- SDL_GL_SwapBuffers();
-}
-
-void GLPong_CleanUp() {
- 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;
- }
- }
-}
-
-void GLPong_Move() {
- Uint8 i;
- Uint8 j;
- for (i = 0; i < sizeof(GLPong.Balls) / sizeof(void *); i++) {
- if (GLPong.Balls[i] != NULL) {
- GLPong.Balls[i]->x += GLPong.Balls[i]->xv;
- GLPong.Balls[i]->y += GLPong.Balls[i]->yv;
- if ( GLPong.Balls[i]->y > (GLPong.h - GLPong.Balls[i]->h) || GLPong.Balls[i]->y < 0.0f ) {
- GLPong.Balls[i]->yv = -GLPong.Balls[i]->yv;
- }
- if ( GLPong.Balls[i]->x > (GLPong.w - GLPong.Balls[i]->w) || GLPong.Balls[i]->x < 0.0f ) {
- GLPong.Balls[i]->xv = -GLPong.Balls[i]->xv;
- }
- }
- }
- /*if (GLPong.Balls[1] != NULL) {
- if (GLPong_BallCollide(GLPong.Balls[0], GLPong.Balls[1])) {
- GLPong.Balls[0]->xv = -GLPong.Balls[0]->xv;
- GLPong.Balls[0]->yv = -GLPong.Balls[0]->yv;
- GLPong.Balls[1]->xv = -GLPong.Balls[1]->xv;
- GLPong.Balls[1]->yv = -GLPong.Balls[1]->yv;
- }
- }*/
-
- /*
- for (i = 0; i < sizeof(GLPong.Balls) / sizeof(void *) - 1; i++) {
- if (GLPong.Balls[i] != NULL) {
- for (j = i + i; j < sizeof(GLPong.Balls) / sizeof(void *); j++) {
- if (j != i && GLPong.Balls[j] != NULL) {
- if (GLPong_BallCollide(GLPong.Balls[i], GLPong.Balls[j])) {
- printf("%d - %d\n", i, j);
- GLPong.Balls[i]->xv *= -1;
- GLPong.Balls[i]->yv *= -1;
- GLPong.Balls[j]->xv *= -1;
- GLPong.Balls[j]->yv *= -1;
- }
- }
- }
- }
- }*/
- /*for (i = 0; i < sizeof(GLPong.Balls) / sizeof(void *); i++) {
- if (GLPong.Balls[i] != NULL) {
- for (j = 0; j < sizeof(GLPong.Balls) / sizeof(void *); i++) {
- if (GLPong.Balls[j] != NULL && i != j) {
- if (GLPong_BallCollide(GLPong.Balls[i], GLPong.Balls[j])) {
- GLPong.Balls[i]->xv *= -1;
- GLPong.Balls[i]->yv *= -1;
- }
- }
- }
- }
- }*/
-}
-
-GLuint SDL_GL_SurfaceToTexture(SDL_Surface * surface) {
- Uint32 rmask, gmask, bmask, amask;
- SDL_Surface * temp = NULL;
- SDL_Surface * tempalpha = NULL;
- GLuint texture;
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
- rmask = 0xff000000;
- gmask = 0x00ff0000;
- bmask = 0x0000ff00;
- amask = 0x000000ff;
-#else
- rmask = 0x000000ff;
- gmask = 0x0000ff00;
- bmask = 0x00ff0000;
- amask = 0xff000000;
-#endif
- tempalpha = SDL_DisplayFormatAlpha(surface);
- SDL_SetAlpha(tempalpha, 0, SDL_ALPHA_TRANSPARENT);
- temp = SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h, 32, bmask, gmask, rmask, amask);
- SDL_FreeSurface(surface);
- SDL_BlitSurface(tempalpha, NULL, temp, NULL);
-
- SDL_FreeSurface(tempalpha);
-
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- SDL_LockSurface(temp);
- glTexImage2D(GL_TEXTURE_2D, 0, 4, temp->w, temp->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, temp->pixels);
- SDL_UnlockSurface(temp);
- SDL_FreeSurface(temp);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-
- return texture;
-}
-
-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;
- }
- if (hratio) {
- *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);
- SDL_FreeSurface(surface);
-
- return SDL_GL_SurfaceToTexture(pow2);
-}
-
-__inline__ unsigned int NextPow2(unsigned int value) {
- unsigned int x;
- __asm("dec %1\n\t"
- "movl $2,%0\n\t"
- "bsr %1,%1\n\t"
- "shl %b1,%0\n\t"
- : "=r" (x)
- : "c" (value)
- );
- return x;
-}
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#include "SDL.h"
+#include "SDL_opengl.h"
+#include "SDL_image.h"
+
+#include "glpong.h"
+
+static int GLPong_Init(GLPong_t * GLPong);
+static int GLPong_HandleEvents();
+
+static GLuint SDL_GL_SurfaceToTexture(SDL_Surface * surface);
+static GLuint SDL_GL_NPOTSurfaceToTexture(SDL_Surface * surface, GLfloat * wratio, GLfloat * hratio);
+
+
+int main(int argc, char * argv[]) {
+ GLPong_t GLPong = {
+ NULL,
+ NULL,
+ GLPONG_WIDTH,
+ GLPONG_HEIGHT
+ };
+
+ int action;
+ int done = 0;
+
+ if (argc) {
+ if (argv) {}
+ }
+
+ if (GLPong_Init(&GLPong) < 0) {
+ fprintf(stderr, "Bailing out.\n");
+ return -1;
+ }
+
+#ifdef DEBUG
+ printf("%d balls\n", length(GLPong.balls));
+#endif
+ GLPong_BallAdd(&GLPong.balls); /* start with one ball */
+#ifdef DEBUG
+ printf("%d balls\n", length(GLPong.balls));
+#endif
+
+ while ( !done ) {
+ action = GLPong_HandleEvents();
+ switch (action) {
+ case GLPONG_EXIT: /* exit */
+ done = 1;
+ break;
+ case GLPONG_ADDBALL: /* add another ball */
+ GLPong_BallAdd(&GLPong.balls);
+ break;
+ }
+ GLPong_BallMoveAll(&GLPong.balls);
+
+ /* begin drawing */
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ GLPong_BallDrawAll(GLPong.balls);
+
+ SDL_GL_SwapBuffers();
+ /* end drawing */
+
+ SDL_Delay(10);
+ }
+
+ /* clean up */
+ GLPong_BallDeleteAll(GLPong.balls);
+
+ return 0;
+}
+
+static int
+GLPong_Init(GLPong_t * GLPong) {
+ SDL_Surface * temp = NULL;
+
+ srand(time(NULL));
+
+ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
+ fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
+ return -1;
+ }
+ atexit(SDL_Quit);
+
+ SDL_WM_SetCaption("glPong", "glPong");
+ SDL_ShowCursor(SDL_DISABLE);
+
+ temp = IMG_Load("ball.png");
+ if (!temp) {
+ fprintf(stderr, "Unable to open ball.png\n");
+ return -1;
+ }
+
+ SDL_WM_SetIcon(temp, NULL);
+
+ SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
+ SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
+ SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
+ SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
+ SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
+ SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+
+ GLPong->Surface = SDL_SetVideoMode(GLPong->w, GLPong->h, 32, SDL_OPENGL);
+ if (!GLPong->Surface) {
+ fprintf(stderr, "Cannot SetVideoMode: %s\n", SDL_GetError());
+ return -1;
+ }
+
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ glOrtho(0.0f, GLPong->w, 0.0f, GLPong->h, 0.0f, 1.0f);
+
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+
+ glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+ printf("OpenGL Vendor: %s\n", glGetString(GL_VENDOR));
+ printf("OpenGL Renderer: %s\n", glGetString(GL_RENDERER));
+ printf("OpenGL Version: %s\n", glGetString(GL_VERSION));
+ printf("OpenGL Extensions: %s\n", glGetString(GL_EXTENSIONS));
+
+ ball_texture = SDL_GL_SurfaceToTexture(temp);
+ SDL_FreeSurface(temp);
+
+ return 0;
+}
+
+static int
+GLPong_HandleEvents() {
+ SDL_Event Event;
+
+ if (SDL_PollEvent(&Event) != 0) {
+ if (Event.type == SDL_QUIT) {
+ return GLPONG_EXIT;
+ }
+ if (Event.type == SDL_KEYDOWN) {
+ if (Event.key.keysym.sym == SDLK_ESCAPE) {
+ return GLPONG_EXIT;
+ } else if (Event.key.keysym.sym == SDLK_SPACE) {
+ return GLPONG_ADDBALL;
+ } else if (Event.key.keysym.sym == SDLK_p) {
+ for (;;) {
+ SDL_Delay(10);
+ if (SDL_PollEvent(&Event) != 0) {
+ if (Event.type == SDL_QUIT) {
+ return GLPONG_EXIT;
+ } else if (Event.type == SDL_KEYDOWN){
+ if (Event.key.keysym.sym == SDLK_ESCAPE) {
+ return GLPONG_EXIT;
+ } else if (Event.key.keysym.sym == SDLK_p) {
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ return GLPONG_NOACTION;
+}
+
+static INLINE unsigned int
+NextPow2(unsigned int value) {
+ unsigned int x;
+ __asm(
+ "dec %1 \n\t" /* so that if the number is a power of
+ * two we don't change it
+ */
+ "movl $2,%0 \n\t"
+ "bsr %1,%1 \n\t"
+ "shl %b1,%0 \n\t"
+ : "=r" (x)
+ : "c" (value)
+ );
+ return x;
+}
+
+static GLuint
+SDL_GL_SurfaceToTexture(SDL_Surface * surface) {
+ GLuint texture;
+
+ SDL_Surface * temp = SDL_CreateRGBSurface(
+ SDL_SWSURFACE,
+ surface->w,
+ surface->h,
+ 32,
+ BMASK,
+ GMASK,
+ RMASK,
+ AMASK
+ );
+
+ SDL_Surface * tempalpha = SDL_DisplayFormatAlpha(surface);
+ SDL_SetAlpha(tempalpha, 0, SDL_ALPHA_TRANSPARENT);
+ SDL_BlitSurface(tempalpha, NULL, temp, NULL);
+
+ SDL_FreeSurface(tempalpha);
+
+ glGenTextures(1, &texture);
+ glBindTexture(GL_TEXTURE_2D, texture);
+ SDL_LockSurface(temp);
+ glTexImage2D(GL_TEXTURE_2D, 0, 4, temp->w, temp->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, temp->pixels);
+ SDL_UnlockSurface(temp);
+ SDL_FreeSurface(temp);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+ return texture;
+}
+
+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;
+ }
+ if (hratio) {
+ *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);
+}
+
diff --git a/glpong.h b/glpong.h
index d48b3fe..43ee592 100644
--- a/glpong.h
+++ b/glpong.h
@@ -1,30 +1,37 @@
-#ifndef GLPONG_H
-#define GLPONG_H
-
-#include "SDL.h"
-#include "SDL_opengl.h"
-
-#include "ball.h"
-#include "paddle.h"
-
-struct {
- SDL_Event Event;
- SDL_Surface * Surface;
- Ball_t * Balls[4];
- Paddle_t Left, Right;
- Uint16 w;
- Uint16 h;
- Uint16 done;
-} GLPong;
-
-void GLPong_Init();
-void GLPong_HandleEvents();
-void GLPong_Draw();
-void GLPong_CleanUp();
-void GLPong_Move();
-
-GLuint SDL_GL_SurfaceToTexture(SDL_Surface * surface);
-GLuint SDL_GL_NPOTSurfaceToTexture(SDL_Surface * surface, GLfloat * wratio, GLfloat * hratio);
-__inline__ unsigned int NextPow2(unsigned int value);
-
-#endif
+#ifndef GLPONG_H
+#define GLPONG_H
+
+#include "SDL.h"
+#include "SDL_opengl.h"
+
+#include "ball.h"
+/*#include "paddle.h"*/
+
+#define GLPONG_WIDTH 640
+#define GLPONG_HEIGHT 480
+
+#define GLPONG_NOACTION 0
+#define GLPONG_EXIT 1
+#define GLPONG_ADDBALL 2
+
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+#define RMASK 0xff000000
+#define GMASK 0x00ff0000
+#define BMASK 0x0000ff00
+#define AMASK 0x000000ff
+#else
+#define RMASK 0x000000ff
+#define GMASK 0x0000ff00
+#define BMASK 0x00ff0000
+#define AMASK 0xff000000
+#endif
+
+typedef struct {
+ SDL_Surface * Surface;
+ Ball_t * balls;
+/* Paddle_t Left, Right;*/
+ Uint16 w;
+ Uint16 h;
+} GLPong_t;
+
+#endif
diff --git a/paddle.c b/paddle.c
deleted file mode 100644
index be1ba87..0000000
--- a/paddle.c
+++ /dev/null
@@ -1,23 +0,0 @@
-#include <stdlib.h>
-
-#include "SDL.h"
-#include "SDL_opengl.h"
-
-#include "paddle.h"
-#include "glpong.h"
-#include "ball.h"
-
-void GLPong_PaddlesCreate() {
- GLPong.Left.w = 8;
- GLPong.Left.h = 64;
- GLPong.Left.x = 0;
- GLPong.Left.y = (GLPong.h / 2) - (GLPong.h / 2);
- GLPong.Left.r = 1.0;
- GLPong.Left.g = 1.0;
- GLPong.Left.b = 1.0;
- GLPong.Left.a = 0.0;
- GLPong.Left.Top.w = GLPong.Left.w;
- GLPong.Left.Top.h = 1;
- GLPong.Left.Top.x = GLPong.Left.x;
- GLPong.Left.Top.y = GLPong.Left.y - 1;
-}
diff --git a/paddle.h b/paddle.h
deleted file mode 100644
index 6ec3893..0000000
--- a/paddle.h
+++ /dev/null
@@ -1,14 +0,0 @@
-#ifndef PADDLE_H
-#define PADDLE_H
-
-typedef struct {
- GLfloat w, h, x, y;
- GLfloat r, g, b, a;
- struct {
- GLfloat w, h, x, y;
- } Top, Bottom;
-} Paddle_t;
-
-void GLPong_PaddlesCreate();
-
-#endif