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

Author Topic: Best way to handle events and drawing with separate threads or functions?  (Read 3456 times)

0 Members and 1 Guest are viewing this topic.

Phoenix9103

  • Newbie
  • *
  • Posts: 2
    • View Profile
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)?

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
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.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Phoenix9103

  • Newbie
  • *
  • Posts: 2
    • View Profile
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?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
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
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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*