@@ -10,7 +10,7 @@ libcommon_a_SOURCES = \ | |||||
video.cpp video.h | video.cpp video.h | ||||
libcommon_a_CXXFLAGS = `pkg-config --cflags sdl gl SDL_image` | 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_CXXFLAGS = `pkg-config --cflags sdl gl SDL_image` | ||||
test_map_LDADD = libcommon.a | test_map_LDADD = libcommon.a | ||||
test_map_LDFLAGS = `pkg-config --libs sdl gl SDL_image` | test_map_LDFLAGS = `pkg-config --libs sdl gl SDL_image` | ||||
@@ -1,91 +0,0 @@ | |||||
// | |||||
// Deus Hax (working title) | |||||
// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net> | |||||
// | |||||
#if defined HAVE_CONFIG_H | |||||
# include "config.h" | |||||
#endif | |||||
#include <SDL.h> | |||||
#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; | |||||
} | |||||
@@ -1,31 +0,0 @@ | |||||
// | |||||
// Deus Hax (working title) | |||||
// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net> | |||||
// | |||||
// | |||||
// 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__ | |||||
@@ -12,29 +12,68 @@ | |||||
#include <SDL.h> | #include <SDL.h> | ||||
#include "sdlvideo.h" | |||||
#include "sdlinput.h" | #include "sdlinput.h" | ||||
#include "game.h" | #include "game.h" | ||||
#include "ticker.h" | #include "ticker.h" | ||||
#include "video.h" | |||||
int main(int argc, char **argv) | 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"); | Game *game = new Game("maps/testmap.tmx"); | ||||
/* Register the input driver */ | |||||
/* Register an input driver */ | |||||
new SdlInput(game); | new SdlInput(game); | ||||
while (!game->Finished()) | 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; | return EXIT_SUCCESS; | ||||
} | } | ||||