implemented animation by using an interval posix timer which uses signal when expired

This commit is contained in:
Bob Polis 2020-10-25 21:06:26 +01:00
parent 30aad07c9f
commit 472be95b86
2 changed files with 40 additions and 2 deletions

View File

@ -27,7 +27,7 @@ else
CXXFLAGS += -D NDEBUG -O3
endif
LDLIBS := -lm -lpthread -lscgui -lSDL2 -lcairo
LDLIBS := -lm -lpthread -lscgui -lSDL2 -lcairo -lscerror -lrt
.PHONY: all clean install

View File

@ -14,6 +14,9 @@
#include <libscgui.hpp>
#include <unistd.h>
#include <cairo/cairo.h>
#include <signal.h>
#include <time.h>
#include <libscerror.hpp>
const int WIDTH {700};
const int HEIGHT {700};
@ -33,7 +36,15 @@ void print_version() {
std::cout << "screensaver version 1.0\n";
}
void timer_signal_handler(int /*sig*/, siginfo_t* /*si*/, void* /*uc*/) {
// timer_t* tmr {si->si_value.sival_ptr};
timer_expired = true;
}
void draw() {
static int x = 300;
static int y = 300;
if (timer_expired) {
timer_expired = false;
sc::gui::ImageLock lock {*main_image};
@ -44,7 +55,7 @@ void draw() {
cairo_fill(cr);
// orange rectangle
cairo_rectangle(cr, 300, 300, 100, 100);
cairo_rectangle(cr, x, y, 100, 100);
cairo_set_source_rgb(cr, 1, 0.5, 0);
cairo_fill(cr);
@ -62,6 +73,12 @@ void draw() {
// show
main_window->show_image(*main_image);
// update
x++;
y++;
if (x > 400) x = 300;
if (y > 400) y = 300;
}
}
@ -103,6 +120,27 @@ int main(int argc, const char * argv[]) {
// }
// }
// install timer signal handler
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = timer_signal_handler;
sigemptyset(&sa.sa_mask);
throw_if_min1(sigaction(SIGRTMAX, &sa, nullptr));
// setup repeating timer with 100 ms interval
timer_t timer;
struct sigevent sev;
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGRTMAX;
sev.sigev_value.sival_ptr = &timer;
struct itimerspec ts;
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 100000000;
ts.it_value.tv_sec = 0;
ts.it_value.tv_nsec = 1;
throw_if_min1(timer_create(CLOCK_REALTIME, &sev, &timer));
throw_if_min1(timer_settime(timer, 0, &ts, nullptr));
// RAII instances
sc::gui::SDLWrapper sdl2;
sc::gui::SDLImageWrapper sdl_image;