From f72cef0e3ba0ba2978e4ae5404e15f05e94b9cb3 Mon Sep 17 00:00:00 2001 From: Adam Saponara Date: Sat, 29 Apr 2023 13:45:52 -0400 Subject: [PATCH] add example in dlang --- README.md | 4 ++-- demo/example.d | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 demo/example.d diff --git a/README.md b/README.md index 12f4b63..1e3edcc 100644 --- a/README.md +++ b/README.md @@ -82,8 +82,8 @@ As mentioned above, there are two options: ### Language bindings (FFI and ABI compats) -Basic examples in PHP, Python, Ruby, Go, and Zig are in the `demo/` directory. -(Feel free to submit PRs for other languages.) +Basic examples in PHP, Python, Ruby, Go, Zig, and D are in the `demo/` +directory. (Feel free to submit PRs for other languages.) ### Examples diff --git a/demo/example.d b/demo/example.d new file mode 100644 index 0000000..4c6eb7d --- /dev/null +++ b/demo/example.d @@ -0,0 +1,48 @@ +// rdmd ../libtermbox.a example.d + +struct tb_event { + ubyte type; + ubyte mod; + ushort key; + uint ch; + int w; + int h; + int x; + int y; +}; + +extern (C) int tb_init(); +extern (C) int tb_shutdown(); +extern (C) int tb_present(); +extern (C) int tb_poll_event(tb_event *event); +extern (C) int tb_printf(int x, int y, uint fg, uint bg, const char *fmt, ...); + +void main() { + tb_event ev; + int y = 0; + + tb_init(); + + tb_printf(0, y++, 0x02 | 0x0100, 0x00, "hello from d"); + tb_printf(0, y++, 0x03, 0x00, "press any key"); + + tb_present(); + tb_poll_event(&ev); + + tb_printf(0, y++, 0x04, 0x00, + "event: type=%d mod=%d key=%d ch=%d w=%d h=%d x=%d y=%d", + ev.type, + ev.mod, + ev.key, + ev.ch, + ev.w, + ev.h, + ev.x, + ev.y); + tb_printf(0, y++, 0x05, 0x00, "press any key to quit"); + + tb_present(); + tb_poll_event(&ev); + + tb_shutdown(); +} -- 2.39.5