Browse code

update benchmark to use scenimp

devnewton authored on 13/09/2015 16:51:04
Showing 9 changed files
... ...
@@ -34,6 +34,7 @@ add_executable(superpaflaballe
34 34
  src/scenimp/assets.cpp
35 35
  src/scenimp/scene.cpp
36 36
  src/scenimp/screen.cpp
37
+ src/scenimp/sprite.cpp
37 38
  src/scenimp/group.cpp
38 39
  src/scenimp/nanim.cpp
39 40
  src/scenimp/node.cpp
... ...
@@ -67,6 +67,17 @@ namespace bourrines {
67 67
     private:
68 68
         World* world_;
69 69
     };
70
+    
71
+    template<typename World>
72
+    class basic_system : public World::system_type {
73
+    public:
74
+
75
+        virtual void process(const active_entity_list&) override {
76
+            process();
77
+        }
78
+
79
+        virtual void process() = 0;
80
+    };
70 81
 
71 82
     template<typename World>
72 83
     class processing_system : public World::system_type {
... ...
@@ -57,29 +57,29 @@ namespace superpaflaballe {
57 57
         }
58 58
 
59 59
         virtual bool accept(bourrines::entity e) override {
60
-            return has<pos_component>(e) && has<dir_component>(e);
60
+            return has<sprite_component>(e) && has<dir_component>(e);
61 61
         }
62 62
 
63 63
         virtual void process(bourrines::entity e) override {
64
-            pos_component& pos = get<pos_component>(e);
64
+            scenimp::point& pos = get<sprite_component>(e).sprite_->pos();
65 65
             dir_component& dir = get<dir_component>(e);
66 66
 
67
-            pos.x_ += dir.dx_;
68
-            pos.y_ += dir.dy_;
67
+            pos.x(pos.x() + dir.dx_);
68
+            pos.y(pos.y() + dir.dy_);
69 69
 
70
-            if (pos.x_ < 0) {
70
+            if (pos.x() < 0) {
71 71
                 dir.dx_ = -dir.dx_;
72 72
             }
73 73
 
74
-            if (pos.y_ < 0) {
74
+            if (pos.y() < 0) {
75 75
                 dir.dy_ = -dir.dy_;
76 76
             }
77 77
 
78
-            if (pos.x_ >= screen_.logical_screen_width()) {
78
+            if (pos.x() >= screen_.logical_screen_width()) {
79 79
                 dir.dx_ = -dir.dx_;
80 80
             }
81 81
 
82
-            if (pos.y_ >= screen_.logical_screen_height()) {
82
+            if (pos.y() >= screen_.logical_screen_height()) {
83 83
                 dir.dy_ = -dir.dy_;
84 84
             }
85 85
         }
... ...
@@ -89,39 +89,31 @@ namespace superpaflaballe {
89 89
 
90 90
     };
91 91
 
92
-    class render_system : public bourrines::selective_processing_system<world> {
92
+    class render_system : public bourrines::basic_system<world> {
93 93
     public:
94 94
 
95
-        render_system(scenimp::screen& s)
96
-        : screen_(s) {
95
+        render_system(scenimp::scene& s)
96
+        : scene_(s) {
97 97
         }
98 98
 
99
-        virtual bool accept(bourrines::entity e) override {
100
-            return has<pos_component>(e) && has<anim_component>(e);
99
+        virtual void process() {
100
+            scene_.render();
101 101
         }
102 102
 
103
-        virtual void process(bourrines::entity e) override {
104
-            const pos_component& pos = get<pos_component>(e);
105
-            const anim_component& anim = get<anim_component>(e);
106
-            SDL_Rect rect;
107
-            rect.w = 32;
108
-            rect.h = 32;
109
-            rect.x = pos.x_;
110
-            rect.y = pos.y_;
111
-            SDL_RenderCopy(screen_.renderer(), anim.play_->current_frame().image().get(), &anim.play_->current_frame().rect(), &rect);
112
-        }
113 103
     private:
114
-        scenimp::screen& screen_;
104
+        scenimp::scene& scene_;
115 105
     };
116 106
 
117 107
     bourrines_benchmark::bourrines_benchmark(scenimp::screen& screen, int num_entity, int num_ticks)
118
-    : screen_(screen), remaining_ticks_(num_ticks) {
108
+    : screen_(screen)
109
+    , scene_(screen.renderer())
110
+    , remaining_ticks_(num_ticks) {
119 111
         timer_.stop();
120 112
         ned_anim_ = screen.assets().animations("ned.json");
121 113
         world_.add_system(1, std::unique_ptr<hera_system>(new hera_system(*this, num_entity)));
122 114
         world_.add_system(2, std::unique_ptr<hades_system>(new hades_system()));
123 115
         world_.add_system(3, std::unique_ptr<move_system>(new move_system(screen)));
124
-        world_.add_system(4, std::unique_ptr<render_system>(new render_system(screen)));
116
+        world_.add_system(4, std::unique_ptr<render_system>(new render_system(scene_)));
125 117
     }
126 118
 
127 119
     bourrines_benchmark::~bourrines_benchmark() {
... ...
@@ -130,15 +122,18 @@ namespace superpaflaballe {
130 122
 
131 123
     void bourrines_benchmark::create_ned() {
132 124
         bourrines::entity e = world_.create_entity();
133
-        pos_component& pos = world_.add<pos_component>(e);
134
-        pos.x_ = std::rand() % screen_.logical_screen_width();
135
-        pos.y_ = std::rand() % screen_.logical_screen_height();
125
+        scenimp::sprite& sprite = scene_.new_sprite();
126
+        world_.add<sprite_component>(e).sprite_ = &sprite;
127
+        auto& pos = sprite.pos();
128
+
129
+        pos.x(std::rand() % screen_.logical_screen_width());
130
+        pos.y(std::rand() % screen_.logical_screen_height());
136 131
 
137 132
         dir_component& dir = world_.add<dir_component>(e);
138 133
         dir.dx_ = (1 + (std::rand() % 10)) * ((std::rand() % 1) ? -1 : 1);
139 134
         dir.dy_ = (1 + (std::rand() % 10)) * ((std::rand() % 1) ? -1 : 1);
140 135
 
141
-        world_.add<anim_component>(e).play_ = ned_anim_->play(scenimp::nanim::loop);
136
+        sprite.set_play(ned_anim_->play(scenimp::nanim::loop));
142 137
         world_.add<life_component>(e).life_ = remaining_ticks_ > 0 ? std::rand() % remaining_ticks_ : 1;
143 138
 
144 139
         world_.changed(e);
... ...
@@ -1,6 +1,7 @@
1 1
 #pragma once
2 2
 
3 3
 #include "scenimp/screen.h"
4
+#include "scenimp/scene.h"
4 5
 #include "scenimp/nanim.h"
5 6
 #include "scenimp/assets.h"
6 7
 #include "bourrines/bourrines.h"
... ...
@@ -9,10 +10,6 @@
9 10
 
10 11
 namespace superpaflaballe {
11 12
 
12
-    struct pos_component {
13
-        int x_, y_;
14
-    };
15
-
16 13
     struct dir_component {
17 14
         int dx_, dy_;
18 15
     };
... ...
@@ -21,11 +18,11 @@ namespace superpaflaballe {
21 18
         int life_;
22 19
     };
23 20
 
24
-    struct anim_component {
25
-        std::shared_ptr< scenimp::nanim::play > play_;
21
+    struct sprite_component {
22
+        scenimp::sprite* sprite_;
26 23
     };
27 24
 
28
-    typedef bourrines::default_world<pos_component, dir_component, life_component, anim_component> world;
25
+    typedef bourrines::default_world<dir_component, life_component, sprite_component> world;
29 26
 
30 27
     class bourrines_benchmark {
31 28
     public:
... ...
@@ -40,6 +37,7 @@ namespace superpaflaballe {
40 37
     private:
41 38
         world world_;
42 39
         scenimp::screen& screen_;
40
+        scenimp::scene scene_;
43 41
         std::shared_ptr< scenimp::nanim::collection > ned_anim_;
44 42
         boost::timer::auto_cpu_timer timer_;
45 43
         int remaining_ticks_;
... ...
@@ -5,7 +5,8 @@
5 5
 namespace scenimp {
6 6
 
7 7
     node::node()
8
-    : parent_(nullptr) {
8
+    : pos_(0, 0)
9
+    , parent_(nullptr) {
9 10
     }
10 11
 
11 12
     node::~node() {
... ...
@@ -6,7 +6,8 @@
6 6
 namespace scenimp {
7 7
 
8 8
     rendering::rendering(SDL_Renderer* r)
9
-    : renderer_(r) {
9
+    : pos_(0, 0),
10
+    renderer_(r) {
10 11
     }
11 12
 
12 13
     rendering::~rendering() {
... ...
@@ -1,25 +1,26 @@
1 1
 #include "scene.h"
2 2
 #include "rendering.h"
3
+#include <iostream>
4
+#include <exception>
3 5
 
4 6
 namespace scenimp {
5 7
 
6 8
     scene::scene(SDL_Renderer* r)
7 9
     : renderer_(r) {
8
-        root_ = &new_group();
9
-
10
+        root_ = group_pool_.construct();
10 11
     }
11 12
 
12 13
     scene::~scene() {
13 14
     }
14 15
 
15 16
     group& scene::new_group(group* parent, int z) {
16
-        group* s = group_pool_.malloc();
17
+        group* s = group_pool_.construct();
17 18
         attach(s, parent,z);
18 19
         return *s;
19 20
     }
20 21
 
21 22
     sprite& scene::new_sprite(group* parent, int z) {
22
-        sprite* s = sprite_pool_.malloc();
23
+        sprite* s = sprite_pool_.construct();
23 24
         attach(s, parent, z);
24 25
         return *s;
25 26
     }
... ...
@@ -28,7 +29,8 @@ namespace scenimp {
28 29
         if (nullptr == parent) {
29 30
             parent = root_;
30 31
         }
31
-        parent->children_.insert(node_in_group(child, z));
32
+        node_in_group n(child, z);
33
+        parent->children_.insert(n);
32 34
         child->parent_ = parent;
33 35
     }
34 36
 
... ...
@@ -1,19 +1,21 @@
1 1
 #include "sprite.h"
2
+#include "rendering.h"
2 3
 
3 4
 namespace scenimp {
4 5
 
5 6
     sprite::sprite()
6 7
     : angle_(0.0)
7
-    , flip_(SDL_FLIP_NONE)
8 8
     , width_(32)
9
-    , height_(32){
9
+    , height_(32)
10
+    , flip_(SDL_FLIP_NONE) {
10 11
     }
11 12
 
12 13
     sprite::~sprite() {
13 14
     }
14 15
 
15
-    void sprite::render(rendering& r) {
16
-        SDL_Rect rect_ = {x : -width_/2, y : -height_/2, w: width_, h: height_};
16
+    void sprite::do_render(rendering& r) {
17
+        const point& pos = r.current_pos();
18
+        SDL_Rect rect_ = {pos.x() + -width_ / 2, pos.y() + -height_ / 2, width_, height_};
17 19
 
18 20
         SDL_RenderCopyEx(r.renderer(), play_->current_frame().image().get(), &play_->current_frame().rect(), &rect_, angle_, NULL, flip_);
19 21
     }
... ...
@@ -11,8 +11,6 @@ namespace scenimp {
11 11
         sprite(const sprite& orig) = delete;
12 12
         virtual ~sprite();
13 13
 
14
-        virtual void render(rendering& r);
15
-
16 14
         std::shared_ptr< nanim::play > play();
17 15
         void set_play(std::shared_ptr< nanim::play > play);
18 16
 
... ...
@@ -27,6 +25,9 @@ namespace scenimp {
27 25
 
28 26
         int height() const;
29 27
         void set_height(int h);
28
+        
29
+    protected:
30
+        virtual void do_render(rendering& r) override;
30 31
 
31 32
     private:
32 33
         double angle_;