Improve The Matrix effect.
This commit is contained in:
parent
98d7a5c00a
commit
be4f7c628f
35
src/matrix.c
35
src/matrix.c
@ -6,53 +6,54 @@
|
|||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#define MATRIX_LETTER_TRAIL 4
|
|
||||||
|
|
||||||
typedef struct MatrixLetter {
|
typedef struct MatrixLetter {
|
||||||
Lfsr *lfsr;
|
|
||||||
int8_t row;
|
int8_t row;
|
||||||
|
int8_t trail_row;
|
||||||
uint8_t column;
|
uint8_t column;
|
||||||
} MatrixLetter;
|
} MatrixLetter;
|
||||||
|
|
||||||
typedef struct MatrixContext {
|
typedef struct MatrixContext {
|
||||||
Lfsr lfsr;
|
Lfsr lfsr;
|
||||||
|
uint8_t trail;
|
||||||
MatrixLetter letters[80];
|
MatrixLetter letters[80];
|
||||||
} MatrixContext;
|
} MatrixContext;
|
||||||
|
|
||||||
static MatrixContext ctx;
|
static MatrixContext mctx;
|
||||||
|
|
||||||
static void matrixLetter_init(MatrixLetter *ctx, Lfsr *lfsr) {
|
static void matrixLetter_init(MatrixLetter *ctx) {
|
||||||
ctx->lfsr = lfsr;
|
ctx->column = lfsr_next(&mctx.lfsr) % 80;
|
||||||
ctx->column = lfsr_next(ctx->lfsr) % 80;
|
ctx->row = lfsr_next(&mctx.lfsr) % 50 - 50;
|
||||||
ctx->row = lfsr_next(ctx->lfsr) % 50 - 50;
|
ctx->trail_row = ctx->row - (lfsr_next(&mctx.lfsr) % mctx.trail + 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void matrixLetter_poll(MatrixLetter *ctx) {
|
static void matrixLetter_poll(MatrixLetter *ctx) {
|
||||||
ctx->row++;
|
ctx->row++;
|
||||||
|
ctx->trail_row++;
|
||||||
if (ctx->row < 0)
|
if (ctx->row < 0)
|
||||||
return;
|
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);
|
video_put(ctx->column, ctx->row, c);
|
||||||
|
|
||||||
if (ctx->row < MATRIX_LETTER_TRAIL)
|
if (ctx->trail_row < 0)
|
||||||
return;
|
return;
|
||||||
video_put(ctx->column, ctx->row - MATRIX_LETTER_TRAIL, ' ');
|
video_put(ctx->column, ctx->trail_row, ' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
void matrix_init(void) {
|
void matrix_init(uint8_t trail) {
|
||||||
lfsr_init(&ctx.lfsr, 1);
|
lfsr_init(&mctx.lfsr, 1);
|
||||||
|
mctx.trail = trail;
|
||||||
|
|
||||||
// using the same LFSR for different letters makes the randomness
|
// using the same LFSR for different letters makes the randomness
|
||||||
// not reproducible in case we decide to refactor the number of letters,
|
// not reproducible in case we decide to refactor the number of letters,
|
||||||
// but that's not an issue we care about at the moment
|
// but that's not an issue we care about at the moment
|
||||||
for (size_t i = 0; i < countof(ctx.letters); ++i) {
|
for (size_t i = 0; i < countof(mctx.letters); ++i) {
|
||||||
matrixLetter_init(&ctx.letters[i], &ctx.lfsr);
|
matrixLetter_init(&mctx.letters[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void matrix_poll(void) {
|
void matrix_poll(void) {
|
||||||
for (size_t i = 0; i < countof(ctx.letters); ++i) {
|
for (size_t i = 0; i < countof(mctx.letters); ++i) {
|
||||||
matrixLetter_poll(&ctx.letters[i]);
|
matrixLetter_poll(&mctx.letters[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,7 +1,9 @@
|
|||||||
#ifndef CEDA_MATRIX_H
|
#ifndef CEDA_MATRIX_H
|
||||||
#define CEDA_MATRIX_H
|
#define CEDA_MATRIX_H
|
||||||
|
|
||||||
void matrix_init(void);
|
#include <stdint.h>
|
||||||
|
|
||||||
|
void matrix_init(uint8_t trail);
|
||||||
void matrix_poll(void);
|
void matrix_poll(void);
|
||||||
|
|
||||||
#endif // CEDA_MATRIX_H
|
#endif // CEDA_MATRIX_H
|
||||||
|
Loading…
Reference in New Issue
Block a user