summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2011-07-02 13:41:48 -0500
committerKamil Kaminski <kamilkss@gmail.com>2011-07-02 13:41:48 -0500
commit616f2216d80848f88605be75fd03049f6d635378 (patch)
tree86f7939e41e80f5806c91a86f9cd6472a251f30e
parenta8dd445ee3f531a92c6e356ec048f6f6474e9eee (diff)
downloadGLPyramid-616f2216d80848f88605be75fd03049f6d635378.tar.gz
GLPyramid-616f2216d80848f88605be75fd03049f6d635378.tar.bz2
GLPyramid-616f2216d80848f88605be75fd03049f6d635378.zip
sdl: move code around, introduce concept of a platform
-rw-r--r--sdl/glframe.c2
-rw-r--r--sdl/mingw322
-rw-r--r--sdl/pyramid.c210
3 files changed, 119 insertions, 95 deletions
diff --git a/sdl/glframe.c b/sdl/glframe.c
index 1c2fca2..e9c31e3 100644
--- a/sdl/glframe.c
+++ b/sdl/glframe.c
@@ -39,7 +39,7 @@ void glframe_get_camera_orientation(GLFrame *frame, M3DMatrix44f m)
m3dCrossProductf(x, frame->v_up, z);
/* matrix has no translation information and is transposed */
- #define M(row,col) m[col*4+row]
+ #define M(row,col) m[col*4+row]
M(0, 0) = x[0];
M(0, 1) = x[1];
M(0, 2) = x[2];
diff --git a/sdl/mingw32 b/sdl/mingw32
index 951fdf7..eb1cbbc 100644
--- a/sdl/mingw32
+++ b/sdl/mingw32
@@ -39,6 +39,6 @@ $(BIN): $(OBJ_FILES)
clean:
rm -rf $(OBJ_DIR)
- rm -f $(BIN_DIR)/$(BIN)
+ rm -f $(BIN_DIR)/$(BIN).exe stdout.txt stderr.txt
-include $(DEP_FILES)
diff --git a/sdl/pyramid.c b/sdl/pyramid.c
index 4d7ea55..3b63168 100644
--- a/sdl/pyramid.c
+++ b/sdl/pyramid.c
@@ -27,32 +27,47 @@
#define FRAMES_PER_SECOND 60
/* function prototypes */
-static void resize(int, int);
+static void window_resize(int, int);
static void setup_opengl(void);
static void setup_sdl(void);
static void setup_glew(void);
static inline void fps_control(unsigned int);
-static void keys(SDL_keysym *, unsigned int *, int);
+static void keys(SDL_keysym *, const unsigned int *, const int);
static void process_events(void);
static void render(void);
+static void platform_init(void);
+static void platform_destroy(void);
/* global */
int program_running = 1;
-static GLFrame camera;
-
-/* display lists identifiers */
-static GLuint ground_list;
-static GLuint triangle_list;
-
-static GLfloat xRot = 0.0f;
-static GLfloat yRot = 0.0f;
+const unsigned int xres = 640;
+const unsigned int yres = 480;
+const unsigned int bpp = 32;
/* few arrays, they could make into the header at some point */
GLfloat fNoLight[] = { 0.0f, 0.0f, 0.0f, 0.0f };
GLfloat fLowLight[] = { 0.25f, 0.25f, 0.25f, 1.0f };
GLfloat fBrightLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
-static void resize(int w, int h)
+/* light values and coordinates */
+GLfloat whiteLight[] = { 0.05f, 0.05f, 0.05f, 1.0f };
+GLfloat sourceLight[] = { 0.75f, 0.75f, 0.75f, 1.0f };
+GLfloat lightPos[] = { -10.f, 5.0f, 5.0f, 1.0f };
+
+/* platform struct */
+static struct
+{
+ GLFrame camera;
+
+ /* display lists identifiers */
+ GLuint ground_list;
+ GLuint triangle_list;
+
+ GLfloat xrot;
+ GLfloat yrot;
+} p;
+
+static void window_resize(int w, int h)
{
printf("window: resizing to %dx%d\n", w, h);
GLfloat fAspect = (GLfloat) w / (GLfloat) h;
@@ -70,22 +85,13 @@ static void resize(int w, int h)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
-
+
/* this needs to be ran again, glut does it for you I suppose */
- SDL_SetVideoMode(w, h, 32, SDL_OPENGL | SDL_RESIZABLE);
+ SDL_SetVideoMode(w, h, bpp, SDL_OPENGL | SDL_RESIZABLE);
}
static void setup_opengl(void)
{
- GLbyte *pBytes;
- GLint iWidth, iHeight, iComponents;
- GLenum eFormat;
-
- /* light values and coordinates */
- GLfloat whiteLight[] = { 0.05f, 0.05f, 0.05f, 1.0f };
- GLfloat sourceLight[] = { 0.75f, 0.75f, 0.75f, 1.0f };
- GLfloat lightPos[] = { -10.f, 5.0f, 5.0f, 1.0f };
-
/* setup fog */
glEnable(GL_FOG);
glFogfv(GL_FOG_COLOR, fLowLight); /* set fog color to match background */
@@ -104,14 +110,14 @@ static void setup_opengl(void)
glLightfv(GL_LIGHT0, GL_DIFFUSE, fBrightLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, fBrightLight);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
-
+
/* enable lighting */
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
-
+
/* enable color tracking */
glEnable(GL_COLOR_MATERIAL);
-
+
/* set Material properties to follow glColor values */
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR, fBrightLight);
@@ -130,52 +136,28 @@ static void setup_opengl(void)
/* gray background */
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
-
- /* load texture */
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- pBytes = gltLoadTGA("stone.tga", &iWidth, &iHeight, &iComponents, &eFormat);
- if (!pBytes)
- fprintf(stderr, "gltLoadTGA: failed to load texture!\n");
- /* load texture image */
- glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat,
- GL_UNSIGNED_BYTE, pBytes);
- free(pBytes);
-
- /* texture filtering, we modify deafult values since we don't have mipmaps */
+ /* original texture loading used to be here */
+
+ /* texture filtering, we modify default values since we don't have mipmaps */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
-
+
/* how OpenGL combines the colors from texels with the color of the underlying
* geometry is controlled by the texture environment mode */
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-
+
/* once texture is loaded and enabled, it will applied to every primitive
* that specifies coordinates */
glEnable(GL_TEXTURE_2D);
-
- /* set the camera to <0,0,0> */
- glframe_reset(&camera);
-
+
/* multisampling for polygons, conflicts with anti-aliasing */
//glEnable(GL_MULTISAMPLE);
-
+
/* don't know about this, but why not */
glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
-
- /* display list, precompile commands */
- ground_list = glGenLists(2);
- triangle_list = ground_list + 1;
-
- glNewList(ground_list, GL_COMPILE);
- gltDrawGround();
- glEndList();
-
- glNewList(triangle_list, GL_COMPILE);
- gltDrawTriangle();
- glEndList();
}
static void setup_sdl(void)
@@ -198,7 +180,7 @@ static void setup_sdl(void)
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
- if ((screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL | SDL_RESIZABLE)) == NULL)
+ if ((screen = SDL_SetVideoMode(xres, yres, bpp, SDL_OPENGL | SDL_RESIZABLE)) == NULL)
{
fprintf(stderr, "unable to set video mode: %s\n", SDL_GetError());
exit(-1);
@@ -209,8 +191,8 @@ static void setup_sdl(void)
* for OpenGL, we do this ourselves */
SDL_Event resizeEvent;
resizeEvent.type = SDL_VIDEORESIZE;
- resizeEvent.resize.w = 640;
- resizeEvent.resize.h = 480;
+ resizeEvent.resize.w = xres;
+ resizeEvent.resize.h = yres;
SDL_PushEvent(&resizeEvent);
}
@@ -231,10 +213,10 @@ static void setup_glew(void)
GLint minor;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
- fprintf(stdout, "version: OpenGL %d.%d\n", major, minor);
+ fprintf(stdout, "version: OpenGL %d.%d detected\n", major, minor);
}
-static inline void fps_control(unsigned int startclock)
+static inline void fps_control(const unsigned int startclock)
{
unsigned int deltaclock = SDL_GetTicks() - startclock;
if (deltaclock < 1000 / FRAMES_PER_SECOND)
@@ -248,47 +230,47 @@ static inline void fps_control(unsigned int startclock)
#endif
}
-static void keys(SDL_keysym *keysym, unsigned int *keys_held, int flag)
+static void keys(SDL_keysym *keysym, const unsigned int *keys_held, const int flag)
{
if (!flag)
{
switch (keysym->sym)
{
case SDLK_ESCAPE: program_running = 0; break;
- case SDLK_w: xRot -= 5.0f; break;
- case SDLK_s: xRot += 5.0f; break;
- case SDLK_a: yRot -= 5.0f; break;
- case SDLK_d: yRot += 5.0f; break;
- case SDLK_UP: glframe_move_forward(&camera, 0.5f); break;
- case SDLK_DOWN: glframe_move_forward(&camera, -0.5f); break;
- case SDLK_LEFT: glframe_rotate_local_y(&camera, 0.1f); break;
- case SDLK_RIGHT: glframe_rotate_local_y(&camera, -0.1f); break;
+ case SDLK_w: p.xrot -= 5.0f; break;
+ case SDLK_s: p.xrot += 5.0f; break;
+ case SDLK_a: p.yrot -= 5.0f; break;
+ case SDLK_d: p.yrot += 5.0f; break;
+ case SDLK_UP: glframe_move_forward(&p.camera, 0.5f); break;
+ case SDLK_DOWN: glframe_move_forward(&p.camera, -0.5f); break;
+ case SDLK_LEFT: glframe_rotate_local_y(&p.camera, 0.1f); break;
+ case SDLK_RIGHT: glframe_rotate_local_y(&p.camera, -0.1f); break;
default: break;
}
}
else
{
if (keys_held[SDLK_w])
- xRot -= 5.0f;
+ p.xrot -= 5.0f;
if (keys_held[SDLK_s])
- xRot += 5.0f;
+ p.xrot += 5.0f;
if (keys_held[SDLK_a])
- yRot -= 5.0f;
+ p.yrot -= 5.0f;
if (keys_held[SDLK_d])
- yRot += 5.0f;
+ p.yrot += 5.0f;
if (keys_held[SDLK_UP])
- glframe_move_forward(&camera, 0.05f);
+ glframe_move_forward(&p.camera, 0.05f);
if (keys_held[SDLK_DOWN])
- glframe_move_forward(&camera, -0.05f);
+ glframe_move_forward(&p.camera, -0.05f);
if (keys_held[SDLK_LEFT])
- glframe_rotate_local_y(&camera, 0.02f);
+ glframe_rotate_local_y(&p.camera, 0.02f);
if (keys_held[SDLK_RIGHT])
- glframe_rotate_local_y(&camera, -0.02f);
+ glframe_rotate_local_y(&p.camera, -0.02f);
}
- xRot = (GLfloat) ((const int) xRot % 360);
- yRot = (GLfloat) ((const int) yRot % 360);
+ p.xrot = (GLfloat) ((const int) p.xrot % 360);
+ p.yrot = (GLfloat) ((const int) p.yrot % 360);
}
/* process SDL events */
@@ -299,11 +281,11 @@ static void process_events(void)
SDLKey sym;
/* helper flag for keys() */
int flag = 0;
-
+
while (SDL_PollEvent(&event))
{
sym = event.key.keysym.sym;
-
+
switch (event.type)
{
case SDL_KEYUP:
@@ -318,12 +300,12 @@ static void process_events(void)
keys(&event.key.keysym, keys_held, flag);
break;
}
- case SDL_VIDEORESIZE: { resize(event.resize.w, event.resize.h); break; }
+ case SDL_VIDEORESIZE: { window_resize(event.resize.w, event.resize.h); break; }
case SDL_QUIT: { program_running = 0; break; }
default: break;
}
}
-
+
/* below code has to be placed here, check for keys that are being constantly held */
if (keys_held[SDLK_w] || keys_held[SDLK_s] || keys_held[SDLK_a] || keys_held[SDLK_d] ||
keys_held[SDLK_UP] || keys_held[SDLK_DOWN] || keys_held[SDLK_LEFT] || keys_held[SDLK_RIGHT])
@@ -343,25 +325,25 @@ static void render(void)
/* save the matrix state and do the rotations */
glPushMatrix();
/* apply camera transform, and draw the ground */
- glframe_apply_camera_transform(&camera);
+ glframe_apply_camera_transform(&p.camera);
glColor3ub(255, 0, 255);
- glCallList(ground_list);
+ glCallList(p.ground_list);
glPushMatrix();
/* move object back and do in place rotation */
glTranslatef(0.0f, 0.2f, -3.5f);
- glRotatef(xRot, 1.0f, 0.0f, 0.0f);
- glRotatef(yRot, 0.0f, 1.0f, 0.0f);
+ glRotatef(p.xrot, 1.0f, 0.0f, 0.0f);
+ glRotatef(p.yrot, 0.0f, 1.0f, 0.0f);
/* draw the pyramid */
glColor3f(1.0f, 1.0f, 1.0f);
- glCallList(triangle_list);
+ glCallList(p.triangle_list);
glPopMatrix();
/* draw a snowman */
glTranslatef(0.0f, 0.0f, -7.0f);
gltDrawSnowman();
-
+
/* restore the matrix state */
glPopMatrix();
@@ -369,11 +351,53 @@ static void render(void)
SDL_GL_SwapBuffers();
}
+static void platform_init(void)
+{
+ /* variables used for texture loading */
+ GLbyte *pBytes;
+ GLint iWidth, iHeight, iComponents;
+ GLenum eFormat;
+
+ /* set the camera to <0,0,0> */
+ glframe_reset(&p.camera);
+ p.xrot = 0;
+ p.yrot = 0;
+
+ /* load texture */
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ pBytes = gltLoadTGA("stone.tga", &iWidth, &iHeight, &iComponents, &eFormat);
+ if (!pBytes)
+ fprintf(stderr, "gltLoadTGA: failed to load texture!\n");
+
+ /* load texture image */
+ glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat,
+ GL_UNSIGNED_BYTE, pBytes);
+ free(pBytes);
+
+ /* display list, precompile commands */
+ p.ground_list = glGenLists(2);
+ p.triangle_list = p.ground_list + 1;
+
+ glNewList(p.ground_list, GL_COMPILE);
+ gltDrawGround();
+ glEndList();
+
+ glNewList(p.triangle_list, GL_COMPILE);
+ gltDrawTriangle();
+ glEndList();
+}
+
+static void platform_destroy(void)
+{
+ glDeleteLists(p.ground_list, 2);
+}
+
int main(int argc, char **argv)
{
setup_sdl();
setup_glew();
setup_opengl();
+ platform_init();
unsigned int startclock;
@@ -386,10 +410,10 @@ int main(int argc, char **argv)
fps_control(startclock);
}
-
- glDeleteLists(ground_list, 2);
+
+ platform_destroy();
puts("bye!");
-
+
return 0;
}