Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
alpha_fader.h
1 #pragma once
2 
3 #include "../particle_script.h"
4 #include "curves.h"
5 
6 namespace smlt {
7 
8 class AlphaFader:
9  public Manipulator {
10 public:
11  AlphaFader(ParticleScript* script, const std::vector<float>& alphas, bool interpolate):
12  Manipulator(script, "alpha_fader"),
13  alphas_(alphas),
14  interpolate_(interpolate) {}
15 
16 private:
17  void do_manipulate(ParticleSystem*, Particle* particles, std::size_t particle_count, float) const {
18  auto size = (float) alphas_.size();
19  float fsize = float(size);
20  auto particle = particles;
21  for(auto i = 0u; i < particle_count; ++i, ++particle) {
22  const float e = (particle->lifetime - particle->ttl);
23  const float n = smlt::fast_divide(e, particle->lifetime);
24  const float fsizen = fsize * n;
25 
26  uint8_t colour = smlt::clamp(fsizen, 0.0f, fsize);
27 
28  particle->colour.a = alphas_[colour];
29 
30  if(interpolate_) {
31  const float f = fsizen - std::floor(fsizen);
32  auto next_alpha = alphas_[std::min((uint32_t) colour + 1, (uint32_t) size - 1)];
33  particle->colour.a = (particle->colour.a * (1.0f - f)) + (next_alpha * f);
34  }
35  }
36  }
37 
38  std::vector<float> alphas_;
39  bool interpolate_ = true;
40 };
41 
42 }
smlt::Manipulator
Definition: particle_script.h:38
smlt::clamp
float clamp(const float x, const float l, const float h)
Definition: utils.h:44
smlt
Definition: animation.cpp:25
smlt::ParticleSystem
Definition: particle_system.h:43
smlt::Particle
Definition: particle_system.h:21
smlt::ParticleScript
Definition: particle_script.h:71
smlt::AlphaFader
Definition: alpha_fader.h:9