Nuklear README

The project's README from github.com/Immediate-Mode-UI/Nuklear. As one markdown file: README.md.

Nuklear

This is a minimal-state, immediate-mode graphical user interface toolkit written in ANSI C and licensed under public domain. It was designed as a simple embeddable user interface for application and does not have any dependencies, a default render backend or OS window/input handling but instead provides a highly modular, library-based approach, with simple input state for input and draw commands describing primitive shapes as output. So instead of providing a layered library that tries to abstract over a number of platform and render backends, it focuses only on the actual UI.

Features

Usage

This library is self-contained in one single header file and can be used either in header-only mode or in implementation mode. The header-only mode is used by default when included and allows including this header in other headers and does not contain the actual implementation.

The implementation mode requires defining the preprocessor macro NK_IMPLEMENTATION in one .c/.cpp file before #includeing this file, e.g.:

#define NK_IMPLEMENTATION
#include "nuklear.h"

IMPORTANT: Every time you include "nuklear.h" you have to define the same optional flags. This is very important; not doing it either leads to compiler errors, or even worse, stack corruptions.

screenshot screen screen2 node skinning gamepad

Example

The example below shows the general structure of a Nuklear application: one-time setup, then once per frame mirror the input, build the user interface and draw it. Nuklear does no rendering itself, so the parts marked your_... are provided by your rendering backend:

/* init gui state (once at startup) */
struct nk_user_font font;
struct nk_context ctx;

font.userdata = nk_handle_ptr(your_font_data);
font.height = your_font_height;
font.width = your_text_width_calculation;
nk_init_fixed(&ctx, calloc(1, MAX_MEMORY), MAX_MEMORY, &font);

while (running) {
    /* mirror your window's input state into nuklear */
    nk_input_begin(&ctx);
    /* nk_input_motion(&ctx, ...), nk_input_button(&ctx, ...),
     * nk_input_key(&ctx, ...), ... */
    nk_input_end(&ctx);

    /* build the user interface */
    {
        enum {EASY, HARD};
        static int op = EASY;
        static float value = 0.6f;

        if (nk_begin(&ctx, "Show", nk_rect(50, 50, 220, 220),
            NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
            /* fixed widget pixel width */
            nk_layout_row_static(&ctx, 30, 80, 1);
            if (nk_button_label(&ctx, "button")) {
                /* event handling */
            }

            /* fixed widget window ratio width */
            nk_layout_row_dynamic(&ctx, 30, 2);
            if (nk_option_label(&ctx, "easy", op == EASY)) op = EASY;
            if (nk_option_label(&ctx, "hard", op == HARD)) op = HARD;

            /* custom widget pixel width */
            nk_layout_row_begin(&ctx, NK_STATIC, 30, 2);
            {
                nk_layout_row_push(&ctx, 50);
                nk_label(&ctx, "Volume:", NK_TEXT_LEFT);
                nk_layout_row_push(&ctx, 110);
                nk_slider_float(&ctx, 0, &value, 1.0f, 0.1f);
            }
            nk_layout_row_end(&ctx);
        }
        nk_end(&ctx);
    }

    /* draw: hand this frame's draw commands to your renderer */
    {
        const struct nk_command *cmd;
        nk_foreach(cmd, &ctx) {
            switch (cmd->type) {
            case NK_COMMAND_LINE:
                your_draw_line_function((const struct nk_command_line*)cmd);
                break;
            case NK_COMMAND_RECT:
                your_draw_rect_function((const struct nk_command_rect*)cmd);
                break;
            default:
                /* [...] */
                break;
            }
        }
        nk_clear(&ctx);
    }
}

Ready-to-use rendering backends for GLFW, SDL, X11, GDI, Direct3D, Vulkan and more are in the demo folder, each with its own build instructions, e.g.:

make -C demo/glfw_opengl3

More complete programs can be found in the example folder. Rendered by one of the backends, the interface built above looks like this:

example

Bindings

There are a number of nuklear bindings for different languages created by other authors. I cannot attest for their quality since I am not necessarily proficient in any of these languages. Furthermore there are no guarantee that all bindings will always be kept up to date:

Credits

Developed by Micha Mettke and every direct or indirect contributor to the GitHub.

Embeds stb_texedit, stb_truetype and stb_rectpack by Sean Barrett (public domain) Embeds ProggyClean.ttf font by Tristan Grimmer (MIT license).

Big thank you to Omar Cornut (ocornut@github) for his imgui library and giving me the inspiration for this library, Casey Muratori for handmade hero and his original immediate-mode graphical user interface idea and Sean Barrett for his amazing single-header libraries which restored my faith in libraries and brought me to create some of my own. Finally Apoorva Joshi for his single-header file packer.

License

Nuklear is avaliable under either the MIT License or public domain. See LICENSE for more info.

Nuklear (Immediate-Mode-UI/Nuklear)

Searching Database…

11.5K ·GitHub
这是什么?Nuklear 是一个用 ANSI C 编写的低状态开销的即时模式(immediate-mode)图形界面工具包,定位为一个简单的可嵌入 UI,直接编译进你自己的应用里。
它是要编译进我应用里的库,还是一个独立程序?是一个单头文件 C 库。它只负责 UI 本身:没有内置的渲染后端,也没有操作系统窗口或输入处理——把它们接起来是你自己的事。
怎么把它加到我的项目里?复制那一个头文件即可。默认就是纯头文件模式;要编译实现部分,在其中一个 .c/.cpp 文件里先定义 NK_IMPLEMENTATION,再包含 nuklear.h。
我需要先安装其他库吗?不需要。它没有任何依赖——甚至可以在不使用 C 标准库的情况下编译——整个代码库只有大约 1.8 万行 C89 代码。
配置时有什么要注意的坑吗?有。每次包含 nuklear.h 时都必须定义相同的可选标志,定义不一致会导致编译错误,更糟的情况下还会造成栈损坏。
它自己不负责绘图,那我怎么把界面显示到屏幕上?你需要接入一个渲染后端。demo 目录里自带了 GLFW、SDL、X11、GDI、Direct3D 和 Vulkan 的现成后端,每个都有各自的构建说明。
这个许可证有什么特别之处吗?非常宽松:你可以选择按 MIT 许可证使用,也可以当作公有领域(public domain)使用,哪种适合你的项目就用哪种。
Nuklear 和 Dear ImGui 有什么关系?作者明确表示,Omar Cornut 的 imgui 库是它的直接灵感来源。Nuklear 沿用了同样的即时模式思路,并用 ANSI C 从零实现为一个约 1.8 万行代码、无依赖的单头文件库。
用它写应用大概是什么样子?先做一次性的初始化,之后每一帧把你的输入同步给 Nuklear,构建界面,再通过你的渲染后端绘制它输出的绘图命令——README 里的示例展示的正是这种结构。
我能在 C 以外的语言里用它吗?可以,社区为 Java、D、Go、Rust、Nim、Lua、Python、C#/.NET、V 等语言提供了绑定。这些绑定由其他作者维护,质量和更新程度参差不齐。
在内存和外观方面我能控制多少?很大:它完全支持换肤和自定义,内存占用小且内存使用完全可控,没有任何全局或隐藏状态,可选的字体烘焙和顶点缓冲输出,另外还支持 UTF-8。
What are similar projects?nuklear, imgui, sokol, DearPyGui, iced, cJSON, bgfx, DEsk
Size
21.1 MB
Stars
11.5K
Age
6y
Commits · 90 Days