From 7a9e33e3529732056773f140b276a02c567abfdf Mon Sep 17 00:00:00 2001 From: giomba Date: Sun, 22 Oct 2023 22:08:21 +0200 Subject: [PATCH] Add flip-flap display effect. --- src/flipflap.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/flipflap.h | 12 +++++++++++ 2 files changed, 68 insertions(+) create mode 100644 src/flipflap.c create mode 100644 src/flipflap.h diff --git a/src/flipflap.c b/src/flipflap.c new file mode 100644 index 0000000..c2c563f --- /dev/null +++ b/src/flipflap.c @@ -0,0 +1,56 @@ +#include "flipflap.h" + +#include "lfsr.h" +#include "video.h" + +typedef struct FlipFlapContext { + const char *image; + uint8_t width; + uint8_t height; + uint8_t row; + uint8_t column; + bool finished; +} FlipFlapContext; + +static FlipFlapContext ctx; + +void flipflap_init(const uint8_t *image, uint8_t width, uint8_t height, + uint8_t row, uint8_t column) { + ctx.image = image; + ctx.width = width; + ctx.height = height; + ctx.row = row; + ctx.column = column; + ctx.finished = false; + + Lfsr lfsr; + lfsr_init(&lfsr, 0xceda); + + for (uint8_t y = 0; y < ctx.height; ++y) { + for (uint8_t x = 0; x < ctx.width; ++x) { + if (*(ctx.image + y * ctx.width + x) == 1) { + *(VIDEO_MEMORY + 80 * (ctx.row + y) + ctx.column + x) = + lfsr_next(&lfsr) % 256; + } + } + } +} + +void flipflap_poll(void) { + ctx.finished = true; + for (uint8_t y = 0; y < ctx.height; ++y) { + for (uint8_t x = 0; x < ctx.width; ++x) { + if (*(ctx.image + y * ctx.width + x) == 1) { + char *p = VIDEO_MEMORY + 80 * (ctx.row + y) + ctx.column + x; + if (*p != 0x86) { + *p += 1; + ctx.finished = false; + } + } + } + } +} + +bool flipflap_finished(void) { + return ctx.finished; +} \ No newline at end of file diff --git a/src/flipflap.h b/src/flipflap.h new file mode 100644 index 0000000..a3bb1a3 --- /dev/null +++ b/src/flipflap.h @@ -0,0 +1,12 @@ +#ifndef CEDA_FLIPFLAP_H +#define CEDA_FLIPFLAH_H + +#include +#include + +void flipflap_init(const uint8_t *image, uint8_t width, uint8_t height, + uint8_t row, uint8_t column); +void flipflap_poll(void); +bool flipflap_finished(void); + +#endif