From 291678d06a29ae6aa1d6638216026ff654f60615 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Sun, 15 Nov 2020 13:16:49 +0100 Subject: [PATCH] adapted to new update/render interface; added fading-rects module --- Makefile | 2 +- fading-rects/FadingRects.cpp | 143 +++++++++++++++++++++++++++++++++++ fading-rects/FadingRects.hpp | 18 +++++ fading-rects/Makefile | 62 +++++++++++++++ huey/Huey.cpp | 9 ++- main.cpp | 3 +- rects/RectSaver.cpp | 9 ++- skembo/Skembo.cpp | 9 ++- 8 files changed, 247 insertions(+), 8 deletions(-) create mode 100644 fading-rects/FadingRects.cpp create mode 100644 fading-rects/FadingRects.hpp create mode 100644 fading-rects/Makefile diff --git a/Makefile b/Makefile index 7e5fa41..32a9ebe 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ BIN := $(shell basename $$(pwd)) -PLUGINS := rects skembo huey +PLUGINS := rects skembo huey fading-rects MAKE += --no-print-directory diff --git a/fading-rects/FadingRects.cpp b/fading-rects/FadingRects.cpp new file mode 100644 index 0000000..1a35a5d --- /dev/null +++ b/fading-rects/FadingRects.cpp @@ -0,0 +1,143 @@ +// +// FadingRects.cpp +// screensaver +// +// Created by Bob Polis at 2020-10-26 +// Copyright (c) 2020 SwiftCoder. All rights reserved. +// + +#include "FadingRects.hpp" +#include +#include +#include +#include +#include +#include +#include + +const int default_frames_per_rect {10}; + +enum class FadingState { + fadein, + sustain, + fadeout +}; + +struct Rect { + double x {0}; + double y {0}; + double width {0}; + double height {0}; + double red {0.0}; + double green {0.0}; + double blue {0.0}; + double alpha {1.0}; + double cur_alpha {0.0}; + double delta {0.0}; + FadingState state {FadingState::fadein}; + int sustain_frame {0}; + double line_width {2.0}; +}; + + +class FadingRects : public ScreensaverPlugin { + public: + FadingRects() = default; + ~FadingRects() = default; + + int fps() const override; + void update () override; + void render() override; + + private: + double _hue {0.0}; + std::vector _rects; + int _frames_per_rect {default_frames_per_rect}; + std::uniform_real_distribution _random_line_width {1.0, 20.0}; + + Color next_color(); +}; + +ScreensaverPlugin* create_instance() { + return new FadingRects; +} + +int FadingRects::fps() const { + return 30; +} + +void FadingRects::update() { + // adjust rects + for (Rect& r : _rects) { + switch (r.state) { + case FadingState::fadein: + r.cur_alpha += r.delta; + if (r.cur_alpha > r.alpha) { + r.cur_alpha = r.alpha; + r.state = FadingState::sustain; + } + break; + case FadingState::sustain: + r.sustain_frame++; + if (r.sustain_frame > fps()) { + r.state = FadingState::fadeout; + } + break; + case FadingState::fadeout: + r.cur_alpha -= r.delta; + break; + } + } + auto it = std::remove_if(_rects.begin(), _rects.end(), [](const Rect& r) { + return r.cur_alpha < 0.0; + }); + _rects.erase(it, _rects.end()); + + // add new rect, if allowed + _frames_per_rect--; + if (_frames_per_rect == 0) { + _frames_per_rect = default_frames_per_rect; + cairo_rectangle_t rr {random_rect()}; + RGB rgb {RGB(next_color())}; + Rect rect; + rect.red = rgb.r; + rect.green = rgb.g; + rect.blue = rgb.b; + rect.alpha = random01(); + rect.cur_alpha = 0.0; + rect.state = FadingState::fadein; + rect.sustain_frame = 0; + rect.delta = rect.alpha / (5 * fps()); + rect.line_width = _random_line_width(_eng); + rect.x = rr.x; + rect.y = rr.y; + rect.width = rr.width; + rect.height = rr.height; + _rects.push_back(rect); + } +} + +void FadingRects::render() { + // clear screen + make_black(); + + // render rects + for (Rect r : _rects) { + cairo_set_source_rgba(_c, r.red, r.green, r.blue, r.cur_alpha); + cairo_rectangle(_c, r.x, r.y, r.width, r.height); + cairo_set_line_width(_c, r.line_width); + cairo_stroke(_c); + } +} + +Color FadingRects::next_color() { + _hue += 0.5; + if (_hue >= 360.0) { + _hue -= 360.0; + } + HSB hsb; + hsb.h = _hue; + hsb.s = random01(); + hsb.b = random01(); + return Color {hsb}; +} diff --git a/fading-rects/FadingRects.hpp b/fading-rects/FadingRects.hpp new file mode 100644 index 0000000..ef1dbec --- /dev/null +++ b/fading-rects/FadingRects.hpp @@ -0,0 +1,18 @@ +// +// FadingRects.hpp +// screensaver +// +// Created by Bob Polis at 2020-10-26 +// Copyright (c) 2020 SwiftCoder. All rights reserved. +// + +#ifndef _Huey_H_ +#define _Huey_H_ + +class ScreensaverPlugin; + +extern "C" { + ScreensaverPlugin* create_instance(); +} + +#endif // _Huey_H_ diff --git a/fading-rects/Makefile b/fading-rects/Makefile new file mode 100644 index 0000000..5309f6f --- /dev/null +++ b/fading-rects/Makefile @@ -0,0 +1,62 @@ +LIBNAME := $(shell basename $$(pwd)) +MAJOR := 1 +MINOR := 0.0 + +UNAME_S := $(shell uname -s) + +ifeq ($(UNAME_S),Darwin) + LINKERNAME := $(LIBNAME).dylib + SONAME := $(LIBNAME).$(MAJOR).dylib + REALNAME := $(LINKERNAME) +else + LINKERNAME := $(LIBNAME).so + SONAME := $(LINKERNAME).$(MAJOR) + REALNAME := $(SONAME).$(MINOR) +endif + +PREFIX ?= .. +LIBDIR ?= $(PREFIX)/plugins + +SRCS := $(wildcard *.cpp) +OBJS := $(subst .cpp,.o,$(SRCS)) +DEPS := $(subst .cpp,.d,$(SRCS)) +HDRS := $(filter-out $(LIBNAME).hpp,$(wildcard *.hpp)) + +CXX ?= g++ + +CXXFLAGS := $(CXXFLAGS) -Wshadow -Wall -Wpedantic -Wextra -g -fno-strict-aliasing -std=c++17 -fPIC +ifeq ($(DEBUG),1) + CXXFLAGS += -D DEBUG -O0 +else + CXXFLAGS += -D NDEBUG -O3 +endif + +LDLIBS := -lcairo -lscscreensaver + +RM := /bin/rm -f +INSTALL := /usr/bin/install -c + +.PHONY: all clean install + +all: $(REALNAME) + +$(REALNAME): $(OBJS) $(DEPS) +ifeq ($(UNAME_S),Darwin) + $(CXX) -dynamiclib -o $(REALNAME) -current_version $(MAJOR) -compatibility_version $(MINOR) $(LDFLAGS) $(LDLIBS) $(OBJS) +else + $(CXX) -g -shared -Wl,-soname,$(SONAME) -o $(REALNAME) $(LDFLAGS) $(LDLIBS) $(OBJS) +endif + +%.o: %.cpp %.d Makefile + $(CXX) $(CXXFLAGS) -MMD -MP -MT $@ -MF $*.d -c $< + +-include *.d + +%.d: ; + +clean: + $(RM) $(OBJS) $(DEPS) $(REALNAME) + +install: $(REALNAME) + $(INSTALL) -d $(LIBDIR) + $(INSTALL) -m 644 $(REALNAME) $(LIBDIR)/$(LIBNAME).saver diff --git a/huey/Huey.cpp b/huey/Huey.cpp index 256967d..048642f 100644 --- a/huey/Huey.cpp +++ b/huey/Huey.cpp @@ -18,7 +18,8 @@ class Huey : public ScreensaverPlugin { ~Huey() = default; int fps() const override; - void draw_frame() override; + void update() override; + void render() override; private: double _hue {0}; @@ -32,7 +33,11 @@ int Huey::fps() const { return 5; } -void Huey::draw_frame() { +void Huey::update() { + +} + +void Huey::render() { // next color HSB hsb; hsb.h = _hue; diff --git a/main.cpp b/main.cpp index f2a6c48..cce3abe 100644 --- a/main.cpp +++ b/main.cpp @@ -52,8 +52,9 @@ void list_plugins() { } void draw() { + main_saver->update(); sc::gui::ImageLock lock {*main_image}; - main_saver->draw_frame(); + main_saver->render(); main_window->dirty(true); main_window->show_image(*main_image); } diff --git a/rects/RectSaver.cpp b/rects/RectSaver.cpp index f3ba970..1634350 100644 --- a/rects/RectSaver.cpp +++ b/rects/RectSaver.cpp @@ -15,7 +15,8 @@ class RectSaver : public ScreensaverPlugin { ~RectSaver() = default; int fps() const override; - void draw_frame() override; + void update() override; + void render() override; }; ScreensaverPlugin* create_instance() { @@ -26,7 +27,11 @@ int RectSaver::fps() const { return 2; } -void RectSaver::draw_frame() { +void RectSaver::update() { + +} + +void RectSaver::render() { cairo_set_source_rgb(_c, random01(), random01(), random01()); cairo_rectangle_t rect {random_rect()}; cairo_rectangle(_c, rect.x, rect.y, rect.width, rect.height); diff --git a/skembo/Skembo.cpp b/skembo/Skembo.cpp index 5310d93..bf0b166 100644 --- a/skembo/Skembo.cpp +++ b/skembo/Skembo.cpp @@ -15,7 +15,8 @@ class Skembo : public ScreensaverPlugin { ~Skembo() = default; int fps() const override; - void draw_frame() override; + void update() override; + void render() override; private: double _hue {0}; @@ -29,7 +30,11 @@ int Skembo::fps() const { return 40; } -void Skembo::draw_frame() { +void Skembo::update() { + +} + +void Skembo::render() { // next color HSB hsb; hsb.h = _hue;