SFML community forums

Help => Window => Topic started by: reethok on May 16, 2013, 08:05:33 pm

Title: Multiple views in a RenderWindow
Post by: reethok on May 16, 2013, 08:05:33 pm
How do I put multiple views to a render window?

I have this code:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow ventana(sf::VideoMode(300, 450, 32), "views",
                             sf::Style::Close);
    ventana.setFramerateLimit(30);

    sf::Sprite sprite;
    sf::Texture texture;

    texture.loadFromFile("asdf.png");
    sprite.setTexture(texture);

    sf::Sprite s_p;
    sf::Texture t_p;

    // 18x19
    t_p.loadFromFile("per.png");
    s_p.setTexture(t_p);

    sf::View view1(sf::FloatRect(0, 0, 149, 149));
    sf::View view2(sf::FloatRect(0, 0, 149, 149));
    view2.setRotation(90);
    sf::View view3(sf::FloatRect(0, 0, 149, 149));
    view3.setRotation(180);
    sf::View view4(sf::FloatRect(0, 0, 149, 149));
    view4.setRotation(270);
    sf::View view5(sf::FloatRect(0, 0, 149, 149));
    view5.setRotation(45);
    sf::View view6(sf::FloatRect(0, 0, 149, 149));
    view6..zoom(0.5f);

    for(int i = 0; i < 3600; i++)
    {
        ventana.clear();
        ventana.draw(sprite);
        ventana.display();
    }
}

I want to have a 150x150 world, in wich i have only a background image or color (preferably, for the 45° degree rotated view), and to set the views in the window this way:

(http://imageshack.us/a/img197/5763/pos.png)

What I want is to display the same world with different perspectives.

Thank you.
Title: Re: Multiple views in a RenderWindow
Post by: Laurent on May 16, 2013, 08:18:24 pm
Read the tutorial on views, it has an exemple with split screen.

Note that you will have to draw your world 6 times, once in every view.
Title: Re: Multiple views in a RenderWindow
Post by: reethok on May 16, 2013, 08:46:04 pm
Thanks for your answer. I have read the tutorial but it dont says how to draw the multiple views. Views are drawable items? So i draw each one in a different position of the window?
Title: Re: Multiple views in a RenderWindow
Post by: Nexus on May 16, 2013, 09:15:52 pm
I have read the tutorial but it dont says how to draw the multiple views.
Read the sections "Defining how the view is viewed" and "Using a view". You have to draw the scene multiple times, every time with a different view.
Title: Re: Multiple views in a RenderWindow
Post by: Laurent on May 16, 2013, 10:08:24 pm
window.setView(view1);
window.draw(world);

window.setView(view2);
window.draw(world);

window.setView(view3);
window.draw(world);

etc.
Title: Re: Multiple views in a RenderWindow
Post by: reethok on May 17, 2013, 01:02:53 am
Finally I did it. Thank you very much. Now my issue is that... the rotated views are a little distorted...

(http://imageshack.us/a/img803/7677/pantallazo1a.png)

I think it is becouse the viewport dont have a exact size:

    sf::View view1(sf::FloatRect(0, 0, 150, 150));
    view1.setViewport(sf::FloatRect(0, 0, 0.5f, 0.33333f));

    sf::View view2(sf::FloatRect(0, 0, 150, 150));
    view2.setViewport(sf::FloatRect(0.5f, 0, 0.5f, 0.33333f));
    view2.setRotation(90);

    sf::View view3(sf::FloatRect(0, 0, 150, 150));
    view3.setViewport(sf::FloatRect(0, 0.33333f, 0.5f, 0.33333f));
    view3.setRotation(180);

    sf::View view4(sf::FloatRect(0, 0, 150, 150));
    view4.setViewport(sf::FloatRect(0.5f, 0.33333f, 0.5f, 0.33333f));
    view4.setRotation(270);

    sf::View view5(sf::FloatRect(0, 0, 150, 150));
    view5.setViewport(sf::FloatRect(0, 0.66666f, 0.5f, 0.33333f));
    view5.setRotation(45);

    sf::View view6(sf::FloatRect(0, 0, 149, 149));
    view6.setViewport(sf::FloatRect(0.5f, 0.66666f, 0.5f, 0.33333f));
    view6.zoom(0.5f);

I mean 0.33333f... etc. Should I use 1/3?
Title: Re: Multiple views in a RenderWindow
Post by: Laurent on May 17, 2013, 07:58:12 am
Quote
I mean 0.33333f... etc. Should I use 1/3?
It wouldn't change anything.
Title: Re: Multiple views in a RenderWindow
Post by: Nexus on May 17, 2013, 12:31:45 pm
I mean 0.33333f... etc. Should I use 1/3?
Just to make that clear, if you write it like this, you have an integer division. Therefore, 1/3 == 0.
Title: Re: Multiple views in a RenderWindow
Post by: reethok on May 17, 2013, 07:10:17 pm
I know, just fast writing.

So, what should I do to mantain a perfect proportion? :/