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

Author Topic: Adding GetX and GetY to drawable  (Read 2821 times)

0 Members and 1 Guest are viewing this topic.

Dig

  • Newbie
  • *
  • Posts: 31
    • View Profile
Adding GetX and GetY to drawable
« on: August 13, 2010, 10:35:11 pm »
I haven't put this as a feature request because I don't know how important it is to anyone else.

I'd like to have GetX() and GetY() methods in drawable because it then provides symmetry with the SetX() and SetY() methods which then can translate to properties in my scripting language (does that make any sense)

If I subclass drawable to add these then that won't work for Sprite, Text, Shape as of course they inherit from Drawable not my new Drawable.

Is there a sensible way to do this without modifying the SFML source?  Or is adding these methods to sf::Drawable directly the only real way to do it.

Any pointers on this would be great.


Cheers
Nick

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Adding GetX and GetY to drawable
« Reply #1 on: August 13, 2010, 10:45:09 pm »
Which scripting language and which binding library do you use? Good ones (boost.python for Python, luabind for Lua) let you bind properties that do not directly have getters, it's not a problem.

And why don't you bind a position property instead, which would return a vector with x and y members?

Anyway, I'm not going to add two functions just to suit your needs ;)
Laurent Gomila - SFML developer

Dig

  • Newbie
  • *
  • Posts: 31
    • View Profile
Adding GetX and GetY to drawable
« Reply #2 on: August 14, 2010, 12:04:30 am »
Quote

Anyway, I'm not going to add two functions just to suit your needs Wink


Wouldn't expect you to this is why its not a feature request.

I'm using lua and tolua++

What I'm wanting to achieve is something like:
Code: [Select]
if mySprite.x < 200 then

Or whatever, with the SetX() method you can do assignment (and map the property to the SetX() method so that works).  

Code: [Select]
mySprite.x = 200

But with no GetX method then I can't read the x position without using:

Code: [Select]
if mySprite:GetPosition().x  < 200 then

The problem is more the symmetry of it I guess.  And one of the reasons for adding a scripting layer is to be able to access SFML and my engine specific code with a simplified syntax.  Get a vector if I want a vector, get a single axis if I only want a single axis.  sf::Drawable already has this to some extent (SetPosition, SetScale takes a vector or two floats, and there are SetX and SetY, SetScaleX and SetScaleY methods).  But stops short for some reason (no GetX or GetY, no GetScaleX or GetScaleY  methods).

I'm sure there is a good reason for it, everything in SFML is clear, concise and well thought out, however I'd like those accessor functions.

I can of course hand code some helper functions at the binding level to make the position directly readable but was wondering if there is a way to 'hook' into a class at that level and add methods...

Maybe its more a C++ archeture questions than a SFML specific question.

Or maybe its too early on a Saturday for me  :roll:

NOTE - After writing all of this I realised could bind it so that we have:

Code: [Select]
mySprite.position.x

for getting and setting using the existing methods and that would actually suit me fine.  I'd still be curious for an approach to add methods to a base class (its probably not possible) but I can work with how things are for now.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Adding GetX and GetY to drawable
« Reply #3 on: August 14, 2010, 11:20:28 am »
It's easy to add members to a script class using non-member functions.

Using Luabind (which I recommend), it would be straight-forward:
Code: [Select]
float DrawableGetX(const sf::Drawable& d)
{
    return d.GetPosition().x;
}
void DrawableSetX(sf::Drawable& d, float x)
{
    return d.SetX(x);
}

class_<sf::Sprite>("Sprite")
    .property("x", &DrawableGetX, &DrawableSetX);


Using other binding libraries, you can most likely do the same thing, but you will probably need to handle a lua_State* rather than directly a sf::Drawable and a float.

Quote
I'm sure there is a good reason for it

Simple: in C++ you can already access X and Y in one line of code (sprite.GetPosition().x), there's no need for a shortcut. This is different for the setter, that's why there is a shortcut for it.

Quote
mySprite.position.x

This is what I suggested in my first post, but I don't think that this is going to work. The problem is that "mySprite.position" will return a temporary sf::Vector2f, and setting its x will have no effect on the sprite since SetPosition will never be called.
As far as I know, the only binding library that handles this properly is my other project, CAMP 8)
Laurent Gomila - SFML developer

Dig

  • Newbie
  • *
  • Posts: 31
    • View Profile
Adding GetX and GetY to drawable
« Reply #4 on: August 14, 2010, 01:03:43 pm »
The main reason I'm not using luabind is that it would be the only part of my engine that requires boost.  

I'll add properties using external functions similar to what you are suggesting.

Hopefully I'll dream up something clever so I can access (read / write) position as a property I like the idea now:

Code: [Select]
mySprite.position.x = 200
otherX = mySprite.position.x
mySprite.scale.x = 2.0
otherScaleX = mySprite.scale.x


etc.,etc.,

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Adding GetX and GetY to drawable
« Reply #5 on: August 14, 2010, 02:02:43 pm »
Quote
Hopefully I'll dream up something clever so I can access (read / write) position as a property I like the idea now

I don't know if it's worth the effort, it won't be easy to implement.
Laurent Gomila - SFML developer