Hi,
I'm working on a wrapping world functionality. So far it works. But only after having moved to the left, so the "left keyboard key is pressed if condition: if(fview2>=1)" is activated at least once with the consequence that everything works perfectly forever.
But if I go only to the right and the condition as mentioned is never activated, the view becomes somehow distorted after going too far to the right. Only after having activated the "left if condition" at least once, the distortion gets away and everything is fine forever. I really don't know whats wrong. Maybe it has something to do with the fact, that the fview2 value changes to strange numbers under certain conditions when it shouldn't have (f.e. 0.09999999 instead of 0.1 or -7.30157e-008 instead of 0). Did I encounter an sfml bug?
Many thanks in advance.
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow mMainWindow(sf::VideoMode(1000,600), "Map", sf::Style::Close);
sf::View view(sf::Vector2f(500, 300), sf::Vector2f(1000, 600));
sf::View view2(sf::Vector2f(500, 300), sf::Vector2f(1000, 600));
mMainWindow.setFramerateLimit(60);
sf::Image mapimage;
mapimage.loadFromFile("world1.jpg"); //1000*600 px
sf::Texture maptexture;
maptexture.loadFromImage(mapimage);
sf::Sprite mapsprite(maptexture);
mapsprite.setPosition(0, 0);
sf::RectangleShape viewrect;
viewrect.setSize(sf::Vector2f(1000, 600));
viewrect.setPosition(0, 0);
viewrect.setFillColor(sf::Color(250,0,0,40));
sf::RectangleShape viewrect2;
viewrect2.setSize(sf::Vector2f(1000, 600));
viewrect2.setPosition(0, 0);
viewrect2.setFillColor(sf::Color(0,0,250,40));
float fview2 = 1;
view2.setViewport(sf::FloatRect(fview2, 0, 1, 1));
while (mMainWindow.isOpen())
{
sf::Event event;
while (mMainWindow.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
mMainWindow.close();
break;
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
system("cls");
view.move(-100, 0);
fview2=fview2+0.1f;
if(fview2>=1)
{
fview2=0;
view.reset(sf::FloatRect(900, 0, 1000, 600));
view2.reset(sf::FloatRect(-100, 0, 1000, 600));
}
view2.setViewport(sf::FloatRect(fview2, 0, 1, 1));
std::cout << "fview2 " << fview2 << std::endl;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
system("cls");
view.move(100, 0);
fview2=fview2-0.1f;
if(fview2<-0.09)
{
fview2=0.9f;
view.reset(sf::FloatRect(0, 0, 1000, 600));
}
view2.setViewport(sf::FloatRect(fview2, 0, 1, 1));
std::cout << "fview2 " << fview2 << std::endl;
}
mMainWindow.clear();
mMainWindow.setView(view);
mMainWindow.draw(mapsprite);
mMainWindow.draw(viewrect);
mMainWindow.setView(view2);
mMainWindow.draw(mapsprite);
mMainWindow.draw(viewrect2);
mMainWindow.display();
}
return 0;
}