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

Author Topic: sf::View and render target  (Read 1908 times)

0 Members and 1 Guest are viewing this topic.

Ockonal

  • Jr. Member
  • **
  • Posts: 58
    • ICQ Messenger - 449909010
    • View Profile
    • WinCode
sf::View and render target
« on: October 20, 2012, 01:23:02 am »
Hello, I have a few questions about how views are working.

    sf::RenderWindow *mApp;
    sf::View *mCamera;
    sf::RenderTexture *mRenderTarget;

So first, everything is rendered into mRenderTarget and at the end of the game cycle I draw final image to mApp.

Here is the function which initializes camera and reinitializes it when window is resized:

void DisplayManager::InitializeCamera(const sf::FloatRect &rect)
{
    // This is automatically called when window is resized
    mCamera->reset(rect);
    mApp->setView(*mCamera);

    mRenderTarget->create(rect.width, rect.height);
    mRenderTarget->setView(*mCamera);
}
 
And it looks like:
    mRenderTarget->display();
    mApp->draw(sf::Sprite(mRenderTarget->getTexture()));
    mApp->display();

The questions:
Is that code right?
How does views work? When I change some parameters of it I have to reinitialize render target and/or window with same view:

         mCamera->zoom(0.96);
        //mApp>setView(*mCamera);  // Required?
        mRenerTarget->setView(*mCamera);

Should I set view to render target only (everything is drawn onto it first)? And why should I setView again and again after any change to the view object? If sf::Window (abstract) takes in arguments address of the view why doesn't it track the changes inside the object?

And the last: when I want to use sf::Window::convertCoords, should I call it from renderTarget (sf::RenderTextuer) or mApp (sf::RenderWindow)?

Thanks ;)
« Last Edit: October 20, 2012, 01:26:07 am by Ockonal »
Developing 2d engine&game using: sfml, box2d, librocket, spark2, zoom&zoost, thor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::View and render target
« Reply #1 on: October 20, 2012, 09:02:43 am »
First, a general comment: why are you using pointers everywhere? None of these objects need to be allocated dynamically. You're complicating your code uselessly.

Quote
Is that code right?
Right for what? We don't even know what you're trying to do :)

Quote
How does views work?
There's the 1.6 tutorial, the 2.0 documentation and a very good article on the wiki. I can't explain it better in a forum post.

Quote
When I change some parameters of it I have to reinitialize render target and/or window with same view
Quote
why should I setView again and again after any change to the view object?
It is explained in the documentation: render-targets handle views by value, which means that they take a copy and not a reference/pointer of your own view. Which means that you must call setView everytime it changes.

Quote
when I want to use sf::Window::convertCoords, should I call it from renderTarget (sf::RenderTextuer) or mApp (sf::RenderWindow)?
Call it where it makes sense for your application. We can't answer this question for you.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
Re: sf::View and render target
« Reply #2 on: October 20, 2012, 01:25:30 pm »
I've written a tutorial of a way to look at sf::View and I tried to cover different use cases: Using sf::View
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ockonal

  • Jr. Member
  • **
  • Posts: 58
    • ICQ Messenger - 449909010
    • View Profile
    • WinCode
Re: sf::View and render target
« Reply #3 on: October 20, 2012, 06:31:14 pm »
Wow, the tutorial is great! Thanks ;)
Developing 2d engine&game using: sfml, box2d, librocket, spark2, zoom&zoost, thor

 

anything