Improve The Matrix effect.

This commit is contained in:
giomba 2023-10-22 22:07:11 +02:00
parent 98d7a5c00a
commit be4f7c628f
2 changed files with 22 additions and 19 deletions

View File

@ -6,53 +6,54 @@
#include <stddef.h>
#define MATRIX_LETTER_TRAIL 4
typedef struct MatrixLetter {
Lfsr *lfsr;
int8_t row;
int8_t trail_row;
uint8_t column;
} MatrixLetter;
typedef struct MatrixContext {
Lfsr lfsr;
uint8_t trail;
MatrixLetter letters[80];
} MatrixContext;
static MatrixContext ctx;
static MatrixContext mctx;
static void matrixLetter_init(MatrixLetter *ctx, Lfsr *lfsr) {
ctx->lfsr = lfsr;
ctx->column = lfsr_next(ctx->lfsr) % 80;
ctx->row = lfsr_next(ctx->lfsr) % 50 - 50;
static void matrixLetter_init(MatrixLetter *ctx) {
ctx->column = lfsr_next(&mctx.lfsr) % 80;
ctx->row = lfsr_next(&mctx.lfsr) % 50 - 50;
ctx->trail_row = ctx->row - (lfsr_next(&mctx.lfsr) % mctx.trail + 3);
}
static void matrixLetter_poll(MatrixLetter *ctx) {
ctx->row++;
ctx->trail_row++;
if (ctx->row < 0)
return;
const uint8_t c = lfsr_next(ctx->lfsr) % 256;
const uint8_t c = lfsr_next(&mctx.lfsr) % 256;
video_put(ctx->column, ctx->row, c);
if (ctx->row < MATRIX_LETTER_TRAIL)
if (ctx->trail_row < 0)
return;
video_put(ctx->column, ctx->row - MATRIX_LETTER_TRAIL, ' ');
video_put(ctx->column, ctx->trail_row, ' ');
}
void matrix_init(void) {
lfsr_init(&ctx.lfsr, 1);
void matrix_init(uint8_t trail) {
lfsr_init(&mctx.lfsr, 1);
mctx.trail = trail;
// using the same LFSR for different letters makes the randomness
// not reproducible in case we decide to refactor the number of letters,
// but that's not an issue we care about at the moment
for (size_t i = 0; i < countof(ctx.letters); ++i) {
matrixLetter_init(&ctx.letters[i], &ctx.lfsr);
for (size_t i = 0; i < countof(mctx.letters); ++i) {
matrixLetter_init(&mctx.letters[i]);
}
}
void matrix_poll(void) {
for (size_t i = 0; i < countof(ctx.letters); ++i) {
matrixLetter_poll(&ctx.letters[i]);
for (size_t i = 0; i < countof(mctx.letters); ++i) {
matrixLetter_poll(&mctx.letters[i]);
}
}
}

View File

@ -1,7 +1,9 @@
#ifndef CEDA_MATRIX_H
#define CEDA_MATRIX_H
void matrix_init(void);
#include <stdint.h>
void matrix_init(uint8_t trail);
void matrix_poll(void);
#endif // CEDA_MATRIX_H