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

Author Topic: SFML 2.0 View for Scrolling...  (Read 7222 times)

0 Members and 1 Guest are viewing this topic.

Orezar

  • Newbie
  • *
  • Posts: 38
    • View Profile
SFML 2.0 View for Scrolling...
« on: March 21, 2012, 07:42:08 pm »
Hey,

my question is, (how) can I use a View for a Button List, with a Scrollbar, to scroll back and forth between these buttons? Is this possible with an View?

I try a little bit out, but the problem is that if I make an Sprite Object on the Position (100, 100) with the Size (64, 64), and a View Object, Reset it on the Values: (100, 100, 64, 64), the whole Screen is filled with the Sprite Object...

Is it possible to make a View with a constant Size without resize the Sprites in her? If I use the Zoom Function the View is resizing or I've missunderstood what?

Because I don't have an Idea how to make it different, the only Idea that I have is to Resize the Objects if the scrollbar is on there Object Position, but this is not really the best solution,
and if I only Draw the Objects in the Area of the scrollbar the other Objects disappear at once without having an transition...

Its a bit hard to explain it in english, but I hope you understood me nevertheless...

Thanks in advance!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SFML 2.0 View for Scrolling...
« Reply #1 on: March 21, 2012, 07:49:54 pm »
The view defines what part of the world is mapped to the entire render target. So the smaller your view is, the more stretched it is.

To change the destination area of the view you must play with its viewport.
Laurent Gomila - SFML developer

Orezar

  • Newbie
  • *
  • Posts: 38
    • View Profile
SFML 2.0 View for Scrolling...
« Reply #2 on: March 21, 2012, 09:02:41 pm »
So it is not possible to make a view with a constant View without streching an object?

I'm experimenting with the Function SetViewport(...) from the Class View but I don't understand the functioning, if I use it like this:

Code: [Select]
view.SetViewport(sf::FloatRect(sprite.GetPosition().x, sprite.GetPosition().y, 64, 64)); // 64 = Width and Height of Sprite

I become an wounderfull black screen, if I erase the Positions from the Parameter and set it on Zero, like these:

Code: [Select]
view.SetViewport(sf::FloatRect(0, 0, 64, 64)); // 64 = Width and Height of Sprite

I become an black screen, if I set it like in the Comment of this Function
described on (0, 0, 0.5, 1) I see the Rectangles on screen but a litte bit smaller... what does the values 0.5 and 1 means... Not the size of the Viewport?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SFML 2.0 View for Scrolling...
« Reply #3 on: March 21, 2012, 09:09:28 pm »
What the values mean is explained in the doc. It's a ratio of the window size, so it's always between 0 and 1 (1 being full size).

If viewport and view rect are the same size, nothing will be stretched.
Laurent Gomila - SFML developer

Orezar

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: SFML 2.0 View for Scrolling...
« Reply #4 on: March 25, 2012, 04:41:30 pm »
Hey,

first, cool new design of the forum. :)

I looked in the documentation and understand that the 0 and 1 from the Viewport
Function is only a factor, and I've become it stretchless after trying lot of numbers...

But there are still two problems for me...  the first is that the View + the Sprite position is on
the Top Left corner on the Screen, and I really don't understand why, and how to change it...

My Code:

        // ...

        // Setting position of Sprite
        sprite.SetPosition(400, 400);

        // View Object
        sf::View view;

        // Resseting the View
        view.Reset(sf::FloatRect(400, 400, 64, 64));

        // Setting the Viewport
        view.SetViewport(sf::FloatRect(0, 0, 0.1, 0.1));

 

And the other question, is there an way to calculate the Viewport factor? :P

Thanks in advance... :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: SFML 2.0 View for Scrolling...
« Reply #5 on: March 25, 2012, 08:20:37 pm »
Quote
the first is that the View + the Sprite position is on
the Top Left corner on the Screen, and I really don't understand why, and how to change it...
The top-left corner of the view is at (400, 400). Your sprite is at (400, 400). So your sprite is on the top-left corner ;)

