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

Author Topic: [SFML 2.0] sf::Thread doesn't display sf::Text correctly  (Read 8890 times)

0 Members and 1 Guest are viewing this topic.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
[SFML 2.0] sf::Thread doesn't display sf::Text correctly
« on: January 25, 2012, 07:37:02 pm »
At the moment I'm experimenting with sf::Thread but I get some problems with global sf::Text variables and I'm not sure if SFML is messing something up or my lack of knowledge.

I choose to use the member function version for thread creation and setup three files as an example.

What I basicly want to do is display how each thread is running.
So the code seems in my opinion okay and using a sf::Mutex for the mUPS doesn't change anything.
The problem now is, that only some random numbers get displayed on the window altough the std::cout looks okay.
When further tempering with the text output I noticed that:
1) When setting the text outside of the Draw() function it doesn't get displayed at all (I set it at the intitializastion).
2) Using the mFPS which is declared outside of the Draw function and assign some numbers all the '0' don't get displayed.
3) When using a sf::Text local to the thread it gets displayed without problems.
4) It seems everything works fine with a non-member function without argument.

What I ask myself now, can't I use external sf::Text? If not how would I share text between threads? Or better how would a use threads to draw stuff?

main.cpp
#include "Task.hpp"

int main(int argc, char **argv)
{
        Task test;
       
        test.Run();
   
        return EXIT_SUCCESS;
}
Task.hpp
#ifndef TASK_HPP
#define TASK_HPP

#include <SFML/Graphics.hpp>

class Task
{
public:
        Task();
        ~Task();

        void Draw();
        void Update();

        void Run();

private:
        sf::Text mFPS;
        sf::Text mUPS;
        bool mRun;
        sf::Mutex mLocker;
        sf::Font mFont;
};

#endif // TASK_HPP

Task.cpp
#include "Task.hpp"

#include <SFML/System.hpp>
#include <sstream>
#include <iostream>

Task::Task() : mFPS(sf::String("FPS: 30")),
                           mUPS(sf::String("UPS: 30")),
                           mRun(false),
                           mLocker(),
                           mFont()
{
        mFont.LoadFromFile("FreeMono.ttf");

        mFPS.SetFont(mFont);
        mFPS.SetPosition(sf::Vector2f(10.f, 10.f));
        mFPS.SetColor(sf::Color::White);

        mUPS.SetFont(mFont);
        mUPS.SetPosition(sf::Vector2f(10.f, 50.f));
        mUPS.SetColor(sf::Color::White);
}

Task::~Task() { }

void Task::Draw()
{
        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
        window.SetFramerateLimit(30);
        sf::Clock clock;

        float currentFPS = 30;

        while(mRun)
        {
                // Process events
                sf::Event event;
                while (window.PollEvent(event))
                {
                        // Close window : exit
                        if (event.Type == sf::Event::Closed)
                        {
                                mRun = false;
                                window.Close();
                        }
                }

                std::stringstream ss;
                currentFPS = 1.f/clock.GetElapsedTime().AsSeconds();
                ss << "FPS: " << currentFPS;
                std::cout << ss.str() << std::endl;
                mFPS.SetString(sf::String(ss.str()));

                window.Clear();
                window.Draw(mFPS);
                window.Draw(mUPS);
                window.Display();

                // Reset
                clock.Restart();
        }
}

void Task::Update()
{
        sf::Clock clock;

        float currentUPS = 30;

        while(mRun)
        {
                currentUPS = 1.f/clock.GetElapsedTime().AsSeconds();
                std::ostringstream oss;
                oss << "UPS: " << (unsigned int)(currentUPS);
                {
                        sf::Lock::Lock(mLocker);
                        mUPS.SetString(sf::String(oss.str()));
                }

                // Reset
                clock.Restart();
        }
}

void Task::Run()
{
        mRun = true;

        sf::Thread thread1(&Task::Draw, this);
        thread1.Launch();

        sf::Thread thread2(&Task::Update, this);
        thread2.Launch();

        thread2.Wait();
        thread1.Wait();
}

Thanks for any help!

eXpl0it3r
« Last Edit: June 09, 2012, 01:04:49 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
[SFML 2.0] sf::Thread doesn't display sf::Text correctly
« Reply #1 on: February 18, 2012, 03:35:27 pm »
It seems the problem isn't so easy to fix, otherwise there probably would've been an answer...

I tried to use the keyword volatile infront of sf::Text which let me to const_cast the volatile sf::Text to sf::Text& for the windw.Draw() function. Unfortunatly it didn't help at all.

Does nobody have an anwser to this?
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
[SFML 2.0] sf::Thread doesn't display sf::Text correctly
« Reply #2 on: February 18, 2012, 03:40:54 pm »
Quote
It seems the problem isn't so easy to fix, otherwise there probably would've been an answer...

Although the code is quite short, I think it's still too long. It contains a lot of unnecessary code that makes it very difficult to understand what you do and what happens. Please reduce it to the strict minimum.

It's hard to follow a code when threads are involved, so make it as easy as possible for us ;)

http://www.sfml-dev.org/forum/viewtopic.php?p=36368#36368
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
[SFML 2.0] sf::Thread doesn't display sf::Text correctly
« Reply #3 on: February 18, 2012, 04:34:33 pm »
Okay I shortened the code, also I think the code from the first post as some side-effects on multiple levels, e.g. I got a crash in WglContext.hpp even though I used an additional font and linked staticlly.
But let's first concentrait on the fact that the text stuff gets kinda messed up.

