SFML community forums

Help => Graphics => Topic started by: Jamin Grey on January 25, 2011, 02:31:00 am

Title: sf::RenderWindow::SetView() is giving two different values.
Post by: Jamin Grey on January 25, 2011, 02:31:00 am
I'm trying to alter the view of a sf::RenderWindow - unfortunately, it seems to behavior weirdly, if I try to alter it from within a function.

I'm using version 1.6 (the official current version of SFML).

Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>

void ChangeView(sf::RenderWindow *window)
{
    window->SetView(sf::View(sf::FloatRect(0, 0, 640, 480)));
}

int main(int argc, char *argv[])
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Titlebar text");
   
    //Get default view's coordinate.
    sf::Vector2f pos1 = window.ConvertCoords(0, 0);

    //Change the view.
    window.SetView(sf::View(sf::FloatRect(0, 0, 640, 480)));
   
    //Get the coordinate now.
    sf::Vector2f pos2 = window.ConvertCoords(0, 0);

    //Change the view via a function.
    ChangeView(&window);
   
    //Get the view now.
    sf::Vector2f pos3 = window.ConvertCoords(0, 0);

    //Print out the results. Why is the third one different?
    std::cout << "Pos1: (" << pos1.x << ", " << pos1.y << ")\n"
              << "Pos2: (" << pos2.x << ", " << pos2.y << ")\n"
              << "Pos3: (" << pos3.x << ", " << pos3.y << ")\n";

    return 0;
}


See how the function 'ChangeView' is calling completely identical code to when I call 'SetView' in main() itself? Both are calling it like this:
Code: [Select]
window.SetView(sf::View(sf::FloatRect(0, 0, 640, 480)));

However, both are giving different results!

My output:
Pos1: (0, 0)
Pos2: (0, 0)
Pos3: (0, 240)
<--- Why's this one different?

I've been looking at this for a few hours now, and can't find my mistake. Why are the results different if I'm calling them in the exact same way? Anyone know what I'm doing wrong?
Title: sf::RenderWindow::SetView() is giving two different values.
Post by: Laurent on January 25, 2011, 07:41:29 am
When you pass a view to the window, it keeps a pointer to it so it has to exist as long as it is used. Which doesn't happen in your code because you pass a temporary sf::View instance which is immediately destroyed.

Note that this is no longer true in SFML 2, views are always copied.
Title: sf::RenderWindow::SetView() is giving two different values.
Post by: Jamin Grey on January 25, 2011, 07:28:34 pm
Thanks, that fixed my issue.