From adf78d03343f627647e968ab5834fe1996ab8f55 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 14 Aug 2010 21:15:41 +0000 Subject: [PATCH] Get rid of SdlVideo, it was pretty useless. --- src/Makefile.am | 2 +- src/sdlvideo.cpp | 91 ------------------------------------------------ src/sdlvideo.h | 31 ----------------- src/test-map.cpp | 55 ++++++++++++++++++++++++----- 4 files changed, 48 insertions(+), 131 deletions(-) delete mode 100644 src/sdlvideo.cpp delete mode 100644 src/sdlvideo.h diff --git a/src/Makefile.am b/src/Makefile.am index 3f417409..73824c3d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,7 +10,7 @@ libcommon_a_SOURCES = \ video.cpp video.h libcommon_a_CXXFLAGS = `pkg-config --cflags sdl gl SDL_image` -test_map_SOURCES = test-map.cpp sdlvideo.cpp sdlvideo.h sdlinput.cpp sdlinput.h +test_map_SOURCES = test-map.cpp sdlinput.cpp sdlinput.h test_map_CXXFLAGS = `pkg-config --cflags sdl gl SDL_image` test_map_LDADD = libcommon.a test_map_LDFLAGS = `pkg-config --libs sdl gl SDL_image` diff --git a/src/sdlvideo.cpp b/src/sdlvideo.cpp deleted file mode 100644 index f063a958..00000000 --- a/src/sdlvideo.cpp +++ /dev/null @@ -1,91 +0,0 @@ -// -// Deus Hax (working title) -// Copyright (c) 2010 Sam Hocevar -// - -#if defined HAVE_CONFIG_H -# include "config.h" -#endif - -#include - -#include "sdlvideo.h" -#include "video.h" - -/* - * SDL Video implementation class - */ - -class SdlVideoData -{ - friend class SdlVideo; - -private: - SDL_Surface *video; - Uint32 start, ticks; - int frames; -}; - -/* - * Public SdlVideo class - */ - -SdlVideo::SdlVideo(char const *title, int width, int height) -{ - data = new SdlVideoData(); - - /* 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); - SDL_ShowCursor(0); - SDL_WM_GrabInput(SDL_GRAB_ON); - - Video::Setup(data->video->w, data->video->h); - - /* Initialise timer */ - data->start = data->ticks = SDL_GetTicks(); - data->frames = 0; -} - -void SdlVideo::PreRender() -{ - Video::Clear(); -} - -void SdlVideo::PostRender(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 SdlVideo::FullScreen() -{ - SDL_WM_ToggleFullScreen(data->video); -} - -SdlVideo::~SdlVideo() -{ - Uint32 total = SDL_GetTicks() - data->start; - printf("%f fps\n", 1000.0f * data->frames / total); - SDL_Quit(); - delete data; -} - diff --git a/src/sdlvideo.h b/src/sdlvideo.h deleted file mode 100644 index 1eb8b371..00000000 --- a/src/sdlvideo.h +++ /dev/null @@ -1,31 +0,0 @@ -// -// Deus Hax (working title) -// Copyright (c) 2010 Sam Hocevar -// - -// -// The SdlVideo class -// ------------------ -// - -#if !defined __DH_SDLVIDEO_H__ -#define __DH_SDLVIDEO_H__ - -class SdlVideoData; - -class SdlVideo -{ -public: - SdlVideo(char const *title, int width, int height); - ~SdlVideo(); - - void PreRender(); - void PostRender(float milliseconds); - void FullScreen(); - -private: - SdlVideoData *data; -}; - -#endif // __DH_SDLVIDEO_H__ - diff --git a/src/test-map.cpp b/src/test-map.cpp index 774c45a3..a509a3d1 100644 --- a/src/test-map.cpp +++ b/src/test-map.cpp @@ -12,29 +12,68 @@ #include -#include "sdlvideo.h" #include "sdlinput.h" #include "game.h" #include "ticker.h" +#include "video.h" int main(int argc, char **argv) { - SdlVideo *video = new SdlVideo("Deus Hax", 640, 480); + /* Initialise SDL */ + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { + fprintf(stderr, "Cannot initialise SDL: %s\n", SDL_GetError()); + return EXIT_FAILURE; + } + + SDL_Surface *video = SDL_SetVideoMode(640, 480, 0, SDL_OPENGL); + if (!video) + { + fprintf(stderr, "Cannot create OpenGL screen: %s\n", SDL_GetError()); + SDL_Quit(); + return EXIT_FAILURE; + } + + SDL_WM_SetCaption("Deus Hax (SDL)", NULL); + SDL_ShowCursor(0); + SDL_WM_GrabInput(SDL_GRAB_ON); + + /* Initialise timer */ + Uint32 start, ticks; + start = ticks = SDL_GetTicks(); + int frames = 0; + + /* Initialise OpenGL */ + Video::Setup(video->w, video->h); + + /* Create a game */ Game *game = new Game("maps/testmap.tmx"); - /* Register the input driver */ + /* Register an input driver */ new SdlInput(game); while (!game->Finished()) { - Ticker::TickGame(33.33333f); + float const delta_time = 33.33333f; + + /* Tick the game */ + Ticker::TickGame(delta_time); + + /* Clear the screen, tick the renderer, and show the frame */ + Video::Clear(); + Ticker::TickRender(delta_time); + SDL_GL_SwapBuffers(); - video->PreRender(); - Ticker::TickRender(33.33333f); - video->PostRender(33.33333f); + /* Clamp to desired framerate */ + while (SDL_GetTicks() < ticks + (delta_time - 0.5f)) + SDL_Delay(1); + ticks = SDL_GetTicks(); + frames++; } - delete video; + Uint32 total = SDL_GetTicks() - start; + printf("%f fps\n", 1000.0f * frames / total); + SDL_Quit(); return EXIT_SUCCESS; }