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

Author Topic: Threading  (Read 1151 times)

0 Members and 1 Guest are viewing this topic.

catalinnic

  • Newbie
  • *
  • Posts: 19
    • View Profile
Threading
« on: September 28, 2019, 03:46:34 pm »
I want to use multi-thread for a level editor with test levels feature, now i want to test multi-thread on this piece of code:
#include <TGUI/TGUI.hpp>
#include <SFML/System.hpp>

sf::View Canvas_view;

// Move canvas view
void Move()
{
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                Canvas_view.move(0, -0.2f);
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                Canvas_view.move(0, 0.2f);
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                Canvas_view.move(-0.2f, 0);
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                Canvas_view.move(-0.2f, 0);
}

sf::Texture texture;
sf::Sprite sprite;

// Test thread
void test(tgui::Canvas::Ptr canvas)
{
        Move();
        canvas->clear();
        canvas->draw(sprite);// Draw sprites
        canvas->setView(Canvas_view);// Set view
        canvas->display();
}

int main()
{
    sf::RenderWindow Window;
        Window.create(sf::VideoMode(800, 400), "app", sf::Style::Default);
        tgui::Gui App;
    App.setTarget(Window);
    sf::View view = Window.getDefaultView();
        // Canvas creaton
        tgui::Canvas::Ptr canvas = tgui::Canvas::create();
        canvas->setPosition(100, 100);
        canvas->setSize(200, 200);
        canvas->clear();
        App.add(canvas, "Canvas");
        texture.loadFromFile("test.png");
        sprite.setTexture(texture);
        sf::Event event;
        while (Window.isOpen())
        {
                while (Window.pollEvent(event))
                {
                        // General stuff
                        if(event.type == sf::Event::Closed)
                                Window.close();
                        if(event.type == sf::Event::Resized)
            {
                view.reset(sf::FloatRect(0 , 0, Window.getSize().x, Window.getSize().y));
                App.setView(view);
            }  
                        App.handleEvent(event);
                }
                Window.clear();
                App.draw();
                Window.setView(view);
                Window.display();
        }
        return 0;
}
And i don't know how to do it properly without crashing the whole program or the computer
« Last Edit: September 28, 2019, 03:48:36 pm by catalinnic »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Threading
« Reply #1 on: September 28, 2019, 04:28:54 pm »
And what's your question? I assume you aren't expecting someone else to write your multi-threading code. ;)

Personally, I recommend to stick to a single thread, especially if you have no knowledge on parallel programming or proper shared resource handling. In addition to adding a lot of complexity to your code and most certainly some hard to debug bugs, you also most likely won't really gain anything from doing so.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/