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,38 +11,36 @@ on Linux and macOS.
** 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
title ~t~. Returns a window handle.
- ~CloseWindow w~ closes a window given its handle ~w~.
- ~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
returning a color as an RGB triplet in the (0,1) range, and taking a
list of three elements ~x‿y‿t~, where ~x~ and ~y~ are the pixel
coordinates and ~t~ is the time.
~w~ and set the corresponding pixel color. ~F~ is a function with
the same arguments as for ~_display~.
** Example
See [[./example.bqn][example.bqn]].
#+begin_src bqn
f←•Import "fenster.bqn"
f←•Import"fenster.bqn"
size←500
w←f.OpenWindow size‿size‿"bqn-fenster example"
Rainbow←{𝕊x‿y‿t:
Rainbow←{𝕊x‿y:
x‿y↩x‿y÷size
t↩(100|t)÷100
h←(x+y)÷2
f.HSVtoRGB⟨360×h,1,1⟩
}
Rainbow f._render w
•Delay 3
f.CloseWindow w
Rainbow f._display size‿size
#+end_src
[[./example.png]]

View file

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

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,
CloseWindow,
_render,
RenderArray
_display,
Black,
White,
Gray,
@ -11,6 +14,8 @@
HSVtoRGB
## FFI functions
FensterFFI"lib.so"•FFI
fensterOpenFensterFFI"i8""fenster_open"">*:i8"
@ -24,7 +29,12 @@ fensterGetWidth←FensterFFI"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
@ -32,17 +42,19 @@ OpenWindow←{𝕊w‿h‿t:
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
tFensterTime
{𝕊xy:
rgbFunc xyt
rgbFunc xy
c+´(2563)×rgb×255
FensterSetPixel fxyc
}¨(w)h
@ -50,6 +62,24 @@ _render←{Func _𝕣 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{𝕩𝕩𝕩}

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) {
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);
}