SFML community forums

Help => Graphics => Topic started by: SEnergy on June 09, 2012, 10:46:19 pm

Title: Get Size and Position of texture/sprite
Post by: SEnergy on June 09, 2012, 10:46:19 pm
Hello,

I'm trying to get position and size of sprite and texture but I'm getting this error:

error is on wsprintf...

Code: [Select]
Unhandled exception at 0x74df9d60 in Lucid Dreaming.exe: 0xC0000005: Access violation reading location 0x00000320.
part of code:

Vector2u PlayerSize = tBackground[0].getSize();
                                char test[512];
                                wsprintf(test, "%s", PlayerSize);
                                MessageBox(NULL, test, NULL, NULL);

and position is returnin NULL value

Vector2f PlayerPosition = sBackground.getPosition();
                                char test[512];
                                wsprintf(test, "%s", PlayerPosition);
                                MessageBox(NULL, test, NULL, NULL);

also is there any way to return them as int x,y?
Title: Re: Get Size and Position of texture/sprite
Post by: Laurent on June 10, 2012, 10:08:06 am
%s expects a null-terminated C string, nothing else (especially not a sf::Vector2).

Look at the documentation of the sf::Vector2 class to find out how to extract its x and y components.
Title: Re: Get Size and Position of texture/sprite
Post by: Zefz on June 10, 2012, 01:25:07 pm
What you want is to use either %u (unsigned integer) or %f (float) for each of the X and Y values in the vector.

For example:
Vector2u PlayerSize = tBackground[0].getSize();
char test[512];
wsprintf(test, "%u, %u", PlayerSize.x, PlayerSize.y);
MessageBox(NULL, test, NULL, NULL);
 
Title: Re: Get Size and Position of texture/sprite
Post by: Laurent on June 10, 2012, 01:28:18 pm
Or better: use the C++ functions instead of this unsafe C stuff.

std::ostringstream oss;
oss << PlayerSize.x << PlayerSize.y;
std::string str = oss.str();