nuklear--context對象

context

context非常重要。
包含所有狀態。
包括窗體,內存,輸入,樣式,堆棧,命令,時間管理
需要被傳遞到所有GUI函數。

使用

#define NK_IMPLEMENTATION
#include "nuklear.h"

要使用特別容易。只要調用隨便下面哪個都可以:
nk_init_default, nk_init_fixed, nk_init, nk_init_custom

/// Each takes in a font handle and 
a specific way of handling memory. Memory control
/// hereby ranges from 
standard library to just specifying a fixed sized block of memory
/// which nuklear has to manage itself from.

關於初始化這個叫做nkcontext的對象,只需要:
/// nk_init_default | Initializes context with standard library memory allocation (malloc,free)
/// nk_init_fixed | Initializes context from single fixed size memory block
/// nk_init | Initializes context with memory allocator callbacks for alloc and free
/// nk_init_custom | Initializes context from two buffers. One for draw commands the other for window/panel/table allocations
選擇上面的任何一種。
/// nk_clear | Called at the end of the frame to reset and prepare the context for the next frame
/// nk_free | Shutdown and free all memory allocated inside the context
/// nk_set_user_data| Utility function to pass user data to draw command

默認內存分配

是使用C標準的malloc和free函數進行內存的分配。
如果你不想被打擾,那可以選用這種。

int nk_init_default(struct nk_context *ctx, const struct nk_user_font *font);

參數:

/// Parameter   | Description
/// ------------|---------------------------------------------------------------
/// __ctx__     | Must point to an either stack or heap allocated `nk_context` struct
/// __font__    | Must point to a previously initialized font handle for more info look at font documentation

差點忘了,官方給了個實例:

/* init gui state */
struct nk_context ctx;
nk_init_fixed(&ctx, calloc(1, MAX_MEMORY), MAX_MEMORY, &font);

enum {EASY, HARD};
static int op = EASY;
static float value = 0.6f;
static int i =  20;

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);

好吧,不是這個的。不過有參考價值。至少現在我們可以這樣寫了:

#define NK_IMPLEMENTATION
#include "nuklear.h"
struct nk_context ctx;
nk_init_default(&ctx,);

返回 0:失敗 1:成功


簡單的固定內存分配

簡單的來說就是分配固定額度的內存。使用的場景呢,就是你的內存很小,或者系統有虛擬內存。而且尤其是虛擬內存的情況下建議使用這種完全自主可控的內存管理
For the later case you can just allocate for example 16MB of virtual memory
/// and only the required amount of memory will actually be committed.

int nk_init_fixed(struct nk_context *ctx, void *memory, nk_size size, const struct nk_user_font *font);

demo:

struct nk_context ctx;
nk_init_fixed(&ctx, calloc(1, MAX_MEMORY), MAX_MEMORY, &font);

參數:

/// Parameter   | Description
/// ------------|---------------------------------------------------------------
/// __ctx__     | Must point to an either stack or heap allocated `nk_context` struct
/// __alloc__   | Must point to a previously allocated memory allocator
/// __font__    | Must point to a previously initialized font handle for more info look at font documentation

返回 0:失敗 1:成功

內存回調

/// Initializes a nk_context struct with memory allocation callbacks for nuklear to allocate
/// memory from. Used internally for nk_init_default and provides a kitchen sink allocation
/// interface to nuklear. Can be useful for cases like monitoring memory consumption.

int nk_init(struct nk_context *ctx, struct nk_allocator *alloc, const struct nk_user_font *font);

參數:
/// ctx | Must point to an either stack or heap allocated nk_context struct
/// alloc | Must point to a previously allocated memory allocator
/// font | Must point to a previously initialized font handle for more info look at font documentation
返回值:0:失敗 1:成功


4

NK_API int nk_init(struct nk_context*, struct nk_allocator*, const struct nk_user_font*);
/*///

nk_init_custom

/// Initializes a nk_context struct from two different either fixed or growing
/// buffers. The first buffer is for allocating draw commands while the second buffer is
/// used for allocating windows, panels and state tables.
///
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~c
/// int nk_init_custom(struct nk_context *ctx, struct nk_buffer *cmds, struct nk_buffer *pool, const struct nk_user_font *font);
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
///
/// Parameter | Description
/// ------------|---------------------------------------------------------------
/// ctx | Must point to an either stack or heap allocated nk_context struct
/// cmds | Must point to a previously initialized memory buffer either fixed or dynamic to store draw commands into
/// pool | Must point to a previously initialized memory buffer either fixed or dynamic to store windows, panels and tables
/// font | Must point to a previously initialized font handle for more info look at font documentation
///
/// Returns either false(0) on failure or true(1) on success.
/
NK_API int nk_init_custom(struct nk_context
, struct nk_buffer *cmds, struct nk_buffer pool, const struct nk_user_font);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章