Tulip  5.3.0
Large graphs analysis and drawing
GlTextureManager.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef Tulip_GLTEXTUREMANAGER_H
22 #define Tulip_GLTEXTUREMANAGER_H
23 
24 #include <tulip/tulipconf.h>
25 
26 #include <tulip/OpenGlIncludes.h>
27 
28 #include <set>
29 #include <map>
30 #include <string>
31 
32 #include <cstdint>
33 
34 namespace tlp {
35 
36 struct GlTexture {
37  GLuint *id;
38  int height;
39  int width;
40  unsigned int spriteNumber;
41 };
42 
43 /**
44  * \brief Class to load textures
45  */
46 class TLP_GL_SCOPE GlTextureLoader {
47 public:
48  /**
49  * Load a texture from a file
50  * in the default implementation only bmp, jpeg and png files
51  * can be loaded.
52  * Return false if an error occurs
53  */
54  virtual bool loadTexture(const std::string &filename, GlTexture &texture);
55 
56  virtual ~GlTextureLoader() {}
57 };
58 
59 /** \brief Class to manage textures
60  * Singleton class to load/store textures need by OpenGL rendering
61  */
62 class TLP_GL_SCOPE GlTextureManager {
63 
64  typedef std::map<std::string, GlTexture> TextureUnit;
65  typedef std::map<uintptr_t, TextureUnit> ContextAndTextureMap;
66 
67 public:
68  /**
69  * Return the texture manager singleton, il singleton doesn't exist this function create it
70  */
71  static GlTextureManager &getInst() {
72  if (!inst)
73  inst = new GlTextureManager();
74 
75  return *inst;
76  }
77 
78  /**
79  * Change the current OpenGl context (each OpenGl window have a different OpenGl context)
80  */
81  void changeContext(uintptr_t context);
82  /**
83  * Remove all textures of an OpenGl context and remove this context
84  */
85  void removeContext(uintptr_t context);
86 
87  /**
88  * Return texture info (id, width and height) for the given name
89  */
90  GlTexture getTextureInfo(const std::string &);
91 
92  /**
93  * Check if a texture fo the given name exists in the current context
94  */
95  bool existsTexture(const std::string &filename);
96  /**
97  * Load texture with given name
98  */
99  bool loadTexture(const std::string &);
100  /**
101  * Remove texture with given name
102  */
103  void deleteTexture(const std::string &);
104  /**
105  * Begin a new texture with given name
106  */
107  void beginNewTexture(const std::string &);
108  /**
109  * Activate a texture with given name
110  */
111  bool activateTexture(const std::string &, unsigned int);
112  /**
113  * Activate a texture with given name
114  */
115  bool activateTexture(const std::string &);
116  /**
117  * Disable texture with given name
118  */
119  void desactivateTexture();
120  /**
121  * Set animationStep for next textures (for next activateTexture)
122  */
123  void setAnimationFrame(unsigned int id) {
124  animationFrame = id;
125  }
126  /**
127  * Get animationStep of next textures
128  */
129  unsigned int getAnimationFrame() {
130  return animationFrame;
131  }
132  /**
133  * Clear vector of textures with error
134  */
135  void clearErrorVector() {
136  texturesWithError.clear();
137  }
138  /**
139  * Remove an entry of vector of textures with error
140  */
141  void removeEntryOfErrorVector(const std::string &name) {
142  texturesWithError.erase(name);
143  }
144 
145  /**
146  * Register an external texture is GlTextureManager
147  */
148  void registerExternalTexture(const std::string &textureName, const GLuint textureId);
149 
150  /**
151  * Get Texture loader
152  */
153  static GlTextureLoader *getTextureLoader() {
154  return loader ? loader : (loader = new GlTextureLoader());
155  }
156 
157  /**
158  * Set Texture loader
159  */
160  static void setTextureLoader(GlTextureLoader *texLoader) {
161  if (loader)
162  delete loader;
163 
164  loader = texLoader;
165  }
166 
167 private:
168  /**
169  * empty private constructor for singleton
170  */
171  GlTextureManager();
172 
173  static GlTextureManager *inst;
174  static GlTextureLoader *loader;
175 
176  uintptr_t currentContext;
177 
178  ContextAndTextureMap texturesMap;
179  std::set<std::string> texturesWithError;
180 
181  unsigned int animationFrame;
182 };
183 } // namespace tlp
184 
185 #endif // Tulip_GLTEXTUREMANAGER_H
186 ///@endcond