skia2/modules/particles/include/SkParticleEffect.h
Brian Osman 125daa4d1a Refactor and further generalization of particle model
- Collapsed the per-particle data into a single struct, and
  use that to communicate with drawables, too. Let the drawables
  manage allocation of xforms, colors, etc. Helpful for non-atlas
  drawables, and just to keep the effect code simpler.
- Having all of the params in a single struct allows us to move
  the remaining animated behaviors into affectors (color/frame).
- Added SkColorCurve, which works like SkCurve for SkColor4f.
  Use that to create a color affector (rather than simple
  start/end colors in the effect params).
- Also put the stable random in SkParticleState. This is going
  to be necessary if/when we change affectors to operate on all
  particles (rather than one at a time). Still need to move t
  value into the particle struct (or eval it from the lifetime
  params on demand).

Change-Id: Icf39116acbfd5d6e8eb91e9affbd8898d106211d
Reviewed-on: https://skia-review.googlesource.com/c/193473
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2019-02-20 18:01:00 +00:00

82 lines
1.9 KiB
C++

/*
* Copyright 2019 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkParticleEffect_DEFINED
#define SkParticleEffect_DEFINED
#include "SkAutoMalloc.h"
#include "SkColor.h"
#include "SkCurve.h"
#include "SkParticleData.h"
#include "SkRandom.h"
#include "SkRefCnt.h"
#include "SkTArray.h"
class SkAnimTimer;
class SkCanvas;
class SkFieldVisitor;
class SkParticleAffector;
class SkParticleDrawable;
class SkParticleEmitter;
class SkParticleEffectParams : public SkRefCnt {
public:
int fMaxCount = 128;
float fEffectDuration = 1.0f;
float fRate = 8.0f;
SkCurve fLifetime = 1.0f;
// Drawable (image, sprite sheet, etc.)
sk_sp<SkParticleDrawable> fDrawable;
// Emitter shape & parameters
sk_sp<SkParticleEmitter> fEmitter;
// Rules that configure particles at spawn time
SkTArray<sk_sp<SkParticleAffector>> fSpawnAffectors;
// Rules that update existing particles over their lifetime
SkTArray<sk_sp<SkParticleAffector>> fUpdateAffectors;
void visitFields(SkFieldVisitor* v);
};
class SkParticleEffect : public SkRefCnt {
public:
SkParticleEffect(sk_sp<SkParticleEffectParams> params, const SkRandom& random);
void start(const SkAnimTimer& timer, bool looping = false);
void update(const SkAnimTimer& timer);
void draw(SkCanvas* canvas);
bool isAlive() { return fSpawnTime >= 0; }
SkParticleEffectParams* getParams() { return fParams.get(); }
private:
void setCapacity(int capacity);
sk_sp<SkParticleEffectParams> fParams;
SkRandom fRandom;
bool fLooping;
double fSpawnTime;
int fCount;
double fLastTime;
float fSpawnRemainder;
SkAutoTMalloc<SkParticleState> fParticles;
SkAutoTMalloc<SkRandom> fStableRandoms;
// Cached
int fCapacity;
};
#endif // SkParticleEffect_DEFINED