Browse code

remove overkill boost usage

devnewton authored on 27/09/2015 10:17:24
Showing 9 changed files
... ...
@@ -61,24 +61,23 @@ namespace superpaflaballe {
61 61
         }
62 62
 
63 63
         virtual void process(bourrines::entity e) override {
64
-            scenimp::point& pos = get<sprite_component>(e).sprite_->pos();
65
-            dir_component& dir = get<dir_component>(e);
64
+            auto& sprite = get<sprite_component>(e).sprite_;
65
+
66
+            sprite->set_angle(sprite->angle() + 1.0);
66 67
 
68
+            auto& pos = sprite->pos();
69
+            dir_component& dir = get<dir_component>(e);
67 70
             pos.x(pos.x() + dir.dx_);
68 71
             pos.y(pos.y() + dir.dy_);
69
-
70 72
             if (pos.x() < 0) {
71 73
                 dir.dx_ = -dir.dx_;
72 74
             }
73
-
74 75
             if (pos.y() < 0) {
75 76
                 dir.dy_ = -dir.dy_;
76 77
             }
77
-
78 78
             if (pos.x() >= screen_.logical_screen_width()) {
79 79
                 dir.dx_ = -dir.dx_;
80 80
             }
81
-
82 81
             if (pos.y() >= screen_.logical_screen_height()) {
83 82
                 dir.dy_ = -dir.dy_;
84 83
             }
... ...
@@ -96,10 +95,10 @@ namespace superpaflaballe {
96 95
         : scene_(s) {
97 96
         }
98 97
 
99
-        virtual void process() {
98
+        virtual void process() override {
100 99
             scene_.render();
101 100
         }
102
-
101
+        
103 102
     private:
104 103
         scenimp::scene& scene_;
105 104
     };
... ...
@@ -117,14 +116,13 @@ namespace superpaflaballe {
117 116
     }
118 117
 
119 118
     bourrines_benchmark::~bourrines_benchmark() {
120
-        std::cout << "bourrines_benchmark accumulated ticks: " << timer_.format() << std::endl;
121 119
     }
122 120
 
123 121
     void bourrines_benchmark::create_ned() {
124 122
         bourrines::entity e = world_.create_entity();
125
-        scenimp::sprite& sprite = scene_.new_sprite();
126
-        world_.add<sprite_component>(e).sprite_ = &sprite;
127
-        auto& pos = sprite.pos();
123
+        auto sprite = scene_.new_sprite();
124
+        world_.add<sprite_component>(e).sprite_ = sprite;
125
+        auto& pos = sprite->pos();
128 126
 
129 127
         pos.x(std::rand() % screen_.logical_screen_width());
130 128
         pos.y(std::rand() % screen_.logical_screen_height());
... ...
@@ -133,7 +131,7 @@ namespace superpaflaballe {
133 131
         dir.dx_ = (1 + (std::rand() % 10)) * ((std::rand() % 1) ? -1 : 1);
134 132
         dir.dy_ = (1 + (std::rand() % 10)) * ((std::rand() % 1) ? -1 : 1);
135 133
 
136
-        sprite.set_play(ned_anim_->play(scenimp::nanim::loop));
134
+        sprite->set_play(ned_anim_->play(scenimp::nanim::loop));
137 135
         world_.add<life_component>(e).life_ = remaining_ticks_ > 0 ? std::rand() % remaining_ticks_ : 1;
138 136
 
139 137
         world_.changed(e);
... ...
@@ -19,7 +19,13 @@ namespace superpaflaballe {
19 19
     };
20 20
 
21 21
     struct sprite_component {
22
-        scenimp::sprite* sprite_;
22
+        std::shared_ptr<scenimp::sprite> sprite_;
23
+        
24
+        ~sprite_component() {
25
+            if(sprite_) {
26
+                sprite_->remove();
27
+            }
28
+        }
23 29
     };
24 30
 
25 31
     typedef bourrines::default_world<dir_component, life_component, sprite_component> world;
... ...
@@ -14,9 +14,21 @@ namespace scenimp {
14 14
     }
15 15
 
16 16
     void group::do_render(rendering& r) {
17
-        for (auto& c : children_.get<container::z_index>()) {
18
-            c.node->render(r);
17
+        for (auto& n : children_) {
18
+            n->render(r);
19 19
         }
20 20
     }
21
-
21
+    void group::add(std::shared_ptr< node > node) {
22
+        if(node->parent_.lock()) {
23
+            throw new std::runtime_error("node is already in a group");
24
+        }
25
+        node->parent_ = std::static_pointer_cast<group>( shared_from_this() );
26
+        children_.insert(node);
27
+    }
28
+    void group::remove(std::shared_ptr< node > node) {
29
+        if(node->parent_.lock() != shared_from_this()) {
30
+            throw new std::runtime_error("cannot remove a node that does not belong to this group");
31
+        }
32
+        children_.erase(node);
33
+    }
22 34
 }
... ...
@@ -1,44 +1,10 @@
1 1
 #pragma once
2 2
 
3 3
 #include "node.h"
4
-#include <boost/multi_index_container.hpp>
5
-#include <boost/multi_index/ordered_index.hpp>
6
-#include <boost/multi_index/hashed_index.hpp>
7
-#include <boost/multi_index/identity.hpp>
8
-#include <boost/multi_index/member.hpp>
9 4
 
10 5
 namespace scenimp {
11 6
 
12
-    namespace container {
13
-        using namespace boost::multi_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
-        };
30
-
31
-        typedef multi_index_container<
32
-        node_in_group,
33
-        indexed_by<
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 > >
36
-        >
37
-        > node_set;
38
-    }
39
-
40
-    using container::node_set;
41
-    using container::node_in_group;
7
+    typedef boost::container::flat_set< std::shared_ptr< node > > node_set;
42 8
 
43 9
     class scene;
44 10
     class rendering;
... ...
@@ -50,14 +16,14 @@ namespace scenimp {
50 16
         virtual ~group();
51 17
 
52 18
         const node_set& children() const;
19
+        void add(std::shared_ptr< node > node);
20
+        void remove(std::shared_ptr< node > node);
53 21
 
54 22
     protected:
55 23
         virtual void do_render(rendering& r);
56 24
 
57 25
     private:
58 26
         node_set children_;
59
-
60
-        friend class scene;
61 27
     };
62 28
 
63 29
 }
... ...
@@ -5,12 +5,17 @@
5 5
 namespace scenimp {
6 6
 
7 7
     node::node()
8
-    : pos_(0, 0)
9
-    , parent_(nullptr) {
8
+    : pos_(0, 0) {
10 9
     }
11 10
 
12 11
     node::~node() {
13 12
     }
13
+    
14
+    void node::remove() {
15
+        if(auto parent = parent_.lock()) {
16
+            parent->remove(shared_from_this());
17
+        }
18
+    }
14 19
 
15 20
     const point& node::pos() const {
16 21
         return pos_;
... ...
@@ -8,12 +8,14 @@ namespace scenimp {
8 8
     class scene;
9 9
     class rendering;
10 10
 
11
-    class node {
11
+    class node : public std::enable_shared_from_this<node> {
12 12
     public:
13 13
         node();
14 14
         node(const node&) = delete;
15 15
         virtual ~node();
16
-
16
+        
17
+        void remove();
18
+        
17 19
         void render(rendering& r);
18 20
 
19 21
         const point& pos() const;
... ...
@@ -24,9 +26,8 @@ namespace scenimp {
24 26
 
25 27
     private:
26 28
         point pos_;
27
-        group* parent_;
29
+        std::weak_ptr<group> parent_;
28 30
 
29
-        friend class scene;
30 31
         friend class group;
31 32
     };
32 33
 
... ...
@@ -6,46 +6,29 @@
6 6
 namespace scenimp {
7 7
 
8 8
     scene::scene(SDL_Renderer* r)
9
-    : renderer_(r) {
10
-        root_ = group_pool_.construct();
9
+    : root_(std::make_shared<group>())
10
+    , renderer_(r) {
11 11
     }
12 12
 
13 13
     scene::~scene() {
14 14
     }
15 15
 
16
-    group& scene::new_group(group* parent, int z) {
17
-        group* s = group_pool_.construct();
18
-        attach(s, parent,z);
19
-        return *s;
20
-    }
21
-
22
-    sprite& scene::new_sprite(group* parent, int z) {
23
-        sprite* s = sprite_pool_.construct();
24
-        attach(s, parent, z);
25
-        return *s;
26
-    }
27
-
28
-    void scene::attach(node* child, group* parent, int z) {
29
-        if (nullptr == parent) {
16
+    std::shared_ptr<group> scene::new_group(std::shared_ptr<group> parent) {
17
+        auto g = std::make_shared<group>();
18
+        if (!parent) {
30 19
             parent = root_;
31 20
         }
32
-        node_in_group n(child, z);
33
-        parent->children_.insert(n);
34
-        child->parent_ = parent;
35
-    }
36
-
37
-    void scene::delete_sprite(sprite* s) {
38
-        detach(s);
39
-        sprite_pool_.free(s);
40
-    }
41
-
42
-    void scene::delete_group(group* g) {
43
-        detach(g);
44
-        group_pool_.free(g);
21
+        parent->add(g);
22
+        return g;
45 23
     }
46 24
 
47
-    void scene::detach(node* child) {
48
-        child->parent_->children_.erase(child);
25
+    std::shared_ptr<sprite> scene::new_sprite(std::shared_ptr<group> parent) {
26
+        auto s = std::make_shared<sprite>();
27
+        if (!parent) {
28
+            parent = root_;
29
+        }
30
+        parent->add(s);
31
+        return s;
49 32
     }
50 33
 
51 34
     void scene::render() {
... ...
@@ -2,7 +2,6 @@
2 2
 
3 3
 #include "group.h"
4 4
 #include "sprite.h"
5
-#include <boost/pool/object_pool.hpp>
6 5
 #include <SDL.h>
7 6
 
8 7
 namespace scenimp {
... ...
@@ -13,20 +12,13 @@ namespace scenimp {
13 12
         scene(const scene&) = delete;
14 13
         ~scene();
15 14
         
16
-        group& new_group(group* parent = nullptr, int z=0);
17
-        sprite& new_sprite(group* parent = nullptr, int z=0);
18
-        
19
-        void delete_sprite(sprite* s);
20
-        void delete_group(group* g);
21
-        
15
+        std::shared_ptr< group > new_group(std::shared_ptr< group > parent = nullptr);
16
+        std::shared_ptr< sprite > new_sprite(std::shared_ptr< group > parent = nullptr);
17
+
22 18
         void render();
23 19
 
24 20
     private:
25
-        void attach(node* child, group* parent, int z);
26
-        void detach(node* child);
27
-        boost::object_pool<group> group_pool_;
28
-        boost::object_pool<sprite> sprite_pool_;
29
-        group* root_;
21
+        std::shared_ptr<group> root_;
30 22
         SDL_Renderer* renderer_;
31 23
     };
32 24
 
... ...
@@ -14,7 +14,7 @@ namespace scenimp {
14 14
             SDL_Quit();
15 15
             throw sdl_exception();
16 16
         }
17
-        renderer_ = SDL_CreateRenderer(window_, -1, 0);
17
+        renderer_ = SDL_CreateRenderer(window_, -1, 0/*SDL_RENDERER_SOFTWARE*/);
18 18
         if (!renderer_) {
19 19
             SDL_DestroyWindow(window_);
20 20
             SDL_Quit();