made the animation more fancy

This commit is contained in:
Bob Polis 2020-10-25 21:55:04 +01:00
parent 472be95b86
commit 0a41b25368

View File

@ -44,6 +44,10 @@ void timer_signal_handler(int /*sig*/, siginfo_t* /*si*/, void* /*uc*/) {
void draw() { void draw() {
static int x = 300; static int x = 300;
static int y = 300; static int y = 300;
static int dx = 1;
static int dy = 1;
static double r = 0;
static double dr = 0.01;
if (timer_expired) { if (timer_expired) {
timer_expired = false; timer_expired = false;
@ -61,7 +65,7 @@ void draw() {
// black rectangle // black rectangle
cairo_rectangle(cr, 200, 200, 100, 100); cairo_rectangle(cr, 200, 200, 100, 100);
cairo_set_source_rgb(cr, 0, 0, 0); cairo_set_source_rgb(cr, r, 0, 0);
cairo_fill(cr); cairo_fill(cr);
// dark green text // dark green text
@ -75,10 +79,14 @@ void draw() {
main_window->show_image(*main_image); main_window->show_image(*main_image);
// update // update
x++; x += dx;
y++; y += dy;
if (x > 400) x = 300; if (x > 400) dx = -1;
if (y > 400) y = 300; if (y > 400) dy = -1;
if (x < 300) dx = 1;
if (y < 300) dy = 1;
r += dr;
if (r > 1) r = 0;
} }
} }
@ -135,7 +143,7 @@ int main(int argc, const char * argv[]) {
sev.sigev_value.sival_ptr = &timer; sev.sigev_value.sival_ptr = &timer;
struct itimerspec ts; struct itimerspec ts;
ts.it_interval.tv_sec = 0; ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 100000000; ts.it_interval.tv_nsec = 25000000;
ts.it_value.tv_sec = 0; ts.it_value.tv_sec = 0;
ts.it_value.tv_nsec = 1; ts.it_value.tv_nsec = 1;
throw_if_min1(timer_create(CLOCK_REALTIME, &sev, &timer)); throw_if_min1(timer_create(CLOCK_REALTIME, &sev, &timer));
@ -146,7 +154,7 @@ int main(int argc, const char * argv[]) {
sc::gui::SDLImageWrapper sdl_image; sc::gui::SDLImageWrapper sdl_image;
// main window // main window
sc::gui::Window& window {sc::gui::Window::new_window("screensaver")}; sc::gui::Window& window {sc::gui::Window::new_window("living art")};
main_window = &window; main_window = &window;
window.add_event_handler([](const SDL_Event& event, bool quit) -> bool { window.add_event_handler([](const SDL_Event& event, bool quit) -> bool {
std::cerr << "closing window " << event.window.windowID << '\n'; std::cerr << "closing window " << event.window.windowID << '\n';