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

Author Topic: sf::String::GetRect::GetWidth() returns wrong value?  (Read 4010 times)

0 Members and 1 Guest are viewing this topic.

Beta_Ravener

  • Jr. Member
  • **
  • Posts: 51
    • ICQ Messenger - 271426715
    • View Profile
sf::String::GetRect::GetWidth() returns wrong value?
« on: June 04, 2011, 10:09:21 pm »
As stated in function description, GetRect should return bounding box of text set to a string. When I use this value (more specificly it's width) as position to draw something immediately after text, this objects is drawn in good distance from text (almost another same text woudl fit into gap).

GetRect seems to work somewhat, because 'b' returns bigger value than 'i', but both returns something else than what I'd expect..

Maybe I'm not understanding "screen coordinates" term correctly, but those coordinates should be same as what goes into sf::Shape::SetPostition, shouldn't they?

Thanks for soon reply!

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
sf::String::GetRect::GetWidth() returns wrong value?
« Reply #1 on: June 04, 2011, 10:18:54 pm »
Show a minimal example of your code that reproduces the behaviour you are describing.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Beta_Ravener

  • Jr. Member
  • **
  • Posts: 51
    • ICQ Messenger - 271426715
    • View Profile
sf::String::GetRect::GetWidth() returns wrong value?
« Reply #2 on: June 04, 2011, 10:55:45 pm »
This is my input procedure:
Code: [Select]
if(Event.Type == sf::Event::TextEntered){
char *ch = new char[2];
sf::String s;
if(unicodeToASCII(Event.Text.Unicode, ch[0])){
                ch[1] = '\0';
inputText += ch;
print(ch);
s.SetText(ch);
cursorScreenPos.x += s.GetRect().GetWidth();
}
}


as you can see, I use width of single input character to move my cursor. Then I'm using this value while drawing cursor:

Code: [Select]
sf::Shape cursorLine;
float cursorWidth = 3.0f;
cursorLine = sf::Shape::Line(0, 0, 0, output.GetSize(), cursorWidth, textColor);
cursorLine.SetPosition( cursorScreenPos.x + cursorWidth/2 - scrollH.getCurScroll() ,cursorScreenPos.y - ScrollV.getCurScroll() );
ConsoleWin.Draw(cursorLine);


Those scroll things aren't important, because while testing they are zero value. This is all code currently affecting cursor position.
For example 'a' returns width of 17, but when I move cursor there, it's almost double the distance it should be..

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
sf::String::GetRect::GetWidth() returns wrong value?
« Reply #3 on: June 04, 2011, 11:03:53 pm »
My guess is that he does a simple equation like this: characterWidth * numCharacters and with the '\0' you actually got two characters and this is added every time giving us the double size.

I am not sure about this but that is the only thing I can come up with. Though it feels a bit far fetch.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Beta_Ravener

  • Jr. Member
  • **
  • Posts: 51
    • ICQ Messenger - 271426715
    • View Profile
sf::String::GetRect::GetWidth() returns wrong value?
« Reply #4 on: June 04, 2011, 11:17:45 pm »
I think that won't be the case because this code gives same result:
Code: [Select]
if(Event.Type == sf::Event::TextEntered){
std::string ch;
sf::String str;
ch.resize(1);
if(unicodeToASCII(Event.Text.Unicode, ch[0])){
inputText += ch;
print(ch);
str.SetText(ch);
cursorScreenPos.x += str.GetRect().GetWidth();
}
}

I debuged to see cursor position and after pressing 'a', it's still 17 - value returned from single GetWidth call, and still too far from character.

Beta_Ravener

  • Jr. Member
  • **
  • Posts: 51
    • ICQ Messenger - 271426715
    • View Profile
sf::String::GetRect::GetWidth() returns wrong value?
« Reply #5 on: June 05, 2011, 12:42:00 am »
I tried setting text to "aa" and using GetCharacterPos(1) meausre 'a' size, but it also returns 17. But I've searched a bit in tutorials and found out that: "In case you need to deal with a string's characters individually, like for displaying a cursor after the n-th character of whatever, sf::String provides a utility function to get the position of any character in the string.

sf::Vector2f Position = Text.GetCharacterPos(4);

The returned position is defined in local coordinates, so you'll need to call TransformToGlobal to get the actual global position. "

So I tried to add:
cursorScreenPos = str.TransformToGlobal(cursorScreenPos);

but nothing happend to coordinates.

Then I tried to divide width by 2 (because as I said, the gap was wide like the string) and what I get is cursor right after last letter! But than I tried to make larger letters with sf::String::SetSize and it doesn't fit anymore again. It looks like this change in size isn't calculated in nor while requesting size of bounding box nor while TransformToGlobal!

After some testing I came to conclusion that one have to calculate factor after changing size like: f = newSize/30.0f; (where 30.0f is deafualt font size) and then multiply bounding box by this factor. As I had my font set to size 15.0f, dividing by 2 resulted in right values. I couldn't find any function to apply this scale or something into font, so I think it's a small bug or needed feature.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
sf::String::GetRect::GetWidth() returns wrong value?
« Reply #6 on: June 05, 2011, 02:08:30 am »
Hmm I think the default size is 30(from the inbuilt arial font) so it apparently goes after that.

You are using SFML 1.x right? If so then this won't be implemented as Laurent doesn't work on the 1.x branch anymore. He is putting all his spare time on shipping SFML 2 instead so I would recommend that you move to there, see if the result is the same and if it is ask him to fix it.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::String::GetRect::GetWidth() returns wrong value?
« Reply #7 on: June 05, 2011, 10:08:47 am »
Could you please provide a complete and minimal example that reproduces this problem? I mean, not parts of your original code, but rather a new piece of code that only shows the problem.

This function has worked great so far so it would be strange if it had such a huge bug.
Laurent Gomila - SFML developer

Beta_Ravener

  • Jr. Member
  • **
  • Posts: 51
    • ICQ Messenger - 271426715
    • View Profile
sf::String::GetRect::GetWidth() returns wrong value?
« Reply #8 on: June 05, 2011, 11:06:57 am »
sorry for false alarm, I hope you didn't lost time looking for bug.

I, of course, couldn't reproduce this behavior. The code I created to test was workign alright so I searched in my full code and found out that I haven't set anywhere size of string I used to measure. Thus I had to multiply value given by this measure string whit a factor mentioned above to get right results, but copying output string into measure string like : ms = os; and then setting the text yields right values.

Sorry once more for bothering you.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::String::GetRect::GetWidth() returns wrong value?
« Reply #9 on: June 05, 2011, 11:10:54 am »
No problem. I knew it was a mistake in your code :lol:
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
sf::String::GetRect::GetWidth() returns wrong value?
« Reply #10 on: June 05, 2011, 11:44:37 am »
Quote from: "Laurent"
No problem. I knew it was a mistake in your code :lol:


The almighty Laurent has spoken!
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

 

anything