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

Author Topic: sf::Text disappearing characters [SFML 2.0]  (Read 5110 times)

0 Members and 1 Guest are viewing this topic.

drhaximus

  • Newbie
  • *
  • Posts: 9
    • View Profile
sf::Text disappearing characters [SFML 2.0]
« on: February 25, 2013, 06:15:33 am »
Hello.
I have a chat function like this...

void addChat(std::string username,int rights,std::string message) {
        std::string chat;
        if(rights>0) {
                chat="[ADMIN] "+username+": "+message;
        }
        else {
                chat=username+": "+message;
        }
        std::string tempChat[6];
       
        for(int i=0;i<=5;i++) {
                tempChat[i]=chatHistory[i].getString();
        }
        chatHistory[0].setColor(sf::Color::Red);
        chatHistory[0].setString(chat);
        chatHistory[1].setString(tempChat[0]);
        chatHistory[2].setString(tempChat[1]);
        chatHistory[3].setString(tempChat[2]);
        chatHistory[4].setString(tempChat[3]);
        chatHistory[5].setString(tempChat[4]);
}

And an initialize function like this...

void chatInit() {
        chatFont.loadFromFile("interfaces/trebucbd.ttf");
        for(int i=0;i<=5;i++) {
                chatHistory[i].setFont(chatFont);
                chatHistory[i].setColor(sf::Color::Black);
                chatHistory[i].setCharacterSize(16);
                chatHistory[i].setString("a");
                chatHistory[i].setPosition(25,i*25+20);
        }
        addChat("SERVER",1,"Welcome to the server!");
}

Then I draw it like this...

void drawChat(sf::RenderWindow& window) {
        for(int i=0;i<=5;i++) {
                window.draw(chatHistory[i]);
        }
}

Before offering solutions, please read this additional information:
  • The text does not lack any characters, in the std::string format, there are no problems with the strings.
  • The text does not continue to work after the initial "Welcome to the server!" message.
  • The font is not the problem, I have tried multiple.
  • I am using SFML 2.0 on Windows 7, compiling with Visual C++ 2010
Thanks, all!

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: sf::Text disappearing characters [SFML 2.0]
« Reply #1 on: February 25, 2013, 06:50:57 am »
Any chance you're trying to draw that black text on a black background?

drhaximus

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: sf::Text disappearing characters [SFML 2.0]
« Reply #2 on: February 25, 2013, 07:39:38 am »
Yes, I'm sure that I'm not drawing it on a black background. If you took the time to check the additional information you would see that I'm able to see the first message without having characters missing >.>

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: sf::Text disappearing characters [SFML 2.0]
« Reply #3 on: February 25, 2013, 08:01:54 am »
Yes, I'm sure that I'm not drawing it on a black background. If you took the time to check the additional information you would see that I'm able to see the first message without having characters missing >.>

If you took the time to actually post working compilable code that exhibits the problem...
but I actually did take the time to check the additional information.  Unfortunately it isn't clear.

What do you mean by first message?  There are 6 messages at all times.  If you can only see the top one which is printed in red (which one might assume you would call the "first" message), then it's a reasonable question to ask.

The following works perfectly fine for me (Note that if this is copy and pasted, you will likely need to change the font that's loaded:)

#include <SFML/Graphics.hpp>
#include <string>

sf::Text chatHistory[6] ;
sf::Font chatFont ;

void addChat(std::string username,int rights,std::string message) ;
void drawChat(sf::RenderWindow& window) ;
void chatInit() ;

std::string simulatedInput[] =
{
    "Abracadabra!",
    "I love m&ms!",
    "No, skittles are the best.",
    "Don't make me laugh.",
    "Wouldn't dream of it.",
    "I heard you dream about your mom.",
    "Nah.  It's your mom I dream about.",
    "You must mean you have a nightmare.",
    "Are you kidding?  Your mom is hot.",
};

const unsigned inputSize = sizeof(simulatedInput) / sizeof(simulatedInput[0]) ;
unsigned inputIndex = 0 ;

int main()
{
    sf::RenderWindow window( sf::VideoMode(800, 600), "sf::Text test" ) ;

    chatInit() ;

    while ( window.isOpen() )
    {
        sf::Event event ;
        while ( window.pollEvent(event) )
        {
            if ( event.type == sf::Event::Closed )
                window.close() ;

            if ( event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Return )
            {
                addChat("cire", 0, simulatedInput[inputIndex]) ;
                inputIndex = (inputIndex+1) % inputSize ;
            }

        }

        window.clear(sf::Color::Blue) ;
        drawChat(window) ;
        window.display() ;
    }

}

void drawChat(sf::RenderWindow& window) {
    for(int i=0;i<=5;i++) {
        window.draw(chatHistory[i]);
    }
}

void chatInit() {
    chatFont.loadFromFile("sketchflow.ttf");
    for(int i=0;i<=5;i++) {
        chatHistory[i].setFont(chatFont);
        chatHistory[i].setColor(sf::Color::Black);
        chatHistory[i].setCharacterSize(16);
        chatHistory[i].setString("a");
        chatHistory[i].setPosition(25,i*25+20);
    }
    addChat("SERVER",1,"Welcome to the server!");
}


void addChat(std::string username,int rights,std::string message) {
    std::string chat;
    if(rights>0) {
        chat="[ADMIN] "+username+": "+message;
    }
    else {
        chat=username+": "+message;
    }
    std::string tempChat[6];
   
    for(int i=0;i<=5;i++) {
        tempChat[i]=chatHistory[i].getString();
    }
    chatHistory[0].setColor(sf::Color::Red);
    chatHistory[0].setString(chat);
    chatHistory[1].setString(tempChat[0]);
    chatHistory[2].setString(tempChat[1]);
    chatHistory[3].setString(tempChat[2]);
    chatHistory[4].setString(tempChat[3]);
    chatHistory[5].setString(tempChat[4]);
}


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: sf::Text disappearing characters [SFML 2.0]
« Reply #4 on: February 25, 2013, 08:07:12 am »
There quite a few existing posts around this problem (have you used the search function?).
Anyways, you can try calling glFlush() before each rendering step.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text disappearing characters [SFML 2.0]
« Reply #5 on: February 25, 2013, 08:51:26 am »
To be more precise, if you're calling text.setString and window.draw(text) in two different threads, you need to call glFlush() after every call to setString. Note that this is just a workaround until a clean solution is found, it's not part of the documented API ;)
Laurent Gomila - SFML developer

drhaximus

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: sf::Text disappearing characters [SFML 2.0]
« Reply #6 on: February 25, 2013, 07:14:46 pm »
@Laurent
You solved the problem! Thank you very much!

@cira
That did not help... Part of the additional information is that the font is not the problem, unless I'm reading that wrong. Yes, there is always 6 messages, but the first one is obviously the first call to addChat

@exploiter
I DID, but every thread I was able to find didn't really have any solutions, I suppose I was using google instead of the forum search  ::)

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: sf::Text disappearing characters [SFML 2.0]
« Reply #7 on: February 25, 2013, 08:03:30 pm »
Quote
That did not help... Part of the additional information is that the font is not the problem, unless I'm reading that wrong. Yes, there is always 6 messages, but the first one is obviously the first call to addChat

I didn't say that the font was the problem.  What I intended to convey was: "I changed the font, so don't blindly copy this without changing it back to something appropriate for your environment."  It was just a heads up.

 

anything