Browse code

add scene and rendering classes

devnewton authored on 12/09/2015 22:46:56
Showing 14 changed files
... ...
@@ -32,10 +32,13 @@ include_directories(${SDL2_TTF_INCLUDE_DIR})
32 32
 add_executable(superpaflaballe
33 33
  src/bourrines_benchmark.cpp
34 34
  src/scenimp/assets.cpp
35
+ src/scenimp/scene.cpp
35 36
  src/scenimp/screen.cpp
36 37
  src/scenimp/group.cpp
37 38
  src/scenimp/nanim.cpp
38 39
  src/scenimp/node.cpp
40
+ src/scenimp/rendering.cpp
41
+ src/scenimp/transform.cpp
39 42
  src/scenimp/sdl_exception.cpp
40 43
  src/intro.cpp
41 44
  src/main.cpp)
... ...
@@ -8,18 +8,14 @@ namespace scenimp {
8 8
     group::~group() {
9 9
     }
10 10
 
11
-    void group::attach(std::shared_ptr<node> node) {
12
-        node->parent_ = std::static_pointer_cast<group>(shared_from_this());
13
-        children_.insert(node);
14
-    }
15
-
16
-    void group::detach(std::shared_ptr<node> node) {
17
-        node->parent_.reset();
18
-        children_.erase(node);
19
-    }
20
-
21 11
     const node_list& group::children() const {
22 12
         return children_;
23 13
     }
14
+    
15
+    void group::render(rendering& r) {
16
+        for(auto& c : children_) {
17
+            c->render(r);
18
+        }
19
+    }
24 20
 
25 21
 }
... ...
@@ -3,20 +3,24 @@
3 3
 #include "node.h"
4 4
 
5 5
 namespace scenimp {
6
+    
7
+    class scene;
8
+    class rendering;
6 9
 
7 10
     class group : public node {
8 11
     public:
9 12
         group();
10
-        group(const group& orig) = delete;
13
+        group(const group&) = delete;
11 14
         virtual ~group();
12 15
 
13
-        void attach(std::shared_ptr< node > node);
14
-        void detach(std::shared_ptr< node > node);
16
+        virtual void render(rendering& r);
15 17
 
16 18
         const node_list& children() const;
17 19
 
18 20
     private:
19 21
         node_list children_;
22
+        
23
+        friend class scene;
20 24
     };
21 25
 
22 26
 }
... ...
@@ -3,16 +3,18 @@
3 3
 
4 4
 namespace scenimp {
5 5
 
6
-    node::node() {
6
+    node::node()
7
+    : parent_(nullptr) {
7 8
     }
8 9
 
9 10
     node::~node() {
10 11
     }
11 12
 
12
-    void node::detach() {
13
-        if (auto lockedParent = parent_.lock()) {
14
-            lockedParent->detach(shared_from_this());
15
-        }
13
+    transform& node::local_transform() {
14
+        return local_transform_;
16 15
     }
17 16
 
17
+    const transform& node::local_transform() const {
18
+        return local_transform_;
19
+    }
18 20
 }
... ...
@@ -1,27 +1,33 @@
1 1
 #pragma once
2 2
 
3
-#include <memory>
3
+#include "transform.h"
4 4
 #include <boost/container/flat_set.hpp>
5 5
 
6 6
 namespace scenimp {
7
-    
7
+
8 8
     class group;
9
+    class scene;
10
+    class rendering;
9 11
 
10
-    class node: public std::enable_shared_from_this<node> {
12
+    class node {
11 13
     public:
12 14
         node();
13
-        node(const node& orig) = delete;
15
+        node(const node&) = delete;
14 16
         virtual ~node();
15
-        
16
-        void detach();
17
+
18
+        virtual void render(rendering& r) = 0;
19
+
20
+        transform& local_transform();
21
+        const transform& local_transform() const;
17 22
 
18 23
     private:
19
-        std::weak_ptr<group> parent_;
20
-        
21
-        friend class group;
24
+        class transform local_transform_;
25
+        group* parent_;
26
+
27
+        friend class scene;
22 28
     };
23 29
 
24
-    typedef boost::container::flat_set< std::shared_ptr<node> > node_list;
30
+    typedef boost::container::flat_set<node* > node_list;
25 31
 
26 32
 }
27 33
 
28 34
new file mode 100644
... ...
@@ -0,0 +1,22 @@
1
+#include <stack>
2
+
3
+#include "rendering.h"
4
+
5
+namespace scenimp {
6
+
7
+    rendering::rendering() {
8
+    }
9
+
10
+    rendering::~rendering() {
11
+    }
12
+
13
+    void rendering::push_transform(const transform& t) {
14
+        transform_stack_.push(t);
15
+    }
16
+    const transform& rendering::current_transform() const {
17
+        return transform_stack_.top();
18
+    }
19
+    void rendering::pop_transform() {
20
+        transform_stack_.pop();
21
+    }
22
+}
0 23
new file mode 100644
... ...
@@ -0,0 +1,24 @@
1
+#pragma once
2
+
3
+#include "transform.h"
4
+#include <stack>
5
+
6
+namespace scenimp {
7
+
8
+    class rendering {
9
+    public:
10
+        rendering();
11
+        rendering(const rendering&) = delete;
12
+        ~rendering();
13
+
14
+        void push_transform(const transform& t);
15
+        const transform& current_transform() const;
16
+        void pop_transform();
17
+
18
+    private:
19
+        std::stack<transform> transform_stack_;
20
+    };
21
+
22
+}
23
+
24
+
0 25
new file mode 100644
... ...
@@ -0,0 +1,53 @@
1
+#include "scene.h"
2
+#include "rendering.h"
3
+
4
+namespace scenimp {
5
+
6
+    scene::scene() {
7
+        root_ = &new_group();
8
+
9
+    }
10
+
11
+    scene::~scene() {
12
+    }
13
+
14
+    group& scene::new_group(group* parent) {
15
+        group* s = group_pool_.malloc();
16
+        attach(s, parent);
17
+        return *s;
18
+    }
19
+
20
+    sprite& scene::new_sprite(group* parent) {
21
+        sprite* s = sprite_pool_.malloc();
22
+        attach(s, parent);
23
+        return *s;
24
+    }
25
+
26
+    void scene::attach(node* child, group* parent) {
27
+        if (nullptr == parent) {
28
+            parent = root_;
29
+        }
30
+        parent->children_.insert(child);
31
+        child->parent_ = parent;
32
+    }
33
+
34
+    void scene::delete_sprite(sprite* s) {
35
+        detach(s);
36
+        sprite_pool_.free(s);
37
+    }
38
+
39
+    void scene::delete_group(group* g) {
40
+        detach(g);
41
+        group_pool_.free(g);
42
+    }
43
+
44
+    void scene::detach(node* child) {
45
+        child->parent_->children_.erase(child);
46
+    }
47
+    
48
+    void scene::render() {
49
+        rendering r;
50
+        root_->render(r);
51
+    }
52
+
53
+}
0 54
new file mode 100644
... ...
@@ -0,0 +1,33 @@
1
+#pragma once
2
+
3
+#include "group.h"
4
+#include "sprite.h"
5
+#include <boost/pool/object_pool.hpp>
6
+
7
+
8
+namespace scenimp {
9
+
10
+    class scene {
11
+    public:
12
+        scene();
13
+        scene(const scene&) = delete;
14
+        ~scene();
15
+        
16
+        group& new_group(group* parent = nullptr);
17
+        sprite& new_sprite(group* parent = nullptr);
18
+        
19
+        void delete_sprite(sprite* s);
20
+        void delete_group(group* g);
21
+        
22
+        void render();
23
+
24
+    private:
25
+        void attach(node* child, group* parent);
26
+        void detach(node* child);
27
+        boost::object_pool<group> group_pool_;
28
+        boost::object_pool<sprite> sprite_pool_;
29
+        group* root_;
30
+    };
31
+
32
+}
33
+
... ...
@@ -7,6 +7,7 @@ namespace scenimp {
7 7
     class screen {
8 8
     public:
9 9
         screen();
10
+        screen(const screen&) = delete;
10 11
         ~screen();
11 12
 
12 13
         int logical_screen_width() const;
... ...
@@ -8,4 +8,16 @@ namespace scenimp {
8 8
     sprite::~sprite() {
9 9
     }
10 10
 
11
+    void sprite::render(rendering& r) {
12
+        //TODO
13
+    }
14
+
15
+    std::shared_ptr< nanim::play > sprite::play() {
16
+        return play_;
17
+    }
18
+
19
+    void set_play(std::shared_ptr< nanim::play > play) {
20
+        play_ = play;
21
+    }
22
+
11 23
 }
... ...
@@ -10,6 +10,11 @@ namespace scenimp {
10 10
         sprite();
11 11
         sprite(const sprite& orig) = delete;
12 12
         virtual ~sprite();
13
+        
14
+        virtual void render(rendering& r);
15
+        
16
+        std::shared_ptr< nanim::play > play();
17
+        void set_play(std::shared_ptr< nanim::play > play);
13 18
     private:
14 19
         std::shared_ptr< nanim::play > play_;
15 20
     };
16 21
new file mode 100644
... ...
@@ -0,0 +1,19 @@
1
+#include "transform.h"
2
+
3
+namespace scenimp {
4
+
5
+    transform::transform()
6
+    : tx_(0)
7
+    , ty_(0) {
8
+    }
9
+
10
+    void transform::translate(int x, int y) {
11
+        tx_ += x;
12
+        ty_ += y;
13
+    }
14
+
15
+    void transform::combine_with(const transform& other) {
16
+        tx_ += other.tx_;
17
+        ty_ += other.ty_;
18
+    }
19
+}
0 20
new file mode 100644
... ...
@@ -0,0 +1,19 @@
1
+#pragma once
2
+
3
+namespace scenimp {
4
+
5
+    class transform {
6
+    public:
7
+        transform();
8
+        void translate(int x, int y);
9
+
10
+        void combine_with(const transform& other);
11
+
12
+    private:
13
+        int tx_, ty_;
14
+
15
+    };
16
+
17
+}
18
+
19
+