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

Author Topic: How to use the ConvertCoords() function proberly  (Read 2552 times)

0 Members and 1 Guest are viewing this topic.

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
How to use the ConvertCoords() function proberly
« on: September 28, 2010, 05:48:17 pm »
I'm trying to do some dynamic drawing. Means: Everything that is on a position you can't see at the moment won't be displayed. For this purpose I'd like to ask, how to use the ConvertCoords() function. Without views I'd do:
Code: [Select]

bool DynamicWindow::IsInSight(const sf::Drawable &Drawable)
{
int X = Drawable.GetPosition().x;
int Y = Drawable.GetPosition().y;

if(X < 0 || X >= m_Width || Y < 0 || Y >= m_Height)//check if the Drawable can be seen
return false;
else
return true;
}

where m_Width and m_Height the size of the displayed window is. But this doesn'T work with views! Help please :P

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
How to use the ConvertCoords() function proberly
« Reply #1 on: September 29, 2010, 04:29:07 pm »
No one knows how to do that?  :shock:

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
How to use the ConvertCoords() function proberly
« Reply #2 on: September 29, 2010, 04:42:04 pm »
You don't need convert coods. You are calculating if a point is inside the rectangle (0, 0, width, height). With views, you shoudl rather check if the point is insde the rectangle(view.x, view.y, view.x+view.width, view.y+view.height).

Hope this helps ;)

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
How to use the ConvertCoords() function proberly
« Reply #3 on: September 29, 2010, 07:39:15 pm »
Thx :)
Works!

But with checking the position it's not as it should be.
I check the Position, so only the top left corner of the sprite.
Even though the sprite is in sight it isn't displayed because the position of the sprite is outside the view.

I tried something like this to get rid of this problem:

Code: [Select]

bool IsInSight(const sf::Drawable &Drawable, sf::View &View)
{
int X = Drawable.GetPosition().x;
int Y = Drawable.GetPosition().y;
int Width = Drawable.GetScale().x;
int Height = Drawable.GetScale().y;

sf::FloatRect Rect(X, Y, X+Width, Y+Height);
sf::FloatRect ViewRect = View.GetRect();

//check if the sprite is in the view
if(ViewRect.Intersects(Rect))
window.Draw(Drawable);
}


But it's just the same :-/ Suggestions?
Finn

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
How to use the ConvertCoords() function proberly
« Reply #4 on: September 29, 2010, 11:24:14 pm »
When you have problems like this, try to print the values you are working with. If you print your Widht and Height you will notice that they are a value near 1.f.

That's because you are getting the scale percentage modifier of the sprite, not the width nor the height. You can't get those values from a sf::Drawable.

You can get the width and height of a sf::Sprite getting it's sf::Image and checking it's width and height.

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
How to use the ConvertCoords() function proberly
« Reply #5 on: September 30, 2010, 07:23:49 am »
Thx :) Dynamic drawing is only be used for sprites, so no problem :) But why doesn't it work with Drawables?

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
How to use the ConvertCoords() function proberly
« Reply #6 on: September 30, 2010, 08:53:34 am »
Quote from: "Finn"
Thx :) Dynamic drawing is only be used for sprites, so no problem :) But why doesn't it work with Drawables?

Because you can't get the size of drawables. That's a base class object that just provides a virtual render function. Other objects derive from drawables and then they can be painted.

 

anything