SFML community forums

Help => General => Topic started by: Phoenix9103 on January 12, 2016, 02:06:09 pm

Title: Best way to handle events and drawing with separate threads or functions?
Post by: Phoenix9103 on January 12, 2016, 02:06:09 pm
So I am new to SFML and I was wondering what the general method of handling events and drawing is. Currently I am using a separate thread with a loop for drawing and the main thread for event handling.

The problem with this is that if I want something like a text object to change depending on an event then I can't do it in a practical manner (as far as I can see) because the text object would have to be created in the main thread and updated in the event code then that change has to be passed into the render thread somehow.

Current Code Example:
int main()
{
    sf::RenderWindow mainWindow(sf::VideoMode(856, 512), "Test");

    mainWindow.setActive(false);
    sf::Thread renderThread(&render, &mainWindow);
    renderThread.launch();
    sf::Text text;
    sf::Font font;
    font.loadFromFile("calibri.ttf");
    text.setFont(font);
    text.setString("Enter Text");

    while(mainWindow.isOpen())
    {
        sf::Event event;
        while(mainWindow.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                mainWindow.close();
            if(event.type == sf::Event::TextEntered)
                text.setString(static_cast<char>(event.text.unicode));
        }
    }
}

void render(sf::RenderWindow *window)
{
    sf::Vector2u windowSize = window->getSize();
    sf::Texture texture;
    sf::Sprite sprite;
    if(!texture.loadFromFile("a.jpg"))
        std::cerr << "Failed to load texture file" << std::endl;
    sprite.setTexture(texture);
    sprite.setTextureRect({0, 0, windowSize.x, windowSize.y});

    while(window->isOpen())
    {
        window->clear(sf::Color::Black);
        window->draw(sprite);
        window->display();
    }
}
 

So how can I do this? Also is it alright to create the objects there or should it be done somewhere else?
How about event handling, can it be done in a separate function that is called in the main loop (to make code neater)?
Title: Re: Best way to handle events and drawing with separate threads or functions?
Post by: zsbzsb on January 12, 2016, 02:23:07 pm
Don't use threads - make your code single threaded. It is highly unlikely that that you will gain anything from using threads, most likely you will just lose performance. Once you have an actual application use a profiler to see where the hot spots are and then optimize that code. Mulithreading is a very advanced concept and it is very easy to do it wrong and much harder to get right.
Title: Re: Best way to handle events and drawing with separate threads or functions?
Post by: Phoenix9103 on January 12, 2016, 03:07:39 pm
Thanks, that makes sense.

So how then can I separate portions of my code? If I was -- for example -- making a game. I wouldn't want the entire thing inside of the main function. But I assume that all of my sprites and textures etc have to be in that main function in order to draw them?
Title: Re: Best way to handle events and drawing with separate threads or functions?
Post by: Hapax on January 12, 2016, 04:40:24 pm
So how then can I separate portions of my code?
Objects.

The first step to try would be to create a "holder" class that holds a lot of your items. That way, only that one object is necessary instead of all of its internal objects. The application itself can be an object too and with all classes being in separate files, the code is separated too.

Here's the code for a simple game that I wrote (for practice):
Puzza (https://github.com/Hapaxia/MyPracticeBeginnerGames/tree/master/Puzza)
It uses objects and has hardly any code in the main function at all. The game itself might not be the best implementation but it should be easy enough to understand and follow through how the objects are used in it.