From 86bf49250736ad268857fb7cb51d0aae903f87fa Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 27 Oct 2016 02:05:26 +0200 Subject: [PATCH] Allow navigating with the keyboard arrows. --- src/gamescenebase.cpp | 4 ++++ src/gamescenebase.hpp | 1 + src/ingamescene.cpp | 41 ++++++++++++++++++++++++++++++++++++++++- src/tileiterator.cpp | 18 +++++++++++++++--- src/tileiterator.hpp | 2 +- 5 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/gamescenebase.cpp b/src/gamescenebase.cpp index 794a60b..70be162 100644 --- a/src/gamescenebase.cpp +++ b/src/gamescenebase.cpp @@ -80,4 +80,8 @@ namespace curry { cloonel::SDLMain* GameSceneBase::sdl_main() { return m_sdlmain; } + + cloonel::InputBag& GameSceneBase::input_bag() { + return *m_input; + } } //namespace curry diff --git a/src/gamescenebase.hpp b/src/gamescenebase.hpp index 8003d3f..41737a9 100644 --- a/src/gamescenebase.hpp +++ b/src/gamescenebase.hpp @@ -26,6 +26,7 @@ namespace curry { virtual void on_destroy() noexcept = 0; virtual void on_update() = 0; cloonel::SDLMain* sdl_main(); + cloonel::InputBag& input_bag(); private: std::unique_ptr m_input; diff --git a/src/ingamescene.cpp b/src/ingamescene.cpp index b3350d1..271dd47 100644 --- a/src/ingamescene.cpp +++ b/src/ingamescene.cpp @@ -1,15 +1,44 @@ #include "ingamescene.hpp" #include "sdlmain.hpp" #include "mycurryConfig.h" +#include "key.hpp" +#include "inputbag.hpp" #include namespace curry { + namespace { + enum Actions : int { + ActionLeft, + ActionUp, + ActionRight, + ActionDown + }; + + void clip (SDL_Rect& parSource, SDL_Rect& parDest, const vec2i& parTopLeft, const vec2i& parBotRight) { + if (parDest.x < parTopLeft.x()) { + const auto diff = parTopLeft.x() - parDest.x; + parDest.x = parTopLeft.x(); + parDest.w -= diff; + parSource.x += diff; + parSource.w -= diff; + } + } + } //unnamed namespace + IngameScene::IngameScene (cloonel::SDLMain* parSDLMain) : GameSceneBase(parSDLMain), m_world(vec2us(96), vec2us(100)), m_viewport(&m_world, vec2i(parSDLMain->WidthHeight())) { + using cloonel::Key; + using cloonel::InputDevice_Keyboard; + //m_viewport.set_position(vec2i(38)); + auto& inp = this->input_bag(); + inp.AddAction(ActionLeft, Key(InputDevice_Keyboard, SDL_SCANCODE_LEFT), "Move left"); + inp.AddAction(ActionUp, Key(InputDevice_Keyboard, SDL_SCANCODE_UP), "Move up"); + inp.AddAction(ActionRight, Key(InputDevice_Keyboard, SDL_SCANCODE_RIGHT), "Move right"); + inp.AddAction(ActionDown, Key(InputDevice_Keyboard, SDL_SCANCODE_DOWN), "Move down"); } IngameScene::~IngameScene() noexcept = default; @@ -19,14 +48,24 @@ namespace curry { } void IngameScene::on_update() { + if (cloonel::IsPressed(input_bag(), ActionRight)) + m_viewport.set_position(m_viewport.position() + vec2i(1, 0)); + if (cloonel::IsPressed(input_bag(), ActionLeft)) + m_viewport.set_position(m_viewport.position() - vec2i(1, 0)); + if (cloonel::IsPressed(input_bag(), ActionUp)) + m_viewport.set_position(m_viewport.position() - vec2i(0, 1)); + if (cloonel::IsPressed(input_bag(), ActionDown)) + m_viewport.set_position(m_viewport.position() + vec2i(0, 1)); + const auto tilesize(m_world.tile_size()); for (auto tile : m_viewport) { vec2us idx(tile.index & 1, (tile.index >> 1) & 1); vec2us src_rect_xy = idx * tilesize; - //std::cout << "Drawing src " << src_rect_xy << " dst " << tile_it.coordinates() << '\n'; + //std::cout << "Drawing src " << src_rect_xy << " dst " << tile.pixel_pos << '\n'; SDL_Rect src{src_rect_xy.x(), src_rect_xy.y(), tilesize.x(), tilesize.y()}; SDL_Rect dst{tile.pixel_pos.x(), tile.pixel_pos.y(), tilesize.x(), tilesize.y()}; + clip(src, dst, vec2i(0), sdl_main()->WidthHeight()); SDL_RenderCopy(this->sdl_main()->GetRenderer(), m_worldtiles.texture(), &src, &dst); } SDL_RenderPresent(this->sdl_main()->GetRenderer()); diff --git a/src/tileiterator.cpp b/src/tileiterator.cpp index 1c61f8a..1718c4b 100644 --- a/src/tileiterator.cpp +++ b/src/tileiterator.cpp @@ -10,7 +10,10 @@ namespace curry { m_index(parEnd ? -1 : 0) { assert(m_viewport); - m_pixel_pos = m_viewport->position(); + m_pixel_pos = vec2i( + -m_viewport->position().x() % m_viewport->world()->tile_size().x(), + -m_viewport->position().y() % m_viewport->world()->tile_size().y() + ); } void TileIterator::increment() { @@ -18,7 +21,14 @@ namespace curry { const WorldGrid& world = *m_viewport->world(); vec2i tile_size(world.tile_size()); - vec2i pos(tile_size * m_index - m_viewport->position()); + auto first_tile_pos = vec2i( + -m_viewport->position().x() % tile_size.x(), + -m_viewport->position().y() % tile_size.y() + ); + vec2i pos(tile_size * m_index + first_tile_pos); + + assert(pos.x() > -tile_size.x() and pos.x() < m_viewport->size().x()); + assert(pos.y() > -tile_size.y() and pos.y() < m_viewport->size().y()); if (pos.x() + tile_size.x() < m_viewport->size().x()) { ++m_index.x(); @@ -26,13 +36,15 @@ namespace curry { else if (pos.y() + tile_size.y() < m_viewport->size().y()) { m_index.x() = 0; ++m_index.y(); + m_pixel_pos.x() = first_tile_pos.x(); + m_pixel_pos.y() += tile_size.y(); } else { m_index = vec2i(-1); } //std::cout << "got index " << m_index << " pixel " << //m_index * tile_size - m_viewport->position() << '\n'; - m_pixel_pos = tile_size * m_index - m_viewport->position(); + m_pixel_pos = tile_size * m_index + first_tile_pos; } ScreenTile TileIterator::dereference() const { diff --git a/src/tileiterator.hpp b/src/tileiterator.hpp index 3e4148e..6dc3074 100644 --- a/src/tileiterator.hpp +++ b/src/tileiterator.hpp @@ -10,7 +10,7 @@ namespace curry { struct ScreenTile { Tile index; - vec2us pixel_pos; + vec2i pixel_pos; }; class TileIterator : public boost::iterator_facade {