Quote
And the other question, is there an way to calculate the Viewport factor?
Calculate according to what?
Do you mean "factor = position / window_size"?
Laurent Gomila - SFML developer

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: SFML 2.0 View for Scrolling...
« Reply #6 on: March 26, 2012, 02:00:26 am »
Are you using window.setView ?
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Orezar

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: SFML 2.0 View for Scrolling...
« Reply #7 on: March 26, 2012, 05:02:30 pm »
@mateandmetal  Yes, I used it, I only forget it on the Code of the posting before...

@Laurent

Quote
The top-left corner of the view is at (400, 400). Your sprite is at (400, 400). So your sprite is on the top-left corner ;)

No, I don't mean the left corner of the View, I mean that the Sprite is on the Left Corner of the Window... like this:



Do not be suprised, in this Picture I'm using still wrong factors...

With factor I mean the third and forth Parameter of the SetViewport(..., ..., factorX, factorY) Method...
I know now how to calculate the Factor:

factorX = Sprite Width / Window Width
factorY = Sprite Height / Window Height

And it works, nothing is streched...

The only Problem that still exist for me is the Problem with the Position of the View and the Sprite...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: SFML 2.0 View for Scrolling...
« Reply #8 on: March 26, 2012, 06:16:18 pm »
Quote
No, I don't mean the left corner of the View, I mean that the Sprite is on the Left Corner of the Window...
If the viewport starts at (0, 0) then the top-left corner of the view and the window are the same.
Laurent Gomila - SFML developer

Orezar

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: SFML 2.0 View for Scrolling...
« Reply #9 on: March 26, 2012, 08:32:02 pm »
But if I set the Position of the Viewport the sprite is not longer drawed on the Window...


view.SetViewport(sf::FloatRect(400.0, 400.0, ..., ...));     // Sprite is not drawed
view.SetViewport(sf::FloatRect(1.0, 1.0, ..., ...));         // Sprite is not drawed
view.SetViewport(sf::FloatRect(0.0, 1.0, ..., ...));         // Sprite is not drawed
 

In the Documentation ->

 

// Initialize the view to a rectangle located at (100, 100) and with a size of 400x200
view.Reset(sf::FloatRect(100, 100, 400, 200));

// Set its target viewport to be half of the window
view.SetViewport(sf::FloatRect(0.f, 0.f, 0.5f, 1.f));

 

Thats why I thought that by the resetting Method, the first two parameters are the Position of the View
and thats why I think that I'm not doing wrong, but it seems like I do...

The code:


// ... Create Sprite with size (64, 64) ...

// The View
sf::View view;

// Reset it, and set it on the location (400, 400) with the size (64, 64)
view.Reset(sf::FloatRect(400, 400, 64, 64));

// Setting Viewport
view.SetViewport(sf::FloatRect(0.0, 0.0,
sprite.GetGlobalBounds().Width  / App.GetWidth(),
sprite.GetGlobalBounds().Height / App.GetHeight()));

// Setting position after init the View...
sprite.SetPosition(400, 400);

 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: SFML 2.0 View for Scrolling...
« Reply #10 on: March 26, 2012, 08:53:57 pm »
Quote
view.SetViewport(sf::FloatRect(400.0, 400.0, ..., ...));     // Sprite is not drawed
view.SetViewport(sf::FloatRect(1.0, 1.0, ..., ...));         // Sprite is not drawed
view.SetViewport(sf::FloatRect(0.0, 1.0, ..., ...));
None of them are valid. The first one doesn't use factors, and the others put the viewport outside the window.

Quote
Thats why I thought that by the resetting Method, the first two parameters are the Position of the View
The parameter defines what part of the world the view sees. The position of the view itself on the window is defined by the viewport.

Read the doc and examples carefully, it seems like you totally misunderstand view ;)
Laurent Gomila - SFML developer

Orezar

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: SFML 2.0 View for Scrolling...
« Reply #11 on: March 27, 2012, 04:25:36 pm »
Ohhh... I understand it know...
I didn't know that the first and second Parameter of the SetViewport Method must given in factors too...

Know it works perfectly!
Thank you for your patience with me... :)

And thanks in general for this nice Libary! :D

 

anything