Using configure method to build knots

This commit is contained in:
Bob Polis 2021-10-06 17:06:12 +02:00
parent 4b88304b25
commit 90e19e3211

View File

@ -21,38 +21,10 @@ struct Lissajous {
double line_width; double line_width;
cairo_rectangle_t frame; cairo_rectangle_t frame;
Lissajous(double f0,
double f1,
double f2,
double f3,
double p0,
double p1,
double p2,
double p3,
int segments,
double shift);
double calc_x(double phi) const; double calc_x(double phi) const;
double calc_y(double phi) const; double calc_y(double phi) const;
}; };
Lissajous::Lissajous(double f0,
double f1,
double f2,
double f3,
double p0,
double p1,
double p2,
double p3,
int segments,
double shift)
{
f[0] = f0; f[1] = f1; f[2] = f2; f[3] = f3;
p[0] = p0; p[1] = p1; p[2] = p2; p[3] = p3;
steps = segments;
delta = shift;
}
double Lissajous::calc_x(double phi) const { double Lissajous::calc_x(double phi) const {
return sin(f[0] * phi + p[0]) * cos(f[1] * phi + p[1]); return sin(f[0] * phi + p[0]) * cos(f[1] * phi + p[1]);
} }
@ -63,12 +35,13 @@ double Lissajous::calc_y(double phi) const {
class Whirling : public ScreensaverPlugin { class Whirling : public ScreensaverPlugin {
public: public:
Whirling(); Whirling() = default;
~Whirling() = default; ~Whirling() = default;
int fps() const override; int fps() const override;
void update() override; void update() override;
void render() override; void render() override;
void configure() override;
private: private:
std::vector<Lissajous> knots; std::vector<Lissajous> knots;
@ -80,17 +53,6 @@ ScreensaverPlugin* create_instance() {
return new Whirling; return new Whirling;
} }
Whirling::Whirling() {
knots.emplace_back(9, 7, 5, 7,
0, 0, 0, 0,
1000,
2 * M_PI / 360.0);
Lissajous& knot = knots.back();
knot.color = {1.0, 1.0, 1.0}; // white
knot.alpha = 1.0; // opaque
knot.line_width = 1.0;
}
int Whirling::fps() const { int Whirling::fps() const {
return 30; return 30;
} }
@ -131,3 +93,23 @@ void Whirling::render_knot(const Lissajous& knot) {
cairo_set_line_width(_c, knot.line_width); cairo_set_line_width(_c, knot.line_width);
cairo_stroke(_c); cairo_stroke(_c);
} }
void Whirling::configure() {
for (const auto& obj : _j["knots"]) {
Lissajous knot;
for (int i = 0; i < 4; ++i) {
knot.f[i] = obj["f"][i];
}
for (int i = 0; i < 4; ++i) {
knot.p[i] = obj["p"][i];
}
knot.steps = obj["steps"];
knot.delta = obj["delta"];
knot.color.r = obj["r"];
knot.color.g = obj["g"];
knot.color.b = obj["b"];
knot.alpha = obj["a"];
knot.line_width = obj["line_width"];
knots.push_back(knot);
}
}