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

Author Topic: [solved] Resizing the view with the window  (Read 15330 times)

0 Members and 1 Guest are viewing this topic.

Dobi

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
[solved] Resizing the view with the window
« 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
« Last Edit: June 22, 2012, 12:13:00 pm by Dobi »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Resizing the view with the window
« Reply #1 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.
Laurent Gomila - SFML developer

Dobi

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Resizing the view with the window
« Reply #2 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;
}

Celtic Minstrel

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: [solved] Resizing the view with the window
« Reply #3 on: June 22, 2012, 04:48:38 pm »
There's an interesting use of assignment being an expression. :P

Dobi

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: [solved] Resizing the view with the window
« Reply #4 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.
« Last Edit: June 25, 2012, 09:18:05 am by Dobi »

Celtic Minstrel

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: [solved] Resizing the view with the window
« Reply #5 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.