Jump to content

Particle fountain: Difference between revisions

m
→‎{{header|Raku}}: Undo bizarre single space indent than somebody found necessary to do
m (syntax highlighting fixup automation)
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;
use NativeCallSDL2::Raw;
 
use SDL2::Raw;
my int ($w, $h) = 800, 800;
my SDL_Window $window;
my int ($w, $h) = 800, 800;
my SDL_WindowSDL_Renderer $windowrenderer;
 
my SDL_Renderer $renderer;
my int $particlenum = 3000;
 
my int $particlenum = 3000;
 
SDL_Init(VIDEO);
$window = SDL_CreateWindow(
SDL_Init(VIDEO);
"Raku Particle System!",
$window = SDL_CreateWindow(
SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
"Raku Particle System!",
$w, $h,
SDL_WINDOWPOS_CENTERED_MASK, SDL_WINDOWPOS_CENTERED_MASK,
$w, $h,RESIZABLE
);
RESIZABLE
$renderer = SDL_CreateRenderer( $window, -1, ACCELERATED );
);
 
$renderer = SDL_CreateRenderer( $window, -1, ACCELERATED );
SDL_ClearError();
 
SDL_ClearError();
my num @positions = 0e0 xx ($particlenum * 2);
my num @positions velocities = 0e0 xx ($particlenum * 2);
my num @velocitieslifetimes = 0e0 xx ( $particlenum * 2);
 
my num @lifetimes = 0e0 xx $particlenum;
my CArray[int32] $points .= new;
my int $numpoints;
my CArray[int32] $points .= new;
my intNum $numpointssaturation = 4e-1;
my Num $saturationspread = 4e15e-1;
my Num $spread&reciprocate = 15e-1;sub { 0 }
my &reciprocate$range = sub { 0 }1.5;
 
