FastLED 3.9.3
Loading...
Searching...
No Matches
fx_layer.h
1#pragma once
2
3#include <stdint.h>
4#include <string.h>
5
6#include "crgb.h"
7#include "fixed_vector.h"
8#include "fx/fx.h"
9#include "namespace.h"
10#include "ref.h"
11#include "fx/frame.h"
12
13//#include <assert.h>
14
15FASTLED_NAMESPACE_BEGIN
16
17FASTLED_SMART_REF(FxLayer);
18class FxLayer : public Referent {
19 public:
20 void setFx(Ref<Fx> newFx) {
21 if (newFx != fx) {
22 release();
23 fx = newFx;
24 }
25 }
26
27 void draw(uint32_t now) {
28 //assert(fx);
29 if (!frame) {
30 frame = FrameRef::New(fx->getNumLeds());
31 }
32
33 if (!running) {
34 // Clear the frame
35 memset(frame->rgb(), 0, frame->size() * sizeof(CRGB));
36 if (fx->hasAlphaChannel()) {
37 memset(frame->alpha(), 0, frame->size());
38 }
39 fx->resume();
40 running = true;
41 }
42 Fx::DrawContext context = {now, frame->rgb()};
43 if (fx->hasAlphaChannel()) {
44 context.alpha_channel = frame->alpha();
45 }
46 fx->draw(context);
47 }
48
49 void pause() {
50 if (fx && running) {
51 fx->pause();
52 running = false;
53 }
54 }
55
56 void release() {
57 pause();
58 fx.reset();
59 }
60
61 Ref<Fx> getFx() { return fx; }
62
63 CRGB *getSurface() { return frame->rgb(); }
64 uint8_t *getSurfaceAlpha() {
65 return fx->hasAlphaChannel() ? frame->alpha() : nullptr;
66 }
67
68 private:
69 Ref<Frame> frame;
70 Ref<Fx> fx;
71 bool running = false;
72};
73
74FASTLED_NAMESPACE_END
virtual void draw(DrawContext context)=0
Definition ref.h:99
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:39