MyCurry/src/gamelib/worldviewport.cpp

90 lines
2.6 KiB
C++

/*
Copyright 2016, 2017 Michele "King_DuckZ" Santullo
This file is part of MyCurry.
MyCurry is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MyCurry is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MyCurry. If not, see <http://www.gnu.org/licenses/>.
*/
#include "worldviewport.hpp"
#include "worldgrid.hpp"
#include "compatibility.h"
#include <cassert>
#include <algorithm>
namespace curry {
namespace {
vec2us starting_index (const WorldViewport& parViewport) a_pure;
vec2us starting_index (const WorldViewport& parViewport) {
const auto pos = vector_cast<vec2i>(parViewport.position());
const auto ts = parViewport.world()->tile_size();
return vec2us(
static_cast<uint16_t>(std::max(0, pos.x()) / ts.x()),
static_cast<uint16_t>(std::max(0, pos.y()) / ts.y())
);
}
} //unnamed namespace
WorldViewport::WorldViewport (WorldGrid* parWorld, vec2f parSize, const WorldSizeNotifiable::DeferredRegister& parDeferredRegister) :
WorldSizeNotifiable(parDeferredRegister),
m_position(vec2f(0.0f), vec2f(0.0f), vec2f(0.0f)),
m_size(parSize),
m_world(parWorld)
{
assert(m_world);
m_position.set(vec2f(0.0f));
}
const WorldGrid* WorldViewport::world() const {
return m_world;
}
const vec2f& WorldViewport::position() const {
return m_position.get();
}
const vec2f& WorldViewport::size() const {
return m_size;
}
auto WorldViewport::begin() -> iterator {
return iterator(this, false);
}
auto WorldViewport::end() -> iterator {
return iterator(this, true);
}
void WorldViewport::set_position (const vec2f& parPos) {
m_position.set(parPos);
}
void WorldViewport::world_size_changed (const vec2us&, const vec2i& parPixelSize) {
m_position.change_minmax(vec2f(0.0f), vector_cast<vec2f>(parPixelSize) - m_size);
}
const TileIndex* WorldViewport::tile (const vec2us& parIndex) const {
return world()->tile(parIndex + starting_index(*this));
}
void WorldViewport::allow_overscan (bool parAllow) {
if (parAllow) {
m_position.change_minmax(-size(), vector_cast<vec2f>(world_size_pixel(*m_world)));
}
else {
m_position.change_minmax(vec2f(0.0f), vector_cast<vec2f>(world_size_pixel(*m_world)) - size());
}
}
} //namespace curry