my $range = 1.5;
sub update (num \df) {
my int $xidx = 0;
sub update (num \df) {
my int $xidxyidx = 01;
my int $yidxpointidx = 10;
loop (my int $pointidxidx = 0; $idx < $particlenum; $idx = $idx + 1) {
loop ( my int $idxwilldraw = 0; $idx < $particlenum; $idx = $idx + 1) {
if my int (@lifetimes[$willdrawidx] <= 0;0e0) {
if (@lifetimes[$idx]rand <= 0e0df) {
@lifetimes[$idx] = 25e-1; # time to live
if (rand < df) {
@lifetimespositions[$idxxidx] = 25e-1; ($w / 20e0).Num; # timestarting toposition livex
@positions[$xidxyidx] = ($wh / 20e010).Num; # starting position# and xy
@positionsvelocities[$yidxxidx] = ($hspread /* 10).Num;rand - $spread/2 + reciprocate()) * 10; # starting velocity # and yx
@velocities[$xidxyidx] = ($spread * rand - $spread/2 + reciprocate().9e0) * 10$h / 20.5; # startingand velocityy x(randomized slightly so points reach different heights)
$willdraw = 1;
@velocities[$yidx] = (rand - 2.9e0) * $h / 20.5; # and y (randomized slightly so points reach different heights)
$willdraw = 1;}
} else }{
if @positions[$yidx] > $h / 10 && @velocities[$yidx] > 0 {
} else {
if @positionsvelocities[$yidx] > $h / 10 &&= @velocities[$yidx] >* -0.3e0; # {"bounce"
}
@velocities[$yidx] = @velocities[$yidx] * -0.3e0; # "bounce"
 
}
@velocities[$yidx] = @velocities[$yidx] + $h/10.Num * df; # adjust velocity
@velocitiespositions[$yidxxidx] = @velocitiespositions[$yidxxidx] + @velocities[$h/10.Numxidx] * df; # adjust velocityposition x
@positions[$xidxyidx] = @positions[$xidxyidx] + @velocities[$xidxyidx] * df; # adjust positionand xy
 
@positions[$yidx] = @positions[$yidx] + @velocities[$yidx] * df; # and y
@lifetimes[$idx] = @lifetimes[$idx] - df;
@lifetimes[$idx] willdraw = @lifetimes[$idx] - df1;
$willdraw = 1;}
 
}
if ($willdraw) {
$points[$pointidx++] = (@positions[$xidx] * 10).floor; # gather all of the points that
if ($willdraw) {
$points[$pointidx++] = (@positions[$xidxyidx] * 10).floor; # gatherare allstill ofgoing theto pointsbe thatrendered
}
$points[$pointidx++] = (@positions[$yidx] * 10).floor; # are still going to be rendered
 
}
$xidx = $xidx + 2;
$xidxyidx = $xidx + 21;
}
$yidx = $xidx + 1;
$numpoints = ($pointidx - 1) div 2;
}
}
$numpoints = ($pointidx - 1) div 2;
 
}
sub render {
SDL_SetRenderDrawColor($renderer, 0x0, 0x0, 0x0, 0xff);
sub render {
SDL_SetRenderDrawColorSDL_RenderClear($renderer, 0x0, 0x0, 0x0, 0xff);
 
SDL_RenderClear($renderer);
SDL_SetRenderDrawColor($renderer, |hsv2rgb(((now % 5) / 5).round(.01), $saturation, 1), 0x7f);
SDL_SetRenderDrawColorSDL_RenderDrawPoints($renderer, |hsv2rgb(((now % 5) / 5).round(.01)$points, $saturation, 1), 0x7fnumpoints);
 
SDL_RenderDrawPoints($renderer, $points, $numpoints);
SDL_RenderPresent($renderer);
}
SDL_RenderPresent($renderer);
 
}
enum KEY_CODES (
K_UP => 82,
enum KEY_CODES (
K_UP K_DOWN => 8281,
K_DOWNK_LEFT => 8180,
K_LEFT K_RIGHT => 8079,
K_RIGHTK_SPACE => 7944,
K_PGUP K_SPACE => 4475,
K_PGUPK_PGDN => 7578,
K_Q K_PGDN => 7820,
);
K_Q => 20,
 
);
say q:to/DOCS/;
Use UP and DOWN arrow keys to modify the saturation of the particle colors.
say q:to/DOCS/;
Use PAGE UP and PAGE DOWN arrow keys to modify the saturation"spread" of the particle colorsparticles.
Toggle reciprocation off / on with the SPACE bar.
Use PAGE UP and PAGE DOWN keys to modify the "spread" of the particles.
Use LEFT and RIGHT arrow keys to modify angle range for reciprocation.
Toggle reciprocation off / on with the SPACE bar.
Press the "q" key to quit.
Use LEFT and RIGHT arrow keys to modify angle range for reciprocation.
DOCS
Press the "q" key to quit.
 
DOCS
my $event = SDL_Event.new;
 
my $event = SDL_Event.new;
my num $df = 0.0001e0;
 
my num $df = 0.0001e0;
main: loop {
my $start = now;
main: loop {
 
my $start = now;
while SDL_PollEvent($event) {
while SDL_PollEvent my $casted_event = SDL_CastEvent($event) {;
 
my $casted_event = SDL_CastEvent($event);
given $casted_event {
given $casted_event when *.type == QUIT {
when *.type == QUITlast {main;
last main;}
when }*.type == WINDOWEVENT {
when * if .typeevent == WINDOWEVENTRESIZED {
if .event == RESIZED$w {= .data1;
$wh = .data1data2;
$h = .data2;}
}
when }*.type == KEYDOWN {
when * if KEY_CODES(.typescancode) ==-> KEYDOWN$comm {
if KEY_CODES(.scancode) -> given $comm {
given $comm when 'K_UP' { $saturation = (($saturation + .1) min 1e0) }
when 'K_UPK_DOWN' { $saturation = (($saturation +- .1) minmax 1e00e0) }
when 'K_DOWNK_PGUP' { $saturationspread = (($saturationspread -+ .1) maxmin 0e05e0) }
when 'K_PGUPK_PGDN' { $spread = (($spread +- .1) minmax 5e02e-1) }
when 'K_PGDNK_RIGHT' { $spreadrange = (($spreadrange -+ .1) maxmin 2e-12e0) }
when 'K_RIGHTK_LEFT' { $range = (($range +- .1) minmax 2e01e-1) }
when 'K_LEFTK_SPACE' { $range&reciprocate = reciprocate(() == 0 ?? sub { $range -* .1sin(now) max} 1e-1)!! sub { 0 } }
when 'K_SPACEK_Q' { &reciprocate = reciprocate() == 0 ?? sub { $range * sin(now) } !! sub { 0last }main }
when 'K_Q' { last main }
}
}
}
}
 
}
update($df);
 
update($df);
render();
 
render();
$df = (now - $start).Num;
 
$df = (now - $start).Num;
print fps();
}
print fps();
 
}
say '';
 
say '';
sub fps {
state $fps-frames = 0;
sub fps {
state $fps-framesnow = 0now;
state $fps-now = now'';
state $fps = ''-frames++;
if now - $fps-frames++;now >= 1 {
if now - $fps-now >= 1[~] {"\r", ' ' x 20, "\r",
$fps = [~] sprintf "\rFPS: %5.1f ", '($fps-frames '/ x(now 20,- "\r",$fps-now));
sprintf "FPS: %5.1f ", ($fps-frames /= (now - $fps-now))0;
$fps-framesnow = 0now;
}
$fps-now = now;
}$fps
}
$fps
 
}
sub hsv2rgb ( $h, $s, $v ){
state %cache;
sub hsv2rgb ( $h, $s, $v ){
state %cache;{"$h|$s|$v"} //= do {
%cache{"$h| my $s|$v"}c //= do$v {* $s;
my $cx = $vc * (1 - abs( (($sh*6) % 2) - 1 ) );
my $xm = $c * (1v - abs( (($h*6) % 2) - 1 ) )c;
[(do mygiven $mh = $v - $c;{
[(do given $h when 0..^1/6 { $c, $x, 0 }
when 01/6..^1/63 { $cx, $xc, 0 }
when 1/63..^1/32 { $x0, $c, 0$x }
when 1/32..^12/23 { 0, $cx, $xc }
when 12/23..^25/36 { 0$x, $x0, $c }
when 2/3..^5/6..1 { $xc, 0, $cx }
} when 5/6)..1 {map: ((*+$c,m) 0,* $x }255).Int]
}
} ).map: ((*+$m) * 255).Int]
}
}
}
</syntaxhighlight>
 
10,327

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.