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