Simplify the API
This commit is contained in:
parent
b7203383a7
commit
9c95b1a5ab
5 changed files with 59 additions and 25 deletions
26
README.org
26
README.org
|
@ -11,38 +11,36 @@ 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
|
||||||
|
|
||||||
See [[./example.bqn][example.bqn]].
|
See [[./example.bqn][example.bqn]].
|
||||||
|
|
||||||
#+begin_src bqn
|
#+begin_src bqn
|
||||||
f←•Import "fenster.bqn"
|
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]]
|
||||||
|
|
11
example.bqn
11
example.bqn
|
@ -2,17 +2,10 @@ 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
|
|
||||||
|
|
BIN
example.png
BIN
example.png
Binary file not shown.
Before Width: | Height: | Size: 712 KiB After Width: | Height: | Size: 819 KiB |
34
fenster.bqn
34
fenster.bqn
|
@ -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
|
||||||
|
|
||||||
fensterOpen←FensterFFI"i8"‿"fenster_open"‿">*:i8"
|
fensterOpen←FensterFFI"i8"‿"fenster_open"‿">*:i8"
|
||||||
|
@ -24,7 +29,12 @@ fensterGetWidth←FensterFFI"u32"‿"fenster_get_width"‿">*:i8"
|
||||||
fensterGetHeight←FensterFFI"u32"‿"fenster_get_height"‿">*:i8"
|
fensterGetHeight←FensterFFI"u32"‿"fenster_get_height"‿">*:i8"
|
||||||
fensterGetPixel←FensterFFI"u32"‿"fenster_get_pixel"‿"*:i8"‿"u32"‿"u32"
|
fensterGetPixel←FensterFFI"u32"‿"fenster_get_pixel"‿"*:i8"‿"u32"‿"u32"
|
||||||
fensterSetPixel←FensterFFI""‿"fenster_set_pixel"‿"*:i8"‿"u32"‿"u32"‿"u32"
|
fensterSetPixel←FensterFFI""‿"fenster_set_pixel"‿"*:i8"‿"u32"‿"u32"‿"u32"
|
||||||
|
fensterSetArray←FensterFFI""‿"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←{𝕊w‿h‿t:
|
OpenWindow←{𝕊w‿h‿t:
|
||||||
f←FensterInit⟨w,h,1+≠t,t∾@⟩
|
f←FensterInit⟨w,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:
|
||||||
w←FensterGetWidth f
|
w←FensterGetWidth f
|
||||||
h←FensterGetHeight f
|
h←FensterGetHeight f
|
||||||
t←FensterTime⟨⟩
|
|
||||||
{𝕊x‿y:
|
{𝕊x‿y:
|
||||||
r‿g‿b←Func x‿y‿t
|
r‿g‿b←Func x‿y
|
||||||
c←+´(⌽256⋆↕3)×⌊r‿g‿b×255
|
c←+´(⌽256⋆↕3)×⌊r‿g‿b×255
|
||||||
FensterSetPixel f‿x‿y‿c
|
FensterSetPixel f‿x‿y‿c
|
||||||
}¨⥊(↕w)⋈⌜↕h
|
}¨⥊(↕w)⋈⌜↕h
|
||||||
|
@ -50,6 +62,24 @@ _render←{Func _𝕣 f:
|
||||||
f
|
f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Render an array directly.
|
||||||
|
RenderArray←{buf𝕊f:
|
||||||
|
bufw‿bufh←≢buf
|
||||||
|
FensterSetArray⟨f,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 _𝕣 w‿h:
|
||||||
|
win←OpenWindow w‿h‿"bqn-fenster"
|
||||||
|
Func _render win
|
||||||
|
⊢•_while_(¬FensterLoop)win
|
||||||
|
CloseWindow win
|
||||||
|
}
|
||||||
|
|
||||||
|
## Utilities
|
||||||
|
|
||||||
Black←0‿0‿0˙
|
Black←0‿0‿0˙
|
||||||
White←1‿1‿1˙
|
White←1‿1‿1˙
|
||||||
Gray←{𝕩‿𝕩‿𝕩}
|
Gray←{𝕩‿𝕩‿𝕩}
|
||||||
|
|
13
lib.c
13
lib.c
|
@ -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);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue