diff options
Diffstat (limited to 'sdl/pyramid.c')
-rw-r--r-- | sdl/pyramid.c | 174 |
1 files changed, 88 insertions, 86 deletions
diff --git a/sdl/pyramid.c b/sdl/pyramid.c index 50b1833..4d7ea55 100644 --- a/sdl/pyramid.c +++ b/sdl/pyramid.c @@ -15,30 +15,34 @@ * it might be time to split the code, and make a shader version... * */ - + #include <SDL/SDL.h> #include <SDL/SDL_image.h> -/* for some reason order of the headers matters */ -#include "math3d.h" -#include "gltools.h" -#include "glframe.h" +#include <GL/glew.h> +#include <GL/freeglut.h> #include <sys/time.h> +#include "glframe.h" +#include "gltools.h" +#include "math3d.h" #define FRAMES_PER_SECOND 60 /* function prototypes */ static void 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 process_events(void); static void render(void); /* global */ int program_running = 1; -GLFrame camera; +static GLFrame camera; /* display lists identifiers */ -GLuint ground_list; -GLuint triangle_list; +static GLuint ground_list; +static GLuint triangle_list; static GLfloat xRot = 0.0f; static GLfloat yRot = 0.0f; @@ -174,6 +178,76 @@ static void setup_opengl(void) glEndList(); } +static void setup_sdl(void) +{ + SDL_Surface *screen; + + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0 ) + { + fprintf(stderr, "unable to init SDL: %s\n", SDL_GetError()); + exit(-1); + } + atexit(SDL_Quit); + SDL_WM_SetCaption("Textured Pyramid", NULL); + SDL_WM_SetIcon(IMG_Load("tux.png"), 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_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); + + if ((screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL | SDL_RESIZABLE)) == NULL) + { + fprintf(stderr, "unable to set video mode: %s\n", SDL_GetError()); + exit(-1); + } + + SDL_EnableUNICODE(1); + /* SDL doesn't trigger off a ResizeEvent at startup, but as we need this + * for OpenGL, we do this ourselves */ + SDL_Event resizeEvent; + resizeEvent.type = SDL_VIDEORESIZE; + resizeEvent.resize.w = 640; + resizeEvent.resize.h = 480; + SDL_PushEvent(&resizeEvent); +} + +static void setup_glew(void) +{ + /* initalize glew */ + GLenum glewerr = glewInit(); + if (GLEW_OK != glewerr) + { + fprintf(stderr, "error: %s\n", glewGetErrorString(glewerr)); + exit(-1); + } + else + fprintf(stdout, "status: using GLEW %s\n", glewGetString(GLEW_VERSION)); + + /* display OpenGL version */ + GLint major; + GLint minor; + glGetIntegerv(GL_MAJOR_VERSION, &major); + glGetIntegerv(GL_MINOR_VERSION, &minor); + fprintf(stdout, "version: OpenGL %d.%d\n", major, minor); +} + +static inline void fps_control(unsigned int startclock) +{ + unsigned int deltaclock = SDL_GetTicks() - startclock; + if (deltaclock < 1000 / FRAMES_PER_SECOND) + SDL_Delay((1000 / FRAMES_PER_SECOND) - deltaclock); + +#ifdef STAT_FPS + char buffer[30] = { 0 }; + sprintf(buffer, "Textured Pyramid: %4d fps", + 1000 / (SDL_GetTicks() - startclock)); + SDL_WM_SetCaption(buffer, NULL); +#endif +} + static void keys(SDL_keysym *keysym, unsigned int *keys_held, int flag) { if (!flag) @@ -297,92 +371,20 @@ static void render(void) int main(int argc, char **argv) { - SDL_Surface *screen; + setup_sdl(); + setup_glew(); + setup_opengl(); - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0 ) - { - fprintf(stderr, "unable to init SDL: %s\n", SDL_GetError()); - exit(-1); - } - atexit(SDL_Quit); - SDL_WM_SetCaption("Textured Pyramid", NULL); - SDL_WM_SetIcon(IMG_Load("tux.png"), 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_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); - - if ((screen = SDL_SetVideoMode(640, 480, 32, SDL_OPENGL | SDL_RESIZABLE)) == NULL) - { - fprintf(stderr, "unable to set video mode: %s\n", SDL_GetError()); - exit(-1); - } - - SDL_EnableUNICODE(1); - /* SDL doesn't trigger off a ResizeEvent at startup, but as we need this - * for OpenGL, we do this ourselves */ - SDL_Event resizeEvent; - resizeEvent.type = SDL_VIDEORESIZE; - resizeEvent.resize.w = 640; - resizeEvent.resize.h = 480; - SDL_PushEvent(&resizeEvent); - - /* initalize glew */ - GLenum glewerr = glewInit(); - if (GLEW_OK != glewerr) - { - fprintf(stderr, "error: %s\n", glewGetErrorString(glewerr)); - return -1; - } - else - fprintf(stdout, "status: using GLEW %s\n", glewGetString(GLEW_VERSION)); - - /* display OpenGL version */ - GLint major; - GLint minor; - glGetIntegerv(GL_MAJOR_VERSION, &major); - glGetIntegerv(GL_MINOR_VERSION, &minor); - fprintf(stdout, "version: OpenGL %d.%d\n", major, minor); + unsigned int startclock; - setup_opengl(); - -#ifdef STAT_FPS - /* fps counter */ - Uint32 startclock = 0; - Uint32 deltaclock = 0; - Uint32 current_fps = 0; -#endif - struct timeval m_LastCount; - struct timeval lcurrent; while (program_running) { - gettimeofday(&m_LastCount, 0); -#ifdef STAT_FPS startclock = SDL_GetTicks(); -#endif + process_events(); render(); - - gettimeofday(&lcurrent, 0); - float fSeconds = (float) (lcurrent.tv_sec - m_LastCount.tv_sec); - float fFraction = (float) (lcurrent.tv_usec - m_LastCount.tv_usec) * 0.000001f; - float delta = fSeconds + fFraction; - - if (delta < 1000 / FRAMES_PER_SECOND) - SDL_Delay((1000 / FRAMES_PER_SECOND) - delta); - -#ifdef STAT_FPS - deltaclock = SDL_GetTicks() - startclock; - if (deltaclock != 0 ) - current_fps = 1000 / deltaclock; - static char buffer[30] = { 0 }; - sprintf(buffer, "Textured Pyramid: %4d fps", current_fps); - SDL_WM_SetCaption(buffer, NULL); -#endif + fps_control(startclock); } glDeleteLists(ground_list, 2); |