OSDN Git Service

fix cmake/Macros.cmake
[moflib/moflib.git] / moflib-1.0 / moflib / moflib / mof / particlegen.cpp
1 #include <mof/particlegen.hpp>
2 #include <mof/Board.hpp>
3 #include <mof/utilities.hpp>
4 #include <mof/streams.hpp>
5 #include <mof/ConsoleIO.hpp>
6 #include <list>
7
8
9 namespace mof
10 {
11 //{{{ impl
12         struct particlegen::impl
13         {
14                 Texture::ptr texture_;
15                 Matrix3DStream world_transform_;
16                 Manipulator<Matrix3D>::ptr particle_transform_;
17                 Manipulator<Color4f>::ptr particle_color_;
18                 std::list<Board*> particles_;
19                 frame_t frame_;///< パーティクルの生成寿命管理
20
21                 impl()
22                         : texture_(std::make_shared<Texture>("image/light.dds")), frame_(0)
23                 {
24                 }
25
26                 ~impl()
27                 {
28                         foreach (auto particle, particles_) {
29                                 delete particle;
30                         }
31                 }
32 //{{{ add_particle
33                 void add_particle()
34                 {
35                         auto p = std::unique_ptr<Board>(new Board());
36                         p->setTexture(texture_);
37                         
38                         // 初期位置をgeneratorの位置とする
39                         auto pos = world_transform_.value();
40                         Manipulator<Matrix3D>::ptr tweens[] = {particle_transform_, makeConstantHandler(pos)};
41                         auto t = makeCascadeHandler<Matrix3D>(tweens[0], lastOf(tweens));
42                         p->setWorldMatrix(t);
43                         
44                         p->setColor(particle_color_);
45                         particles_.push_back(p.release());
46                 }
47 //}}}
48         };
49 //}}}
50 //{{{ constructor
51         particlegen::particlegen()
52                 : pimpl_(new impl())
53         {
54                 {
55                         Scaling3D::keyframe_t keyframes1[] = 
56                                 {
57                                         makeKeyFrame(0, Vector3D(0, 0, 0)),
58                                         makeKeyFrame(50, Vector3D(0.25f, 0.25f, 0.25f)),
59                                         makeKeyFrame(200, Vector3D(0.5f, 0.5f, 0.5f)),
60                                 };
61                         auto tw1 = std::make_shared<Scaling3D>(keyframes1[0], lastOf(keyframes1));
62
63                         Translation3D::keyframe_t keyframes2[] = 
64                                 {
65                                         makeKeyFrame(0, Vector3D(0, 0, 0)),
66                                         makeKeyFrame(500, Vector3D(0, 1.5, 0)),
67                                 };
68                         auto tw2 = std::make_shared<Translation3D>(keyframes2[0], lastOf(keyframes2));
69
70                         Manipulator<Matrix3D>::ptr tweens[] = {tw1, tw2};
71                         pimpl_->particle_transform_ = makeCascadeHandler<Matrix3D>(tweens[0], lastOf(tweens));
72                 }
73                 
74                 {
75                         KeyFrameAnimation<Color4f>::KeyFrame keyframes[] = 
76                                 {
77                                         makeKeyFrame(0, Color4f(0, 1, 1, 0)),
78                                         makeKeyFrame(200, Color4f(1, 0, 1, 0)),
79                                         makeKeyFrame(400, Color4f(0, 0, 1, 0)),
80                                 };
81                         pimpl_->particle_color_ = 
82                                 makeKeyFrameAnimationHandler<Color4f>(keyframes[0], lastOf(keyframes));
83                 }
84
85         }
86 //}}}
87 //{{{ destructor
88         particlegen::~particlegen()
89         {
90         }
91 //}}}
92 //{{{ world_transform
93         const Matrix3DStream& particlegen::world_transform() const
94         {
95                 return pimpl_->world_transform_;
96         }
97 //}}}
98 //{{{ world_transform
99         Matrix3DStream& particlegen::world_transform()
100         {
101                 return pimpl_->world_transform_;
102         }
103 //}}}
104 //{{{ update
105         void particlegen::update()
106         {
107                 if (pimpl_->frame_++ % 20 == 0) {
108                         pimpl_->add_particle();
109                         if (pimpl_->frame_ > 500) {
110                                 delete pimpl_->particles_.front();
111                                 pimpl_->particles_.pop_front();// 寿命は500
112                         }
113                 }
114
115                 pimpl_->world_transform_.update();
116                 foreach (auto p, pimpl_->particles_) p->update();
117         }
118 //}}}
119 //{{{ drawables
120         const std::list<Board*>& particlegen::drawables() const
121         {
122                 return pimpl_->particles_;
123         }
124 //}}}
125 }// namespace mof