Browse code

simplify z ordering

devnewton authored on 13/09/2015 15:14:08
Showing 6 changed files
... ...
@@ -14,14 +14,9 @@ namespace scenimp {
14 14
     }
15 15
 
16 16
     void group::do_render(rendering& r) {
17
-        for (auto& c : children_) {
18
-            c->render(r);
17
+        for (auto& c : children_.get<container::z_index>()) {
18
+            c.node->render(r);
19 19
         }
20 20
     }
21
-    
22
-    void group::set_z(node* child, int z) {
23
-        auto it = children_.find(child);
24
-        children_.modify(it, [z](node* n) { n->z_ = z; });
25
-    }
26 21
 
27 22
 }
... ...
@@ -5,25 +5,40 @@
5 5
 #include <boost/multi_index/ordered_index.hpp>
6 6
 #include <boost/multi_index/hashed_index.hpp>
7 7
 #include <boost/multi_index/identity.hpp>
8
-#include <boost/multi_index/mem_fun.hpp>
8
+#include <boost/multi_index/member.hpp>
9 9
 
10 10
 namespace scenimp {
11 11
 
12 12
     namespace container {
13 13
         using namespace boost::multi_index;
14
-        
15
-        struct z_index {};
14
+
15
+        struct node_in_group {
16
+            node_in_group(class node* n, int z)
17
+            : node(n)
18
+            , z(z) {
19
+            }
20
+
21
+            class node* node;
22
+            int z;
23
+        };
24
+
25
+        struct node_index {
26
+        };
27
+
28
+        struct z_index {
29
+        };
16 30
 
17 31
         typedef multi_index_container<
18
-        node*,
32
+        node_in_group,
19 33
         indexed_by<
20
-            hashed_unique< identity< node* > >,
21
-            ordered_non_unique< tag< z_index >, const_mem_fun<node, int, &node::z > >
34
+        hashed_unique< tag<node_index>, member< node_in_group, node*, &node_in_group::node > >,
35
+        ordered_non_unique< tag< z_index >, member< node_in_group, int, &node_in_group::z > >
22 36
         >
23 37
         > node_set;
24 38
     }
25
-    
39
+
26 40
     using container::node_set;
41
+    using container::node_in_group;
27 42
 
28 43
     class scene;
29 44
     class rendering;
... ...
@@ -35,7 +50,6 @@ namespace scenimp {
35 50
         virtual ~group();
36 51
 
37 52
         const node_set& children() const;
38
-        void set_z(node* child, int z);
39 53
 
40 54
     protected:
41 55
         virtual void do_render(rendering& r);
... ...
@@ -5,8 +5,7 @@
5 5
 namespace scenimp {
6 6
 
7 7
     node::node()
8
-    : parent_(nullptr),
9
-    z_(0) {
8
+    : parent_(nullptr) {
10 9
     }
11 10
 
12 11
     node::~node() {
... ...
@@ -26,7 +25,4 @@ namespace scenimp {
26 25
         r.pop_pos(*this);
27 26
     }
28 27
 
29
-    int node::z() const {
30
-        return z_;
31
-    }
32 28
 }
... ...
@@ -18,7 +18,6 @@ namespace scenimp {
18 18
 
19 19
         const point& pos() const;
20 20
         point& pos();
21
-        int z() const;
22 21
 
23 22
     protected:
24 23
         virtual void do_render(rendering& r) = 0;
... ...
@@ -26,7 +25,6 @@ namespace scenimp {
26 25
     private:
27 26
         point pos_;
28 27
         group* parent_;
29
-        int z_;
30 28
 
31 29
         friend class scene;
32 30
         friend class group;
... ...
@@ -12,23 +12,23 @@ namespace scenimp {
12 12
     scene::~scene() {
13 13
     }
14 14
 
15
-    group& scene::new_group(group* parent) {
15
+    group& scene::new_group(group* parent, int z) {
16 16
         group* s = group_pool_.malloc();
17
-        attach(s, parent);
17
+        attach(s, parent,z);
18 18
         return *s;
19 19
     }
20 20
 
21
-    sprite& scene::new_sprite(group* parent) {
21
+    sprite& scene::new_sprite(group* parent, int z) {
22 22
         sprite* s = sprite_pool_.malloc();
23
-        attach(s, parent);
23
+        attach(s, parent, z);
24 24
         return *s;
25 25
     }
26 26
 
27
-    void scene::attach(node* child, group* parent) {
27
+    void scene::attach(node* child, group* parent, int z) {
28 28
         if (nullptr == parent) {
29 29
             parent = root_;
30 30
         }
31
-        parent->children_.insert(child);
31
+        parent->children_.insert(node_in_group(child, z));
32 32
         child->parent_ = parent;
33 33
     }
34 34
 
... ...
@@ -13,8 +13,8 @@ namespace scenimp {
13 13
         scene(const scene&) = delete;
14 14
         ~scene();
15 15
         
16
-        group& new_group(group* parent = nullptr);
17
-        sprite& new_sprite(group* parent = nullptr);
16
+        group& new_group(group* parent = nullptr, int z=0);
17
+        sprite& new_sprite(group* parent = nullptr, int z=0);
18 18
         
19 19
         void delete_sprite(sprite* s);
20 20
         void delete_group(group* g);
... ...
@@ -22,7 +22,7 @@ namespace scenimp {
22 22
         void render();
23 23
 
24 24
     private:
25
-        void attach(node* child, group* parent);
25
+        void attach(node* child, group* parent, int z);
26 26
         void detach(node* child);
27 27
         boost::object_pool<group> group_pool_;
28 28
         boost::object_pool<sprite> sprite_pool_;