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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Ultralegendary

Pages: [1]
1
General / SFML- Thread
« on: December 06, 2020, 07:04:51 am »
hi, i have been working with SFML for 2 months.
I tried to do visualizing sorting methods.
After some research i found that threading is the only way which satisfy my needs.
But i failed to implement threading to my code. Got some errors, unexpected outputs.
Here is the code:
Code: [Select]
#include<SFML/Graphics.hpp>
#include<iostream>
#include<functional>

class sorting
{
     :// some code
     public:
          update_merge_sort(int a,int b)
          {
          // merge sort things
          // contains recursion function
          }

};

int main()
{
sorting objects;
sf::RenderWindow w(sf::VideoMode(1550, 850), "SORTING ALGORITHMS", sf::Style::Default);
sf::Thread thread_1(std::bind(&sorting::update_merge_sort,&objects,0,0));//declared global because the this has to run aslong as program runs, thread is not launched

sf::Event e;
while (w.isOpen()) {
while (w.pollEvent(e))
{
mos_pos = w.mapPixelToCoords(sf::Mouse::getPosition(w));
                        switch (e.type) {
                                 case 0:
                                           w.close(); break;
                                 case sf::Event::KeyPressed:
                                           if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                                           {       // start the thread here;
                                                    /*
                                                       thread_1(sf::Thread(std::bind(&sorting::update_merge_sort, &objects, 0, count - 1)));
                                                       thread_1=(sf::Thread(std::bind(&sorting::update_merge_sort, &objects, 0, count - 1)));
                                                       std::thread th(&sorting::update_merge_sort, &objects, 0, count - 1);

                                                        //sf::Thread ti((std::bind(&sorting::update_merge_sort, &objects, 0, count - 1)));

                                //thread_1(new t1);--
   
                                  thread_1.launch();


                                                   */
                                            }
                        }
              ://render things
           }
}



i cant find a way to start the thread when key is pressed.
I tried declaring thread inside IF statement, But the thread scope gets over there itself.
So i used sf::Thread, and tried calling the sf::Thread constructor to update the thread values, but failed.
I tried all possible ways mentioned which has been inside comments.

I Hope you can provide a way to solve this .




Pages: [1]
anything