Allow navigating with the keyboard arrows.
This commit is contained in:
parent
a70c97a569
commit
86bf492507
5 changed files with 61 additions and 5 deletions
|
@ -80,4 +80,8 @@ namespace curry {
|
|||
cloonel::SDLMain* GameSceneBase::sdl_main() {
|
||||
return m_sdlmain;
|
||||
}
|
||||
|
||||
cloonel::InputBag& GameSceneBase::input_bag() {
|
||||
return *m_input;
|
||||
}
|
||||
} //namespace curry
|
||||
|
|
|
@ -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<cloonel::InputBag> m_input;
|
||||
|
|
|
@ -1,15 +1,44 @@
|
|||
#include "ingamescene.hpp"
|
||||
#include "sdlmain.hpp"
|
||||
#include "mycurryConfig.h"
|
||||
#include "key.hpp"
|
||||
#include "inputbag.hpp"
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
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());
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -10,7 +10,7 @@ namespace curry {
|
|||
|
||||
struct ScreenTile {
|
||||
Tile index;
|
||||
vec2us pixel_pos;
|
||||
vec2i pixel_pos;
|
||||
};
|
||||
|
||||
class TileIterator : public boost::iterator_facade<TileIterator, Tile, boost::bidirectional_traversal_tag, ScreenTile> {
|
||||
|
|
Loading…
Add table
Reference in a new issue