SFML community forums
Help => Graphics => Topic started by: panithadrum 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!
// 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
-
Can you provide a complete and minimal source code that I can test directly?
-
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 :)
-
I wrote a minimal example. This must be thread related.
#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"...
-
You have to declare a valid graphics context in any thread that uses the graphics module.
void Run()
{
sf::Context context;
for(unsigned int i = 0; i < image_vector.size(); ++i)
{
image_map[image_vector[i]].LoadFromFile(image_vector[i]);
}
}
-
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.
-
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.
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?
-
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?
-
Seems to work if I wait for the sf::Context in the thread to be created before I create the sf::Window
-
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.
-
Don't blame the documentation, it doesn't exist yet (for SFML 2) ;)
Of course it will contain explanations about this tricky stuff.
-
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.