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

Author Topic: need help with image manager.  (Read 4517 times)

0 Members and 1 Guest are viewing this topic.

Dark Byte

  • Newbie
  • *
  • Posts: 30
    • View Profile
need help with image manager.
« 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();
}


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
need help with image manager.
« Reply #1 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 ;)
Laurent Gomila - SFML developer

Dark Byte

  • Newbie
  • *
  • Posts: 30
    • View Profile
need help with image manager.
« Reply #2 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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
need help with image manager.
« Reply #3 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?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Dark Byte

  • Newbie
  • *
  • Posts: 30
    • View Profile
need help with image manager.
« Reply #4 on: October 15, 2010, 11:45:36 pm »
Does anyone know why the images are white? With the correct width and height.

Relic

  • Newbie
  • *
  • Posts: 43
    • View Profile
need help with image manager.
« Reply #5 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.

Dark Byte

  • Newbie
  • *
  • Posts: 30
    • View Profile
need help with image manager.
« Reply #6 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
need help with image manager.
« Reply #7 on: October 16, 2010, 07:32:11 pm »
RetrieveImage returns a copy instead of the original image. Use a reference (const sf::Image&) instead.
Laurent Gomila - SFML developer

Relic

  • Newbie
  • *
  • Posts: 43
    • View Profile
need help with image manager.
« Reply #8 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.

 

anything