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

Author Topic: SFML 2.0 and sf::view  (Read 2037 times)

0 Members and 1 Guest are viewing this topic.

Nibbler

  • Newbie
  • *
  • Posts: 2
    • View Profile
SFML 2.0 and sf::view
« on: June 14, 2012, 02:22:24 pm »
Hello,

I want to use the sf::view class in sfml 2.0 to be able to implement a kind of camera in my application.
I started to implement what I wanted in sfml 1.6 but for many reasons I switch to sfml 2.0. My implementation of views worked well with the sfml 1.6, but it doesn't in 2.0. I can't move the view, I don't know why.

Here is the piece of code :

  while (_app->isOpen())
    {
      while (_app->pollEvent(event))
        {
          if (event.type == sf::Event::Closed)
            _app->close();
          if (event.type == sf::Event::KeyPressed)
            {
              float Offset = 200.f; // I need to change that. I know :-)                                                                                                                                                    
              if (event.key.code == sf::Keyboard::Up) _view.move( 0,      -Offset);
              if (event.key.code == sf::Keyboard::Down)  _view.move( 0,       Offset);
              if (event.key.code == sf::Keyboard::Left)  _view.move(-Offset,  0);
              if (event.key.code == sf::Keyboard::Right) _view.move( Offset,  0);
            }
        }

      _app->clear();
     // Here come the function that draw my sprites...
      _app->display();
    }

With that kind of code, I am suppose to move the view. Apparently, the view move pretty well (I tested by getting the coordonates of the center of the view which is modified when I press a key), but on the window nothing happen.

Can you explain me why ?

Thank you.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: SFML 2.0 and sf::view
« Reply #1 on: June 14, 2012, 02:52:03 pm »
I'm not sure what you do at: // Here come the function that draw my sprites...

Because you have to apply the new view to the render window: window.setView(_app->setView(_view));
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nibbler

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: SFML 2.0 and sf::view
« Reply #2 on: June 14, 2012, 04:12:58 pm »
Here is the function I call at the line you mentionned.

void Map::drawSprites()
{
  std::list<Object>::iterator it;
  int pos_x;
  int pos_y;

  for (it = _listObject.begin(); it != _listObject.end(); ++it)
    {
      sf::Vector2<unsigned int> vectorWinSize;
      sf::Vector2<unsigned int> vectorImageSize;

      vectorWinSize = _app->getSize();
      vectorImageSize = _imageMap["grass"].getSize();
      pos_x = ((*it).getX() - (*it).getY()) * (vectorImageSize.x / 2) + (vectorWinSize.x / 2);
      pos_y = ((*it).getX() + (*it).getY()) * (vectorImageSize.y / 2 - 4) + 50;

      sf::Sprite sprite;
      sprite.setTexture(_imageMap["grass"]);
      sprite.move(pos_x, pos_y);
      _app->draw(sprite);
    }
}

It's very simple, I iterate on a std::list of objects, and I draw them in the window.
I don't understand what do you mean by window.setView(_app->setView(_view));. I don't have any kind of window variable. My variable _app is the variable which initialize the screen.

Thank you for your help. :)

Edit:
I solved my problem.
Indeed, the _app->setView method was called at the wrong place. :)
« Last Edit: June 14, 2012, 04:21:59 pm by Nibbler »