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

Author Topic: Setting pixels per unit  (Read 2552 times)

0 Members and 2 Guests are viewing this topic.

Winski

  • Newbie
  • *
  • Posts: 2
    • View Profile
Setting pixels per unit
« on: October 12, 2013, 09:18:59 pm »
I've been searching for like 2 hours and I couldn't find the answer - maybe I didn't formulate the inquiry properly, so sorry in advance if the topic is doubled.
I am messing around with SFML and I wanted to improve my drawing loop. For now, when I want to draw some sprite/tile/whatever I need to use pixels as the position for them. What I would like to have is to set something like: pixelToUnitRatio(100); which would mean that when I create a sprite of size 100px*100px I receive it as an 1.0f*1.0f object (for now I would get 100.0f*100.0f). This would help me with creating bigger worlds - units are easier to read and each tile could have a size of 1.0f*1.0f. How to solve that?
Thanks in advance

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Setting pixels per unit
« Reply #1 on: October 12, 2013, 09:50:20 pm »
SFML doesn't provide such unit conversions, but it's rather simple to do, you just need your own layer of abstraction between the draw logic and your world logic. There are many ways to achieve this, thus it's hard to formulate a "generic" solution. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Setting pixels per unit
« Reply #2 on: October 12, 2013, 09:50:50 pm »
Use sf::View and scale it accordingly.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Winski

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Setting pixels per unit
« Reply #3 on: October 12, 2013, 10:00:36 pm »
@eXpl0it3r: That's what I am doing so far, I was just wondering if I can make it easier
@Nexus: That's why I thought of this :) - I wanted to use something more like view of size (8,6) instead of (800, 600), which means 10 tiles in x, 8 tiles in y instead of 800px in x, 600px in y - this can be done by view.scale(1/(pixelsPerUnit)); but I would need to apply the same scale to each sprite during creation, which would be very inconvenient for me.
Anyway, thanks you both for immediate help, problem is solved :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Setting pixels per unit
« Reply #4 on: October 12, 2013, 10:17:08 pm »
but I would need to apply the same scale to each sprite during creation, which would be very inconvenient for me
Why? When you apply the same scale to the drawable objects and the view, there is no need for a view in the first place...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Setting pixels per unit
« Reply #5 on: October 12, 2013, 11:54:40 pm »
Or the more fundamental question:

Is it really that much harder to write (800,600) than (8,6)?  It seems like inventing and implementing a whole new unit would be far more irritating, time-consuming and error-prone than writing some zeroes at the end of every number.

The only sensible thing I know of that comes close to this (aside from the obvious sf::Views) would be a tilemap class that has a built-in fixed size for each tile, so when it draws tiles[2][1] it draws it at 200,100.  I hope that's what the OP actually wants.
« Last Edit: October 12, 2013, 11:58:09 pm by Ixrec »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Setting pixels per unit
« Reply #6 on: October 13, 2013, 12:04:17 am »
Is it really that much harder to write (800,600) than (8,6)?
I think different units become especially convenient as soon as they are used in the user interface, e.g. to display a distance between objects. And yes, a few magnitudes may really make a difference, since we're not so used to numbers in decimal scale.

On the other hand, such measures concern game logic only, and the correct place to scale the units would be when passing the values to the (ideally) independent graphics module. That's goes towards eXpl0it3r's idea...
« Last Edit: October 13, 2013, 12:05:51 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: