diff --git a/.gitignore b/.gitignore index c12b044f..2541189f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +*.o src/test-map diff --git a/src/Makefile b/src/Makefile index 57a26eec..3e227fc9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,8 +1,13 @@ +SRC = test-map.cpp video.cpp + all: test-map -test-map: test-map.cpp - g++ -Wall -O3 $^ -o $@ `pkg-config --cflags --libs sdl gl SDL_image` +test-map: $(SRC:%.cpp=%.o) + g++ -Wall -O3 $^ -o $@ `pkg-config --libs sdl gl SDL_image` + +%.o: %.cpp + g++ -Wall -O3 -c $^ -o $@ `pkg-config --cflags sdl gl SDL_image` clean: rm -f test-map diff --git a/src/test-map.cpp b/src/test-map.cpp index cd359ed0..49bb305e 100644 --- a/src/test-map.cpp +++ b/src/test-map.cpp @@ -17,7 +17,7 @@ #include -int frames; +#include "video.h" /* Storage for one texture */ GLuint texture[1]; @@ -51,31 +51,6 @@ void MakeVBOs(void) glGenBuffers(3, buflist); } -void InitGL(int Width, int Height) -{ - // Resize method - glViewport(0, 0, Width, Height); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(0, Width, Height, 0, -1, 10); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - // Init method - glEnable(GL_TEXTURE_2D); - LoadGLTextures(); - MakeVBOs(); - glShadeModel(GL_SMOOTH); - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); - glClearDepth(1.0); - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_LEQUAL); - glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); - - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); -} - void PutMap(int const *themap) { // Put map @@ -191,67 +166,43 @@ void DrawScene() PutMap(ground); //glTranslatef(10.0f * sinf(0.16f * frames), 10.0f * cosf(0.16f * frames), 0.0f); PutMap(l1objects); - - SDL_GL_SwapBuffers(); } int main(int argc, char **argv) { - SDL_Surface *video; - int done; - - /* Initialize SDL for video output */ - if (SDL_Init(SDL_INIT_VIDEO) < 0) - { - fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError()); - exit(1); - } - - /* Create a 640x480 OpenGL screen */ - video = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL); - if (!video) - { - fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError()); - SDL_Quit(); - exit(2); - } - - /* Set the title bar in environments that support it */ - SDL_WM_SetCaption("Deus Hax", NULL); - - /* Loop, drawing and checking events */ - InitGL(640, 480); - done = 0; - frames = 0; - Uint32 ticks = SDL_GetTicks(); - Uint32 start = ticks; - while (!done) - { - DrawScene(); - frames++; - - /* This could go in a separate function */ - SDL_Event event; - while (SDL_PollEvent(&event)) + Video *video = new Video("Deus Hax", 640, 480); + + int done; + + /* Loop, drawing and checking events */ + LoadGLTextures(); + MakeVBOs(); + + done = 0; + while (!done) { - if (event.type == SDL_QUIT) - done = 1; - if (event.type == SDL_KEYDOWN) - { - if (event.key.keysym.sym == SDLK_RETURN) - SDL_WM_ToggleFullScreen(video); - else if (event.key.keysym.sym == SDLK_ESCAPE) - done = 1; - } + DrawScene(); + + video->Refresh(33.33333f); + + /* This could go in a separate function */ + SDL_Event event; + while (SDL_PollEvent(&event)) + { + if (event.type == SDL_QUIT) + done = 1; + if (event.type == SDL_KEYDOWN) + { + if (event.key.keysym.sym == SDLK_RETURN) + video->FullScreen(); + else if (event.key.keysym.sym == SDLK_ESCAPE) + done = 1; + } + } } - while (SDL_GetTicks() < ticks + 33) - SDL_Delay(1); - ticks = SDL_GetTicks(); - } - printf("%i fps\n", frames * 1000 / (SDL_GetTicks() - start)); - SDL_Quit(); + delete video; - return EXIT_SUCCESS; + return EXIT_SUCCESS; } diff --git a/src/video.cpp b/src/video.cpp new file mode 100644 index 00000000..61e270bb --- /dev/null +++ b/src/video.cpp @@ -0,0 +1,114 @@ + +#ifdef WIN32 +# define WIN32_LEAN_AND_MEAN +# include +#endif +#if defined __APPLE__ && defined __MACH__ +# include +#else +# define GL_GLEXT_PROTOTYPES +# include +# include +#endif + +#include + +#include "video.h" + +/* + * Video implementation class + */ + +class VideoData +{ + friend class Video; + +private: + SDL_Surface *video; + Uint32 start, ticks; + int frames; +}; + +/* + * Public Video class + */ + +Video::Video(char const *title, int width, int height) +{ + data = new VideoData(); + + /* Initialise SDL */ + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { + fprintf(stderr, "Cannot initialise SDL: %s\n", SDL_GetError()); + exit(EXIT_FAILURE); + } + + data->video = SDL_SetVideoMode(width, height, 0, SDL_OPENGL); + if (!data->video) + { + fprintf(stderr, "Cannot create OpenGL screen: %s\n", SDL_GetError()); + SDL_Quit(); + exit(EXIT_FAILURE); + } + + SDL_WM_SetCaption(title, NULL); + + /* Initialise OpenGL */ + glViewport(0, 0, data->video->w, data->video->h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, data->video->w, data->video->h, 0, -1, 10); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glEnable(GL_TEXTURE_2D); + glShadeModel(GL_SMOOTH); + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClearDepth(1.0); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + /* Initialise timer */ + data->start = data->ticks = SDL_GetTicks(); + data->frames = 0; +} + +int Video::GetWidth() const +{ + return data->video->w; +} + +int Video::GetHeight() const +{ + return data->video->h; +} + +void Video::Refresh(float milliseconds) +{ + if (milliseconds > 0.0f) + while (SDL_GetTicks() < data->ticks + (milliseconds - 0.5f)) + SDL_Delay(1); + data->ticks = SDL_GetTicks(); + data->frames++; + + SDL_GL_SwapBuffers(); +} + +void Video::FullScreen() +{ + SDL_WM_ToggleFullScreen(data->video); +} + +Video::~Video() +{ + Uint32 total = SDL_GetTicks() - data->start; + printf("%f fps\n", 1000.0f * data->frames / total); + SDL_Quit(); + delete data; +} + diff --git a/src/video.h b/src/video.h new file mode 100644 index 00000000..98e80731 --- /dev/null +++ b/src/video.h @@ -0,0 +1,17 @@ + +class VideoData; + +class Video +{ +public: + Video(char const *title, int width, int height); + ~Video(); + + int GetWidth() const; + int GetHeight() const; + void Refresh(float milliseconds); + void FullScreen(); + +private: + VideoData *data; +};