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

Author Topic: SFML2: Strange behavior (threading GL)  (Read 4615 times)

0 Members and 1 Guest are viewing this topic.

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
SFML2: Strange behavior (threading GL)
« on: November 22, 2009, 11:00:03 pm »
I am using Windows XP, SFML2, revision 1279
I have an sf::Image manager, and it loads well. But when I try to use a sf::Sprite, it is not well drawn, but well saved to file!
Code: [Select]
// Load is called once
void Scene::load()
{
marker_sprite.SetImage(*game.resource["data/image/marker.bmp"]);
marker_sprite.GetImage()->SaveToFile("marker.bmp"); // The file is saved ok
}

// Draw is called every frame
void Scene::draw()
{
game.window.Draw(marker_sprite); // Sprite just draws a white square
// If I try to save the image in this scope, the image is saved ok
marker_sprite.GetImage()->SaveToFile("marker2.bmp");
}

// To make sure that my stored image is not deleted, I save the image when I close the window
void Scene::closed()
{
marker_sprite.GetImage()->SaveToFIle("marker3.bmp");
}


All three saved bmp's are OK! I don't understand why it draws a white square.

If I use a sf::Image stored in my Scene class it works.

I think it's not my fault because I can save the images correctly :-S

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SFML2: Strange behavior (threading GL)
« Reply #1 on: November 22, 2009, 11:17:35 pm »
Can you provide a complete and minimal source code that I can test directly?
Laurent Gomila - SFML developer

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
SFML2: Strange behavior (threading GL)
« Reply #2 on: November 22, 2009, 11:43:50 pm »
Hmm Let me test it some more time. I've been deeping a lot, and then I tried the simplest example possible.

Seems that my resource manager is the problem, but I still wonder why I could save images and not to draw them :)

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
SFML2: Strange behavior (threading GL)
« Reply #3 on: November 22, 2009, 11:59:38 pm »
I wrote a minimal example. This must be thread related.
Code: [Select]

#include <map>
#include <string>
#include <sfml/Graphics.hpp>

class ResourceTwo : public sf::Thread
{
private:
// Container
std::map<std::string, sf::Image> image_map;
std::vector<std::string> image_vector;

public:
// Push image to wish list
void PushImage(const std::string& file_name)
{
image_vector.push_back(file_name);
}
// Get image
const sf::Image& operator[](const std::string& file_name)
{
return image_map[file_name];
}

private:
// Thread
void Run()
{
for(unsigned int i = 0; i < image_vector.size(); ++i)
{
image_map[image_vector[i]].LoadFromFile(image_vector[i]);
}
}
}resource;

std::string image_name("data/image/marker.bmp");

int main()
{
resource.PushImage(image_name);
resource.Launch();
resource.Wait();

sf::Sprite sprite;
sprite.SetImage(resource[image_name]);
sprite.GetImage()->SaveToFile("lol.bmp");

sf::RenderWindow window;
window.Create(sf::VideoMode(320, 240), "Error");

window.Clear();
window.Draw(sprite);
window.Display();

sf::Event sf_event;
while(window.IsOpened())
{
while(window.GetEvent(sf_event))
{
if(sf_event.Type == sf::Event::Closed)
window.Close();
}
}
}


EDIT: Definetively it is the thread loading fault. Any chances to load something in a thread?

EDIT2: Oh, I saw a loading thread in the wiki. What is the problem? Could you explain, Laurent?
Maybe I have to active my window when loading my image?
If so, a std::cerr message is shown in the console: something like "error activating OpenGL context"...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SFML2: Strange behavior (threading GL)
« Reply #4 on: November 23, 2009, 10:27:45 am »
You have to declare a valid graphics context in any thread that uses the graphics module.

Code: [Select]
  void Run()
   {
      sf::Context context;

      for(unsigned int i = 0; i < image_vector.size(); ++i)
      {
         image_map[image_vector[i]].LoadFromFile(image_vector[i]);
      }
   }
Laurent Gomila - SFML developer

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
SFML2: Strange behavior (threading GL)
« Reply #5 on: November 23, 2009, 11:27:28 am »
Ok... I don't understand this class. It works fine simply putting sf::Context in my thread, but it just works for 1.5 and 1.6.

Using 2.0 the result is the same (it doesn't upload the pixels to the GPU), and I also get OpenGL messages in the console.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SFML2: Strange behavior (threading GL)
« Reply #6 on: November 23, 2009, 03:54:37 pm »
OpenGL context handling has changed a lot in SFML 2.

This situation worked in SFML 1.5, but many other don't. sf::Context in SFML 2 make everything work.

Quote
Using 2.0 the result is the same (it doesn't upload the pixels to the GPU), and I also get OpenGL messages in the console.

You mean that using sf::Context doesn't solve anything? Can you show your updated code?
Laurent Gomila - SFML developer

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
SFML2: Strange behavior (threading GL)
« Reply #7 on: November 23, 2009, 06:13:06 pm »
Sorry I didn't notice that my code worked now :D

EDIT: Hmm... Seems that most of the time I get "Failed to share the OpenGL context" in SFML2

Any way to solve that? Maybe is my notebook's fault. The code is the same you write (creating a sf::Context in the thread function).

Laurent, will you write something to check internal SFML errors?

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
SFML2: Strange behavior (threading GL)
« Reply #8 on: November 24, 2009, 12:40:16 pm »
Seems to work if I wait for the sf::Context in the thread to be created before I create the sf::Window

stubler

  • Newbie
  • *
  • Posts: 17
    • View Profile
Add this to threading tutorial
« Reply #9 on: March 15, 2010, 03:38:01 am »
I spent about two days chasing down an error with this solution. This should be added to the tutorial on threading.

I was trying to create a multitheaded project in which the primary thread animated images as a slide show while a background thread queued up images. I was using a development version of SFML 2.0 and assumed my problem was with RenderImage. It took a while but I stripped the problem back to a minimal implementation and eventually found this post. It should be made more conspicuous.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SFML2: Strange behavior (threading GL)
« Reply #10 on: March 15, 2010, 08:44:35 am »
Don't blame the documentation, it doesn't exist yet (for SFML 2) ;)

Of course it will contain explanations about this tricky stuff.
Laurent Gomila - SFML developer

stubler

  • Newbie
  • *
  • Posts: 17
    • View Profile
SFML2: Strange behavior (threading GL)
« Reply #11 on: March 15, 2010, 12:15:33 pm »
Sorry,
From the sixth post in this discussion thread, I assumed that this requirement ( sf::Context context; ) was required for multithreaded application using SFML 1.5 as well.