Want more features on Pastebin? Sign Up, it's FREE!
Guest

Awl + SFML Demo

By: a guest on Sep 7th, 2011  |  syntax: C++  |  size: 2.00 KB  |  views: 170  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <Awl/Awl.hpp>
  2. #include <SFML/Graphics.hpp>
  3. #include <SFML/OpenGL.hpp>
  4.  
  5. sf::Texture tex1;
  6. sf::Texture tex2;
  7. sf::Texture tex3;
  8. bool loaded[3] = {false};
  9.  
  10. int main()
  11. {
  12.         sf::RenderWindow win(sf::VideoMode(640, 480), "Image Loader");
  13.         tex1.Create(1000, 700);
  14.         tex2.Create(1000, 700);
  15.         tex3.Create(1000, 700);
  16.         sf::Sprite sp1(tex1);
  17.         sf::Sprite sp2(tex2);
  18.         sf::Sprite sp3(tex3);
  19.        
  20.         sp1.SetPosition(10, 10);
  21.         sp2.SetPosition(50, 50);
  22.         sp3.SetPosition(100, 100);
  23.        
  24.         // Execute this block in an asynchronous way
  25.         AwlAsyncBlock
  26.         {
  27.                 tex1.LoadFromFile("big_image1.png");
  28.                 glFlush(); // Make sure the texture is updated in the main thread's GL context
  29.                
  30.                 // We don't want to care about concurrent access issues, so execute this
  31.                 // on the main thread ;
  32.                 // We could also protect our actions with a mutex, that's your choice
  33.                 AwlMainThreadBlock
  34.                 {
  35.                         // Suppose this is a non-atomic operation and suppose that doing
  36.                         // this in a concurrent way would cause issues
  37.                         loaded[0] = true;
  38.                 }
  39.                 AwlCloseMainThreadBlock;
  40.                
  41.                 // Repeat with second texture
  42.                 tex2.LoadFromFile("big_image2.png");
  43.                 glFlush();
  44.                
  45.                 AwlMainThreadBlock {
  46.                         loaded[1] = true;
  47.                 } AwlCloseMainThreadBlock;
  48.                
  49.                 // Repeat with third texture
  50.                 tex3.LoadFromFile("big_image3.png");
  51.                 glFlush();
  52.                
  53.                 AwlMainThreadBlock {
  54.                         loaded[2] = true;
  55.                 } AwlCloseMainThreadBlock;
  56.         }
  57.         AwlCloseAsyncBlock;
  58.        
  59.        
  60.         while (win.IsOpened() && aw::WorkLoop::Default().Run())
  61.         {
  62.                 sf::Event ev;
  63.                 // Here we run the WorkLoop to process the tasks we wanted to perform
  64.                 // on the main thread
  65.                 while (win.PollEvent(ev))
  66.                 {
  67.                         if (ev.Type == sf::Event::Closed)
  68.                                 win.Close();
  69.                        
  70.                         if (ev.Type == sf::Event::KeyPressed && ev.Key.Code == sf::Keyboard::Escape)
  71.                                 win.Close();
  72.                 }
  73.                
  74.                 win.Clear();
  75.                 // Show how loading progresses (we may not see any difference with 3 images but.. whatever)
  76.                 if (loaded[0]) win.Draw(sp1);
  77.                 if (loaded[1]) win.Draw(sp2);
  78.                 if (loaded[2]) win.Draw(sp3);
  79.                
  80.                 win.Display();
  81.         }
  82. }
clone this paste RAW Paste Data