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

Author Topic: sf::RenderWindow::SetView() is giving two different values.  (Read 1645 times)

0 Members and 1 Guest are viewing this topic.

Jamin Grey

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Of Stranger Flames
sf::RenderWindow::SetView() is giving two different values.
« 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?
Of Stranger Flames - My work-in-progress para-historical (and classically-inspired) 2D rpg.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::RenderWindow::SetView() is giving two different values.
« Reply #1 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.
Laurent Gomila - SFML developer

Jamin Grey

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Of Stranger Flames
sf::RenderWindow::SetView() is giving two different values.
« Reply #2 on: January 25, 2011, 07:28:34 pm »
Thanks, that fixed my issue.
Of Stranger Flames - My work-in-progress para-historical (and classically-inspired) 2D rpg.