Browse code

add progressbar scenimp node

devnewton authored on 27/09/2015 18:39:42
Showing 8 changed files
... ...
@@ -40,6 +40,7 @@ add_executable(superpaflaballe
40 40
  src/scenimp/group.cpp
41 41
  src/scenimp/nanim.cpp
42 42
  src/scenimp/node.cpp
43
+ src/scenimp/progressbar.cpp
43 44
  src/scenimp/rectangle.cpp
44 45
  src/scenimp/rendering.cpp
45 46
  src/scenimp/geometry.cpp
... ...
@@ -11,15 +11,16 @@ namespace superpaflaballe {
11 11
     public:
12 12
 
13 13
         virtual bool accept(bourrines::entity e) override {
14
-            return has<life_component>(e);
14
+            return has<life_component>(e) && has<sprite_component>(e);
15 15
         }
16 16
 
17 17
         virtual void process(bourrines::entity e) override {
18 18
             life_component& life = get<life_component>(e);
19
-
19
+            get<sprite_component>(e).lifebar_->set_progress(life.life_);
20 20
             if (life.life_-- < 0) {
21 21
                 world().kill_entity(e);
22 22
             }
23
+
23 24
         }
24 25
 
25 26
     };
... ...
@@ -124,7 +125,7 @@ namespace superpaflaballe {
124 125
         bourrines::entity e = world_.create_entity();
125 126
         auto group = scene_.new_group();
126 127
         auto sprite = scene_.new_sprite(group);
127
-        
128
+
128 129
         auto& component = world_.add<sprite_component>(e);
129 130
         component.sprite_ = sprite;
130 131
         component.group_ = group;
... ...
@@ -132,7 +133,7 @@ namespace superpaflaballe {
132 133
         auto& pos = group->pos();
133 134
         pos.x(std::rand() % screen_.logical_screen_width());
134 135
         pos.y(std::rand() % screen_.logical_screen_height());
135
-        
136
+
136 137
         dir_component& dir = world_.add<dir_component>(e);
137 138
         dir.dx_ = (1 + (std::rand() % 10)) * ((std::rand() % 1) ? -1 : 1);
138 139
         dir.dy_ = (1 + (std::rand() % 10)) * ((std::rand() % 1) ? -1 : 1);
... ...
@@ -142,10 +143,16 @@ namespace superpaflaballe {
142 143
         label->set_font(ned_font_);
143 144
         label->set_text("ned");
144 145
         label->pos().y(32);
145
-        
146
+
146 147
         scene_.new_rectangle(group)->set_color({255, 0, 0, 255});
147 148
 
148
-        world_.add<life_component>(e).life_ = remaining_ticks_ > 0 ? std::rand() % remaining_ticks_ : 1;
149
+
150
+        int life = world_.add<life_component>(e).life_ = remaining_ticks_ > 0 ? std::rand() % remaining_ticks_ : 1;
151
+
152
+        component.lifebar_ = scene_.new_progressbar(group);
153
+        component.lifebar_->pos().y(-16);
154
+        component.lifebar_->set_maximum(life);
155
+        component.lifebar_->set_progress(life);
149 156
 
150 157
         world_.changed(e);
151 158
     }
... ...
@@ -5,6 +5,7 @@
5 5
 #include "scenimp/nanim.h"
6 6
 #include "scenimp/assets.h"
7 7
 #include "bourrines/bourrines.h"
8
+#include "scenimp/progressbar.h"
8 9
 
9 10
 #include <boost/timer/timer.hpp>
10 11
 
... ...
@@ -21,6 +22,7 @@ namespace superpaflaballe {
21 22
     struct sprite_component {
22 23
         std::shared_ptr<scenimp::group> group_;
23 24
         std::shared_ptr<scenimp::sprite> sprite_;
25
+        std::shared_ptr<scenimp::progressbar> lifebar_;
24 26
         
25 27
         ~sprite_component() {
26 28
             if(group_) {
27 29
new file mode 100644
... ...
@@ -0,0 +1,68 @@
1
+#include "progressbar.h"
2
+#include "rendering.h"
3
+
4
+namespace scenimp {
5
+
6
+    progressbar::progressbar()
7
+    : width_(32)
8
+    , height_(8)
9
+    , progress_(100)
10
+    , maximum_(100)
11
+    , background_color_({255, 204, 0, 255})
12
+    , border_color_({255, 255, 255, 255})
13
+    , progress_color_({255, 0, 0, 255}) {
14
+    }
15
+
16
+    progressbar::~progressbar() {
17
+    }
18
+
19
+    void progressbar::do_render(rendering& r) {
20
+        const point& pos = r.current_pos();
21
+
22
+        SDL_Rect rect = {pos.x() + -width_ / 2, pos.y() + -height_ / 2, width_, height_};
23
+        SDL_SetRenderDrawColor(r.renderer(), background_color_.r, background_color_.g, background_color_.b, background_color_.a);
24
+        SDL_RenderFillRect(r.renderer(), &rect);
25
+
26
+        SDL_Rect rect_ = {pos.x() + -width_ / 2, pos.y() + -height_ / 2, (progress_ * width_) / maximum_, height_};
27
+        SDL_SetRenderDrawColor(r.renderer(), progress_color_.r, progress_color_.g, progress_color_.b, progress_color_.a);
28
+        SDL_RenderFillRect(r.renderer(), &rect_);
29
+
30
+        SDL_SetRenderDrawColor(r.renderer(), border_color_.r, border_color_.g, border_color_.b, border_color_.a);
31
+        SDL_RenderDrawRect(r.renderer(), &rect);
32
+        SDL_SetRenderDrawColor(r.renderer(), 0, 0, 0, 255);
33
+    }
34
+
35
+    int progressbar::width() const {
36
+        return width_;
37
+    }
38
+
39
+    void progressbar::set_width(int w) {
40
+        width_ = w;
41
+    }
42
+
43
+    int progressbar::height() const {
44
+        return height_;
45
+    }
46
+
47
+    void progressbar::set_height(int h) {
48
+        height_ = h;
49
+    }
50
+
51
+    int progressbar::progress() {
52
+        return progress_;
53
+    }
54
+
55
+    void progressbar::set_progress(int p) {
56
+        progress_ = boost::algorithm::clamp(p, 0, maximum_);
57
+    }
58
+
59
+    int progressbar::maximum() const {
60
+        return maximum_;
61
+    }
62
+
63
+    void progressbar::set_maximum(int m) {
64
+        maximum_ = std::max(m, 1);
65
+        progress_ = boost::algorithm::clamp(progress_, 0, maximum_);
66
+    }
67
+
68
+}
0 69
new file mode 100644
... ...
@@ -0,0 +1,42 @@
1
+#pragma once
2
+
3
+#include "node.h"
4
+#include <SDL_pixels.h>
5
+#include <boost/algorithm/clamp.hpp>
6
+
7
+namespace scenimp {
8
+
9
+    class progressbar : public node {
10
+    public:
11
+        progressbar();
12
+        progressbar(const progressbar& orig) = delete;
13
+        virtual ~progressbar();
14
+
15
+        int width() const;
16
+        void set_width(int w);
17
+
18
+        int height() const;
19
+        void set_height(int h);
20
+        
21
+        int progress();
22
+        void set_progress(int p);
23
+        
24
+        int maximum() const;
25
+        void set_maximum(int m);
26
+
27
+    protected:
28
+        virtual void do_render(rendering& r) override;
29
+
30
+    private:
31
+        int width_;
32
+        int height_;
33
+        int progress_;
34
+        int maximum_;
35
+        SDL_Color background_color_;
36
+        SDL_Color border_color_;
37
+        SDL_Color progress_color_;
38
+    };
39
+
40
+}
41
+
42
+
... ...
@@ -15,11 +15,11 @@ namespace scenimp {
15 15
     void rectangle::do_render(rendering& r) {
16 16
         prerender(r);
17 17
         const point& pos = r.current_pos();
18
-        SDL_Rect rect_ = {pos.x() + -width_ / 2, pos.y() + -height_ / 2, width_, height_};
18
+        SDL_Rect rect = {pos.x() + -width_ / 2, pos.y() + -height_ / 2, width_, height_};
19 19
         if (filled_) {
20
-            SDL_RenderFillRect(r.renderer(), &rect_);
20
+            SDL_RenderFillRect(r.renderer(), &rect);
21 21
         } else {
22
-            SDL_RenderDrawRect(r.renderer(), &rect_);
22
+            SDL_RenderDrawRect(r.renderer(), &rect);
23 23
         }
24 24
         postrender(r);
25 25
     }
... ...
@@ -2,6 +2,7 @@
2 2
 
3 3
 #include "label.h"
4 4
 #include "group.h"
5
+#include "progressbar.h"
5 6
 #include "rectangle.h"
6 7
 #include "sprite.h"
7 8
 #include <SDL.h>
... ...
@@ -14,10 +15,25 @@ namespace scenimp {
14 15
         scene(const scene&) = delete;
15 16
         ~scene();
16 17
 
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);}
18
+        std::shared_ptr< group > new_group(std::shared_ptr< group > parent = nullptr) {
19
+            return new_node<group>(parent);
20
+        }
21
+
22
+        std::shared_ptr< rectangle > new_rectangle(std::shared_ptr< group > parent = nullptr) {
23
+            return new_node<rectangle>(parent);
24
+        }
25
+
26
+        std::shared_ptr< sprite > new_sprite(std::shared_ptr< group > parent = nullptr) {
27
+            return new_node<sprite>(parent);
28
+        }
29
+
30
+        std::shared_ptr< label > new_label(std::shared_ptr< group > parent = nullptr) {
31
+            return new_node<label>(parent);
32
+        }
33
+
34
+        std::shared_ptr< progressbar > new_progressbar(std::shared_ptr< group > parent = nullptr) {
35
+            return new_node<progressbar>(parent);
36
+        }
21 37
 
22 38
         void render();
23 39
 
... ...
@@ -32,7 +48,7 @@ namespace scenimp {
32 48
             parent->add_child(n);
33 49
             return n;
34 50
         }
35
-        
51
+
36 52
         std::shared_ptr<group> root_;
37 53
         SDL_Renderer* renderer_;
38 54
     };
... ...
@@ -15,8 +15,8 @@ namespace scenimp {
15 15
 
16 16
     void sprite::do_render(rendering& r) {
17 17
         const point& pos = r.current_pos();
18
-        SDL_Rect rect_ = {pos.x() + -width_ / 2, pos.y() + -height_ / 2, width_, height_};
19
-        SDL_RenderCopyEx(r.renderer(), play_->current_frame().image().get(), &play_->current_frame().rect(), &rect_, angle_, NULL, flip_);
18
+        SDL_Rect rect = {pos.x() + -width_ / 2, pos.y() + -height_ / 2, width_, height_};
19
+        SDL_RenderCopyEx(r.renderer(), play_->current_frame().image().get(), &play_->current_frame().rect(), &rect, angle_, NULL, flip_);
20 20
     }
21 21
 
22 22
     std::shared_ptr< nanim::play > sprite::play() {