summaryrefslogtreecommitdiff
path: root/chapter31/src/main/smp/sys/gfx.smp
blob: 1f0e3ca9083c3251eb1b8e856e7e095981f8086d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Minimal pixel-window interface; drawing belongs in Simple.
// One fixed-size window, main thread only. Pixel format is 0x00RRGGBB.
// open/present/poll return OK, CLOSED (normal user close/Esc), or ERROR.
int OK = 0;
int CLOSED = 1;
int ERROR = -1;
int LEFT = 1;
int ESCAPE = 256;

{ i64 u32 u32 -> i32 } simple_gfx_open = "C";
{ i64 u32 -> i32 } simple_gfx_present = "C";
{ i32 -> i32 } simple_gfx_poll = "C";
{ i32 } simple_gfx_mouse_x = "C";
{ i32 } simple_gfx_mouse_y = "C";
{ i32 -> i32 } simple_gfx_button = "C";
{ i32 -> i32 } simple_gfx_key = "C";
{ i32 } simple_gfx_close = "C";

// Failure prints a diagnostic to stderr and returns ERROR; callers decide
// whether to exit or fall back to text. No native library is loaded until here.
val open = { u8[~] title, u32 width, u32 height ->
    i64 ptr = aryu8.str0(title);
    simple_gfx_open(ptr,width,height);
};
// Array length must be width*height. The native layer copies for OS repaints;
// it never keeps a pointer into the Simple heap after this call returns.
val present = { u32[~] pixels ->
    i64 ptr = pixels;
    simple_gfx_present(ptr,pixels#);
};
// wait=true waits about 17ms before polling, avoiding a busy loop.
val poll = { bool wait -> simple_gfx_poll(wait); };
val mouseX = { -> simple_gfx_mouse_x(); };
val mouseY = { -> simple_gfx_mouse_y(); };
val button = { int button -> simple_gfx_button(button); };
// Printable keys use uppercase ASCII; special keys use MiniFB's key codes.
val key = { int key -> simple_gfx_key(key); };
val close = { -> simple_gfx_close(); };