]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/partcache.h
Use std::unique_ptr for managing memory
[libs/gltk.git] / source / partcache.h
index b856f9da47a486151f3a10b62c51f1f0c60fb02c..a10efdd084e2a9b5c463fd3ac555886635bb7c36 100644 (file)
@@ -1,21 +1,49 @@
 #ifndef MSP_GLTK_PARTCACHE_H_
 #define MSP_GLTK_PARTCACHE_H_
 
+#include <memory>
 #include <msp/gl/mesh.h>
 #include <msp/gl/texture2d.h>
+#include "mspgltk_api.h"
 
 namespace Msp {
 namespace GLtk {
 
+class Part;
+
 struct CachedPart
 {
-       const GL::Texture2D *texture;
-       GL::Mesh *mesh;
+       const Part *part = nullptr;
+       const GL::Texture2D *texture = nullptr;
+       std::unique_ptr<GL::Mesh> mesh;
+};
+
+class MSPGLTK_API PartCache
+{
+public:
+       class Rebuild
+       {
+       private:
+               PartCache &cache;
+
+       public:
+               Rebuild(PartCache &c): cache(c) { cache.begin_rebuild(); }
+               ~Rebuild() { cache.end_rebuild(); }
+       };
+
+private:
+       bool rebuilding = false;
+       std::vector<CachedPart> parts;
+       std::vector<CachedPart>::iterator next;
+       std::vector<CachedPart>::iterator current;
 
-       CachedPart();
-       ~CachedPart();
+public:
+       void begin_rebuild();
+       void insert_special(const Part &);
+       GL::Mesh &create_mesh(const Part &, const GL::Texture2D &);
+       void end_rebuild();
 
-       void clear_mesh();
+       const std::vector<CachedPart> &get_parts() const { return parts; }
 };
 
 } // namespace GLtk