TDME2  1.9.200
SimpleTextureAtlas.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <unordered_map>
5 #include <vector>
6 
7 #include <tdme/tdme.h>
8 #include <tdme/engine/fwd-tdme.h>
10 
11 using std::string;
12 using std::unordered_map;
13 using std::vector;
14 
16 
17 /**
18  * Simple texture atlas
19  * @author Andreas Drewke
20  */
22 public:
23  static constexpr int TEXTURE_IDX_NONE { -1 };
24  static constexpr int ATLAS_TEXTURE_SIZE { 512 };
25  static constexpr int ATLAS_TEXTURE_BORDER { 32 };
26 
27  // forbid class copy
29 
30  /**
31  * Public constructor
32  * @param id texture id
33  */
34  SimpleTextureAtlas(const string& id);
35 
36  /**
37  * Public destructor
38  */
40 
41  /**
42  * Returns specific atlas texture index within atlas
43  * @param texture texture
44  * @return atlas texture id
45  */
46  inline int getTextureIdx(Texture* texture) {
47  auto it = textureToAtlasTextureIdxMapping.find(texture);
48  if (it == textureToAtlasTextureIdxMapping.end()) {
49  return TEXTURE_IDX_NONE;
50  }
51  return it->second;
52  }
53 
54  /**
55  * Add texture
56  * @param texture texture
57  * @return atlas texture id
58  */
59  int addTexture(Texture* texture);
60 
61  /**
62  * Remove texture
63  * @param texture texture
64  */
65  void removeTexture(Texture* texture);
66 
67  /**
68  * Update texture atlas
69  */
70  void update();
71 
72  /**
73  * @return is requiring update
74  */
75  inline bool isRequiringUpdate() {
76  return requiresUpdate;
77  }
78 
79  /**
80  * @return atlas texture
81  */
83  return atlasTexture;
84  }
85 
86 private:
87  bool requiresUpdate { false };
89  Texture* atlasTexture { nullptr };
90  unordered_map<Texture*, int> textureReferenceCounter;
91  unordered_map<Texture*, int> textureToAtlasTextureIdxMapping;
92  unordered_map<int, Texture*> atlasTextureIdxToTextureMapping;
93  vector<int> freeTextureIds;
94 };
Texture entity.
Definition: Texture.h:24
void removeTexture(Texture *texture)
Remove texture.
unordered_map< Texture *, int > textureReferenceCounter
unordered_map< Texture *, int > textureToAtlasTextureIdxMapping
int addTexture(Texture *texture)
Add texture.
int getTextureIdx(Texture *texture)
Returns specific atlas texture index within atlas.
SimpleTextureAtlas(const string &id)
Public constructor.
void update()
Update texture atlas.
unordered_map< int, Texture * > atlasTextureIdxToTextureMapping
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6