Simplify the API

This commit is contained in:
Dimitri Lozeve 2024-11-11 22:24:22 +01:00
parent b7203383a7
commit 9c95b1a5ab
5 changed files with 59 additions and 25 deletions

View file

@ -11,14 +11,19 @@ on Linux and macOS.
** API ** API
High-level API:
- ~F _display width‿height~ creates a window of the given dimensions
and displays its left operand. ~F~ should be a function returning a
color as an RGB triplet in the (0,1) range, and taking a list of two
elements ~x‿y~, where ~x~ and ~y~ are the pixel coordinates.
Low-level API:
- ~OpenWindow w‿h‿t~ opens a window of width ~w~ and height ~h~, with - ~OpenWindow w‿h‿t~ opens a window of width ~w~ and height ~h~, with
title ~t~. Returns a window handle. title ~t~. Returns a window handle.
- ~CloseWindow w~ closes a window given its handle ~w~. - ~CloseWindow w~ closes a window given its handle ~w~.
- ~F _render w~ runs the function ~F~ at each coordinate of the window - ~F _render w~ runs the function ~F~ at each coordinate of the window
~w~ and set the corresponding pixel color. ~F~ should be a function ~w~ and set the corresponding pixel color. ~F~ is a function with
returning a color as an RGB triplet in the (0,1) range, and taking a the same arguments as for ~_display~.
list of three elements ~x‿y‿t~, where ~x~ and ~y~ are the pixel
coordinates and ~t~ is the time.
** Example ** Example
@ -29,20 +34,13 @@ f←•Import "fenster.bqn"
size←500 size←500
w←f.OpenWindow size‿size‿"bqn-fenster example" Rainbow←{𝕊x‿y:
Rainbow←{𝕊x‿y‿t:
x‿y↩x‿y÷size x‿y↩x‿y÷size
t↩(100|t)÷100
h←(x+y)÷2 h←(x+y)÷2
f.HSVtoRGB⟨360×h,1,1⟩ f.HSVtoRGB⟨360×h,1,1⟩
} }
Rainbow f._render w Rainbow f._display size‿size
•Delay 3
f.CloseWindow w
#+end_src #+end_src
[[./example.png]] [[./example.png]]

View file

@ -2,17 +2,10 @@ f←•Import"fenster.bqn"
size500 size500
wf.OpenWindow sizesize"bqn-fenster example" Rainbow{𝕊xy:
Rainbow{𝕊xyt:
xyxy÷size xyxy÷size
t(100|t)÷100
h(x+y)÷2 h(x+y)÷2
f.HSVtoRGB360×h,1,1 f.HSVtoRGB360×h,1,1
} }
Rainbow f._render w Rainbow f._display sizesize
•Delay 3
f.CloseWindow w

Binary file not shown.

Before

Width:  |  Height:  |  Size: 712 KiB

After

Width:  |  Height:  |  Size: 819 KiB

Before After
Before After

View file

@ -1,7 +1,10 @@
FensterLoop,
OpenWindow, OpenWindow,
CloseWindow, CloseWindow,
_render, _render,
RenderArray
_display,
Black, Black,
White, White,
Gray, Gray,
@ -11,6 +14,8 @@
HSVtoRGB HSVtoRGB
## FFI functions
FensterFFI"lib.so"•FFI FensterFFI"lib.so"•FFI
fensterOpenFensterFFI"i8""fenster_open"">*:i8" fensterOpenFensterFFI"i8""fenster_open"">*:i8"
@ -24,7 +29,12 @@ fensterGetWidth←FensterFFI"u32"‿"fenster_get_width"‿">*:i8"
fensterGetHeightFensterFFI"u32""fenster_get_height"">*:i8" fensterGetHeightFensterFFI"u32""fenster_get_height"">*:i8"
fensterGetPixelFensterFFI"u32""fenster_get_pixel""*:i8""u32""u32" fensterGetPixelFensterFFI"u32""fenster_get_pixel""*:i8""u32""u32"
fensterSetPixelFensterFFI"""fenster_set_pixel""*:i8""u32""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: OpenWindow{𝕊wht:
fFensterInitw,h,1+t,t@ fFensterInitw,h,1+t,t@
FensterOpen f FensterOpen f
@ -32,17 +42,19 @@ OpenWindow←{𝕊w‿h‿t:
f f
} }
# Close a window.
CloseWindow{𝕊f: CloseWindow{𝕊f:
FensterClose f FensterClose f
FensterLoop 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: _render{Func _𝕣 f:
wFensterGetWidth f wFensterGetWidth f
hFensterGetHeight f hFensterGetHeight f
tFensterTime
{𝕊xy: {𝕊xy:
rgbFunc xyt rgbFunc xy
c+´(2563)×rgb×255 c+´(2563)×rgb×255
FensterSetPixel fxyc FensterSetPixel fxyc
}¨(w)h }¨(w)h
@ -50,6 +62,24 @@ _render←{Func _𝕣 f:
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˙ Black000˙
White111˙ White111˙
Gray{𝕩𝕩𝕩} Gray{𝕩𝕩𝕩}

13
lib.c
View file

@ -29,3 +29,16 @@ uint32_t fenster_get_pixel(struct fenster *f, int i, int j) {
void fenster_set_pixel(struct fenster *f, int i, int j, uint32_t color) { void fenster_set_pixel(struct fenster *f, int i, int j, uint32_t color) {
fenster_pixel(f, i, j) = color; fenster_pixel(f, i, j) = color;
} }
void fenster_set_array(struct fenster *f, int start_i, int start_j,
int bufwidth, int bufheight,
uint32_t buf[bufwidth * bufheight]) {
int max_i = start_i + bufwidth < f->width ? bufwidth : f->width - start_i;
int max_j = start_j + bufheight < f->height ? bufheight : f->height - start_j;
for (int i = 0; i < max_i; i++) {
for (int j = 0; j < max_j; j++) {
fenster_pixel(f, start_i + i, start_j + j) = buf[j * bufwidth + i];
}
}
fenster_loop(f);
}