Browse code

add rectangle scenimp node

devnewton authored on 27/09/2015 18:02:40
Showing 10 changed files
... ...
@@ -35,10 +35,12 @@ add_executable(superpaflaballe
35 35
  src/scenimp/label.cpp
36 36
  src/scenimp/scene.cpp
37 37
  src/scenimp/screen.cpp
38
+ src/scenimp/shape_base.cpp
38 39
  src/scenimp/sprite.cpp
39 40
  src/scenimp/group.cpp
40 41
  src/scenimp/nanim.cpp
41 42
  src/scenimp/node.cpp
43
+ src/scenimp/rectangle.cpp
42 44
  src/scenimp/rendering.cpp
43 45
  src/scenimp/geometry.cpp
44 46
  src/scenimp/sdl_exception.cpp
... ...
@@ -142,6 +142,8 @@ namespace superpaflaballe {
142 142
         label->set_font(ned_font_);
143 143
         label->set_text("ned");
144 144
         label->pos().y(32);
145
+        
146
+        scene_.new_rectangle(group)->set_color({255, 0, 0, 255});
145 147
 
146 148
         world_.add<life_component>(e).life_ = remaining_ticks_ > 0 ? std::rand() % remaining_ticks_ : 1;
147 149
 
... ...
@@ -3,7 +3,9 @@
3 3
 
4 4
 namespace scenimp {
5 5
 
6
-    const SDL_Color defaultLabelColor = {255, 255, 255, 0};
6
+    namespace {
7
+        const SDL_Color defaultLabelColor = {255, 255, 255, 0};
8
+    }
7 9
 
8 10
     label::label()
9 11
     : color_(defaultLabelColor)
10 12
new file mode 100644
... ...
@@ -0,0 +1,42 @@
1
+#include "rectangle.h"
2
+#include "rendering.h"
3
+
4
+namespace scenimp {
5
+
6
+    rectangle::rectangle()
7
+    : width_(32)
8
+    , height_(32)
9
+    , filled_(false) {
10
+    }
11
+
12
+    rectangle::~rectangle() {
13
+    }
14
+
15
+    void rectangle::do_render(rendering& r) {
16
+        prerender(r);
17
+        const point& pos = r.current_pos();
18
+        SDL_Rect rect_ = {pos.x() + -width_ / 2, pos.y() + -height_ / 2, width_, height_};
19
+        if (filled_) {
20
+            SDL_RenderFillRect(r.renderer(), &rect_);
21
+        } else {
22
+            SDL_RenderDrawRect(r.renderer(), &rect_);
23
+        }
24
+        postrender(r);
25
+    }
26
+
27
+    int rectangle::width() const {
28
+        return width_;
29
+    }
30
+
31
+    void rectangle::set_width(int w) {
32
+        width_ = w;
33
+    }
34
+
35
+    int rectangle::height() const {
36
+        return height_;
37
+    }
38
+
39
+    void rectangle::set_height(int h) {
40
+        height_ = h;
41
+    }
42
+}
0 43
new file mode 100644
... ...
@@ -0,0 +1,33 @@
1
+#pragma once
2
+
3
+#include "shape_base.h"
4
+
5
+namespace scenimp {
6
+
7
+    class rectangle : public shape_base {
8
+    public:
9
+        rectangle();
10
+        rectangle(const rectangle& orig) = delete;
11
+        virtual ~rectangle();
12
+
13
+        bool filled() const;
14
+        void set_filled(bool f);
15
+        
16
+        int width() const;
17
+        void set_width(int w);
18
+
19
+        int height() const;
20
+        void set_height(int h);
21
+
22
+    protected:
23
+        virtual void do_render(rendering& r) override;
24
+
25
+    private:
26
+        int width_;
27
+        int height_;
28
+        bool filled_;
29
+    };
30
+
31
+}
32
+
33
+
... ...
@@ -13,33 +13,6 @@ namespace scenimp {
13 13
     scene::~scene() {
14 14
     }
15 15
 
16
-    std::shared_ptr<group> scene::new_group(std::shared_ptr<group> parent) {
17
-        auto g = std::make_shared<group>();
18
-        if (!parent) {
19
-            parent = root_;
20
-        }
21
-        parent->add_child(g);
22
-        return g;
23
-    }
24
-
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_child(s);
31
-        return s;
32
-    }
33
-    
34
-    std::shared_ptr<label> scene::new_label(std::shared_ptr<group> parent) {
35
-        auto l = std::make_shared<label>();
36
-        if (!parent) {
37
-            parent = root_;
38
-        }
39
-        parent->add_child(l);
40
-        return l;
41
-    }
42
-
43 16
     void scene::render() {
44 17
         rendering r(renderer_);
45 18
         root_->render(r);
... ...
@@ -2,6 +2,7 @@
2 2
 
3 3
 #include "label.h"
4 4
 #include "group.h"
5
+#include "rectangle.h"
5 6
 #include "sprite.h"
6 7
 #include <SDL.h>
7 8
 
... ...
@@ -12,14 +13,26 @@ namespace scenimp {
12 13
         scene(SDL_Renderer* r);
13 14
         scene(const scene&) = delete;
14 15
         ~scene();
15
-        
16
-        std::shared_ptr< group > new_group(std::shared_ptr< group > parent = nullptr);
17
-        std::shared_ptr< sprite > new_sprite(std::shared_ptr< group > parent = nullptr);
18
-        std::shared_ptr< label > new_label(std::shared_ptr< group > parent = nullptr);
16
+
17
+        std::shared_ptr< group > new_group(std::shared_ptr< group > parent = nullptr) {return new_node<group>(parent);}
18
+        std::shared_ptr< rectangle > new_rectangle(std::shared_ptr< group > parent = nullptr) {return new_node<rectangle>(parent);}
19
+        std::shared_ptr< sprite > new_sprite(std::shared_ptr< group > parent = nullptr) {return new_node<sprite>(parent);}
20
+        std::shared_ptr< label > new_label(std::shared_ptr< group > parent = nullptr) {return new_node<label>(parent);}
19 21
 
20 22
         void render();
21 23
 
22 24
     private:
25
+
26
+        template<class T>
27
+        std::shared_ptr<T> new_node(std::shared_ptr< group> parent = nullptr) {
28
+            auto n = std::make_shared<T>();
29
+            if (!parent) {
30
+                parent = root_;
31
+            }
32
+            parent->add_child(n);
33
+            return n;
34
+        }
35
+        
23 36
         std::shared_ptr<group> root_;
24 37
         SDL_Renderer* renderer_;
25 38
     };
26 39
new file mode 100644
... ...
@@ -0,0 +1,28 @@
1
+#include "shape_base.h"
2
+
3
+namespace scenimp {
4
+
5
+    shape_base::shape_base() {
6
+    }
7
+
8
+    shape_base::~shape_base() {
9
+    }
10
+
11
+    void shape_base::prerender(rendering& r) {
12
+        SDL_SetRenderDrawColor(r.renderer(), color_.r, color_.g, color_.b, color_.a);
13
+
14
+    }
15
+
16
+    void shape_base::postrender(rendering& r) {
17
+        SDL_SetRenderDrawColor(r.renderer(), 0, 0, 0, 255);
18
+    }
19
+
20
+    SDL_Color shape_base::color() const {
21
+        return color_;
22
+    }
23
+
24
+    void shape_base::set_color(SDL_Color c) {
25
+        color_ = c;
26
+    }
27
+
28
+}
0 29
\ No newline at end of file
1 30
new file mode 100644
... ...
@@ -0,0 +1,25 @@
1
+#pragma once
2
+
3
+#include "node.h"
4
+#include "rendering.h"
5
+
6
+namespace scenimp {
7
+
8
+    class shape_base : public node {
9
+    public:
10
+        shape_base();
11
+        shape_base(const shape_base& orig) = delete;
12
+        virtual ~shape_base();
13
+
14
+        SDL_Color color() const;
15
+        void set_color(SDL_Color c);
16
+    protected:
17
+
18
+        void prerender(rendering& r);
19
+        void postrender(rendering& r);
20
+
21
+    private:
22
+        SDL_Color color_;
23
+    };
24
+}
25
+
... ...
@@ -16,7 +16,6 @@ namespace scenimp {
16 16
     void sprite::do_render(rendering& r) {
17 17
         const point& pos = r.current_pos();
18 18
         SDL_Rect rect_ = {pos.x() + -width_ / 2, pos.y() + -height_ / 2, width_, height_};
19
-
20 19
         SDL_RenderCopyEx(r.renderer(), play_->current_frame().image().get(), &play_->current_frame().rect(), &rect_, angle_, NULL, flip_);
21 20
     }
22 21
 
... ...
@@ -59,5 +58,4 @@ namespace scenimp {
59 58
     void sprite::set_height(int h) {
60 59
         height_ = h;
61 60
     }
62
-
63 61
 }