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

Author Topic: sf::string  (Read 5142 times)

0 Members and 1 Guest are viewing this topic.

buggy123

  • Newbie
  • *
  • Posts: 26
    • View Profile
sf::string
« on: November 19, 2009, 07:32:42 am »
I'm trying to make "selectable text" meaning that when the mouse hovers over the words, it gets highlighted and stuff. However, the text is changing in length. How do I do this?

Is a way to figure out how much pixels each letter is or something?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::string
« Reply #1 on: November 19, 2009, 07:39:37 am »
You can calculate the total size of the string with the GetRect() function.
You can also get the 2D position of a character inside the text with the GetCharacterPos() function.
Laurent Gomila - SFML developer

Imbue

  • Full Member
  • ***
  • Posts: 104
    • View Profile
sf::string
« Reply #2 on: November 19, 2009, 08:48:04 pm »
I very recently did this same thing.

Here is my relevant code, which converts a mouse position to the caret position. Note the averaging of the last value, this is so the caret snaps to the nearest gap.

mString is a pointer to my string.

Code: [Select]
   size_t TextBox::GetCharacter(float x) const
    {
        size_t caret = 0;
        float lastPos = 0.0f;
        const std::string s = mString->GetText();
        while (caret < s.length())
        {
            const float curPos = mString->GetCharacterPos(caret + 1).x + mString->GetPosition().x;

            if ((lastPos + curPos) / 2.0f > x)
                break;

            ++caret;
            lastPos = curPos;
        }

        return caret;
    }


Knowing where to draw the caret is just a call to mString->GetCharacterPos(caret).

buggy123

  • Newbie
  • *
  • Posts: 26
    • View Profile
sf::string
« Reply #3 on: November 23, 2009, 01:43:51 am »
Thanks for the answers.

So anyway, I tried to create basic class that would let me display text.

 
Code: [Select]
class TextEngine{
public:
sf::Font Font;

std::string GetFont();
int CreateFont(std::string FileDir);
sf::String CreateText(sf::String Text, std::string String, float Size, float X, float Y);
int DisplayText(sf::RenderWindow Window, sf::String FileToDisplay);
};

std::string TextEngine::GetFont(){
std::string FileName;
lua_getglobal(Menu,"Font");
FileName = (std::string)lua_tostring(Menu, -1);
lua_pop(Menu, 1);

return FileName;
};

int TextEngine::CreateFont(std::string FileDir)
{
Font.LoadFromFile(FileDir);

return 0;
}

sf::String TextEngine::CreateText(sf::String Text, std::string String, float Size, float X, float Y){
Text.SetText(String);
Text.SetFont(Font);
Text.SetSize(Size);
Text.SetX(X);
Text.SetY(Y);

return Text;
};

int TextEngine::DisplayText(sf::RenderWindow Window, sf::String FileToDisplay){
Window.Draw(FileToDisplay);
return 0;
};


but whenever I try to use it, it gives me compiler errors that refers me to this "NonCopyable.hpp" page.

Am I accessing a private member? How do I fix this?

chipsman

  • Newbie
  • *
  • Posts: 23
    • View Profile
sf::string
« Reply #4 on: November 23, 2009, 08:55:00 am »
In your last function :

Code: [Select]
int TextEngine::DisplayText(sf::RenderWindow Window, sf::String FileToDisplay){
                                                            Window.Draw(FileToDisplay);
                                                            return 0;
};


You try to get a copy of sf::RenderWindow. The fact is sf::RenderWindow has sf::NonCopyable class as base Class. That means you can't copy an instance of sf::RenderWindow. You should try this:

Code: [Select]
int TextEngine::DisplayText(sf::RenderWindow& Window, sf::String FileToDisplay){
                                                            Window.Draw(FileToDisplay);
                                                            return 0;
};

buggy123

  • Newbie
  • *
  • Posts: 26
    • View Profile
sf::string
« Reply #5 on: November 28, 2009, 09:40:48 pm »
thanks for the answer!

Another problem I've run into with fonts is that when I set the size high (like 100) the text becomes blurry. I use a .ttf font.

Is there font types that uses vector graphics rather then raster graphics? can I load/use them with SFML?

Or is there any solution to increase the quality? (I'm quite fond of the font I'm using)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::string
« Reply #6 on: November 28, 2009, 10:27:59 pm »
If the size of the font is the same as the size of the string then the quality should be the best.
Laurent Gomila - SFML developer

buggy123

  • Newbie
  • *
  • Posts: 26
    • View Profile
sf::string
« Reply #7 on: November 29, 2009, 03:38:55 am »
oh, I get it. What if I just set the Font(not string) size really high? will it take up more resources and slow the program down?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::string
« Reply #8 on: November 29, 2009, 10:41:32 am »
It will just take more video memory.
Laurent Gomila - SFML developer