Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Learning sf::View ; can't display a single thing  (Read 961 times)

0 Members and 1 Guest are viewing this topic.

Epholys

  • Newbie
  • *
  • Posts: 2
    • View Profile
Learning sf::View ; can't display a single thing
« 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;
}
 
« Last Edit: June 12, 2014, 03:29:08 pm by Epholys »

Fumasu

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Learning sf::View ; can't display a single thing
« Reply #1 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));

Epholys

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Learning sf::View ; can't display a single thing
« Reply #2 on: June 12, 2014, 03:33:33 pm »
Thanks for your answer, that worked.

I can't believe it was something so simple.