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

Author Topic: Passing a parameterized function into sf::Thread object  (Read 6288 times)

0 Members and 1 Guest are viewing this topic.

Bridge

  • Newbie
  • *
  • Posts: 3
    • View Profile
Passing a parameterized function into sf::Thread object
« on: August 22, 2012, 12:26:36 am »
Earlier I successfully managed to adapt my stab at Conway's Game of Life to SFML and it runs perfectly, but I'm in the process of expanding it and the first thing I wanted to do was to display a timer in the upper left corner of the screen. My idea was to have a function like this:

void timer(sf::RenderWindow& rw, sf::String& str){
    sf::Clock c;
    float t;
    while (isRunning == true){
        t = c.GetElapsedTime();
        c.Reset();
        std::stringstream ss;
        ss << t;
        std::string elapsedTime = ss.str();
        str.SetText(elapsedTime);
        rw.Draw(str);
    }
}
(which looks pretty good, I'm still new to SFML so if this looks weird please say so)

However it makes no sense to get the elapsed time each game cycle so I decided to put it in a separate thread so that it can evenly increment the time and display it independently from the game itself. It gives me a error when I try to do this however:

    //Create new thread
    sf::Thread thread(&timer, mw, time);
    thread.Launch();

Code: [Select]
Error log:

||=== Game of Life, Debug ===|
C:\Game of Life\main.cpp||In function 'int main()':|
C:\Game of Life\main.cpp|65|error: no matching function for call to 'sf::Thread::Thread(void (*)(sf::RenderWindow&, sf::String&), sf::RenderWindow&, sf::String&)'|
C:\Users\Gylfi\Documents\SFML\SFML-1.6\include\SFML\System\Win32\Thread.hpp|92|note: candidates are: sf::Thread::Thread()|
C:\Users\Gylfi\Documents\SFML\SFML-1.6\include\SFML\System\Win32\Thread.hpp|57|note:                 sf::Thread::Thread(void (*)(void*), void*)|
C:\Users\Gylfi\Documents\SFML\SFML-1.6\include\SFML\System\Win32\Thread.hpp|45|note:                 sf::Thread::Thread(const sf::Thread&)|
||=== Build finished: 1 errors, 0 warnings ===|

The tutorial says you can pass in the parameters the threaded function needs but it is not working. How do you go about passing in a function that takes parameters into sf::Thread objects?
« Last Edit: August 22, 2012, 07:58:10 pm by Bridge »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Passing a parameterized function into sf::Thread object
« Reply #1 on: August 22, 2012, 12:40:27 am »
For such things you should always take a look at the documentation, where you'd have found the argument list for sf::Thread and as you can see it only take one UserData variable and that one has to be a pointer.

Also for your task it's not really good or usefull to use a second thread. Calling GetElapsedTime() has such a small overhead that one can't even start to argue about, where as if you'd want to draw in two diffrent threads you'd always have to set the active conetext to the right thread etc which has a way bigger overhead than receiving an float from a function.

I strongly recommend you to use SFML 2, is in all ways superior to SFML 1.6 and less buggy and since it already has binaries for the RC you won't need to compile SFML on your own.
Also note that this forum has code=cpp tags, so please make use of them!
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Bridge

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Passing a parameterized function into sf::Thread object
« Reply #2 on: August 22, 2012, 01:29:04 am »
For such things you should always take a look at the documentation, where you'd have found the argument list for sf::Thread and as you can see it only take one UserData variable and that one has to be a pointer.

Also for your task it's not really good or usefull to use a second thread. Calling GetElapsedTime() has such a small overhead that one can't even start to argue about, where as if you'd want to draw in two diffrent threads you'd always have to set the active conetext to the right thread etc which has a way bigger overhead than receiving an float from a function.

I strongly recommend you to use SFML 2, is in all ways superior to SFML 1.6 and less buggy and since it already has binaries for the RC you won't need to compile SFML on your own.
Also note that this forum has code=cpp tags, so please make use of them!

My concern was that because I am searching through each element of a 50x50 array to determine the next state and drawing 50x50 bitmaps each cycle (which is not a good idea, I still have to correct that) that calling GetElapsedTime inside the while(<>.IsOpened()) would make the timer not increment smoothly. If you say it doesn't really matter then I'll forget the whole thread business. Also I'll keep a look out for 2.0 and I didn't realize you had to specify the formatting in the code tags so I'm sorry (I'll keep it in mind for next time). Thanks for the quick reply.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Passing a parameterized function into sf::Thread object
« Reply #3 on: August 22, 2012, 08:28:20 am »
Quote
I didn't realize you had to specify the formatting in the code tags so I'm sorry (I'll keep it in mind for next time)
You can also edit your first post.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Passing a parameterized function into sf::Thread object
« Reply #4 on: August 22, 2012, 09:14:17 am »
sf::Clock makes use of system timers, thus they are not affected by your code, i.e. the clock will increase no matter what you do. ;)
If you're drawing something similare to a tilemap, I can point you to the sf::VertexArray in SFML 2, it is quite performant for such things.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Bridge

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Passing a parameterized function into sf::Thread object
« Reply #5 on: August 22, 2012, 07:47:44 pm »
Quote
I didn't realize you had to specify the formatting in the code tags so I'm sorry (I'll keep it in mind for next time)
You can also edit your first post.

Hardly seems worthwhile because this thread is largely useless. Unless someone wants the source code (it's not the most efficient Life engine out there but it is fairly good) you're free to just lock this.

sf::Clock makes use of system timers, thus they are not affected by your code, i.e. the clock will increase no matter what you do. ;)
If you're drawing something similare to a tilemap, I can point you to the sf::VertexArray in SFML 2, it is quite performant for such things.

I see, that's quite handy. The timer does indeed work smoothly and now all I had to do is some formatting.

Also, sf::VertexArray looks good but the game engine was already written (I was just adapting it to SFML to draw it more efficiently; drawing ASCII characters in the Windows console caused some errors and it was ugly).

 

anything