From c81c7b573aa98953ed77fdf8b318e67d3cbe267a Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Sun, 15 Sep 2024 07:54:55 +0200 Subject: Added Zig and X11 example for creating simple Window --- zig-x11/.clang-format | 4 ++++ zig-x11/Makefile | 8 ++++++++ zig-x11/main.zig | 32 ++++++++++++++++++++++++++++++++ zig-x11/window.c | 15 +++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 zig-x11/.clang-format create mode 100644 zig-x11/Makefile create mode 100644 zig-x11/main.zig create mode 100644 zig-x11/window.c (limited to 'zig-x11') diff --git a/zig-x11/.clang-format b/zig-x11/.clang-format new file mode 100644 index 0000000..84ac618 --- /dev/null +++ b/zig-x11/.clang-format @@ -0,0 +1,4 @@ +BasedOnStyle: LLVM +ColumnLimit: 120 +IndentWidth: 4 + diff --git a/zig-x11/Makefile b/zig-x11/Makefile new file mode 100644 index 0000000..910dd26 --- /dev/null +++ b/zig-x11/Makefile @@ -0,0 +1,8 @@ +default: + zig run main.zig -lX11 -lc + +window-test: + clang window.c -o window -lX11 + +window-test-zig: + zig cc window.c -o window -lX11 diff --git a/zig-x11/main.zig b/zig-x11/main.zig new file mode 100644 index 0000000..daa4cd5 --- /dev/null +++ b/zig-x11/main.zig @@ -0,0 +1,32 @@ +const std = @import("std"); +const xlib = @cImport({ + @cInclude("X11/Xlib.h"); +}); + +pub fn main() !void { + const display = xlib.XOpenDisplay(null); + if (display == null) { + return error.DisplayOpenFailed; + } + + const rootWindow = xlib.XDefaultRootWindow(display); + + const mainWindow = xlib.XCreateSimpleWindow( + display, + rootWindow, + 0, + 0, + 800, + 600, + 0, + 0, + 0xFF0000, + ); + + _ = xlib.XMapWindow(display, mainWindow); + _ = xlib.XFlush(display); + + while (true) { + std.time.sleep(1 * std.time.ns_per_s); + } +} diff --git a/zig-x11/window.c b/zig-x11/window.c new file mode 100644 index 0000000..80110c4 --- /dev/null +++ b/zig-x11/window.c @@ -0,0 +1,15 @@ +#include +#include + +int main() { + Display *MainDisplay = XOpenDisplay(0); + Window RootWindow = XDefaultRootWindow(MainDisplay); + + Window MainWindow = XCreateSimpleWindow(MainDisplay, RootWindow, 0, 0, 800, 600, 0, 0, 0xFF0000); + XMapWindow(MainDisplay, MainWindow); + XFlush(MainDisplay); + + for (;;) { + sleep(1); + } +} -- cgit v1.2.3