From 76248e7443fa9d967cc6d63d953e70a00fe4791e Mon Sep 17 00:00:00 2001 From: Philip J Freeman Date: Tue, 3 Apr 2018 17:41:31 -0700 Subject: pixbounce: add pixbounce module --- src/modules/pixbounce/Makefile.am | 3 + src/modules/pixbounce/draw.h | 16 ++++ src/modules/pixbounce/pixbounce.c | 172 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 191 insertions(+) create mode 100644 src/modules/pixbounce/Makefile.am create mode 100644 src/modules/pixbounce/draw.h create mode 100644 src/modules/pixbounce/pixbounce.c (limited to 'src') diff --git a/src/modules/pixbounce/Makefile.am b/src/modules/pixbounce/Makefile.am new file mode 100644 index 0000000..a55374a --- /dev/null +++ b/src/modules/pixbounce/Makefile.am @@ -0,0 +1,3 @@ +noinst_LIBRARIES = libpixbounce.a +libpixbounce_a_SOURCES = draw.h pixbounce.c +libpixbounce_a_CPPFLAGS = -I@top_srcdir@/src diff --git a/src/modules/pixbounce/draw.h b/src/modules/pixbounce/draw.h new file mode 100644 index 0000000..0b68c00 --- /dev/null +++ b/src/modules/pixbounce/draw.h @@ -0,0 +1,16 @@ +#ifndef _DRAW_H +#define _DRAW_H + +#include + +/* helper for scaling rgb colors and packing them into an pixel */ +static inline uint32_t makergb(uint32_t r, uint32_t g, uint32_t b, float intensity) +{ + r = (((float)intensity) * r); + g = (((float)intensity) * g); + b = (((float)intensity) * b); + + return (((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)); +} + +#endif diff --git a/src/modules/pixbounce/pixbounce.c b/src/modules/pixbounce/pixbounce.c new file mode 100644 index 0000000..764f559 --- /dev/null +++ b/src/modules/pixbounce/pixbounce.c @@ -0,0 +1,172 @@ +#include +#include +#include + +#include "draw.h" +#include "rototiller.h" + +/* Copyright (C) 2018 Philip J. Freeman */ + +int pix_width = 16; +int pix_height = 16; +int num_pix = 4; + +char pix_map[][16*16] = { + + { + 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 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, 1, 1, 0, 0, 0, 0, 0, 0, + }, +}; + +/* randomly pick another pixmap from the list, excluding the current one. */ +static int pick_pix(int num_pics, int last_pic) +{ + int pix_num = last_pic; + + while(pix_num == last_pic) + pix_num = rand() % num_pics; + + return pix_num; +} + +static void pixbounce_render_fragment(void *context, fb_fragment_t *fragment) +{ + static int initialized=0; + static int x, y, multiplier; + static int x_dir, y_dir; + static int pix_num; + + int width = fragment->width, height = fragment->height; + + if(!initialized) { + srand(time(NULL) + getpid()); + int multiplier_x = width / pix_width; + int multiplier_y = height / pix_height; + if(multiplier_x>multiplier_y) { + multiplier = multiplier_y * 77 / 100; + } else if(multiplier_y>multiplier_x) { + multiplier = multiplier_x * 77 / 100; + } + x = rand()%(width - (pix_width * multiplier)); + y = rand()%(height - (pix_height * multiplier)); + pix_num = rand() % num_pix; + x_dir = 1; + y_dir = 1; + initialized = 1; + } + + /* blank the frame */ + fb_fragment_zero(fragment); + + /* translate pixmap to multiplier size and draw it to the fragment */ + for(int cursor_y=0; cursor_y < pix_height*multiplier; cursor_y++) { + for(int cursor_x=0; cursor_x < pix_width*multiplier; cursor_x++) { + int pix_offset = ((cursor_y/multiplier)*pix_width) + (cursor_x/multiplier); + if(pix_map[pix_num][pix_offset] == 0) continue; + fb_fragment_put_pixel_unchecked( + fragment, x+cursor_x, y+cursor_y, + makergb(0xFF, 0xFF, 0xFF, 1) + ); + } + } + + /* update pixmap location */ + if(x+x_dir < 0) { + x_dir = 1; + pix_num = pick_pix(num_pix, pix_num); + } + if((x+(pix_width*multiplier))+x_dir > width) { + x_dir = -1; + pix_num = pick_pix(num_pix, pix_num); + } + if(y+y_dir < 0) { + y_dir = 1; + pix_num = pick_pix(num_pix, pix_num); + } + if((y+(pix_height*multiplier))+y_dir > height) { + y_dir = -1; + pix_num = pick_pix(num_pix, pix_num); + } + x = x+x_dir; + y = y+y_dir; +} + +rototiller_module_t pixbounce_module = { + .render_fragment = pixbounce_render_fragment, + .name = "pixbounce", + .description = "pixmap bounce", + .author = "Philip J Freeman ", + .license = "GPLv2", +}; -- cgit v1.2.1