Mercurial > games > semicongine
annotate src/xlib_helpers.nim @ 2:213fdf8d31dd
did: hello world triangle, a bit of code organization
author | Sam <sam@basx.dev> |
---|---|
date | Mon, 19 Dec 2022 10:41:20 +0700 |
parents | 5daf3f236d87 |
children |
rev | line source |
---|---|
0 | 1 import |
2 x11/xlib, | |
3 x11/xutil, | |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
0
diff
changeset
|
4 x11/x, |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
0
diff
changeset
|
5 x11/keysym |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
0
diff
changeset
|
6 |
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
0
diff
changeset
|
7 export keysym |
0 | 8 |
9 var deleteMessage*: Atom | |
10 | |
11 template checkXlibResult*(call: untyped) = | |
12 let value = call | |
13 if value == 0: | |
14 raise newException(Exception, "Xlib error: " & astToStr(call) & " returned " & $value) | |
15 | |
16 proc xlibInit*(): (PDisplay, Window) = | |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
0
diff
changeset
|
17 checkXlibResult XInitThreads() |
0 | 18 let display = XOpenDisplay(nil) |
19 if display == nil: | |
20 quit "Failed to open display" | |
21 | |
22 let | |
23 screen = XDefaultScreen(display) | |
24 rootWindow = XRootWindow(display, screen) | |
25 foregroundColor = XBlackPixel(display, screen) | |
26 backgroundColor = XWhitePixel(display, screen) | |
27 | |
28 let window = XCreateSimpleWindow(display, rootWindow, -1, -1, 800, 600, 0, foregroundColor, backgroundColor) | |
29 checkXlibResult XSetStandardProperties(display, window, "Nim X11", "window", 0, nil, 0, nil) | |
30 checkXlibResult XSelectInput(display, window, ButtonPressMask or KeyPressMask or ExposureMask) | |
31 checkXlibResult XMapWindow(display, window) | |
32 | |
2
213fdf8d31dd
did: hello world triangle, a bit of code organization
Sam <sam@basx.dev>
parents:
0
diff
changeset
|
33 deleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", XBool(false)) |
0 | 34 checkXlibResult XSetWMProtocols(display, window, addr(deleteMessage), 1) |
35 | |
36 return (display, window) | |
37 | |
38 proc xlibFramebufferSize*(display: PDisplay, window: Window): (int, int) = | |
39 var attribs: XWindowAttributes | |
40 checkXlibResult XGetWindowAttributes(display, window, addr(attribs)) | |
41 return (int(attribs.width), int(attribs.height)) |