Tulip  5.1.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 <stdint.h>
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  /**
70  * Return the texture manager singleton, il singleton doesn't exist this function create it
71  */
72  static GlTextureManager &getInst() {
73  if(!inst)
74  inst=new GlTextureManager();
75 
76  return *inst;
77  }
78 
79  /**
80  * Change the current OpenGl context (each OpenGl window have a different OpenGl context)
81  */
82  void changeContext(uintptr_t context);
83  /**
84  * Remove all textures of an OpenGl context and remove this context
85  */
86  void removeContext(uintptr_t context);
87 
88  /**
89  * Return texture info (id, width and height) for the given name
90  */
91  GlTexture getTextureInfo(const std::string&);
92 
93  /**
94  * Check if a texture fo the given name exists in the current context
95  */
96  bool existsTexture(const std::string& filename);
97  /**
98  * Load texture with given name
99  */
100  bool loadTexture(const std::string&);
101  /**
102  * Remove texture with given name
103  */
104  void deleteTexture(const std::string &);
105  /**
106  * Begin a new texture with given name
107  */
108  void beginNewTexture(const std::string&);
109  /**
110  * Activate a texture with given name
111  */
112  bool activateTexture(const std::string&,unsigned int);
113  /**
114  * Activate a texture with given name
115  */
116  bool activateTexture(const std::string&);
117  /**
118  * Disable texture with given name
119  */
120  void desactivateTexture();
121  /**
122  * Set animationStep for next textures (for next activateTexture)
123  */
124  void setAnimationFrame(unsigned int id) {
125  animationFrame=id;
126  }
127  /**
128  * Get animationStep of next textures
129  */
130  unsigned int getAnimationFrame() {
131  return animationFrame;
132  }
133  /**
134  * Clear vector of textures with error
135  */
136  void clearErrorVector() {
137  texturesWithError.clear();
138  }
139  /**
140  * Remove an entry of vector of textures with error
141  */
142  void removeEntryOfErrorVector(const std::string &name) {
143  texturesWithError.erase(name);
144  }
145 
146 
147  /**
148  * Register an external texture is GlTextureManager
149  */
150  void registerExternalTexture(const std::string &textureName, const GLuint textureId);
151 
152  /**
153  * Get Texture loader
154  */
155  static GlTextureLoader* getTextureLoader() {
156  return loader ? loader : (loader = new GlTextureLoader());
157  }
158 
159  /**
160  * Set Texture loader
161  */
162  static void setTextureLoader(GlTextureLoader* texLoader) {
163  if (loader)
164  delete loader;
165 
166  loader = texLoader;
167  }
168 
169 private:
170 
171  /**
172  * empty private constructor for singleton
173  */
174  GlTextureManager();
175 
176  static GlTextureManager* inst;
177  static GlTextureLoader* loader;
178 
179  uintptr_t currentContext;
180 
181  ContextAndTextureMap texturesMap;
182  std::set<std::string> texturesWithError;
183 
184  unsigned int animationFrame;
185 
186 };
187 
188 }
189 
190 #endif // Tulip_GLTEXTUREMANAGER_H
191 ///@endcond