Particle fountain: Difference between revisions
m
→{{header|Raku}}: Undo bizarre single space indent than somebody found necessary to do
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Thundergnat (talk | contribs) m (→{{header|Raku}}: Undo bizarre single space indent than somebody found necessary to do) |
||
Line 571:
Has options to vary the direction at which the fountain sprays, the "spread" angle and the color of the emitted particles.
<syntaxhighlight lang="raku" line>use NativeCall;
my int ($w, $h) = 800, 800;
my SDL_Window $window;
my int $particlenum = 3000;
SDL_Init(VIDEO);
$window = SDL_CreateWindow(
"Raku Particle System!",
SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
$w, $h,
);
$renderer = SDL_CreateRenderer( $window, -1, ACCELERATED );
SDL_ClearError();
my num @positions = 0e0 xx ($particlenum * 2);
my CArray[int32] $points .= new;
my int $numpoints;
sub update (num \df) {
my int $xidx = 0;
loop (my int $
if
if (
@lifetimes[$idx] = 25e-1; # time to live
$willdraw = 1;
} else
if @positions[$yidx] > $h / 10 && @velocities[$yidx] > 0 {
}
@velocities[$yidx] = @velocities[$yidx] + $h/10.Num * df; # adjust velocity
@lifetimes[$idx] = @lifetimes[$idx] - df;
if ($willdraw) {
$points[$pointidx++] = (@positions[$xidx] * 10).floor; # gather all of the points that
}
$xidx = $xidx + 2;
}
$numpoints = ($pointidx - 1) div 2;
}
sub render {
SDL_SetRenderDrawColor($renderer, 0x0, 0x0, 0x0, 0xff);
SDL_SetRenderDrawColor($renderer, |hsv2rgb(((now % 5) / 5).round(.01), $saturation, 1), 0x7f);
SDL_RenderPresent($renderer);
}
enum KEY_CODES (
K_UP => 82,
K_PGUP
K_Q
);
say q:to/DOCS/;
Use UP and DOWN arrow keys to modify the saturation of the particle colors.
Toggle reciprocation off / on with the SPACE bar.
Use LEFT and RIGHT arrow keys to modify angle range for reciprocation.
Press the "q" key to quit.
DOCS
my $event = SDL_Event.new;
my num $df = 0.0001e0;
main: loop {
my $start = now;
while SDL_PollEvent($event) {
given $casted_event {
when
when
update($df);
render();
$df = (now - $start).Num;
print fps();
}
say '';
sub fps {
state $fps-frames = 0;
if now - $fps-
}
}
sub hsv2rgb ( $h, $s, $v ){
state %cache;
[(do
}
}
}
</syntaxhighlight>
|