SFML community forums

Help => Graphics => Topic started by: Epholys on June 12, 2014, 02:56:07 pm

Title: Learning sf::View ; can't display a single thing
Post by: Epholys on June 12, 2014, 02:56:07 pm
Hello everyone!

I tried using sf::View with the example of the tutorials but it seems that using sf::RenderWindow::setView doesn't work with me. If I use it, i can't display the red quare as I want in the following code, I only get a black window:


#include<SFML/Graphics.hpp>

int main()
{
        sf::RectangleShape rect (sf::Vector2f(100,100));
        rect.setFillColor(sf::Color::Red);

        sf::RenderWindow window (sf::VideoMode(1000,800), "ViewTest");

        // Initialize the view to a rectangle located at (100, 100) and with a size of 400x200
        sf::View view (sf::FloatRect(100, 100, 400, 200));

// Apply it
        window.setView(view);
// Render stuff

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
}
                window.clear();
                window.draw(rect);
                window.display();
        }

        return 0;
}
 
Title: Re: Learning sf::View ; can't display a single thing
Post by: Fumasu on June 12, 2014, 03:26:30 pm
Hi.

The RectangleShape is not shown beause it is not inside your view.
It's position is 0,0 with a size of 100, 100 and your view starts at position 100, 100 with a size of 400, 200. So it is outside your view.

You have to move the rect inside the view e.g:
Code: [Select]
rect.setPosition(sf::Vector2f(100, 100));
Title: Re: Learning sf::View ; can't display a single thing
Post by: Epholys on June 12, 2014, 03:33:33 pm
Thanks for your answer, that worked.

I can't believe it was something so simple.