New Code:
#include <SFML/Graphics.hpp>
#include <sstream>

class Task
{
public:
        Task() : mCounter(sf::String("FPS: 30"))
        {
                mCounter.SetPosition(sf::Vector2f(10.f, 10.f));
                mCounter.SetColor(sf::Color::White);
        }

        void Draw()
        {
                sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
                float counter = 30;

                while(window.IsOpen())
                {
                        sf::Event event;
                        while(window.PollEvent(event))
                                if(event.Type == sf::Event::Closed)
                                        window.Close();

                        counter++;
                        mCounter.SetString("FPS: " + GetString(counter));
                        window.Clear();
                        window.Draw(mCounter);
                        window.Display();
                }
        }
       
        void Run()
        {
                sf::Thread thread1(&Task::Draw, this);
                thread1.Launch();
                thread1.Wait();
        }

private:
        sf::Text mCounter;

        sf::String GetString(float aFloat)
        {
                std::stringstream ss;
                ss << aFloat;
                return sf::String(ss.str());
        }
};

int main()
{
        Task test;
        test.Run();
        return 0;
}

I still call the Run() function which creates now only one thread.
The sf::Text mCounter gets declared and initialized inside the main thread. Additionally I introduced a GetString() function which converts a float to a sf::String.

I now get displayed the numbers, but when ever there should be a 0 it just doesn't get displayed. Also the 'FPS:' doesn't get shown.



Does sf::Text also need a valid OpenGL context? I've just seen that on Linux you can't create a sf::Texture before initializing the RenderWindow.
« Last Edit: June 09, 2012, 01:05:09 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
[SFML 2.0] sf::Thread doesn't display sf::Text correctly
« Reply #4 on: February 18, 2012, 08:45:08 pm »
Quote
Task() : mCounter(sf::String("FPS: 30"))
This causes the problem with me.

The letters and numbers inside that string (so the "FPS:" and the '0', and apparently also the '3') are gone.

Everything will work when changing it to this:
Quote
Task() : mCounter(sf::String(""))


I hope this helps with finding the problem.
TGUI: C++ SFML GUI

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
[SFML 2.0] sf::Thread doesn't display sf::Text correctly
« Reply #5 on: February 18, 2012, 09:59:34 pm »
Quote
The letters and numbers inside that string (so the "FPS:" and the '0', and apparently also the '3') are gone.


Hmmm so it looks like the those letters get reserved by the main thread and can't be used in the other thread or something like this.

Thanks for finding this!  :)
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
[SFML 2.0] sf::Thread doesn't display sf::Text correctly
« Reply #6 on: February 18, 2012, 11:20:22 pm »
This code works for me (Windows 7 64-bits, nvidia graphics card, SFML debug static).

What's your OS? Graphics card? Revision of SFML 2? Are your drivers up to date?
Laurent Gomila - SFML developer

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
[SFML 2.0] sf::Thread doesn't display sf::Text correctly
« Reply #7 on: February 19, 2012, 10:33:47 am »
I am using Ubuntu 64-bit with a ATI card (I didn't install any driver because they cause too much problems).

I don't know how text is drawn, but if every letter is an image then the problem is already known (loading textures before RenderWindow on Linux).
TGUI: C++ SFML GUI

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SFML 2.0] sf::Thread doesn't display sf::Text correctly
« Reply #8 on: February 19, 2012, 11:41:41 am »
Quote
I don't know how text is drawn, but if every letter is an image then the problem is already known (loading textures before RenderWindow on Linux).

Hmm yes, that's probably the cause of the problem.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
[SFML 2.0] sf::Thread doesn't display sf::Text correctly
« Reply #9 on: February 19, 2012, 04:24:59 pm »
I'm on Windows 7 x64, ATI GFX card (Radeon HD 6470M), newest driver (8.821.1.7000), linked 64 static and build with the newest commit of SFML 2.0 (091fbd9d426b3b8bdd3a07f69bea3c8a48d0d0ce).

So either the Linux bug thingy is the same on Windows under certain circumstances or it's something diffrent.

Btw why can't you fix the ATI bug if you know where the problem is?
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
[SFML 2.0] sf::Thread doesn't display sf::Text correctly
« Reply #10 on: February 19, 2012, 04:59:51 pm »
Quote
Btw why can't you fix the ATI bug if you know where the problem is?

There's no ATI bug in SFML 2.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
[SFML 2.0] sf::Thread doesn't display sf::Text correctly
« Reply #11 on: February 19, 2012, 05:38:44 pm »
Quote from: "Laurent"
There's no ATI bug in SFML 2.


Now you get me very confused...  :shock:
Then how do you call this bug right there: http://www.sfml-dev.org/forum/viewtopic.php?t=4741
Aaanyway it's offtopic...

Any solution to my problem, how can I help narrowing down the problem (since it doesn't happen on your side)?
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
[SFML 2.0] sf::Thread doesn't display sf::Text correctly
« Reply #12 on: February 19, 2012, 06:03:41 pm »
Quote
Then how do you call this bug right there: http://www.sfml-dev.org/forum/viewtopic.php?t=4741

I call it a bug ;)
It probably happens with non-ATI cards too, since it's caused by a problem in SFML.
Laurent Gomila - SFML developer

 

anything