SFML community forums

Help => Graphics => Topic started by: Dobi on June 22, 2012, 11:41:02 am

Title: [solved] Resizing the view with the window
Post by: Dobi on June 22, 2012, 11:41:02 am
Hi,

I want my view to scale with my window, so that I actually see more of my scene when the window is maximized. My test code looks as follows:
#include <SFML/Graphics.hpp>
int main()
{
        sf::RenderWindow App(sf::VideoMode(640, 480), "Resize test");
        while (App.IsOpened())
        {
                sf::Event Event;
                while (App.GetEvent(Event))
                {
                        if (Event.Type == sf::Event::Closed)
                                App.Close();
                        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                                App.Close();
                        if (Event.Type == sf::Event::Resized)
                                App.SetView(sf::View(sf::FloatRect(0.f, 0.f,
                                                static_cast<float>(App.GetWidth()),
                                                static_cast<float>(App.GetHeight()))));
                }
                App.Clear();
                App.Draw(sf::Shape::Rectangle(100.f, 100.f, 300.f, 300.f, sf::Color::Blue));
                App.Draw(sf::Shape::Rectangle(200.f, 200.f, 400.f, 400.f, sf::Color::Red));
                App.Display();
        }
        return EXIT_SUCCESS;
}
The problem is, that nothing is visible any more (all black) after the window's size is changed.
Is this some strange behaviour of my PC or am I just doing something wrong? :)
(I am using SFML 1.6 with Visual C++ 2010 express at the moment.)

Regards,
Dobi
Title: Re: Resizing the view with the window
Post by: Laurent on June 22, 2012, 11:44:37 am
In SFML 1.6, the view is used by reference, which means that you must keep the sf::View object alive as long as the window uses it. You can't set a temporary variable like you do.
Title: Re: Resizing the view with the window
Post by: Dobi on June 22, 2012, 12:12:41 pm
Oh, I should have figured that our from the sources. Thank you for the quick help.

For future searchers:
#include <SFML/Graphics.hpp>
int main()
{
        sf::RenderWindow App(sf::VideoMode(640, 480), "Resize test");
        sf::View view(App.GetDefaultView());
        while (App.IsOpened())
        {
                sf::Event Event;
                while (App.GetEvent(Event))
                {
                        if (Event.Type == sf::Event::Closed)
                                App.Close();
                        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                                App.Close();
                        if (Event.Type == sf::Event::Resized)
                                App.SetView(view = sf::View(sf::FloatRect(0.f, 0.f,
                                                static_cast<float>(App.GetWidth()),
                                                static_cast<float>(App.GetHeight()))));
                }
                App.Clear();
                App.Draw(sf::Shape::Rectangle(100.f, 100.f, 300.f, 300.f, sf::Color::Blue));
                App.Draw(sf::Shape::Rectangle(200.f, 200.f, 400.f, 400.f, sf::Color::Red));
                App.Display();
        }
        return EXIT_SUCCESS;
}
Title: Re: [solved] Resizing the view with the window
Post by: Celtic Minstrel on June 22, 2012, 04:48:38 pm
There's an interesting use of assignment being an expression. :P
Title: Re: [solved] Resizing the view with the window
Post by: Dobi on June 25, 2012, 09:15:32 am
Yeah, it's valid standard c++. ;)
Quote
5.17 Assignment and compound assignment operators [expr.ass]
1 The assignment operator (=) and the compound assignment operators all group right-to-left. All require a
modifiable lvalue as their left operand and return an lvalue referring to the left operand.
Title: Re: [solved] Resizing the view with the window
Post by: Celtic Minstrel on June 26, 2012, 02:16:50 am
I know it's valid; I just haven't seen it used as a function argument before (that I can remember). Mostly I've seen (and sometimes used) it in if statements.