]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/partcache.cpp
Simplify constructors with C++11
[libs/gltk.git] / source / partcache.cpp
index e270dc4775eef4f005161304587fa3c58d17f915..f4c085e5baa47c4230786eba92a2feb50ac8cf50 100644 (file)
@@ -1,24 +1,86 @@
+#include "part.h"
 #include "partcache.h"
 
+using namespace std;
+
 namespace Msp {
 namespace GLtk {
 
-CachedPart::CachedPart():
-       texture(0),
-       mesh(0)
-{ }
-
 CachedPart::~CachedPart()
 {
        delete mesh;
 }
 
-void CachedPart::clear_mesh()
+
+void PartCache::begin_rebuild()
+{
+       if(rebuilding)
+               throw logic_error("nested rebuild");
+       rebuilding = true;
+
+       next = parts.begin();
+       current = parts.end();
+}
+
+void PartCache::insert_special(const Part &part)
+{
+       if(part.get_name().empty())
+               throw invalid_argument("PartCache::insert_special");
+       if(!rebuilding)
+               throw logic_error("!rebuilding");
+
+       for(current=next; current!=parts.end(); ++current)
+               if(current->part==&part)
+               {
+                       parts.erase(next, current);
+                       break;
+               }
+
+       if(current==parts.end())
+               current = parts.insert(next, CachedPart());
+       else
+               *current = CachedPart();
+       current->part = ∂
+
+       next = current;
+       ++next;
+}
+
+GL::Mesh &PartCache::create_mesh(const Part &part, const GL::Texture2D &tex)
 {
-       if(!mesh)
-               mesh = new GL::Mesh((GL::TEXCOORD2, GL::COLOR4_UBYTE, GL::VERTEX2));
+       if(!rebuilding)
+               throw logic_error("!rebuilding");
+
+       if(current!=parts.end() && current->texture==&tex)
+               return *current->mesh;
+
+       for(current=next; current!=parts.end(); ++current)
+               if(current->texture==&tex)
+               {
+                       parts.erase(next, current);
+                       break;
+               }
+
+       if(current==parts.end())
+       {
+               current = parts.insert(next, CachedPart());
+               current->texture = &tex;
+               current->mesh = new GL::Mesh((GL::TEXCOORD2, GL::COLOR4,GL::UNSIGNED_BYTE, GL::VERTEX2));
+       }
        else
-               mesh->clear();
+               current->mesh->clear();
+       current->part = ∂
+
+       next = current;
+       ++next;
+
+       return *current->mesh;
+}
+
+void PartCache::end_rebuild()
+{
+       rebuilding = false;
+       parts.erase(next, parts.end());
 }
 
 } // namespace GLtk