#include #include #include #include "SDL.h" #include "SDL_opengl.h" #include "SDL_image.h" #include "SDL_net.h" #include #include "glpong3d.h" #include "text.h" #include "fps.h" #include "paddle.h" #include "ball.h" static int GLPong_Init(GLPong_t * GLPong); static int GLPong_HandleEvents(void); static void GLPong_Draw(const GLPong_t * GLPong); static bool GLPong_Collide(const Ball_t * ball, const Paddle_t * paddle); static void GLPong_CleanUp(void); static void GLPong_Move(GLPong_t * GLPong); static void SDL_GL_GetMouseState(GLfloat * x, GLfloat * y); static GLuint SDL_GL_SurfaceToTexture(SDL_Surface * surface); int main(int argc, char * argv[]) { GLPong_t GLPong; unsigned int frames = 0; int done = 0; int action; if (argc) { if (argv) {} } if (GLPong_Init(&GLPong) < 0) { fprintf(stderr, "Bailing out.\n"); return -1; } GLPong_FPSInit(); while (!done) { action = GLPong_HandleEvents(); switch (action) { case GLPONG_EXIT: /* exit */ done = 1; break; } GLPong_Move(&GLPong); /* begin drawing */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GLPong_Draw(&GLPong); frames++; if (frames == 30) { frames = 0; } GLPong_TextDrawFPS(GLPong_FPSCount()); SDL_GL_SwapBuffers(); /* end drawing */ SDL_Delay(10); } GLPong_CleanUp(); return 0; } static int GLPong_Init(GLPong_t * GLPong) { SDL_Surface * temp = NULL; GLuint texture; 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("glpong3d", "glpong3d"); /* 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, 8); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); if (GLPong_TextInit() < 0) { fprintf(stderr, "Cannot initialize GLC context: %d\n", glcGetError()); return -1; } atexit(GLPong_TextDeinit); if (SDLNet_Init() < 0) { fprintf(stderr, "Cannot initialize SDL_net: %s\n", SDL_GetError()); return -1; } atexit(SDLNet_Quit); if (SDL_SetVideoMode(GLPONG_WIDTH, GLPONG_HEIGHT, 32, SDL_OPENGL) == NULL) { fprintf(stderr, "SDL_SetVideoMode failed: %s\n", SDL_GetError()); return -1; } glViewport(0, 0, GLPONG_WIDTH, GLPONG_HEIGHT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, GLPONG_WIDTH / GLPONG_HEIGHT, -GLPONG_FRONT_Z, -GLPONG_BACK_Z); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glShadeModel(GL_SMOOTH); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); printf("OpenGL Vendor: %s\n", (char *)glGetString(GL_VENDOR)); printf("OpenGL Renderer: %s\n", (char *)glGetString(GL_RENDERER)); printf("OpenGL Version: %s\n", (char *)glGetString(GL_VERSION)); printf("OpenGL Extensions: %s\n", (char *)glGetString(GL_EXTENSIONS)); /*temp = IMG_Load("ball.png");*/ /* disabled since it's already loaded for the icon */ texture = SDL_GL_SurfaceToTexture(temp); GLPong_BallInit(&GLPong->ball, texture); SDL_FreeSurface(temp); temp = IMG_Load("paddle-skyos.png"); 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); glNewList(box, GL_COMPILE); glBegin(GL_LINE_LOOP); glVertex3f(-1.5f, -1.0f, 0.0f); /* Lower Left */ glVertex3f( 1.5f, -1.0f, 0.0f); /* Lower Right */ glVertex3f( 1.5f, 1.0f, 0.0f); /* Upper Right */ glVertex3f(-1.5f, 1.0f, 0.0f); /* Upper Left */ glEnd(); glEndList(); glPointSize(10.0f); return 0; } static int GLPong_HandleEvents(void) { 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_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 void GLPong_Move(GLPong_t * GLPong) { GLfloat x, y; SDL_GL_GetMouseState(&x, &y); GLPong_PaddleMove(&GLPong->front_paddle, x, y); GLPong_BallMove(&GLPong->ball); if (GLPong_Collide(&GLPong->ball, &GLPong->front_paddle)) { if (GLPong->ball.zv < 0) { GLPong->ball.zv -= 0.005f; } else { GLPong->ball.zv += 0.005f; } #ifdef DEBUG printf("Collision: x: %.3f, y: %.2f, z: %.2f\n\tPaddle: xv: %.3f, yv: %.3f\n", GLPong->ball.x, GLPong->ball.y, GLPong->ball.z, GLPong_PaddleXV(&GLPong->front_paddle), GLPong_PaddleYV(&GLPong->front_paddle)); #endif } else if (GLPong_Collide(&GLPong->ball, &GLPong->back_paddle)) { if (GLPong->ball.zv < 0) { GLPong->ball.zv -= 0.005f; } else { GLPong->ball.zv += 0.005f; } #ifdef DEBUG printf("Collision: x: %.3f, y: %.2f, z: %.2f\n\tPaddle: xv: %.3f, yv: %.3f\n", GLPong->ball.x, GLPong->ball.y, GLPong->ball.z, GLPong_PaddleXV(&GLPong->back_paddle), GLPong_PaddleYV(&GLPong->back_paddle)); #endif } } void GLPong_Draw(const GLPong_t * GLPong) { float count; glColor3f(0.0f, 1.0f, 0.0f); for (count = GLPONG_FRONT_Z; count >= GLPONG_BACK_Z; count -= 1.0f) { glLoadIdentity(); glTranslatef(0.0f, 0.0f, count); /* XXX: optimize by moving glLoadIdentity out of loop? */ glCallList(box); } glLoadIdentity(); glTranslatef(0.0f, 0.0f, 0.0f); glBegin(GL_LINES); glVertex3f(-1.5f, -1.0f, GLPONG_FRONT_Z); /* Lower Left */ glVertex3f(-1.5f, -1.0f, GLPONG_BACK_Z); glEnd(); glBegin(GL_LINES); glVertex3f( 1.5f, -1.0f, GLPONG_FRONT_Z); /* Lower Right */ glVertex3f( 1.5f, -1.0f, GLPONG_BACK_Z); glEnd(); glBegin(GL_LINES); glVertex3f( 1.5f, 1.0f, GLPONG_FRONT_Z); /* Upper Right */ glVertex3f( 1.5f, 1.0f, GLPONG_BACK_Z); glEnd(); glBegin(GL_LINES); glVertex3f(-1.5f, 1.0f, GLPONG_FRONT_Z); /* Upper Left */ glVertex3f(-1.5f, 1.0f, GLPONG_BACK_Z); glEnd(); GLPong_PaddleDraw(&GLPong->back_paddle); GLPong_BallDraw(&GLPong->ball); GLPong_PaddleDraw(&GLPong->front_paddle); } bool GLPong_Collide(const Ball_t * ball, const Paddle_t * paddle) { if (ball->z == paddle->z) { if (ball->x + ball->w < paddle->x) return false; /* if ball is left of paddle */ if (ball->x > paddle->x + paddle->w) return false; /* if ball is right of paddle */ if (ball->y > paddle->y + paddle->h) return false; /* if ball is above paddle */ if (ball->y + ball->h < paddle->y) return false; /* if ball is below paddle */ return true; } return false; } void GLPong_CleanUp(void) { } static __inline__ unsigned int NextPow2(unsigned int value) { #if (defined __i386__ || defined __amd64__ || defined __x86_64__) \ && (defined __GNUC__) 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) ); #else unsigned int x = 2; while (x < value) { x *= 2; } #endif return x; } static GLuint SDL_GL_SurfaceToTexture(SDL_Surface * surface) { GLuint texture; GLenum format = GL_RGB; if (surface->format->Rmask & RMASK) { format = GL_RGB; if (surface->format->BitsPerPixel == 32) { format = GL_RGBA; } } else if (surface->format->Rmask & BMASK) { format = GL_BGR; if (surface->format->BitsPerPixel == 32) { format = GL_BGRA; } } glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); SDL_LockSurface(surface); glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w, surface->h, 0, format, GL_UNSIGNED_BYTE, surface->pixels); SDL_UnlockSurface(surface); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); return texture; } static void SDL_GL_GetMouseState(GLfloat * x, GLfloat * y) { int SDL_x, SDL_y; SDL_PumpEvents(); SDL_GetMouseState(&SDL_x, &SDL_y); *x = ((float)SDL_x / GLPONG_WIDTH) * 3.3f - 1.65f; *y = ((float)SDL_y / GLPONG_HEIGHT) * 2.5f - 1.25f; #ifdef DEBUG printf("SDL (x, y): (%d, %d)\tGL (x, y): (%.3f, %.3f)\n", SDL_x, SDL_y, *x, *y); #endif }