diff options
Diffstat (limited to 'sdl/pyramid.c')
-rw-r--r-- | sdl/pyramid.c | 99 |
1 files changed, 79 insertions, 20 deletions
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(); |