1 /* 2 * Copyright 2019 Google LLC 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkParticleEffect_DEFINED 9 #define SkParticleEffect_DEFINED 10 11 #include "SkAutoMalloc.h" 12 #include "SkCurve.h" 13 #include "SkRandom.h" 14 #include "SkRefCnt.h" 15 #include "SkTArray.h" 16 17 class SkCanvas; 18 class SkFieldVisitor; 19 class SkParticleAffector; 20 class SkParticleDrawable; 21 struct SkParticleState; 22 23 class SkParticleEffectParams : public SkRefCnt { 24 public: 25 int fMaxCount = 128; 26 float fEffectDuration = 1.0f; 27 float fRate = 8.0f; 28 SkCurve fLifetime = 1.0f; 29 30 // Drawable (image, sprite sheet, etc.) 31 sk_sp<SkParticleDrawable> fDrawable; 32 33 // Rules that configure particles at spawn time 34 SkTArray<sk_sp<SkParticleAffector>> fSpawnAffectors; 35 36 // Rules that update existing particles over their lifetime 37 SkTArray<sk_sp<SkParticleAffector>> fUpdateAffectors; 38 39 void visitFields(SkFieldVisitor* v); 40 }; 41 42 class SkParticleEffect : public SkRefCnt { 43 public: 44 SkParticleEffect(sk_sp<SkParticleEffectParams> params, const SkRandom& random); 45 46 void start(double now, bool looping = false); 47 void update(double now); 48 void draw(SkCanvas* canvas); 49 isAlive()50 bool isAlive() const { return fSpawnTime >= 0; } getCount()51 int getCount() const { return fCount; } 52 53 private: 54 void setCapacity(int capacity); 55 56 sk_sp<SkParticleEffectParams> fParams; 57 58 SkRandom fRandom; 59 60 bool fLooping; 61 double fSpawnTime; 62 63 int fCount; 64 double fLastTime; 65 float fSpawnRemainder; 66 67 SkAutoTMalloc<SkParticleState> fParticles; 68 SkAutoTMalloc<SkRandom> fStableRandoms; 69 70 // Cached 71 int fCapacity; 72 }; 73 74 #endif // SkParticleEffect_DEFINED 75