SFML community forums

Help => Graphics => Topic started by: Dark Byte on October 14, 2010, 11:15:54 pm

Title: need help with image manager.
Post by: Dark Byte on October 14, 2010, 11:15:54 pm
I need help creating an image manager. I have one, but calling something like
Code: [Select]

sprite.SetImage(Manager.RetrieveImage("gfx/player.tga"))


Will cause the program to crash.

Here is the source:
ImageManager.h
Code: [Select]

#include <map>

class ImageManager
{
 private:
map<string, sf::Image> Data;

 public:
sf::Image RetrieveImage(const string path);
bool LoadImage(string path);
int ImageCount();

} ;



ImageManager.cpp
Code: [Select]

#include "ImageManager.h"

sf::Image ImageManager::RetrieveImage(const string path)
{
if ( this->Data.find(path) != this->Data.end() )
{
return this->Data[path];
}
}

bool ImageManager::LoadImage(string path)
{
if ( this->Data.find(path) !=  this->Data.end() )
return true;
else
{
sf::Image IMGTemp;
if ( IMGTemp.LoadFromFile(path) )
{
this->Data[path] = IMGTemp;
return true;
}
else
return false;
}
}

int ImageManager::ImageCount()
{
return this->Data.size();
}

Title: need help with image manager.
Post by: Laurent on October 14, 2010, 11:18:15 pm
In RetrieveImage, if the path is not found, nothing is returned. You have to return a sf::Image instance, or throw an exception.

Your compiler may be very badly configured, if it silently compiles this piece of code ;)
Title: need help with image manager.
Post by: Dark Byte on October 15, 2010, 03:24:30 am
Alright. Thanks.
This is what I have for RetrieveImage
Code: [Select]

sf::Image ImageManager::RetrieveImage(const string path)
{
if ( this->Data.find(path) != this->Data.end() )
{
return this->Data[path];
}
else
{
try
{
throw "failed to load image "+path;
}
catch (string E)
{
cout << "An exception occured: " << E;
}
}
}


When an image is returned it has the correct width and height, but it is fully white. I am passing a pointer of the a global image manager to a function to draw the graphics. COuld that be what is wrong?
Title: need help with image manager.
Post by: Nexus on October 15, 2010, 07:50:37 am
Quote from: "Dark Byte"
Code: [Select]
try
{
throw "failed to load image "+path;
}
catch (string E)
{
cout << "An exception occured: " << E;
}
Why throw an exception just to catch it immediately?
Title: need help with image manager.
Post by: Dark Byte on October 15, 2010, 11:45:36 pm
Does anyone know why the images are white? With the correct width and height.
Title: need help with image manager.
Post by: Relic on October 16, 2010, 08:08:23 am
This may help you: http://www.sfml-dev.org/forum/viewtopic.php?t=2998

And SFML Api doc says:
Code: [Select]
void sf::Sprite::SetImage  ( const Image &  image,  
  bool  adjustToNewSize = false  
 )    

Change the source image of the sprite.

The image argument refers to an image that must exist as long as the sprite uses it. Indeed, the sprite doesn't store its own copy of the image, but rather keeps a pointer to the one that you passed to this function. If the source image is destroyed and the sprite tries to use it, it may appear as a white rectangle. If adjustToNewSize is true, the SubRect property of the sprite is adjusted to the size of the new image. If it is false, the SubRect is unchanged.

So, maybe you eventually assign a wrong image pointer to your sprite. You should show a bit more code to make out what goes wrong.
Title: need help with image manager.
Post by: Dark Byte on October 16, 2010, 05:01:46 pm
I found the problem. It is with RetrieveImage. I made the map public and when I use something like
Code: [Select]

Sprite.SetImage( IM.Data["gfx/player.bmp"] );


It works. So I need to find the problem with RetrieveImage.
Title: need help with image manager.
Post by: Laurent on October 16, 2010, 07:32:11 pm
RetrieveImage returns a copy instead of the original image. Use a reference (const sf::Image&) instead.
Title: need help with image manager.
Post by: Relic on October 16, 2010, 08:27:22 pm
Damn. :oops: I did not notice that Dark Byte uses a copy:
Code: [Select]
sf::Image ImageManager::RetrieveImage(const string path)
In fact, the sprite gets a pointer to a temporary image object, therefore it is white.