adapted to new update/render interface; added fading-rects module

This commit is contained in:
Bob Polis 2020-11-15 13:16:49 +01:00
parent 73d41d4774
commit 291678d06a
8 changed files with 247 additions and 8 deletions

View File

@ -1,5 +1,5 @@
BIN := $(shell basename $$(pwd))
PLUGINS := rects skembo huey
PLUGINS := rects skembo huey fading-rects
MAKE += --no-print-directory

View File

@ -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 <libscscreensaver.hpp>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <random>
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<Rect> _rects;
int _frames_per_rect {default_frames_per_rect};
std::uniform_real_distribution<double> _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};
}

View File

@ -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_

62
fading-rects/Makefile Normal file
View File

@ -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

View File

@ -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;

View File

@ -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);
}

View File

@ -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);

View File

@ -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;