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

Author Topic: [Solved] SetCenter  (Read 6709 times)

0 Members and 1 Guest are viewing this topic.

dewyatt

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
    • http://dewyatt.blogspot.com
[Solved] SetCenter
« on: July 06, 2008, 06:46:26 pm »
I know that SetRotationCenter, etc, have all been combined into SetCenter in 1.3.
So I changed all my calls to use SetCenter but I just noticed it doesn't seem to be working correctly.

It looks like everything is rotating around it's top-left corner.
Here's some code:
Code: [Select]

mSprite.SetImage(theGlyph->Texture);
mSprite.SetPosition(static_cast<float>(x), static_cast<float>(y));
mSprite.SetSubRect(sf::IntRect(0, 0, theGlyph->Texture.GetWidth(), theGlyph->Texture.GetHeight()));

mSprite.SetCenter(0.5f * theGlyph->Texture.GetWidth(), 0.5f * theGlyph->Texture.GetHeight());
mSprite.SetRotation(mRotation);
mSprite.SetScaleX(mHScale);
mSprite.SetScaleY(mVScale);

mWindow->Draw(mSprite);

It's from my sfmlTTF library (which is more appropriate for my game than sf::String).

Xylankant

  • Newbie
  • *
  • Posts: 32
    • View Profile
    • http://xylankant.xy.funpic.de
[Solved] SetCenter
« Reply #1 on: July 06, 2008, 07:23:57 pm »
well, you actually set your center at a top-left pixel of your sprite, so I'd say rotating around it's left corner is actually correct...

dewyatt

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
    • http://dewyatt.blogspot.com
[Solved] SetCenter
« Reply #2 on: July 06, 2008, 07:29:58 pm »
Sorry, wrong code.
Changed it.

Xylankant

  • Newbie
  • *
  • Posts: 32
    • View Profile
    • http://xylankant.xy.funpic.de
[Solved] SetCenter
« Reply #3 on: July 06, 2008, 07:38:43 pm »
mhhh, are you sure there is a GetWidth()-Method for Sprites?
I can't find one in the Doc, try using GetSize().x and GetSize().y instead...

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
[Solved] SetCenter
« Reply #4 on: July 06, 2008, 07:41:48 pm »
The problem is you're getting the width of the image rather than the sprite. (which is a sub-rectangle of the image)

EDIT: Also, for consistency reasons, shouldn't sf::Image also use the GetSize method?

dewyatt

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
    • http://dewyatt.blogspot.com
[Solved] SetCenter
« Reply #5 on: July 06, 2008, 07:47:35 pm »
Well I'm reusing the sprite.
And It's not really a sub-rectangle, it uses the full image:
Code: [Select]

mSprite.SetSubRect(sf::IntRect(0, 0, theGlyph->Texture.GetWidth(), theGlyph->Texture.GetHeight()));


The 'Texture' is an sf::Image.
This all worked fine with 1.2.

Xylankant

  • Newbie
  • *
  • Posts: 32
    • View Profile
    • http://xylankant.xy.funpic.de
[Solved] SetCenter
« Reply #6 on: July 06, 2008, 07:49:54 pm »
I don't know 1.2, but reading the documentation of 1.3, I'm not able to find GetWidth() or GetHeight() for either Sprites or Images. (What is what I expected, since I've never before used GetWidth()/Height() ...)

Did you try GetSize().x and GetSize().y? These Methods should give you the Height and Width!

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
[Solved] SetCenter
« Reply #7 on: July 06, 2008, 07:49:54 pm »
So this was intentional?

Code: [Select]
mSprite.SetCenter(0.5f * theGlyph->Texture.GetWidth(), 0.5f * theGlyph->Texture.GetHeight());


I would think you would want to do this:

Code: [Select]
mSprite.SetCenter(0.5f * mSprite.GetSize().x, 0.5f * mSprite.GetSize().y);



Quote from: "Xylankant"
I don't know 1.2, but reading the documentation of 1.3, I'm not able to find GetWidth() or GetHeight() for either Sprites or Images. (What is what I expected, since I've never before used GetWidth()/Height() ...)

Did you try GetSize().x and GetSize().y? These Methods should give you the Height and Width!


GetWidth() does exist for sf::Image.


EDIT: I see you edited your post and I get it now. :P

So is SetSubRect() even needed?

dewyatt

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
    • http://dewyatt.blogspot.com
[Solved] SetCenter
« Reply #8 on: July 06, 2008, 07:58:24 pm »
Quote from: "Wizzard"
So this was intentional?

Code: [Select]
mSprite.SetCenter(0.5f * theGlyph->Texture.GetWidth(), 0.5f * theGlyph->Texture.GetHeight());


I would think you would want to do this:

Code: [Select]
mSprite.SetCenter(0.5f * mSprite.GetSize().x, 0.5f * mSprite.GetSize().y);


That does seem to be correct.
I'm guessing sf::Sprite::GetSize returns the transformed size?

Quote from: "Wizzard"
EDIT: I see you edited your post and I get it now. :P
So is SetSubRect() even needed?

Yes, I edit quick and often. :)
I believe SetSubRect is needed because I'm re-using the sprite with different sf::Images all the time.
I seem to recall it crashed if I didn't use SetSubRect().

I still haven't figured this out, I'm trying though.

dewyatt

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
    • http://dewyatt.blogspot.com
[Solved] SetCenter
« Reply #9 on: July 06, 2008, 08:17:02 pm »
I looked through older version of my code and tried using some of the older code.
It's still not working.
I have test programs for sfmlTTF that were linked with SFML 1.2 and things rotate as expected.
I think something changed or broke from SFML 1.2/1.3.

I'm still testing things.

dewyatt

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
    • http://dewyatt.blogspot.com
[Solved] SetCenter
« Reply #10 on: July 06, 2008, 08:21:51 pm »
EDIT: Son of a B.

I knew one day I would make one of these moronic mistakes.
I've been recompiling but using my old executable because I had copied it from "Debug/" to "/".
This is why I always change my projects to output to $(SolutionDir).
So I don't need to load resources with "../" or copy the exe to "../".

OKAY, i'm going to try to confirm I can fix this now.

dewyatt

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
    • http://dewyatt.blogspot.com
[Solved] SetCenter
« Reply #11 on: July 06, 2008, 08:29:25 pm »
Yeah, fixed it.

I can't even remember what I changed.
The final code is like this:
Code: [Select]

mSprite.SetImage(theGlyph->Texture);
mSprite.SetSubRect(sf::IntRect(0, 0, theGlyph->Texture.GetWidth(), theGlyph->Texture.GetHeight()));
mSprite.SetPosition(static_cast<float>(x), static_cast<float>(y));

mSprite.SetCenter(mRotationCenterX * mSprite.GetSize().x, mRotationCenterY * mSprite.GetSize().y);
mSprite.SetRotation(mRotation);
mSprite.SetScaleX(mHScale);
mSprite.SetScaleY(mVScale);

mWindow->Draw(mSprite);

I'm so pissed I wasted my time and your time on this.
I've been programming for way to long to make these type of mistakes.
Oh well, I guess I'm just tired.

Somewhere along the line this caused glyphs to not be drawn on the baseline.
I'll have to figure that out now.