1 | /* |
2 | Copyright (C) 2010 Henry de Valence <hdevalence@gmail.com> |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | |
9 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License along |
15 | with this program; if not, write to the Free Software Foundation, Inc., |
16 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 | |
18 | */ |
19 | |
20 | #ifndef TEXTUREMANAGER_H |
21 | #define TEXTUREMANAGER_H |
22 | |
23 | #include <QObject> |
24 | #include <QHash> |
25 | |
26 | #include <config-kstars.h> |
27 | |
28 | class QGLWidget; |
29 | class QImage; |
30 | |
31 | /** @brief a singleton class to manage texture loading/retrieval |
32 | */ |
33 | class TextureManager : public QObject |
34 | { |
35 | Q_OBJECT |
36 | public: |
37 | /** @short Create the instance of TextureManager */ |
38 | static TextureManager* Create(); |
39 | |
40 | /** Return texture image. If image is not found in cache tries to |
41 | * load it from disk if that fails too returns reference to empty |
42 | * image. */ |
43 | static const QImage& getImage(const QString& name); |
44 | |
45 | #ifdef HAVE_OPENGL |
46 | /** Bind OpenGL texture. Acts similarly to getImage but does |
47 | * nothing if image is not found in the end */ |
48 | static void bindTexture(const QString& name, QGLWidget* cxt); |
49 | |
50 | /** Bind OpenGL texture using QImage as source */ |
51 | static void bindFromImage(const QImage& image, QGLWidget* cxt); |
52 | #endif |
53 | |
54 | private: |
55 | /** Shorthand for iterator to hashtable */ |
56 | typedef QHash<QString,QImage>::const_iterator CacheIter; |
57 | |
58 | /** Private constructor */ |
59 | explicit TextureManager(QObject* parent = 0); |
60 | /** Try find image in the cache and then to load it from disk if |
61 | * it's not found */ |
62 | static CacheIter findTexture(const QString& name); |
63 | |
64 | |
65 | // Pointer to singleton instance |
66 | static TextureManager* m_p; |
67 | // List of named textures |
68 | QHash<QString,QImage> m_textures; |
69 | |
70 | // Prohibit copying |
71 | TextureManager(const TextureManager&); |
72 | TextureManager& operator = (const TextureManager&); |
73 | }; |
74 | |
75 | #endif // TEXTUREMANAGER_H |
76 | |