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 - Phoenix9103

Pages: [1]
1
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?

2
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)?

Pages: [1]
anything