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

Author Topic: Views  (Read 2050 times)

0 Members and 1 Guest are viewing this topic.

Reborn

  • Newbie
  • *
  • Posts: 25
    • View Profile
Views
« on: April 06, 2014, 12:21:08 pm »
Hello,
i want to use Views as Viewports. For example perhaps a Pic:
Without window.setView()

Its a window with 640x480 Pixels. I want to set a Viewport to the half of the x axis.
With window.setViewport(0, 0, 320, 480) for example, and the window just shall show the content in this area for the next draws until the next window.setViewport call occurs.

I have played a litte with views, but i didn't understand it, and i read this:
http://sfml-dev.org/tutorials/2.1/graphics-view.php

My idea is, to draw everything in viewports, so i simply can set the coordinates for all Sprites in it.
« Last Edit: April 06, 2014, 12:45:46 pm by Reborn »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Views
« Reply #1 on: April 06, 2014, 02:38:05 pm »
There's an example in the tutorial that does exactly what you want. So what's your problem?
Laurent Gomila - SFML developer

Reborn

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Views
« Reply #2 on: April 06, 2014, 02:43:50 pm »
I don't understand views. When i do it like in this tutorial, the sprites allways get streched. And I not want set percentage points.

When i use this:
sf::FloatRect visibleArea(100, 100, window_width, window_height);
window.setView(sf::View(visibleArea));
my Sprites move to the left top corner, when i change window_width to window_width * 2, the sprites gets streched, why?
« Last Edit: April 06, 2014, 02:48:56 pm by Reborn »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Views
« Reply #3 on: April 06, 2014, 03:43:32 pm »
The "visibleArea" rectangle is the area of the 2D world that will get displayed in your window. So if you move it, everything moves in the opposite direction. If you resize it, everything will be stretched.

From what I understand of your post, you don't really want to play with the visible area of the view, but rather with the viewport.
Laurent Gomila - SFML developer

Anrock

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Views
« Reply #4 on: April 07, 2014, 11:25:14 am »
Is there any simple way make size of view equal to size of viewport to get 1:1 scale?
Say i want split my 1000x1000 window to two vertical parts. I make view with 0, 0, 0.5f, 0.5f, but then i have to manualy calculate size of this view depending on resolution of whole window and viewport size ratios like that
void OperatingSystem::adjustPanel(const sf::RenderWindow& win, sf::View& view)
{
        view.reset({std::round(view.getViewport().width / 2),
                        std::round(view.getViewport().height / 2),
                        std::round(win.getSize().x * view.getViewport().width),
                        std::round(win.getSize().y * view.getViewport().height)});
}
« Last Edit: April 07, 2014, 11:41:45 am by Anrock »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Views
« Reply #5 on: April 07, 2014, 08:29:41 pm »
Yes, you have to calculate the size of the view according to the viewport factors (or the other way round). It's not trivial but I think it's simple enough (one multiplication) ;)
Laurent Gomila - SFML developer

Reborn

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Views
« Reply #6 on: April 26, 2014, 03:33:52 pm »
I extended the RenderTarget-Class now by a new function:
void RenderTarget::setViewportt(const sf::FloatRect& rect)
{
        sf::Vector2u oldSize = getSize();
        m_view.reset(sf::FloatRect(0.f, 0.f, rect.width, rect.height));
        m_view.setViewport(sf::FloatRect(rect.left / oldSize.x, rect.top / oldSize.y,
                                                                         rect.width / oldSize.x, rect.height / oldSize.y));
        m_cache.viewChanged = true;
}
Now i exactly have what i want, i can make offsets for my sprites with a cutting off at the edge. But my question now is, is that a good coding style? Perhaps i do something wrong to reach my goal? Or i do something wrong when i think i have to reach this goal?

My Plan is it, to create Surface-Classes for everything i want draw. For example a Field Surface-Class called SurfaceField needs to dispaly a Field and the figures on it, then i can simply write:
int main()
{
        SurfaceField sf;
        SurfaceChat sc;
        renderWindow.setViewportt(sf::FloatRect(50, 50, 200, 200));
        sf.draw(renderWindow);
        renderWindow.setViewportt(sf::FloatRect(100, 100, 200, 200));
        sc.draw(renderWindow);
}
 
So i simply can move my chat and my field without have to worry about the offsets in the different Objects.
« Last Edit: April 26, 2014, 03:47:39 pm by Reborn »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Views
« Reply #7 on: April 26, 2014, 04:03:05 pm »
The code inside the function seems fine, but the function template is not because you're modifying an SFML class to do something that could very easily be implemented using the class' public API.  This forces you to learn implementation details you shouldn't have to think about and might get wrong, and makes it non-trivial for you to upgrade to future versions of SFML.  It would be better as a simple utility function somewhere inside your main.cpp.

On a higher level, I'm not really clear what your goal is (among other things, I don't know what you mean by "surface" and "field"), so I can't say if this is the right way to go about it or not.  But if you expect to do a lot of what you described in your first post, you may want to consider making a NonScalingView class deriving from sf::View with a constructor that does exactly what your utility function does.
« Last Edit: April 26, 2014, 04:05:06 pm by Ixrec »

Reborn

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Views
« Reply #8 on: April 26, 2014, 04:14:10 pm »
My problem is, that i normaly calculate the x-achis in my Surfaces everytime to the relativ absolutly 0 Point which is in the upper left corner. But my field have for example a offset from [200, 200] pixels relative to the absolutly 0 point and the unit on this field have a offset from again [200, 200] pixels relative to the field 0 point. I don't want implement the offsets in the classes and i don't want allways give the offsets about parameters to the instance.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Views
« Reply #9 on: April 26, 2014, 05:14:59 pm »
That sounds like a completely unrelated problem, and I can't really tell what you're trying to do or asking for help with, so you may want to start a separate thread with a much more thorough explanation and a complete/minimal code example.

 

anything