#include #include #include sf::Texture tex1; sf::Texture tex2; sf::Texture tex3; bool loaded[3] = {false}; int main() { sf::RenderWindow win(sf::VideoMode(640, 480), "Image Loader"); tex1.Create(1000, 700); tex2.Create(1000, 700); tex3.Create(1000, 700); sf::Sprite sp1(tex1); sf::Sprite sp2(tex2); sf::Sprite sp3(tex3); sp1.SetPosition(10, 10); sp2.SetPosition(50, 50); sp3.SetPosition(100, 100); // Execute this block in an asynchronous way AwlAsyncBlock { tex1.LoadFromFile("big_image1.png"); glFlush(); // Make sure the texture is updated in the main thread's GL context // We don't want to care about concurrent access issues, so execute this // on the main thread ; // We could also protect our actions with a mutex, that's your choice AwlMainThreadBlock { // Suppose this is a non-atomic operation and suppose that doing // this in a concurrent way would cause issues loaded[0] = true; } AwlCloseMainThreadBlock; // Repeat with second texture tex2.LoadFromFile("big_image2.png"); glFlush(); AwlMainThreadBlock { loaded[1] = true; } AwlCloseMainThreadBlock; // Repeat with third texture tex3.LoadFromFile("big_image3.png"); glFlush(); AwlMainThreadBlock { loaded[2] = true; } AwlCloseMainThreadBlock; } AwlCloseAsyncBlock; while (win.IsOpened() && aw::WorkLoop::Default().Run()) { sf::Event ev; // Here we run the WorkLoop to process the tasks we wanted to perform // on the main thread while (win.PollEvent(ev)) { if (ev.Type == sf::Event::Closed) win.Close(); if (ev.Type == sf::Event::KeyPressed && ev.Key.Code == sf::Keyboard::Escape) win.Close(); } win.Clear(); // Show how loading progresses (we may not see any difference with 3 images but.. whatever) if (loaded[0]) win.Draw(sp1); if (loaded[1]) win.Draw(sp2); if (loaded[2]) win.Draw(sp3); win.Display(); } }