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

Author Topic: I need a little help with my Image Manager  (Read 2686 times)

0 Members and 1 Guest are viewing this topic.

MarthKoopa

  • Guest
I need a little help with my Image Manager
« on: July 03, 2010, 05:14:45 am »
This is a little image manager I wrote, but I don't know what I'm doing wrong.  Shouldn't this work?  (am using SFML 2.0 with Visual Studio 2010 Ultimate)

Code: [Select]

class zImage : public Image{
    public:
    string Name;
    string Category;

    void Load(string GetCategory, string getName){
        Name         = getName;
        Category     = GetCategory;
        LoadFromFile ( "Graphics/" + Category + "/" + Name + ".png" );
        SetSmooth    ( false );
    }
};

class ImageManager{
    std::vector<zImage> ImageList;

    public:
    zImage &GetImage(string GetCategory, string GetName){
        for ( long index = 0; index < (long)ImageList.size(); index++ ) {
            if (ImageList[index].Category == GetCategory){
                if (ImageList[index].Name == GetName){
                    return ImageList[index];
                }
            }
        }

        // Image was not found, so create it and return it
        zImage tempImage;
        tempImage.Load(GetCategory, GetName);
        ImageList.push_back(tempImage);
        return tempImage;
    }
}Images;


Example usage:

Code: [Select]

Sword.SetImage(Images.GetImage("Swords", "Iron"));


I get an unhandled exception and it brings me to this code in MutexImpl.cpp

Code: [Select]

////////////////////////////////////////////////////////////
void MutexImpl::Lock()
{
    EnterCriticalSection(&myMutex);
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
I need a little help with my Image Manager
« Reply #1 on: July 03, 2010, 09:08:47 am »
Code: [Select]
       zImage tempImage;
        tempImage.Load(GetCategory, GetName);
        ImageList.push_back(tempImage);
        return tempImage;

Here you return a reference to the local/temporary image, not the copy that is stored inside the list.
You compiler should warn you about that :P
Laurent Gomila - SFML developer

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
I need a little help with my Image Manager
« Reply #2 on: July 13, 2010, 07:52:18 am »
Is there any constructor of your class ?
Does it work after 5 seconds or 2 ?
Mindiell
----

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
I need a little help with my Image Manager
« Reply #3 on: July 13, 2010, 08:17:38 am »
Don't use std::vector, it may have to move its elements elsewhere in memory when it needs to grow, invalidating all the references that you returned previously.

You should use a std::map, using the name as the key.
Laurent Gomila - SFML developer

 

anything