指定範囲内の描画について
Rinifisu 2015/02/15(Sun) 11:31
Reputeless 2015/02/19(Thu) 20:30
これでどうでしょうか。
Sprite を使えばもう少し効率的になります。
# include <Siv3D.hpp>
void DrawTiled(const RectF& rect, const TextureRegion& textureRegion)
{
assert(textureRegion.size.x != 0.0 && textureRegion.size.y != 0.0);
const int w = static_cast<int>(rect.w / textureRegion.size.x);
const int h = static_cast<int>(rect.h / textureRegion.size.y);
for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
textureRegion.draw(rect.pos + textureRegion.size * Vec2(x, y));
}
}
const double x2 = rect.w - textureRegion.size.x * w;
const double y2 = rect.h - textureRegion.size.y * h;
const float ua = static_cast<float>(x2 / textureRegion.size.x);
const float va = static_cast<float>(y2 / textureRegion.size.y);
if (x2 > 0.0)
{
TextureRegion t = textureRegion;
t.size.x = x2;
t.uvRect.right = t.uvRect.left + (t.uvRect.right - t.uvRect.left) * ua;
for (int y = 0; y < h; ++y)
{
t.draw(rect.pos + textureRegion.size * Vec2(w, y));
}
}
if (y2 > 0.0)
{
TextureRegion t = textureRegion;
t.size.y = y2;
t.uvRect.bottom = t.uvRect.top + (t.uvRect.bottom - t.uvRect.top) * va;
for (int x = 0; x < w; ++x)
{
t.draw(rect.pos + textureRegion.size * Vec2(x, h));
}
}
if (x2 > 0.0 && y2 > 0.0)
{
TextureRegion t = textureRegion;
t.size.x = x2;
t.size.y = y2;
t.uvRect.right = t.uvRect.left + (t.uvRect.right - t.uvRect.left) * ua;
t.uvRect.bottom = t.uvRect.top + (t.uvRect.bottom - t.uvRect.top) * va;
t.draw(rect.pos + textureRegion.size * Vec2(w, h));
}
}
void Main()
{
const Texture texture(L"Example/Windmill.png");
while (System::Update())
{
DrawTiled(Rect(50, 50, Mouse::Pos().movedBy(-50, -50)), texture(300, 150, 100, 100));
}
}