TDME2  1.9.200
TextureManager.cpp
Go to the documentation of this file.
2 
3 #include <string>
4 #include <unordered_map>
5 
6 #include <tdme/tdme.h>
7 #include <tdme/engine/Texture.h>
11 #include <tdme/utilities/Console.h>
12 
13 using std::string;
14 using std::unordered_map;
15 
22 
23 TextureManager::TextureManager(Renderer* renderer): mutex("texturemanager-mutex") {
24  this->renderer = renderer;
25 }
26 
28  for (const auto& [textureManagedId, textureManaged]: textures) delete textureManaged;
29 }
30 
32 {
33  // check if we already manage this texture
34  mutex.lock();
35  auto textureManagedIt = textures.find(id);
36  if (textureManagedIt != textures.end()) {
37  //
38  auto textureManaged = textureManagedIt->second;
39  textureManaged->incrementReferenceCounter();
40  mutex.unlock();
41  // yep, return renderer texture id
42  created = false;
43  return textureManaged;
44  }
45  // create texture
46  auto textureId = renderer->createTexture();
47  // create managed texture
48  auto textureManaged = new TextureManager_TextureManaged(id, textureId);
49  // add it to our textures
50  textureManaged->incrementReferenceCounter();
51  textures[id] = textureManaged;
52  //
53  mutex.unlock();
54  //
55  created = true;
56  return textureManaged;
57 }
58 
59 int32_t TextureManager::addTexture(Texture* texture, int contextIdx)
60 {
61  bool created;
62  auto textureManaged = addTexture(texture->getId(), created);
63  auto rendererId = textureManaged->getRendererId();
64 
65  // upload if it was created
66  if (created == true) {
67  // bind texture
68  renderer->bindTexture(contextIdx, rendererId);
69  // upload texture
70  renderer->uploadTexture(contextIdx, texture);
71  // unbind texture
72  renderer->bindTexture(contextIdx, renderer->ID_NONE);
73  //
74  textureManaged->setUploaded(true);
75  }
76 
77  // return renderer id
78  return rendererId;
79 }
80 
81 int32_t TextureManager::addCubeMapTexture(const string& id, Texture* textureLeft, Texture* textureRight, Texture* textureTop, Texture* textureBottom, Texture* textureFront, Texture* textureBack, int contextIdx)
82 {
83  bool created;
84  auto textureManaged = addTexture(id, created);
85  auto rendererId = textureManaged->getRendererId();
86 
87  // upload if it was created
88  if (created == true) {
89  // bind texture
90  renderer->bindCubeMapTexture(contextIdx, rendererId);
91  // upload texture
92  renderer->uploadCubeMapTexture(contextIdx, textureLeft, textureRight, textureTop, textureBottom, textureFront, textureBack);
93  // unbind texture
95  //
96  textureManaged->setUploaded(true);
97  }
98 
99  // return renderer id
100  return rendererId;
101 }
102 
103 void TextureManager::removeTexture(const string& textureId)
104 {
105  mutex.lock();
106  auto textureManagedIt = textures.find(textureId);
107  if (textureManagedIt != textures.end()) {
108  auto textureManaged = textureManagedIt->second;
109  if (textureManaged->decrementReferenceCounter()) {
110  // delete texture
111  auto textureRendererId = textureManaged->getRendererId();
112  // remove from our list
113  textures.erase(textureManagedIt);
114  //
115  mutex.unlock();
116  // remove from renderer
117  renderer->disposeTexture(textureRendererId);
118  delete textureManaged;
119  //
120  return;
121  }
122  //
123  mutex.unlock();
124  return;
125  }
126  mutex.unlock();
127  Console::println(string("Warning: texture not loaded by texture manager: " + textureId));
128 }
Texture entity.
Definition: Texture.h:24
const string & getId() const
Definition: Texture.h:172
void removeTexture(Texture *texture)
Removes a texture from manager.
unordered_map< string, TextureManager_TextureManaged * > textures
int32_t addTexture(Texture *texture, int contextIdx=0)
Adds a texture to manager.
int32_t addCubeMapTexture(const string &id, Texture *textureLeft, Texture *textureRight, Texture *textureTop, Texture *textureBottom, Texture *textureFront, Texture *textureBack, int contextIdx=0)
Adds a cube map texture to manager.
TextureManager_TextureManaged * addTexture(const string &id, bool &created)
Adds a texture to manager.
virtual void bindCubeMapTexture(int contextIdx, int32_t textureId)=0
Binds a cube map texture with given id or unbinds when using ID_NONE.
virtual int32_t createTexture()=0
Creates a texture.
virtual void bindTexture(int contextIdx, int32_t textureId)=0
Binds a texture with given id or unbinds when using ID_NONE.
virtual void disposeTexture(int32_t textureId)=0
Dispose a texture.
virtual void uploadTexture(int contextIdx, Texture *texture)=0
Uploads texture data to current bound texture.
virtual void uploadCubeMapTexture(int contextIdx, Texture *textureLeft, Texture *textureRight, Texture *textureTop, Texture *textureBottom, Texture *textureFront, Texture *textureBack)=0
Uploads cube map texture data to current bound texture.
Mutex implementation.
Definition: Mutex.h:19
void unlock()
Unlocks this mutex.
Definition: Mutex.h:54
void lock()
Locks the mutex, additionally mutex locks will block until other locks have been unlocked.
Definition: Mutex.h:47
Console class.
Definition: Console.h:29