Hi there,
sorry to retrieve this old thread, but I've a question concerning the code that was posted in the last post.
Is it possible to get it working for multiple tiles, that are aligned horizontally to get a earth-like "map" (which means in 2D terms: If you leave it to the left side you will reappear from the right side and vice versa :-) )
Well, I've been trying to implement that for some days now (Visaual c++ 10/SFML 2.1) :-), but it produces strange efffects and doesn't work as intended. The "blue part" (s. Attachment) is somehow "finite". Many thanks in advance for any advice! Here is the code (the main part was taken from the last post where some modifications have been applied).
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow Window(sf::VideoMode(1000, 768), "", sf::Style::Close);
sf::View View(Window.getDefaultView());
sf::FloatRect fBounds(0.f, 0.f, 2000.f, 500.f);
sf::FloatRect fBounds2(0.f, 0.f, 2000.f, 500.f); // arbitrary > view "SIZE"
sf::Texture Texture;
sf::Texture Texture2;
Texture.loadFromFile("mapleft.png");
Texture2.loadFromFile("mapright.png");
sf::IntRect iBounds(fBounds);
sf::IntRect iBounds2(fBounds2);
Texture.setRepeated(true);
Texture2.setRepeated(true);
sf::Sprite Sprite(Texture, iBounds);
sf::Sprite Sprite2(Texture2, iBounds2);
const sf::Vector2f viewStart(fBounds.left + (fBounds.width / 2), fBounds.top + (fBounds.height / 2));
const sf::Vector2f viewStart2(fBounds2.left + (fBounds2.width / 2), fBounds2.top + (fBounds2.height / 2));
const sf::Vector2f spriteStart(fBounds.left, fBounds.top);
const sf::Vector2f spriteStart2(fBounds2.left, fBounds2.top);
while (Window.isOpen())
{
sf::Event event;
while (Window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
Window.close();
}
if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Right))
{
View.move(+150, 0);
}
if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Left))
{
View.move(-150, 0);
}
if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Up))
{
View.move(0, -150);
}
if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Down))
{
View.move(0, +150);
}
}
const sf::Vector2f viewOffset(viewStart - View.getCenter());
sf::Vector2f spriteOffset;
sf::Vector2f spriteOffset2;
spriteOffset.x = floor(viewOffset.x / Texture.getSize().x) * Texture.getSize().x;
spriteOffset2.x = floor(viewOffset.x / Texture.getSize().x) * Texture.getSize().x;
Sprite.setPosition(spriteStart - spriteOffset);
Sprite2.setPosition(spriteStart2-spriteStart - spriteOffset2+spriteOffset);
Window.clear();
Window.setView(View);
Window.draw(Sprite);
Window.draw(Sprite2);
Window.display();
}
}