]> git.tdb.fi Git - libs/gltk.git/blob - source/partcache.cpp
77b51d2cbc2096a101eb941b20e45f6b23568c6a
[libs/gltk.git] / source / partcache.cpp
1 #include "part.h"
2 #include "partcache.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GLtk {
8
9 CachedPart::CachedPart(CachedPart &&other):
10         part(other.part),
11         texture(other.texture),
12         mesh(other.mesh)
13 {
14         other.mesh = nullptr;
15 }
16
17 CachedPart &CachedPart::operator=(CachedPart &&other)
18 {
19         delete mesh;
20         part = other.part;
21         texture = other.texture;
22         mesh = other.mesh;
23         other.mesh = nullptr;
24         return *this;
25 }
26
27 CachedPart::~CachedPart()
28 {
29         delete mesh;
30 }
31
32
33 void PartCache::begin_rebuild()
34 {
35         if(rebuilding)
36                 throw logic_error("nested rebuild");
37         rebuilding = true;
38
39         next = parts.begin();
40         current = parts.end();
41 }
42
43 void PartCache::insert_special(const Part &part)
44 {
45         if(part.get_name().empty())
46                 throw invalid_argument("PartCache::insert_special");
47         if(!rebuilding)
48                 throw logic_error("!rebuilding");
49
50         for(current=next; current!=parts.end(); ++current)
51                 if(current->part==&part)
52                 {
53                         parts.erase(next, current);
54                         break;
55                 }
56
57         if(current==parts.end())
58                 current = parts.insert(next, CachedPart());
59         else
60                 *current = CachedPart();
61         current->part = ∂
62
63         next = current;
64         ++next;
65 }
66
67 GL::Mesh &PartCache::create_mesh(const Part &part, const GL::Texture2D &tex)
68 {
69         if(!rebuilding)
70                 throw logic_error("!rebuilding");
71
72         if(current!=parts.end() && current->texture==&tex)
73                 return *current->mesh;
74
75         for(current=next; current!=parts.end(); ++current)
76                 if(current->texture==&tex)
77                 {
78                         parts.erase(next, current);
79                         break;
80                 }
81
82         if(current==parts.end())
83         {
84                 current = parts.insert(next, CachedPart());
85                 current->texture = &tex;
86                 current->mesh = new GL::Mesh((GL::TEXCOORD2, GL::COLOR4,GL::UNSIGNED_BYTE, GL::VERTEX2));
87         }
88         else
89                 current->mesh->clear();
90         current->part = ∂
91
92         next = current;
93         ++next;
94
95         return *current->mesh;
96 }
97
98 void PartCache::end_rebuild()
99 {
100         rebuilding = false;
101         parts.erase(next, parts.end());
102 }
103
104 } // namespace GLtk
105 } // namespace Msp