bqn-fenster/fenster.bqn
2024-11-11 22:26:24 +01:00

99 lines
2.4 KiB
BQN
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

FensterLoop,
OpenWindow,
CloseWindow,
_render,
RenderArray
_display,
Black,
White,
Gray,
Red,
Green,
Blue,
HSVtoRGB
## FFI functions
FensterFFI"lib.so"•FFI
fensterOpenFensterFFI"i8""fenster_open"">*:i8"
fensterLoopFensterFFI"i8""fenster_loop"">*:i8"
fensterCloseFensterFFI"""fenster_close"">*:i8"
fensterSleepFensterFFI"""fenster_sleep"">u32"
fensterTimeFensterFFI"u64""fenster_time"
fensterInitFensterFFI"*:i8""fenster_init""u32""u32""u32""*u8:c8"
fensterGetWidthFensterFFI"u32""fenster_get_width"">*:i8"
fensterGetHeightFensterFFI"u32""fenster_get_height"">*:i8"
fensterGetPixelFensterFFI"u32""fenster_get_pixel""*:i8""u32""u32"
fensterSetPixelFensterFFI"""fenster_set_pixel""*:i8""u32""u32""u32"
fensterSetArrayFensterFFI"""fenster_set_array""*:i8""u32""u32""u32""u32""*u32"
## Low-level functions
# Open a window with given width, height, and title
# Returns the window's handle.
OpenWindow{𝕊wht:
fFensterInitw,h,1+t,t@
FensterOpen f
FensterLoop f
f
}
# Close a window.
CloseWindow{𝕊f:
FensterClose f
FensterLoop f
}
# Render a function in the window.
# The function should take a list x‿y of coordinates and return an r‿𝕘‿b color.
_render{Func _𝕣 f:
wFensterGetWidth f
hFensterGetHeight f
{𝕊xy:
rgbFunc xy
c+´(2563)×rgb×255
FensterSetPixel fxyc
}¨(w)h
FensterLoop f
f
}
# Render an array directly.
RenderArray{buf𝕊f:
bufwbufhbuf
FensterSetArrayf,0,0,bufw,bufh,buf
}
## High-level function
# Open a window, render a function in it, and wait for the user to close it.
_display{Func _𝕣 wh:
winOpenWindow wh"bqn-fenster"
Func _render win
•_while_(¬FensterLoop)win
CloseWindow win
}
## Utilities
Black000˙
White111˙
Gray{𝕩𝕩𝕩}
Red100˙
Green010˙
Blue001˙
# Convert colors from HSV to RGB.
# 𝕩 is a triple 0 ≤ h‿s‿v ≤ 360‿1‿1
# Output is a triple 0 ≤ r‿g‿b ≤ 1
HSVtoRGB{𝕊h,s,v:
cv×s
xc×1-|1-2|h÷60
mv-c
rgb(h÷60)cx0,xc0,0cx,0xc,x0c,c0x
m+rgb
}