0

Visual Studio Express 2012, CTP1 c++ compiler

The following code works. It loads an image and displays it on the window until you close it.

#include <memory>
#include <SFML/Graphics.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(800,600), "hello");

    auto Load = []() -> std::unique_ptr<sf::Texture> {
        std::unique_ptr<sf::Texture> tex_ptr(new sf::Texture);
        tex_ptr->loadFromFile("hello.png");
        return tex_ptr;
    };    
    auto tex_ptr = Load();
    sf::Sprite spr(*tex_ptr);

    while (window.isOpen())
    {
        sf::Event ev;
        while (window.pollEvent(ev))
        {
            if (ev.type == sf::Event::Closed)
                window.close();
        }    
        window.clear();
        window.draw(spr);
        window.display();
    }
}

In the following code, I'm trying to load the image asyncronously using std::async. It prints "load success", which indicates that the load succeeded in the lambda. Then, outside, after I've retrieved the texture from the future, I check other properties of the texture. The size prints out correctly. However, no image shows up. I just get a black window, which closes on command.

#include <future>
#include <memory>
#include <iostream>
#include <SFML/Graphics.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(800,600), "hello");

    auto Load = []() -> std::unique_ptr<sf::Texture> {
        std::unique_ptr<sf::Texture> tex_ptr(new sf::Texture);
        if (tex_ptr->loadFromFile("hello.png"))
            std::cout << "load success\n";
        else
            std::cout << "load failure\n";
        return tex_ptr;
    };

    auto tex_ptr_future = std::async(std::launch::async, Load);

    auto tex_ptr = tex_ptr_future.get();
    sf::Sprite spr(*tex_ptr);

    // Oddly, this prints out exactly what I expect
    auto size = tex_ptr->getSize();
    std::cout << size.x << 'x' << size.y << '\n';

    while (window.isOpen())
    {
        sf::Event ev;
        while (window.pollEvent(ev))
        {
            if (ev.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(spr); // nothin'
        window.display();
    }
}

Does anybody see anything I'm doing wrong?

5
  • 2
    That code works for me. Maybe it's because OpenGL can't really share state between threads and SFML uses OpenGL internally? The size field obviously doesn't need OpenGL to work.
    – Pubby
    Dec 17, 2012 at 7:23
  • @Pubby Of course! Only one OpenGL context could be assigned to a thread and an OpenGL context can only be assigned to a single thread. That means that you need to create a new OpenGL context for your loader's thread and share its resources with the main context. Dec 17, 2012 at 7:26
  • @Pubby: Sounds reasonable. So how does it work for you? Dec 17, 2012 at 7:27
  • @BenjaminLindley I don't know why it works for me, but I can confirm Load is being run in a separate thread. Maybe it's not using OGL for me?
    – Pubby
    Dec 17, 2012 at 7:29
  • @Pubby Maybe it's not running asynchronously (on the same thread). As for SFML not using OpenGL, not feasible. Dec 17, 2012 at 7:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.