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

Author Topic: SFML 2 views  (Read 5218 times)

0 Members and 1 Guest are viewing this topic.

chessguy

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
SFML 2 views
« on: August 09, 2012, 12:11:02 am »
Greeting all. Learned much in both C++ and SFML since I have been here last. My current project, though, would be much easier with views.

However, even after the doc and a tuturial I found regarding it (there was a forum post on here about it), and some attempts myself, I am very befuddled. In the game engine I had used previously, views were still a bit confusing at times, but simply consisted of variables like width/height of the view/viewport, and something along the lines of position. (top left corner, that is).

SFML views, though, have got me confused. So, I suppose, I can try to explain what I want to do.

What I want to do is, make the view the same size as the rendering region (and same for the viewport, but that is default). I then want this view to continue moving southEast-ish - that is, down and right - to follow a slope. Simply enough, you would think. At first, I first made it as simply sf::View view, and so center was 500/500. I later re-set the center, (as view height/2, view width/2) and this was better. (previously, a line that started at 0,0 was near the right of the screen, still at the top, when it should have been the top left - the view was somehow shifted right), but now looked as expected. Anyhow, this slope of mine is randomly generated. as such, if you use set incriments, you may either end up to far above, or to below, the slope. However, the camera will actually follow the player. I may impliment adding a zoom so the slope always stays in few, we shall see. Anyhow, currently (due to 'dropoffs" in the slope), I make it so if the view is further down then the highest point currently in view (more or less - technically, it is a point that was the highest when the last highest goes out of view), so when the player goes out of view, I can kill them and it isn't so ugly.

When I added this, though, the lines were there one second and gone the next. The y position went from 300.2 to -299.8, causing it to go out of view, I beleive. I just have no idea why. This prompted me to make this point - I figure things will all be easier if I understand just how allthe aspects of views work as they are implimented in SFML, specifically how the center works - what is it set relative to? How does it affect transformations?

So I suppose I am asking if somebody can go over each aspect of views and explain quite how they work, with emphasis on coordinates and the center.

I realize that is a bit general, but I feel it would all be useful information on a concept that, if the tuturial is any indication, and my own confusion at the lack in the documentation (setCenter is "set the center of the view", a fact I understand from the name but I don't really know exactly what that does, for exactly what it is relative from?)

Thanks if you can help clear this up for me at all.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: SFML 2 views
« Reply #1 on: August 09, 2012, 12:27:06 am »
I was also confused with view's that's why I researched a bit and then created this tutorial, but as you stated you've probably already read that one. What didn't you understand? I made examples and images which show exactly what the center of the view relates to and how the coordinate system gets affected etc. and added even a full blown example, so I'm unsure what's missing. :-\

Also in most use cases that easiest way to deal with views is just to call move() and not care about where the center point now is located. ;)

Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: SFML 2 views
« Reply #2 on: August 09, 2012, 12:52:51 am »
Center is the point that is currently in the center of view in sfml coordination system.
Size is the size of view's rectangle, in the center of that rectangle is the center of the view.
Center of view's rectangle is the center of the view.
So your view is streched between 4 points:
TopLeft:
x = (center.x-size.x/2)
y = (center.y-size.y/2)
TopRight:
x=(center.x+size.x/2)
y=(center.y-size.y/2)
BottomRight:
x=(center.x+size.x/2)
y=(center.y+size.y/2)
BottomLeft:
x=(center.x-size.x/2)
y=(center.y+size.y/2)

sf::View is created by default with size of 1000x1000 and center at (500,500) maybe try initialize it with your render windows default view first.
Can you show some code examples of your problem?
« Last Edit: August 09, 2012, 12:57:51 am by FRex »
Back to C++ gamedev with SFML in May 2023

chessguy

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
Re: SFML 2 views
« Reply #3 on: August 09, 2012, 04:45:19 am »
I was also confused with view's that's why I researched a bit and then created this tutorial, but as you stated you've probably already read that one. What didn't you understand? I made examples and images which show exactly what the center of the view relates to and how the coordinate system gets affected etc. and added even a full blown example, so I'm unsure what's missing. :-\

Also in most use cases that easiest way to deal with views is just to call move() and not care about where the center point now is located. ;)

Your tuturial was the main one I found. While you state it is fully explained - well, it probably is, but I don't quite understand it. For example, you said that the center is defined from the center (means?) and something about a reversed X axis... I didn't get it at all at that point.

Oh... I suppose I could do that with some maths, yes. Still, the way I did it was like...

if (view goes passed boundary)
set view center to current view center's x, boundary line - half view window width

Wait a second... I think I just found my error.

Ahah. It works - albiet, it looks ugly, but concept proven :)

I think your right, though. Move ought to be <= easiness of positioning the center.

Anyhow - if I understand this correctly... the center of the view is, well, automatically in the center of the view, and thus setting the center moves it. (and thus not like setting the origin). Is that correct?

In that case, zoom and rotate make sense. And if you create the view using a center and size, then is just creates the rectangle as if from 0,0, then move it till the center is equal to the center parameter, correct?

In that case, my only questionns are, how/why are the coordinate systems different (flipped x for example), does the window defaultly use a view, (follow-up to last) how do you control, when using views, what is drawn in what could be called the physical location of the render window? (for example, if a view does not cover the entire screen, and something is drawn within the window regio ncoordinates, will it be drawn there? If so, how could this be prevented? Finally, how exactly it determines what gets rendered (rasterized?) where.

Sorry, lots of questions :)

TheVirtualDragon

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Control + Alt + Delete!
Re: SFML 2 views
« Reply #4 on: August 23, 2012, 03:06:37 pm »
Thanks for the tutorial eXpl0it3r, it's really helpful - especially the part about view ports.