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

Author Topic: mapPixelsToCoords vs mapCoordsToPixels  (Read 1555 times)

0 Members and 1 Guest are viewing this topic.

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
mapPixelsToCoords vs mapCoordsToPixels
« on: March 22, 2020, 07:56:44 pm »
Hello,

I was wondering if someone can explain exactly why you would use one over the other because I am having mixed results and I'm never 100% certain why. Obviously, they are opposites but I just don't understand why you would need mapCoordsToPixels? In which situation would you need this?

When it comes to the mouse sometimes it just isn't accurate, even getting it relative to the window. I usually only use these functions for the mouse (I'd like to know another reason). This seems to be when you use a view, which makes sense. But sometimes pixelsToCoords give me the accurate mouse position (say I'm trying to highlight the correct tile) and other times it doesn't work and I'll use coordsToPixels and it works.

Some better clarity ande detail would be appreciated. Because I find the explanation on it confusing despite how obvious it sounds reading the function name. Also are displaying isometric tiles going to get you using one over the other?
« Last Edit: March 22, 2020, 07:58:43 pm by SFMLNewGuy »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: mapPixelsToCoords vs mapCoordsToPixels
« Reply #1 on: March 22, 2020, 09:25:18 pm »
To clarify a little the terms first:
pixels: the actual pixels on the display*.
co-ords: the positions within the view; the values you wish to represent each part of the window (these can be almost any arbitrary).

When you set a position or do anything in the graphics module, you are specifying the co-ords. These, later get converted to pixels when displaying on the display.
The mouse position, however, (and also things like the position or size of the window) are always in pixels.

Remember that co-ords is relative to the window but the window is relative to pixels.

If your view is the default one for the window, the co-ords will match the pixels, especially if you "reveal more when resizing the window", although co-ords can specify fractions of a pixel (in that case) whereas pixels cannot (which is why pixels are integers and co-ords are floats).

An extreme (but not too complicated) example would be:
creating a window size of 800 x 600 (pixels),
setting its view to a size of 1 x 1 (co-ords).

This means that all positions in co-ords (stuff you draw, for example) will be between 0 and 1 both horizontally and vertically and the centre of the window would be (0.5, 0.5). e.g. top-left would be (0, 0), top-right would be (1, 0), bottom-right would be (1, 1) and bottom-left would be (0, 1).

If your mouse was exactly in the centre of the window, it's position would be in pixels (400, 300) e.g. half of the window size in pixels.
Converting those pixels to co-ords (using mapPixelsToCoords) would give you (0.5, 0.5) instead, which is the centre within the view (as mentioned above).

If you want to draw something at the centre of the window, which is where the mouse is in this example, you would set it's position to (0.5, 0.5) - the view co-ords. If you set it the values in the pixel position, it would still set it using the co-ordinate system of the view and, with 1 being the width of the window in co-ords, 300 would be 300 window widths!

Converting co-ords to pixels is argueably less common but is not completely useless. However, it's you'd need to fully understand all this view stuff first before you'd think of a reason you'd need it ;D

* this may be affected by DPI settings.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: mapPixelsToCoords vs mapCoordsToPixels
« Reply #2 on: March 24, 2020, 02:40:52 am »
Hey,

Sorry to get back a little late. I appreciate the response and example. I'm going to have to read it a few times to really grasp it haha. I know when you are setting up a view for split-screen for example, you would use the 0-1 to get the view to appear where you want.

I'm pretty sick right now and having trouble thinking, but I just wanted to say thanks for the help.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: mapPixelsToCoords vs mapCoordsToPixels
« Reply #3 on: March 26, 2020, 10:13:13 pm »
I should clarify a little to avoid mixing up two parts: views and viewports.


View
A view is a way of setting up the co-ordinate system for a rectangular area (usually the entire window - I'll elaborate a bit later). Basically, the stuff I explained above is about setting up views. The assumption should be that the views I previously mentioned were all applied to the entire window.

Viewport
A viewport is a way of changing the actual output rectangle from the entire window to a smaller part of it. For the splitscreens (as you mentioned), you could use two viewports - one for each side. Everything drawn is clipped to its viewport (in the same way it's clipped to the window otherwise it would be drawing outside the window).

Elaboration on Views
Now that I've explained a little about what viewports are, I can explain why I said "rectangular area" above. Normally, the viewport of a view is the entire window so it looks like there isn't a viewport. Also, normally, the view is the same "size" (in co-ordinates) as the window so looks like there isn't a view.
If you don't change a viewport, it's always the same size as the window. Therefore everything drawn to a window/view will be shown unless outside of the window.
When "resizing" a view, you don't actually change the size, you only change the co-ordinate system (the numbers used to represent positions).



Maybe I'll throw some pictures together to illustrate my point.
However, there is a tutorial on SFML's Wiki that explains views with pictures already:
https://github.com/SFML/SFML/wiki/Tutorial%3A-Using-View
Maybe that will give a better insight.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*