Static / Namespace / Global / ???

Hi, I am starting a little game project, a 2D RPG using Qt and OpenGL. I am just starting and would like to have an Art class, which would contain all the arts (textures, sprites, images in general) in the game. The art is to be stored in a hashtable (a pixmap is an image) :

QHash<QString, QPixmap> images;

and recupering the desired image with the following method :

1
2
3
4
QPixmap Art::getImage(QString name)
{
    return images.value(name);
}


My problem is that I would want to have access to this method from anywhere in the application, here's an example :

1
2
3
4
5
6
7
8
9
10
11
for(int col=0; col<colNb; ++col)
    {
        for(int row=0; row<rowNb; ++row)
        {
            InventorySlot* temp = new InventorySlot();
            temp->setFixedSize(boxSize, boxSize);
            temp->setPixmap(Art::getImage("emptyBox"));
            invSlots.append(temp);
            layout->addWidget(temp, row, col);
        }
    }


I get that I need a namespace to have this syntax, I just don't get how I can make it the way I want.

Is this possible ? Is this Art class a good way to get what I want ? If not, is there a more elegant method ?

Thank you !
1
2
3
4
namespace Art{
   QHash<QString, QPixmap> images;
   QPixmap getImage(QString name);
}

or
1
2
3
4
5
6
class Art{
   public:
   static QHash<QString, QPixmap> images;
   static QPixmap getImage(QString name);
};
QHash<QString, QPixmap> Art::images;

I'm not sure what will happen if you put the first snippet in a header. Possibly a redefinition error. From the second snippet, you put the class part in a header and then declare all static member variables in some cpp.
Topic archived. No new replies allowed.