From df4d01bf468853713906cc427c9635380a4c60bb Mon Sep 17 00:00:00 2001 From: Adam Saponara Date: Fri, 22 Sep 2023 00:46:25 -0400 Subject: [PATCH] add nim example --- README.md | 1 + demo/example.nim | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 demo/example.nim diff --git a/README.md b/README.md index ce9e288..c780753 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ free to submit PRs for other languages.) * D * Go +* Nim * PHP * Python * Ruby diff --git a/demo/example.nim b/demo/example.nim new file mode 100644 index 0000000..6ccc4d5 --- /dev/null +++ b/demo/example.nim @@ -0,0 +1,48 @@ +# nim c -r example.nim +const libtermbox2 = "../libtermbox2.so" + +type Event = object + etype {.importc :"type".}: uint8 + emod {.importc :"mod".}: uint8 + key: uint16 + ch: uint32 + w, h, x, y: int32 + +proc tb_init(): cint {.importc, dynlib: libtermbox2.} +proc tb_shutdown(): cint {.importc, dynlib: libtermbox2.} +proc tb_present(): cint {.importc, dynlib: libtermbox2.} +proc tb_poll_event(ev: ptr Event): cint {.importc, dynlib: libtermbox2.} +proc tb_printf(x: cint, y: cint, fg: uint32, bg: uint32, fmt: cstring): cint {.importc, dynlib: libtermbox2, varargs.} + +var ev: Event + +discard tb_init() + +var y: int = 0; +discard tb_printf(0.cint, y.cint, 0x0102.uint32, 0x00.uint32, "hello from nim") +y += 1 +discard tb_printf(0.cint, y.cint, 0x03.uint32, 0x00.uint32, "press any key") +y += 1 +discard tb_present() + +discard tb_poll_event(ev.addr) + +discard tb_printf(0.cint, y.cint, 0x04.uint32, 0x00.uint32, + "event: type=%d mod=%d key=%d ch=%d w=%d h=%d x=%d y=%d", + ev.etype, + ev.emod, + ev.key, + ev.ch, + ev.w, + ev.h, + ev.x, + ev.y) +y += 1 +discard tb_present() + +discard tb_printf(0.cint, y.cint, 0x05.uint32, 0x00.uint32, "press any key to quit") +y += 1 +discard tb_present() +discard tb_poll_event(ev.addr) + +discard tb_shutdown() -- 2.39.5