diff options
-rw-r--r-- | 128x128.png | bin | 0 -> 15898 bytes | |||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | ball.c | 6 | ||||
-rw-r--r-- | ball.h | 12 | ||||
-rw-r--r-- | glpong3d.c | 242 | ||||
-rw-r--r-- | glpong3d.h | 30 | ||||
-rw-r--r-- | paddle.c | 6 | ||||
-rw-r--r-- | paddle.h | 4 |
8 files changed, 263 insertions, 39 deletions
diff --git a/128x128.png b/128x128.png Binary files differnew file mode 100644 index 0000000..7094b27 --- /dev/null +++ b/128x128.png @@ -9,7 +9,7 @@ EXT=.exe EXE=glpong3d${EXT}
-OBJS=glpong3d.o
+OBJS=glpong3d.o ball.o paddle.o
all: ${OBJS}
${CC} ${LDFLAGS} ${OBJS} -o ${EXE} ${LIBRARIES}
@@ -0,0 +1,6 @@ +#include "SDL.h"
+#include "SDL_opengl.h"
+
+#include "glpong3d.h"
+#include "ball.h"
+#include "paddle.h"
@@ -0,0 +1,12 @@ +#ifndef BALL_H
+#define BALL_H
+
+GLuint ball_texture;
+
+typedef struct {
+ GLfloat w, h, x, y, z;
+ GLfloat r, g, b, a;
+ GLfloat xv, yv, zv;
+} Ball_t;
+
+#endif
@@ -1,65 +1,231 @@ +#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
#include "SDL.h"
#include "SDL_opengl.h"
#include "SDL_image.h"
-#include <stdio.h>
-#include <stdlib.h>
+#include "glpong3d.h"
int main(int argc, char * argv[]) {
- SDL_Surface * Surface = NULL;
- SDL_Event Event;
-
- int width = 640;
- int height = 480;
-
if (argc) {
if (argv) {}
}
- if (SDL_Init(SDL_INIT_VIDEO) != 0) {
- fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
- return -1;
+ GLPong_Init();
+
+ while ( !GLPong.done ) {
+ GLPong_HandleEvents();
+ /* GLPong_Move();*/
+ GLPong_Draw();
+ SDL_Delay(10);
}
- atexit(SDL_Quit);
+ GLPong_CleanUp();
+
+ return 0;
+}
+
+void GLPong_Init() {
+ SDL_Surface * temp = NULL;
- Event.type = SDL_NOEVENT;
+ GLPong.done = 0;
- Surface = SDL_SetVideoMode(width, height, 0, SDL_OPENGL);
+ GLPong.w = 800;
+ GLPong.h = 600;
+
+ GLPong.Ball.w = 1.0f;
+ GLPong.Ball.h = 1.0f;
+ GLPong.Ball.x = -0.5f;
+ GLPong.Ball.y = -0.5f;
+ GLPong.Ball.z = -3.0f;
+ GLPong.Ball.xv = 0.0f;
+ GLPong.Ball.yv = 0.0f;
+ GLPong.Ball.zv = -0.05f;
+
+ SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
+ atexit(SDL_Quit);
SDL_WM_SetCaption("glpong3d", "glpong3d");
+ temp = IMG_Load("ball.png");
+ SDL_WM_SetIcon(temp, NULL);
+ SDL_FreeSurface(temp);
+ SDL_ShowCursor(SDL_DISABLE);
- glShadeModel(GL_SMOOTH);
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- glClearDepth(1.0f);
- glDepthFunc(GL_LEQUAL);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
+ 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);
- glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
- glEnable(GL_BLEND);
+ GLPong.Surface = SDL_SetVideoMode(GLPong.w, GLPong.h, 32, SDL_OPENGL);
+ glViewport(0, 0, GLPong.w, GLPong.h);
- glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
- gluPerspective(45.0f, width / (float)height, 0.1f, 50.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
+ gluPerspective(45.0f, GLPong.w / GLPong.h, 0.1f, 50.0f);
+
+ 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);
- while (1) {
- if (!SDL_PollEvent(&Event)) {
- if (Event.type == SDL_QUIT) {
- return 0;
- } else if (Event.type == SDL_KEYDOWN) {
- if (Event.key.keysym.sym == SDLK_ESCAPE) {
- return 0;
+ 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));
+
+ temp = IMG_Load("128x128.png");
+ 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_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;
+ }
+ }
+ }
}
}
}
-
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- SDL_GL_SwapBuffers();
- SDL_Delay(10);
}
+}
+
+void GLPong_Draw() {
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- return 0;
+ glBindTexture(GL_TEXTURE_2D, ball_texture);
+ glEnable(GL_TEXTURE_2D);
+ glLoadIdentity();
+ glTranslatef(GLPong.Ball.x, GLPong.Ball.y, GLPong.Ball.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 */
+ glTexCoord2f(1.0f, 0.0f); glVertex2f(0.0f, 0.0f); /* Upper Right */
+ glTexCoord2f(0.0f, 0.0f); glVertex2f(GLPong.Ball.w, 0.0f); /* Upper Left */
+ glEnd();
+ glDisable(GL_TEXTURE_2D);
+
+ SDL_GL_SwapBuffers();
+}
+
+void GLPong_CleanUp() {
+
}
+
+void GLPong_Move() {
+ GLPong.Ball.x += GLPong.Ball.xv;
+ GLPong.Ball.y += GLPong.Ball.yv;
+ GLPong.Ball.z += GLPong.Ball.zv;
+ if (GLPong.Ball.z < -12.0f) {
+ GLPong.Ball.zv = -GLPong.Ball.zv;
+ } else if (GLPong.Ball.z > -3.0f) {
+ GLPong.Ball.zv = -GLPong.Ball.zv;
+ }
+}
+
+__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;
+}
+
+GLuint SDL_GL_SurfaceToTexture(SDL_Surface * surface) {
+ Uint32 rmask, gmask, bmask, amask;
+ GLuint texture;
+ GLenum format = GL_RGB;
+
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+ rmask = 0xff000000;
+ gmask = 0x00ff0000;
+ bmask = 0x0000ff00;
+ amask = 0x000000ff;
+#else
+ rmask = 0x000000ff;
+ gmask = 0x0000ff00;
+ bmask = 0x00ff0000;
+ amask = 0xff000000;
+#endif
+
+ if (surface->format->Rmask & rmask) {
+ format = GL_RGB;
+ printf("GL_RGB");
+ if (surface->format->BitsPerPixel == 32) {
+ format = GL_RGBA;
+ printf("A");
+ }
+ } else if (surface->format->Rmask & bmask) {
+ format = GL_BGR;
+ printf("GL_BGR");
+ if (surface->format->BitsPerPixel == 32) {
+ format = GL_BGRA;
+ printf("A");
+ }
+ }
+ printf("\n");
+
+ 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;
+}
+/*
+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);
+}*/
diff --git a/glpong3d.h b/glpong3d.h new file mode 100644 index 0000000..edc6394 --- /dev/null +++ b/glpong3d.h @@ -0,0 +1,30 @@ +#ifndef GLPONG3D_H
+#define GLPONG3D_H
+
+#include "SDL.h"
+#include "SDL_opengl.h"
+
+#include "ball.h"
+#include "paddle.h"
+
+struct {
+ SDL_Event Event;
+ SDL_Surface * Surface;
+ Ball_t Ball;
+ /*Paddle_t Front, Back;*/
+ GLfloat w;
+ GLfloat h;
+ Sint8 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
diff --git a/paddle.c b/paddle.c new file mode 100644 index 0000000..36f33e7 --- /dev/null +++ b/paddle.c @@ -0,0 +1,6 @@ +#include "SDL.h"
+#include "SDL_opengl.h"
+
+#include "glpong3d.h"
+#include "ball.h"
+#include "paddle.h"
diff --git a/paddle.h b/paddle.h new file mode 100644 index 0000000..754cdbf --- /dev/null +++ b/paddle.h @@ -0,0 +1,4 @@ +#ifndef PADDLE_H
+#define PADDLE_H
+
+#endif
|