| @@ -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` | |||
| @@ -0,0 +1,77 @@ | |||
| // | |||
| // 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 "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; | |||
| } | |||
| @@ -0,0 +1,34 @@ | |||
| // | |||
| // Deus Hax (working title) | |||
| // Copyright (c) 2010 Sam Hocevar <sam@hocevar.net> | |||
| // | |||
| // | |||
| // 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__ | |||
| @@ -13,6 +13,7 @@ | |||
| #include <SDL.h> | |||
| #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; | |||