blob: 73979768ef816634fcbfa5b3b77701c5e07cb2bc [file] [log] [blame]
Brian Osman7c979f52019-02-12 13:27:51 -05001/*
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 "SkColor.h"
13#include "SkCurve.h"
14#include "SkParticleData.h"
15#include "SkRandom.h"
Brian Osman7c979f52019-02-12 13:27:51 -050016#include "SkRefCnt.h"
Brian Osman7c979f52019-02-12 13:27:51 -050017#include "SkTArray.h"
18
19class SkAnimTimer;
20class SkCanvas;
21class SkFieldVisitor;
Brian Osman7c979f52019-02-12 13:27:51 -050022class SkParticleAffector;
Brian Osman543d2e22019-02-15 14:29:38 -050023class SkParticleDrawable;
Brian Osman7c979f52019-02-12 13:27:51 -050024class SkParticleEmitter;
25struct SkRSXform;
26
Brian Osman7c979f52019-02-12 13:27:51 -050027class SkParticleEffectParams : public SkRefCnt {
28public:
Brian Osman8b6283f2019-02-14 16:55:21 -050029 int fMaxCount = 128;
30 float fEffectDuration = 1.0f;
31 float fRate = 8.0f;
32 SkCurve fLifetime = 1.0f;
33 SkColor4f fStartColor = { 1.0f, 1.0f, 1.0f, 1.0f };
34 SkColor4f fEndColor = { 1.0f, 1.0f, 1.0f, 1.0f };
Brian Osman7c979f52019-02-12 13:27:51 -050035
Brian Osman543d2e22019-02-15 14:29:38 -050036 // Drawable (image, sprite sheet, etc.)
37 sk_sp<SkParticleDrawable> fDrawable;
Brian Osman7c979f52019-02-12 13:27:51 -050038
39 // Emitter shape & parameters
40 sk_sp<SkParticleEmitter> fEmitter;
41
Brian Osman5c1f8eb2019-02-14 14:49:55 -050042 // Rules that configure particles at spawn time
43 SkTArray<sk_sp<SkParticleAffector>> fSpawnAffectors;
44
45 // Rules that update existing particles over their lifetime
46 SkTArray<sk_sp<SkParticleAffector>> fUpdateAffectors;
Brian Osman7c979f52019-02-12 13:27:51 -050047
48 void visitFields(SkFieldVisitor* v);
49};
50
51class SkParticleEffect : public SkRefCnt {
52public:
Brian Osman5c1f8eb2019-02-14 14:49:55 -050053 SkParticleEffect(sk_sp<SkParticleEffectParams> params, const SkRandom& random);
Brian Osman7c979f52019-02-12 13:27:51 -050054
Brian Osman5c1f8eb2019-02-14 14:49:55 -050055 void start(const SkAnimTimer& timer, bool looping = false);
56 void update(const SkAnimTimer& timer);
Brian Osman7c979f52019-02-12 13:27:51 -050057 void draw(SkCanvas* canvas);
58
Brian Osman5c1f8eb2019-02-14 14:49:55 -050059 bool isAlive() { return fSpawnTime >= 0; }
60
Brian Osman7c979f52019-02-12 13:27:51 -050061 SkParticleEffectParams* getParams() { return fParams.get(); }
62
63private:
64 void setCapacity(int capacity);
65
Brian Osman7c979f52019-02-12 13:27:51 -050066 struct Particle {
67 double fTimeOfBirth;
68 double fTimeOfDeath;
69 SkRandom fStableRandom;
70
71 // Texture coord rects and colors are stored in parallel arrays for drawAtlas.
72 SkParticlePoseAndVelocity fPV;
73 };
74
75 sk_sp<SkParticleEffectParams> fParams;
Brian Osman7c979f52019-02-12 13:27:51 -050076
Brian Osman5c1f8eb2019-02-14 14:49:55 -050077 SkRandom fRandom;
78
79 bool fLooping;
80 double fSpawnTime;
81
Brian Osman7c979f52019-02-12 13:27:51 -050082 int fCount;
83 double fLastTime;
84 float fSpawnRemainder;
85
86 SkAutoTMalloc<Particle> fParticles;
87 SkAutoTMalloc<SkRSXform> fXforms;
Brian Osman543d2e22019-02-15 14:29:38 -050088 SkAutoTMalloc<float> fFrames;
Brian Osman7c979f52019-02-12 13:27:51 -050089 SkAutoTMalloc<SkColor> fColors;
90
91 // Cached
92 int fCapacity;
93};
94
95#endif // SkParticleEffect_DEFINED