TDME2 1.9.121
VBOManager.cpp
Go to the documentation of this file.
2
3#include <string>
4#include <unordered_map>
5
6#include <tdme/tdme.h>
11
12using std::string;
13using std::unordered_map;
14
20
21VBOManager::VBOManager(Renderer* renderer): rwLock("vbomanager-rwlock") {
22 this->renderer = renderer;
23}
24
26 for (auto it = vbos.begin(); it != vbos.end(); ++it) {
27 delete it->second;
28 }
29}
30
31VBOManager_VBOManaged* VBOManager::addVBO(const string& vboId, int32_t ids, bool useGPUMemory, bool shared, bool& created) {
33 // check if we already manage this vbo
34 auto vboManagedIt = vbos.find(vboId);
35 if (vboManagedIt != vbos.end()) {
36 auto vboManaged = vboManagedIt->second;
37 vboManaged->incrementReferenceCounter();
38 rwLock.unlock();
39 // yep, return vbo managed
40 created = false;
41 return vboManaged;
42 }
43 // create vertex buffer objects
44 auto vboIds = renderer->createBufferObjects(ids, useGPUMemory, shared);
45 // create managed texture
46 auto vboManaged = new VBOManager_VBOManaged(vboId, vboIds);
47 // add it to our textures
48 vboManaged->incrementReferenceCounter();
49 vbos[vboManaged->getId()] = vboManaged;
50 rwLock.unlock();
51 created = true;
52
53 // return vbo managed
54 return vboManaged;
55}
56
59 // check if we already manage this vbo
60 auto vboManagedIt = vbos.find(vboId);
61 if (vboManagedIt != vbos.end()) {
62 auto vboManaged = vboManagedIt->second;
63 vboManaged->incrementReferenceCounter();
64 rwLock.unlock();
65 // yep, return vbo managed
66 return vboManaged;
67 }
68 // nope
69 rwLock.unlock();
70 return nullptr;
71}
72
73void VBOManager::removeVBO(const string& vboId) {
75 auto vboManagedIt = vbos.find(vboId);
76 if (vboManagedIt != vbos.end()) {
77 auto vboManaged = vboManagedIt->second;
78 if (vboManaged->decrementReferenceCounter()) {
79 auto vboIds = vboManaged->getVBOIds();
80 // delete vbos from renderer
82 // remove from our list
83 vbos.erase(vboManagedIt);
84 delete vboManaged;
85 }
86 rwLock.unlock();
87 return;
88 }
89 rwLock.unlock();
90 Console::println(string("Warning: vbo not managed by vbo manager: ") + vboId);
91}
unordered_map< string, VBOManager_VBOManaged * > vbos
Definition: VBOManager.h:30
VBOManager_VBOManaged * addVBO(const string &vboId, int32_t ids, bool useGPUMemory, bool shared, bool &created)
Adds a VBO to manager or retrieve VBO if existing.
Definition: VBOManager.cpp:31
VBOManager_VBOManaged * getVBO(const string &vboId)
Retrieves a VBO managed from manager.
Definition: VBOManager.cpp:57
void removeVBO(const string &vboId)
Removes a VBO from manager.
Definition: VBOManager.cpp:73
virtual void disposeBufferObjects(vector< int32_t > &bufferObjectIds)=0
Disposes a frame buffer object.
virtual vector< int32_t > createBufferObjects(int32_t buffers, bool useGPUMemory, bool shared)=0
Generate buffer objects for vertex data and such.
Implementation for read/write lock.
Definition: ReadWriteLock.h:21
void writeLock()
Locks for writing / exclusive lock.
void unlock()
Unlocks this read write lock.
void readLock()
Locks for reading / shared lock.
Console class.
Definition: Console.h:26