SFML community forums

Help => Graphics => Topic started by: Jatentaki on May 08, 2009, 06:42:15 pm

Title: [Solved] Problem with sf::sprite.SetImage problem
Post by: Jatentaki on May 08, 2009, 06:42:15 pm
Code: [Select]

int temp;
fin >> temp;
if (temp == 1)
{
   if (!Image.LoadFromFile("s596.bmp"))
   {
      fout << "Error s596.bmp" << std::endl;
   }
   else
   {
      fout << "s596.bmp loaded" << std::endl;
   }
   Sprite[i][j].SetImage(Image);
}
   if (temp == 2)
  {
     if (!Image.LoadFromFile("s543.bmp"))
     {
        fout << "Error s543.bmp" << std::endl;
     }
     else
     {
        fout << "s543.bmp loaded" << std::endl;
     }
     Sprite[i][j].SetImage(Image);
}
Sprite[i][j].SetPosition(0.f+(32*j), 0.f+(32*i));


I am using this inside a loop whichs aim is to write a array of 32x32px pictures in a nice square using data from file :) As you see it reads to temp variable a number from this map file and then in one case shows first picture and in second case second picture. I am new to programming graphic things so correct me if im wrong but i think creating huge amounts of sf::image files would be a waste of RAM so i try to reuse this variable. The problem is that whole square consists of sprites of only one kind (s596.bmp). As you can see I made a nice log but it shows that both types are being loaded and there are no errors in it. Whats the problem?

1) I used search and didnt find solution
2) I changed the error/no error messages to log from polish to english but if its the cause of problems im not gonna use c++ anymore  :P
Title: [Solved] Problem with sf::sprite.SetImage problem
Post by: Hiura on May 08, 2009, 07:02:17 pm
Where is defined 'Image' ?

You may want use an ImageManager (http://hiura.tuxfamily.org/wiki/doku.php?id=cpp:sf:resourcemanager) to save RAM.
Title: [Solved] Problem with sf::sprite.SetImage problem
Post by: Jatentaki on May 08, 2009, 07:13:50 pm
Code: [Select]

    sf::Image Image;
    // Create the sprite
    sf::Sprite Sprite[20][20];

Here is Sprite declaration and I will have a look at resource manager in a moment ;)
Title: [Solved] Problem with sf::sprite.SetImage problem
Post by: Hiura on May 08, 2009, 07:21:46 pm
At present time your code doesn't work the way you expect.

You have only one Image for more than one file. So the second loading will erase the first picture. And at the end, all your sprite will draw the same Image.

You have to use one sf::Image per file. Or use a Manager like mine.
Title: [Solved] Problem with sf::sprite.SetImage problem
Post by: Jatentaki on May 08, 2009, 07:23:42 pm
Ah, I forgot sprite is just a pointer to image :/ Sorry for your waste of time
Title: [Solved] Problem with sf::sprite.SetImage problem
Post by: Hiura on May 08, 2009, 07:44:07 pm
no problem  :wink: