Browse code

organize scenegraph

devnewton authored on 13/09/2015 00:13:57
Showing 18 changed files
... ...
@@ -38,7 +38,7 @@ add_executable(superpaflaballe
38 38
  src/scenimp/nanim.cpp
39 39
  src/scenimp/node.cpp
40 40
  src/scenimp/rendering.cpp
41
- src/scenimp/transform.cpp
41
+ src/scenimp/geometry.cpp
42 42
  src/scenimp/sdl_exception.cpp
43 43
  src/intro.cpp
44 44
  src/main.cpp)
... ...
@@ -9,8 +9,8 @@
9 9
 
10 10
 namespace scenimp {
11 11
 
12
-    assets::assets(screen& screen)
13
-    : screen_(screen) {
12
+    assets::assets(SDL_Renderer* r)
13
+    : renderer_(r) {
14 14
         pathPrefixes_.push_back("../assets/");
15 15
         pathPrefixes_.push_back(PREFIX "/games/superpaflaballe/");
16 16
         if (TTF_Init() != 0) {
... ...
@@ -99,7 +99,7 @@ namespace scenimp {
99 99
     }
100 100
 
101 101
     std::shared_ptr< SDL_Texture > assets::load_texture(const std::string path) {
102
-        return std::shared_ptr< SDL_Texture >(IMG_LoadTexture(screen_.renderer(), path.c_str()), SDL_DestroyTexture);
102
+        return std::shared_ptr< SDL_Texture >(IMG_LoadTexture(renderer_, path.c_str()), SDL_DestroyTexture);
103 103
     }
104 104
 
105 105
     std::shared_ptr< nanim::collection > assets::animations(const std::string& path) {
... ...
@@ -6,11 +6,9 @@
6 6
 
7 7
 namespace scenimp {
8 8
 
9
-    class screen;
10
-
11 9
     class assets {
12 10
     public:
13
-        assets(screen& s);
11
+        assets(SDL_Renderer* r);
14 12
         ~assets();
15 13
 
16 14
         std::shared_ptr< nanim::collection > animations(const std::string& path);
... ...
@@ -25,12 +23,12 @@ namespace scenimp {
25 23
         std::shared_ptr< Mix_Music > load_music(const std::string& path);
26 24
         std::shared_ptr< SDL_Texture > load_texture(const std::string path);
27 25
 
28
-        screen& screen_;
29 26
         std::vector<std::string> pathPrefixes_;
30 27
         std::map<std::string, std::weak_ptr< nanim::collection > > animations_;
31 28
         std::map<std::pair<std::string, int/*font size*/>, std::weak_ptr< TTF_Font > > fonts_;
32 29
         std::map<std::string, std::weak_ptr< SDL_Texture > > textures_;
33 30
         std::map<std::string, std::weak_ptr< Mix_Music > > musics_;
31
+        SDL_Renderer* renderer_;
34 32
     };
35 33
 
36 34
 }
37 35
new file mode 100644
... ...
@@ -0,0 +1 @@
1
+#include "geometry.h"
0 2
\ No newline at end of file
1 3
new file mode 100644
... ...
@@ -0,0 +1,12 @@
1
+#pragma once
2
+
3
+#include <boost/geometry/geometries/point_xy.hpp>
4
+#include <boost/container/flat_set.hpp>
5
+
6
+namespace scenimp {
7
+
8
+    typedef boost::geometry::model::d2::point_xy<int> point;
9
+
10
+}
11
+
12
+
... ...
@@ -1,4 +1,5 @@
1 1
 #include "group.h"
2
+#include "rendering.h"
2 3
 
3 4
 namespace scenimp {
4 5
 
... ...
@@ -11,9 +12,9 @@ namespace scenimp {
11 12
     const node_list& group::children() const {
12 13
         return children_;
13 14
     }
14
-    
15
-    void group::render(rendering& r) {
16
-        for(auto& c : children_) {
15
+
16
+    void group::do_render(rendering& r) {
17
+        for (auto& c : children_) {
17 18
             c->render(r);
18 19
         }
19 20
     }
... ...
@@ -1,9 +1,10 @@
1 1
 #pragma once
2 2
 
3
+
3 4
 #include "node.h"
4 5
 
5 6
 namespace scenimp {
6
-    
7
+
7 8
     class scene;
8 9
     class rendering;
9 10
 
... ...
@@ -13,13 +14,14 @@ namespace scenimp {
13 14
         group(const group&) = delete;
14 15
         virtual ~group();
15 16
 
16
-        virtual void render(rendering& r);
17
-
18 17
         const node_list& children() const;
19 18
 
19
+    protected:
20
+        virtual void do_render(rendering& r);
21
+
20 22
     private:
21 23
         node_list children_;
22
-        
24
+
23 25
         friend class scene;
24 26
     };
25 27
 
... ...
@@ -1,5 +1,6 @@
1 1
 #include "node.h"
2 2
 #include "group.h"
3
+#include "rendering.h"
3 4
 
4 5
 namespace scenimp {
5 6
 
... ...
@@ -10,11 +11,17 @@ namespace scenimp {
10 11
     node::~node() {
11 12
     }
12 13
 
13
-    transform& node::local_transform() {
14
-        return local_transform_;
14
+    const point& node::pos() const {
15
+        return pos_;
15 16
     }
16 17
 
17
-    const transform& node::local_transform() const {
18
-        return local_transform_;
18
+    point& node::pos() {
19
+        return pos_;
20
+    }
21
+
22
+    void node::render(rendering& r) {
23
+        r.push_pos(*this);
24
+        do_render(r);
25
+        r.pop_pos(*this);
19 26
     }
20 27
 }
... ...
@@ -1,6 +1,6 @@
1 1
 #pragma once
2 2
 
3
-#include "transform.h"
3
+#include "geometry.h"
4 4
 #include <boost/container/flat_set.hpp>
5 5
 
6 6
 namespace scenimp {
... ...
@@ -15,13 +15,16 @@ namespace scenimp {
15 15
         node(const node&) = delete;
16 16
         virtual ~node();
17 17
 
18
-        virtual void render(rendering& r) = 0;
18
+        void render(rendering& r);
19 19
 
20
-        transform& local_transform();
21
-        const transform& local_transform() const;
20
+        const point& pos() const;
21
+        point& pos();
22
+
23
+    protected:
24
+        virtual void do_render(rendering& r) = 0;
22 25
 
23 26
     private:
24
-        class transform local_transform_;
27
+        point pos_;
25 28
         group* parent_;
26 29
 
27 30
         friend class scene;
... ...
@@ -1,22 +1,34 @@
1 1
 #include <stack>
2 2
 
3 3
 #include "rendering.h"
4
+#include "node.h"
4 5
 
5 6
 namespace scenimp {
6 7
 
7
-    rendering::rendering() {
8
+    rendering::rendering(SDL_Renderer* r)
9
+    : renderer_(r) {
8 10
     }
9 11
 
10 12
     rendering::~rendering() {
11 13
     }
12 14
 
13
-    void rendering::push_transform(const transform& t) {
14
-        transform_stack_.push(t);
15
+    SDL_Renderer* rendering::renderer() {
16
+        return renderer_;
15 17
     }
16
-    const transform& rendering::current_transform() const {
17
-        return transform_stack_.top();
18
+
19
+    void rendering::push_pos(const node& n) {
20
+        const point& node_pos = n.pos();
21
+        pos_.x(pos_.x() + node_pos.x());
22
+        pos_.y(pos_.y() + node_pos.y());
23
+    }
24
+
25
+    const point& rendering::current_pos() const {
26
+        return pos_;
18 27
     }
19
-    void rendering::pop_transform() {
20
-        transform_stack_.pop();
28
+
29
+    void rendering::pop_pos(const node& n) {
30
+        const point& node_pos = n.pos();
31
+        pos_.x(pos_.x() - node_pos.x());
32
+        pos_.y(pos_.y() - node_pos.y());
21 33
     }
22 34
 }
... ...
@@ -1,22 +1,26 @@
1 1
 #pragma once
2 2
 
3
-#include "transform.h"
4
-#include <stack>
3
+#include "geometry.h"
4
+#include <SDL.h>
5 5
 
6 6
 namespace scenimp {
7
+    class node;
7 8
 
8 9
     class rendering {
9 10
     public:
10
-        rendering();
11
+        rendering(SDL_Renderer* r);
11 12
         rendering(const rendering&) = delete;
12 13
         ~rendering();
13 14
 
14
-        void push_transform(const transform& t);
15
-        const transform& current_transform() const;
16
-        void pop_transform();
15
+        void push_pos(const node& n);
16
+        const point& current_pos() const;
17
+        void pop_pos(const node& n);
18
+        
19
+        SDL_Renderer* renderer();
17 20
 
18 21
     private:
19
-        std::stack<transform> transform_stack_;
22
+        point pos_;
23
+        SDL_Renderer* renderer_;
20 24
     };
21 25
 
22 26
 }
... ...
@@ -3,7 +3,8 @@
3 3
 
4 4
 namespace scenimp {
5 5
 
6
-    scene::scene() {
6
+    scene::scene(SDL_Renderer* r)
7
+    : renderer_(r) {
7 8
         root_ = &new_group();
8 9
 
9 10
     }
... ...
@@ -44,9 +45,9 @@ namespace scenimp {
44 45
     void scene::detach(node* child) {
45 46
         child->parent_->children_.erase(child);
46 47
     }
47
-    
48
+
48 49
     void scene::render() {
49
-        rendering r;
50
+        rendering r(renderer_);
50 51
         root_->render(r);
51 52
     }
52 53
 
... ...
@@ -3,13 +3,13 @@
3 3
 #include "group.h"
4 4
 #include "sprite.h"
5 5
 #include <boost/pool/object_pool.hpp>
6
-
6
+#include <SDL.h>
7 7
 
8 8
 namespace scenimp {
9 9
 
10 10
     class scene {
11 11
     public:
12
-        scene();
12
+        scene(SDL_Renderer* r);
13 13
         scene(const scene&) = delete;
14 14
         ~scene();
15 15
         
... ...
@@ -27,6 +27,7 @@ namespace scenimp {
27 27
         boost::object_pool<group> group_pool_;
28 28
         boost::object_pool<sprite> sprite_pool_;
29 29
         group* root_;
30
+        SDL_Renderer* renderer_;
30 31
     };
31 32
 
32 33
 }
... ...
@@ -21,7 +21,7 @@ namespace scenimp {
21 21
             throw sdl_exception();
22 22
         }
23 23
         SDL_RenderSetLogicalSize(renderer_, 1920, 1080);
24
-        assets_ = new scenimp::assets(*this);
24
+        assets_ = new scenimp::assets(renderer_);
25 25
     }
26 26
 
27 27
     screen::~screen() {
... ...
@@ -2,22 +2,60 @@
2 2
 
3 3
 namespace scenimp {
4 4
 
5
-    sprite::sprite() {
5
+    sprite::sprite()
6
+    : angle_(0.0)
7
+    , flip_(SDL_FLIP_NONE)
8
+    , width_(32)
9
+    , height_(32){
6 10
     }
7 11
 
8 12
     sprite::~sprite() {
9 13
     }
10 14
 
11 15
     void sprite::render(rendering& r) {
12
-        //TODO
16
+        SDL_Rect rect_ = {x : -width_/2, y : -height_/2, w: width_, h: height_};
17
+
18
+        SDL_RenderCopyEx(r.renderer(), play_->current_frame().image().get(), &play_->current_frame().rect(), &rect_, angle_, NULL, flip_);
13 19
     }
14 20
 
15 21
     std::shared_ptr< nanim::play > sprite::play() {
16 22
         return play_;
17 23
     }
18 24
 
19
-    void set_play(std::shared_ptr< nanim::play > play) {
25
+    void sprite::set_play(std::shared_ptr< nanim::play > play) {
20 26
         play_ = play;
21 27
     }
22 28
 
29
+    SDL_RendererFlip sprite::flip() const {
30
+        return flip_;
31
+    }
32
+
33
+    void sprite::set_flip(SDL_RendererFlip flip) {
34
+        flip_ = flip;
35
+    }
36
+
37
+    double sprite::angle()const {
38
+        return angle_;
39
+    }
40
+
41
+    void sprite::set_angle(double a) {
42
+        angle_ = a;
43
+    }
44
+
45
+    int sprite::width() const {
46
+        return width_;
47
+    }
48
+
49
+    void sprite::set_width(int w) {
50
+        width_ = w;
51
+    }
52
+
53
+    int sprite::height() const {
54
+        return height_;
55
+    }
56
+
57
+    void sprite::set_height(int h) {
58
+        height_ = h;
59
+    }
60
+
23 61
 }
... ...
@@ -10,12 +10,29 @@ namespace scenimp {
10 10
         sprite();
11 11
         sprite(const sprite& orig) = delete;
12 12
         virtual ~sprite();
13
-        
13
+
14 14
         virtual void render(rendering& r);
15
-        
15
+
16 16
         std::shared_ptr< nanim::play > play();
17 17
         void set_play(std::shared_ptr< nanim::play > play);
18
+
19
+        SDL_RendererFlip flip() const;
20
+        void set_flip(SDL_RendererFlip flip);
21
+
22
+        double angle()const;
23
+        void set_angle(double a);
24
+
25
+        int width() const;
26
+        void set_width(int w);
27
+
28
+        int height() const;
29
+        void set_height(int h);
30
+
18 31
     private:
32
+        double angle_;
33
+        int width_;
34
+        int height_;
35
+        SDL_RendererFlip flip_;
19 36
         std::shared_ptr< nanim::play > play_;
20 37
     };
21 38
 
22 39
deleted file mode 100644
... ...
@@ -1,19 +0,0 @@
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
-}
20 0
deleted file mode 100644
... ...
@@ -1,19 +0,0 @@
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
-