diff --git a/src/Makefile.am b/src/Makefile.am index 0f7b9a83..b8947c38 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -9,7 +9,7 @@ libcommon_a_SOURCES = \ joystick.cpp joystick.h asset.cpp asset.h ticker.cpp ticker.h libcommon_a_CXXFLAGS = `pkg-config --cflags sdl gl SDL_image` -test_map_SOURCES = test-map.cpp sdlvideo.cpp sdlvideo.h +test_map_SOURCES = test-map.cpp sdlvideo.cpp sdlvideo.h 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/sdlinput.cpp b/src/sdlinput.cpp new file mode 100644 index 00000000..4339d121 --- /dev/null +++ b/src/sdlinput.cpp @@ -0,0 +1,77 @@ +// +// Deus Hax (working title) +// Copyright (c) 2010 Sam Hocevar +// + +#if defined HAVE_CONFIG_H +# include "config.h" +#endif + +#include + +#include "sdlinput.h" + +/* + * SDL Input implementation class + */ + +class SdlInputData +{ + friend class SdlInput; + +private: + Game *game; +}; + +/* + * Public SdlInput class + */ + +SdlInput::SdlInput(Game *game) +{ + data = new SdlInputData(); + + data->game = game; +} + +Asset::Group SdlInput::GetGroup() +{ + return GROUP_BEFORE; +} + +void SdlInput::TickGame(float delta_time) +{ + Asset::TickGame(delta_time); + + if (data->game->Finished()) + destroy = 1; + + /* Handle mouse input */ + int mx, my; + SDL_GetMouseState(&mx, &my); + data->game->SetMouse(mx * (640 - 32) / 640, my * (480 - 32) / 480); + + /* Handle keyboard and WM input */ + SDL_Event event; + while (SDL_PollEvent(&event)) + { + if (event.type == SDL_QUIT) + data->game->Quit(); + if (event.type == SDL_KEYDOWN) + { + if (event.key.keysym.sym == SDLK_ESCAPE) + data->game->Quit(); +#if 0 + else if (event.key.keysym.sym == SDLK_RETURN) + video->FullScreen(); +#endif + } + } + +} + +SdlInput::~SdlInput() +{ + delete data; +} + diff --git a/src/sdlinput.h b/src/sdlinput.h new file mode 100644 index 00000000..4a1582b5 --- /dev/null +++ b/src/sdlinput.h @@ -0,0 +1,34 @@ +// +// Deus Hax (working title) +// Copyright (c) 2010 Sam Hocevar +// + +// +// The SdlInput class +// ------------------ +// + +#if !defined __DH_SDLINPUT_H__ +#define __DH_SDLINPUT_H__ + +#include "asset.h" +#include "game.h" + +class SdlInputData; + +class SdlInput : public Asset +{ +public: + SdlInput(Game *game); + virtual ~SdlInput(); + +protected: + virtual Group GetGroup(); + virtual void TickGame(float delta_time); + +private: + SdlInputData *data; +}; + +#endif // __DH_SDLINPUT_H__ + diff --git a/src/test-map.cpp b/src/test-map.cpp index c39a9991..9350dd65 100644 --- a/src/test-map.cpp +++ b/src/test-map.cpp @@ -13,6 +13,7 @@ #include #include "sdlvideo.h" +#include "sdlinput.h" #include "game.h" #include "ticker.h" @@ -21,34 +22,17 @@ int main(int argc, char **argv) Video *video = new SdlVideo("Deus Hax", 640, 480); Game *game = new Game("maps/testmap.tmx"); + /* Register the input driver */ + new SdlInput(game); + while (!game->Finished()) { - /* Test stuff */ - int mx, my; - SDL_GetMouseState(&mx, &my); - game->SetMouse(mx * (640 - 32) / 640, my * (480 - 32) / 480); - Ticker::TickGame(33.33333f); video->PreRender(); Ticker::TickRender(33.33333f); game->Render(); video->PostRender(33.33333f); - - /* This could go in a separate function */ - SDL_Event event; - while (SDL_PollEvent(&event)) - { - if (event.type == SDL_QUIT) - game->Quit(); - if (event.type == SDL_KEYDOWN) - { - if (event.key.keysym.sym == SDLK_RETURN) - video->FullScreen(); - else if (event.key.keysym.sym == SDLK_ESCAPE) - game->Quit(); - } - } } delete game;