-
-
Save timo/5129c1f3de2f793d98b6 to your computer and use it in GitHub Desktop.
nice (but very slow) wavy blocky animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use NativeCall; | |
use SDL2::Raw; | |
use nqp; | |
constant W = 1024; | |
constant H = 786; | |
constant TILESIZE = 24; | |
constant COLS = W div TILESIZE; | |
constant ROWS = H div TILESIZE; | |
my $window = SDL_CreateWindow("Snake", | |
SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK, | |
W, H, | |
OPENGL); | |
my $render = SDL_CreateRenderer($window, -1, ACCELERATED +| PRESENTVSYNC); | |
my $event = SDL_Event.new; | |
my $colrange := ^256; | |
my $probability := ^4; | |
# having a "double buffer" is a bit better than keeping a list of coordinates | |
# to update around and going through the grid twice. | |
my @grid = [(^2).roll xx (COLS)] xx (ROWS); | |
my @o'grid = @grid>>.clone; | |
my $cols_list := ^COLS; | |
my $rows_list := ^ROWS; | |
my SDL_Rect $tgt .= new(0, 0, TILESIZE, TILESIZE); | |
my num $last_frame_start = nqp::time_n; | |
main: loop { | |
my num $start = nqp::time_n; | |
my $dt = $start - $last_frame_start // 0.00001; | |
while SDL_PollEvent($event) { | |
my $casted_event = SDL_CastEvent($event); | |
given $casted_event { | |
when *.type == QUIT { | |
last main; | |
} | |
} | |
} | |
for ^COLS -> $x { | |
$tgt.x = $x * TILESIZE; | |
my int $px = $x - 1; | |
my int $nx = $x + 1; | |
for ^ROWS -> $y { | |
my int $py = $y - 1; | |
my int $ny = $y + 1; | |
my $b = @grid[$y][$x]; | |
if 10.rand < 2 { | |
my @neighbours = @grid[$py][$px] // Inf, @grid[$py][$x] // Inf, @grid[$py][$nx] // Inf | |
,@grid[$y ][$px] // Inf, Inf, @grid[$y ][$nx] // Inf | |
,@grid[$ny][$px] // Inf, @grid[$ny][$x] // Inf, @grid[$ny][$nx] // Inf; | |
if @neighbours.min < $b { | |
@o'grid[$y][$x] = $b; | |
} else { | |
@o'grid[$y][$x] = $b + 1; | |
} | |
} else { | |
@o'grid[$y][$x] = $b; | |
} | |
$tgt.y = $y * TILESIZE; | |
my int $col = ($b * 20) % 251; | |
SDL_SetRenderDrawColor($render, $col, $col, $col, 255); | |
SDL_RenderFillRect($render, $tgt); | |
} | |
} | |
my @tmp := @grid; | |
@grid := @o'grid; | |
@o'grid := @tmp; | |
SDL_RenderPresent($render); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment