summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2011-07-18 14:42:56 -0500
committerKamil Kaminski <kamilkss@gmail.com>2011-07-18 14:42:56 -0500
commit9053faa7cac1cf05a3659d1b5b24d456d96e4d22 (patch)
treefd70a9f1567ebef3e07f6060118fbb24d1509756
parenta780d11c48d0901a9e019687ce127a1d4f4672fb (diff)
downloadGLPyramid-9053faa7cac1cf05a3659d1b5b24d456d96e4d22.tar.gz
GLPyramid-9053faa7cac1cf05a3659d1b5b24d456d96e4d22.tar.bz2
GLPyramid-9053faa7cac1cf05a3659d1b5b24d456d96e4d22.zip
sdl: make platform more generic by splitting some code into client struct
-rw-r--r--sdl/gltools.c3
-rw-r--r--sdl/luatools.c2
-rw-r--r--sdl/platform.c43
-rw-r--r--sdl/platform.h17
-rw-r--r--sdl/pyramid.c99
5 files changed, 100 insertions, 64 deletions
diff --git a/sdl/gltools.c b/sdl/gltools.c
index 2449cfc..7fc92f7 100644
--- a/sdl/gltools.c
+++ b/sdl/gltools.c
@@ -30,7 +30,10 @@ GLuint gltLoadTGATexture(const char *fname)
pBytes = gltLoadTGA(fname, &iWidth, &iHeight, &iComponents, &eFormat);
if (!pBytes)
+ {
fprintf(stderr, "gltLoadTGA: failed to load texture!\n");
+ return -1;
+ }
glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat,
GL_UNSIGNED_BYTE, pBytes);
diff --git a/sdl/luatools.c b/sdl/luatools.c
index 5116df1..b403750 100644
--- a/sdl/luatools.c
+++ b/sdl/luatools.c
@@ -30,7 +30,7 @@ const char *luaGetFieldString(lua_State *L, const char *key)
return ret;
}
- ret = lua_tostring(L, -1);
+ ret = lua_tostring(L, -1); /* should this be freed at some point? */
lua_pop(L, 1);
return ret;
diff --git a/sdl/platform.c b/sdl/platform.c
index a44bfd4..2d770a8 100644
--- a/sdl/platform.c
+++ b/sdl/platform.c
@@ -26,7 +26,7 @@ const GLfloat fShinyLight[] = { 0.70f, 0.70f, 0.70f, 1.0f };
const GLfloat fBrightLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
/* light values and coordinates */
-const GLfloat lightPos[] = { -10.f, 5.0f, 5.0f, 1.0f };
+const GLfloat lightPos[] = { -10.f, 5.0f, 5.0f, 1.0f };
/* variables that should be already defined and declared for us by main program */
extern unsigned int xres_w;
@@ -40,8 +40,6 @@ extern unsigned int maxfps_w;
void platform_init(struct platform *p)
{
- memset(p, 0, sizeof(struct platform));
-
/* Lua */
/* create new lua state */
p->L = luaL_newstate();
@@ -49,54 +47,33 @@ void platform_init(struct platform *p)
/* load lua libraries */
luaL_openlibs(p->L);
- /* ToDo: how would you make platform more generic? */
- /* note, this should be already zero'ed out */
- memset(&p->config_table, 0, sizeof(p->config_table));
-
/* initialize SDL, GLEW, and OpenGL */
load_config(p);
p->screen = setup_sdl();
setup_glew();
setup_opengl();
- /* set the camera to <0,0,0> */
- glframe_reset(&p->camera);
- p->textures[0] = gltLoadTGATexture("stone.tga");
- p->textures[1] = gltLoadTGATexture("grass.tga");
-
- /* display list, precompile commands */
- p->ground_list = glGenLists(2);
- p->triangle_list = p->ground_list + 1;
-
- /* a triangle with a texture */
- glNewList(p->triangle_list, GL_COMPILE);
- glBindTexture(GL_TEXTURE_2D, p->textures[0]);
- /* glBindTexture(GL_TEXTURE_2D, p->textures[1]); */
- glColor3f(1.0f, 1.0f, 1.0f);
- glDrawTriangle();
- glEndList();
-
- /* a ground drawn using magneta lines */
- glNewList(p->ground_list, GL_COMPILE);
- glColor3ub(255, 0, 255);
- glDrawGround();
- glEndList();
-
/* add a simple timer / callback function */
p->timer_id = SDL_AddTimer(5000, sdlTimerCallback, &(p->camera));
/* apply a custom cursor */
p->my_cursor = sdlInitCursor(arrow);
SDL_SetCursor(p->my_cursor);
+
+ /* set the camera to <0,0,0> */
+ glframe_reset(&p->camera);
+
+ /* init client */
+ p->client_init(p->c);
}
+/* it's a good idea to destroy things in reverse order */
void platform_destroy(struct platform *p)
{
- glDeleteTextures(2, p->textures);
- glDeleteLists(p->ground_list, 2);
+ /* destroy client */
+ p->client_destroy(p->c);
SDL_FreeSurface(p->screen);
-
lua_close(p->L);
}
diff --git a/sdl/platform.h b/sdl/platform.h
index dc34788..d546749 100644
--- a/sdl/platform.h
+++ b/sdl/platform.h
@@ -7,21 +7,13 @@
#include "glframe.h"
#include "luatools.h"
+struct client;
+
/* platform struct */
struct platform
{
GLFrame camera;
- /* display lists identifiers */
- GLuint ground_list;
- GLuint triangle_list;
-
- /* pyramid texture handle */
- GLuint textures[2];
-
- GLfloat xrot;
- GLfloat yrot;
-
/* SDL surface, our screen */
SDL_Surface *screen;
@@ -31,6 +23,11 @@ struct platform
/* Lua */
lua_State *L;
struct luat_table_platform config_table;
+
+ /* client */
+ struct client *c;
+ int (*client_init)(struct client *);
+ int (*client_destroy)(struct client *);
};
extern const GLfloat fNoLight[];
diff --git a/sdl/pyramid.c b/sdl/pyramid.c
index 1260dff..94c3946 100644
--- a/sdl/pyramid.c
+++ b/sdl/pyramid.c
@@ -31,15 +31,10 @@
#include "sdltools.h"
#include "window.h"
-/* function prototypes */
-static void keys(SDL_keysym *, const unsigned int *, const int);
-static void process_events(void);
-static void render(void);
-
/* globals */
struct platform p;
-/* sane defaults, these can be overwritten by config.lua */
+/* sane defaults, these are overwritable by config.lua */
int program_running = 1;
unsigned int xres_w = 640;
unsigned int yres_w = 480;
@@ -50,6 +45,61 @@ const char *window_icon_path = "tux.png";
const unsigned int sdl_video_flags = SDL_OPENGL | SDL_RESIZABLE;
unsigned int maxfps_w = 60;
+/* client extends functionality of the host platform */
+struct client
+{
+ /* display lists identifiers */
+ GLuint ground_list;
+ GLuint triangle_list;
+
+ /* pyramid texture handle */
+ GLuint textures[2];
+
+ GLfloat xrot;
+ GLfloat yrot;
+};
+
+/* function prototypes */
+static int client_init(struct client *);
+static int client_destroy(struct client *);
+static void keys(SDL_keysym *, const unsigned int *, const int);
+static void process_events(void);
+static void render(void);
+
+static int client_init(struct client *p)
+{
+ p->textures[0] = gltLoadTGATexture("stone.tga");
+ p->textures[1] = gltLoadTGATexture("grass.tga");
+
+ /* display list, precompile commands */
+ p->ground_list = glGenLists(2);
+ p->triangle_list = p->ground_list + 1;
+
+ /* a triangle with a texture */
+ glNewList(p->triangle_list, GL_COMPILE);
+ glBindTexture(GL_TEXTURE_2D, p->textures[0]);
+ /* glBindTexture(GL_TEXTURE_2D, p->textures[1]); */
+ glColor3f(1.0f, 1.0f, 1.0f);
+ glDrawTriangle();
+ glEndList();
+
+ /* a ground drawn using magneta lines */
+ glNewList(p->ground_list, GL_COMPILE);
+ glColor3ub(255, 0, 255);
+ glDrawGround();
+ glEndList();
+
+ return 0;
+}
+
+static int client_destroy(struct client *p)
+{
+ glDeleteTextures(2, p->textures);
+ glDeleteLists(p->ground_list, 2);
+
+ return 0;
+}
+
static void keys(SDL_keysym *keysym, const unsigned int *keys_held, const int flag)
{
if (!flag)
@@ -57,10 +107,10 @@ static void keys(SDL_keysym *keysym, const unsigned int *keys_held, const int fl
switch (keysym->sym)
{
case SDLK_ESCAPE: program_running = 0; 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_w: p.c->xrot -= 5.0f; break;
+ case SDLK_s: p.c->xrot += 5.0f; break;
+ case SDLK_a: p.c->yrot -= 5.0f; break;
+ case SDLK_d: p.c->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;
@@ -73,13 +123,13 @@ static void keys(SDL_keysym *keysym, const unsigned int *keys_held, const int fl
else
{
if (keys_held[SDLK_w])
- p.xrot -= 5.0f;
+ p.c->xrot -= 5.0f;
if (keys_held[SDLK_s])
- p.xrot += 5.0f;
+ p.c->xrot += 5.0f;
if (keys_held[SDLK_a])
- p.yrot -= 5.0f;
+ p.c->yrot -= 5.0f;
if (keys_held[SDLK_d])
- p.yrot += 5.0f;
+ p.c->yrot += 5.0f;
if (keys_held[SDLK_UP])
glframe_move_forward(&p.camera, 0.05f);
@@ -96,8 +146,8 @@ static void keys(SDL_keysym *keysym, const unsigned int *keys_held, const int fl
glframe_rotate_local_x(&p.camera, -0.02f);
}
- p.xrot = (GLfloat) ((const int) p.xrot % 360);
- p.yrot = (GLfloat) ((const int) p.yrot % 360);
+ p.c->xrot = (GLfloat) ((const int) p.c->xrot % 360);
+ p.c->yrot = (GLfloat) ((const int) p.c->yrot % 360);
}
static void process_events(void)
@@ -154,16 +204,16 @@ static void render(void)
glPushMatrix();
/* apply camera transform, and draw the ground */
glframe_apply_camera_transform(&p.camera, 1);
- glCallList(p.ground_list);
+ glCallList(p.c->ground_list);
glPushMatrix();
/* move object back and do in place rotation */
glTranslatef(0.0f, 0.2f, -3.5f);
- glRotatef(p.xrot, 1.0f, 0.0f, 0.0f);
- glRotatef(p.yrot, 0.0f, 1.0f, 0.0f);
+ glRotatef(p.c->xrot, 1.0f, 0.0f, 0.0f);
+ glRotatef(p.c->yrot, 0.0f, 1.0f, 0.0f);
/* draw the pyramid */
- glCallList(p.triangle_list);
+ glCallList(p.c->triangle_list);
glPopMatrix();
/* draw a snowman */
@@ -182,6 +232,15 @@ static void render(void)
int main(int argc, char **argv)
{
+ memset(&p, 0, sizeof(p));
+
+ /* setup client platform */
+ struct client c;
+ p.c = &c;
+ p.client_init = &client_init;
+ p.client_destroy = &client_destroy;
+
+ /* initialize host platform */
platform_init(&p);
gltErrorCheck();