diff options
author | Kyle K <kylek389@gmail.com> | 2011-07-01 22:45:47 -0500 |
---|---|---|
committer | Kamil Kaminski <kamilkss@gmail.com> | 2011-07-01 22:45:47 -0500 |
commit | a8dd445ee3f531a92c6e356ec048f6f6474e9eee (patch) | |
tree | 10ec4b8038e0c3a25cfc730f883ea3bdb855551b /sdl | |
parent | 9129b7ff505f7b296462dcd9e027c892a9c75c52 (diff) | |
download | GLPyramid-a8dd445ee3f531a92c6e356ec048f6f6474e9eee.tar.gz GLPyramid-a8dd445ee3f531a92c6e356ec048f6f6474e9eee.tar.bz2 GLPyramid-a8dd445ee3f531a92c6e356ec048f6f6474e9eee.zip |
sdl: split code, update makefile, fix header inclusion
Diffstat (limited to 'sdl')
-rw-r--r-- | sdl/Makefile | 35 | ||||
-rw-r--r-- | sdl/glframe.c | 1 | ||||
-rw-r--r-- | sdl/gltools.c | 6 | ||||
-rw-r--r-- | sdl/gltools.h | 2 | ||||
-rw-r--r-- | sdl/math3d.c | 2 | ||||
-rw-r--r-- | sdl/math3d.h | 6 | ||||
-rw-r--r-- | sdl/pyramid.c | 174 | ||||
-rw-r--r-- | sdl/shader.h | 2 |
8 files changed, 121 insertions, 107 deletions
diff --git a/sdl/Makefile b/sdl/Makefile index 580179b..1d881e9 100644 --- a/sdl/Makefile +++ b/sdl/Makefile @@ -1,5 +1,5 @@ -PROG = pyramid -OBJS = $(PROG).o math3d.o gltools.o glframe.o shader.o +BIN = pyramid +SRC = pyramid.c math3d.c gltools.c glframe.c shader.c CC = gcc CFLAGS = -Wall -std=c99 DBGFLAGS = -g -O0 @@ -14,25 +14,36 @@ SDL_LDFLAGS := $(shell sdl-config --libs) SDL_image_CFLAGS := $(shell pkg-config --cflags SDL_image) SDL_image_LDFLAGS := $(shell pkg-config --libs SDL_image) -$(PROG): $(OBJS) - $(CC) $(LDFLAGS) $(SDL_LDFLAGS) $(SDL_image_LDFLAGS) $(OBJS) -o $@ +OBJ_DIR = obj +OBJ_REL = $(addsuffix .o, $(subst .c,,$(SRC))) +OBJ_ABS = $(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(subst .c,,$(SRC)))) -$(PROG).o: %.o: %.c math3d.h gltools.h glframe.h - $(CC) -c $(CFLAGS) $(SDL_CFLAGS) $(SDL_image_CFLAGS) $< +$(BIN): $(OBJ_DIR) $(OBJ_REL) + $(CC) $(LDFLAGS) $(SDL_LDFLAGS) $(SDL_image_LDFLAGS) $(OBJ_ABS) -o $@ + +pyramid.o: %.o: %.c math3d.h gltools.h glframe.h + $(CC) -c $(CFLAGS) $(SDL_CFLAGS) $(SDL_image_CFLAGS) $< -o $(OBJ_DIR)/$@ math3d.o: %.o: %.c %.h - $(CC) -c $(CFLAGS) $< + $(CC) -c $(CFLAGS) $< -o $(OBJ_DIR)/$@ gltools.o: %.o: %.c %.h math3d.h - $(CC) -c $(CFLAGS) $< + $(CC) -c $(CFLAGS) $< -o $(OBJ_DIR)/$@ glframe.o: %.o: %.c %.h math3d.h - $(CC) -c $(CFLAGS) $< + $(CC) -c $(CFLAGS) $< -o $(OBJ_DIR)/$@ shader.o: %.o: %.c %.h math3d.h - $(CC) -c $(CFLAGS) $< + $(CC) -c $(CFLAGS) $< -o $(OBJ_DIR)/$@ + +$(OBJ_DIR): + mkdir -p $(OBJ_DIR) -.PHONY: clean +.PHONY: clean info clean: - rm -f *.o ./$(PROG) + rm -rf $(OBJ_DIR) + rm -f $(BIN) + +info: + @echo $(OBJ_REL) diff --git a/sdl/glframe.c b/sdl/glframe.c index e9cb7c5..1c2fca2 100644 --- a/sdl/glframe.c +++ b/sdl/glframe.c @@ -7,6 +7,7 @@ * */ +#include <GL/glew.h> #include "glframe.h" void glframe_reset(GLFrame *frame) diff --git a/sdl/gltools.c b/sdl/gltools.c index 7244f4b..7bc85ef 100644 --- a/sdl/gltools.c +++ b/sdl/gltools.c @@ -6,6 +6,12 @@ * */ +#include <GL/glew.h> +#include <GL/freeglut.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include "math3d.h" #include "gltools.h" GLint gltWriteTGA(const char *szFileName) diff --git a/sdl/gltools.h b/sdl/gltools.h index 121c285..0a53555 100644 --- a/sdl/gltools.h +++ b/sdl/gltools.h @@ -1,8 +1,6 @@ #ifndef _GLTOOLS_H_ #define _GLTOOLS_H_ -#include "math3d.h" - /* this is the targa header, pragmas are needed to do the voodoo magic */ /* http://gcc.gnu.org/onlinedocs/gcc-4.4.0/gcc/ */ #pragma pack(1) diff --git a/sdl/math3d.c b/sdl/math3d.c index 998641b..e189a01 100644 --- a/sdl/math3d.c +++ b/sdl/math3d.c @@ -6,6 +6,8 @@ * */ +#include <math.h> +#include <string.h> #include "math3d.h" void m3dFindNormalf(M3DVector3f result, const M3DVector3f point1, diff --git a/sdl/math3d.h b/sdl/math3d.h index c337195..5c04daa 100644 --- a/sdl/math3d.h +++ b/sdl/math3d.h @@ -1,12 +1,8 @@ #ifndef _MATH3D_H_ #define _MATH3D_H_ -#include <GL/glew.h> -#include <GL/freeglut.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> #include <math.h> +#include <string.h> #define M3D_PI (3.14159265358979323846) #define M3D_PI_DIV_180 (0.017453292519943296) 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); diff --git a/sdl/shader.h b/sdl/shader.h index 0433f1c..dc1e905 100644 --- a/sdl/shader.h +++ b/sdl/shader.h @@ -1,7 +1,5 @@ #ifndef _SHADER_H_ #define _SHADER_H_ -#include "math3d.h" - #endif |