File indexing completed on 2024-04-21 03:45:09

0001 /*
0002     SPDX-FileCopyrightText: 2010 Henry de Valence <hdevalence@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QObject>
0010 #include <QHash>
0011 
0012 #include <config-kstars.h>
0013 
0014 class QGLWidget;
0015 class QImage;
0016 
0017 /**
0018  * @brief a singleton class to manage texture loading/retrieval
0019  */
0020 class TextureManager : public QObject
0021 {
0022     Q_OBJECT
0023   public:
0024     /** @short Create the instance of TextureManager */
0025     static TextureManager *Create();
0026     /** @short Release the instance of TextureManager */
0027     static void Release();
0028 
0029     /**
0030      * Return texture image. If image is not found in cache tries to
0031      * load it from disk if that fails too returns reference to empty image.
0032      */
0033     static const QImage &getImage(const QString &name);
0034 
0035     /**
0036      * Clear the cache and discover the directories to load textures from.
0037      */
0038     static void discoverTextureDirs();
0039 #ifdef HAVE_OPENGL
0040     /**
0041      *  Bind OpenGL texture. Acts similarly to getImage but does
0042      *  nothing if image is not found in the end
0043      */
0044     static void bindTexture(const QString &name, QGLWidget *cxt);
0045 
0046     /** Bind OpenGL texture using QImage as source */
0047     static void bindFromImage(const QImage &image, QGLWidget *cxt);
0048 #endif
0049 
0050   private:
0051     /** Shorthand for iterator to hashtable */
0052     typedef QHash<QString, QImage>::const_iterator CacheIter;
0053 
0054     /** Private constructor */
0055     explicit TextureManager(QObject *parent = nullptr);
0056     /** Try find image in the cache and then to load it from disk if it's not found */
0057     static CacheIter findTexture(const QString &name);
0058 
0059     // Pointer to singleton instance
0060     static TextureManager *m_p;
0061     // List of named textures
0062     QHash<QString, QImage> m_textures;
0063 
0064     // List of texture directories
0065     std::vector<QString> m_texture_directories;
0066 
0067     // Prohibit copying
0068     TextureManager(const TextureManager &);
0069     TextureManager &operator=(const TextureManager &);
0070 };