first commit

This commit is contained in:
Bob Polis 2020-10-27 13:39:55 +01:00
commit 3c8d820c30
3 changed files with 179 additions and 0 deletions

84
Makefile Normal file
View File

@ -0,0 +1,84 @@
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 ?= /usr/local
BINDIR ?= $(PREFIX)/bin
CONFIGDIR ?= $(PREFIX)/etc
INCLUDEDIR ?= $(PREFIX)/include
LIBDIR ?= $(PREFIX)/lib
DATADIR ?= $(PREFIX)/share
MANDIR ?= $(DATADIR)/man
DOCDIR ?= $(DATADIR)/$(LIBNAME)/doc
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 :=
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: ;
$(LIBNAME).hpp: $(HDRS)
@echo updating $(LIBNAME).hpp
@cp /dev/null $(LIBNAME).hpp
@for h in $(HDRS); \
do \
cat $$h >> $(LIBNAME).hpp; \
done
clean:
$(RM) $(OBJS) $(DEPS) $(REALNAME) $(LIBNAME).hpp
install: $(REALNAME) $(LIBNAME).hpp
$(INSTALL) -d $(LIBDIR)
$(INSTALL) -m 644 $(REALNAME) $(LIBDIR)
$(INSTALL) -d $(INCLUDEDIR)
$(INSTALL) -m 644 $(LIBNAME).hpp $(INCLUDEDIR)
ifeq ($(UNAME_S),Darwin)
cd $(LIBDIR) && ln -sf $(REALNAME) $(SONAME)
else
ldconfig
cd $(LIBDIR) && ln -sf $(SONAME) $(LINKERNAME)
endif

56
ScreensaverPlugin.cpp Normal file
View File

@ -0,0 +1,56 @@
//
// ScreensaverPlugin.cpp
// tmp
//
// Created by Bob Polis at 2020-10-26
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#include "ScreensaverPlugin.hpp"
ScreensaverPlugin::ScreensaverPlugin(cairo_t* context, const cairo_rectangle_t& rect)
: _c {context}, _r {rect}, _x_dist {rect.x, rect.width}, _y_dist {rect.y, rect.height} {
}
void ScreensaverPlugin::make_black() {
cairo_set_source_rgb(_c, 0, 0, 0);
cairo_rectangle(_c, 0, 0, _r.width, _r.height);
cairo_fill(_c);
}
cairo_rectangle_t ScreensaverPlugin::random_rect() {
cairo_rectangle_t rect;
double v1, v2;
v1 = random_x();
v2 = random_x();
if (v1 < v2) {
rect.x = v1;
rect.width = v2 - v1;
} else {
rect.x = v2;
rect.width = v1 - v2;
}
v1 = random_y();
v2 = random_y();
if (v1 < v2) {
rect.y = v1;
rect.height = v2 - v1;
} else {
rect.y = v2;
rect.height = v1 - v2;
}
return rect;
}
double ScreensaverPlugin::random_x() {
return _x_dist(_eng);
}
double ScreensaverPlugin::random_y() {
return _y_dist(_eng);
}
double ScreensaverPlugin::random01() {
return _01_dist(_eng);
}

39
ScreensaverPlugin.hpp Normal file
View File

@ -0,0 +1,39 @@
//
// ScreensaverPlugin.hpp
// screensaver
//
// Created by Bob Polis at 2020-10-26
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#ifndef _ScreensaverPlugin_H_
#define _ScreensaverPlugin_H_
#include <cairo/cairo.h>
#include <random>
class ScreensaverPlugin {
public:
ScreensaverPlugin(cairo_t* context, const cairo_rectangle_t& rect);
virtual ~ScreensaverPlugin() = default;
virtual void draw_frame() = 0;
virtual int fps() const = 0;
protected:
cairo_t* _c;
cairo_rectangle_t _r;
std::random_device _dev;
std::default_random_engine _eng {_dev()};
std::uniform_real_distribution<> _01_dist {};
std::uniform_real_distribution<> _x_dist;
std::uniform_real_distribution<> _y_dist;
void make_black();
cairo_rectangle_t random_rect();
double random_x();
double random_y();
double random01();
};
#endif // _ScreensaverPlugin_H_