TDME2  1.9.200
TextureAtlas.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  * Texture atlas
19  * @author Andreas Drewke
20  */
22 public:
23  static constexpr bool VERBOSE { false };
24  static constexpr int TEXTURE_IDX_NONE { -1 };
25 
26  /**
27  * Texture Atlas Texture
28  */
29  struct AtlasTexture {
31  Texture* texture { nullptr };
33  int textureIdx { -1 };
34  int left { -1 };
35  int top { -1 };
36  int width { -1 };
37  int height { -1 };
38  int line { -1 };
39  };
40 
41  // forbid class copy
43 
44  /**
45  * Public constructor
46  * @param id texture id
47  */
48  TextureAtlas(const string& id);
49 
50  /**
51  * Public destructor
52  */
53  ~TextureAtlas();
54 
55  /**
56  * Returns specific atlas texture index within atlas
57  * @param texture texture
58  * @return atlas texture id
59  */
60  inline int getTextureIdx(Texture* texture) {
61  auto it = textureToAtlasTextureIdxMapping.find(texture);
62  if (it == textureToAtlasTextureIdxMapping.end()) {
63  return TEXTURE_IDX_NONE;
64  }
65  return it->second;
66  }
67 
68  /**
69  * Returns specific atlas texture information within atlas
70  * @param textureIdx texture index
71  * @return atlas texture information
72  */
73  inline const AtlasTexture* getAtlasTexture(int textureIdx) {
74  auto it = atlasTextureIdxToAtlasTextureMapping.find(textureIdx);
75  if (it == atlasTextureIdxToAtlasTextureMapping.end()) {
76  return nullptr;
77  }
78  return &it->second;
79  }
80 
81  /**
82  * Add texture
83  * @param texture texture
84  * @return atlas texture id
85  */
86  int addTexture(Texture* texture);
87 
88  /**
89  * Remove texture
90  * @param texture texture
91  */
92  void removeTexture(Texture* texture);
93 
94  /**
95  * Update texture atlas
96  */
97  void update();
98 
99  /**
100  * @return is requiring update
101  */
102  inline bool isRequiringUpdate() {
103  return requiresUpdate;
104  }
105 
106  /**
107  * @return atlas texture
108  */
110  return atlasTexture;
111  }
112 
113 private:
114  bool requiresUpdate { false };
116  Texture* atlasTexture { nullptr };
117  unordered_map<Texture*, int> textureReferenceCounter;
118  unordered_map<Texture*, int> textureToAtlasTextureIdxMapping;
119  unordered_map<int, AtlasTexture> atlasTextureIdxToAtlasTextureMapping;
120  vector<int> freeTextureIds;
121 };
Texture entity.
Definition: Texture.h:24
void removeTexture(Texture *texture)
Remove texture.
~TextureAtlas()
Public destructor.
unordered_map< Texture *, int > textureReferenceCounter
Definition: TextureAtlas.h:117
const AtlasTexture * getAtlasTexture(int textureIdx)
Returns specific atlas texture information within atlas.
Definition: TextureAtlas.h:73
TextureAtlas(const string &id)
Public constructor.
unordered_map< Texture *, int > textureToAtlasTextureIdxMapping
Definition: TextureAtlas.h:118
unordered_map< int, AtlasTexture > atlasTextureIdxToAtlasTextureMapping
Definition: TextureAtlas.h:119
int addTexture(Texture *texture)
Add texture.
static constexpr bool VERBOSE
Definition: TextureAtlas.h:23
int getTextureIdx(Texture *texture)
Returns specific atlas texture index within atlas.
Definition: TextureAtlas.h:60
static constexpr int TEXTURE_IDX_NONE
Definition: TextureAtlas.h:24
void update()
Update texture atlas.
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6