Integrate nuklear into engine

This commit is contained in:
2023-11-08 10:04:47 +01:00
parent 16972e4f9a
commit efada40908
3 changed files with 66 additions and 35 deletions

View File

@@ -19,13 +19,19 @@ typedef struct BzAppDesc {
BzAppRenderFunc render; BzAppRenderFunc render;
BzAppDeinitFunc deinit; BzAppDeinitFunc deinit;
bool useNuklear;
void *userData; void *userData;
} BzAppDesc; } BzAppDesc;
extern struct nk_context *NK;
extern bool bzMain(BzAppDesc *appDesc, int argc, const char **argv); extern bool bzMain(BzAppDesc *appDesc, int argc, const char **argv);
#ifdef BZ_ENTRYPOINT #ifdef BZ_ENTRYPOINT
#include <raylib.h> #include <raylib-nuklear.h>
struct nk_context *NK = NULL;
// https://www.raylib.com/examples/core/loader.html?name=core_custom_logging // https://www.raylib.com/examples/core/loader.html?name=core_custom_logging
static void bzRaylibLogger(int msgType, const char *text, va_list args) { static void bzRaylibLogger(int msgType, const char *text, va_list args) {
@@ -85,16 +91,27 @@ int main(int argc, const char **argv) {
SetTargetFPS(appDesc.fps); SetTargetFPS(appDesc.fps);
// Initialize modules // Initialize modules
if (appDesc.useNuklear) {
NK = InitNuklear(16);
}
// User initialize // User initialize
if (appDesc.init && !appDesc.init(appDesc.userData)) { if (appDesc.init && !appDesc.init(appDesc.userData)) {
return 1; return 1;
} }
while (!WindowShouldClose()) { while (!WindowShouldClose()) {
if (NK)
UpdateNuklear(NK);
if (appDesc.update) if (appDesc.update)
appDesc.update(0.0f, appDesc.userData); appDesc.update(0.0f, appDesc.userData);
BeginDrawing();
if (appDesc.render) if (appDesc.render)
appDesc.render(0.0f, appDesc.userData); appDesc.render(0.0f, appDesc.userData);
if (NK)
DrawNuklear(NK);
EndDrawing();
} }
// User deinitialize // User deinitialize
@@ -102,6 +119,10 @@ int main(int argc, const char **argv) {
appDesc.deinit(appDesc.userData); appDesc.deinit(appDesc.userData);
// Deinitialize modules // Deinitialize modules
if (NK) {
UnloadNuklear(NK);
NK = NULL;
}
CloseWindow(); CloseWindow();
bzLoggerDeinit(); bzLoggerDeinit();

View File

@@ -1,39 +1,40 @@
#include <raylib-nuklear.h> #define BZ_ENTRYPOINT
#include <breeze.h>
int main() { enum {EASY, HARD};
InitWindow(640, 480, "raylib-nuklear example"); static int op = EASY;
static float value = 0.6f;
static int i = 20;
// Create the Nuklear Context void render(float dt, int *game) {
int fontSize = 10; ClearBackground(WHITE);
struct nk_context *ctx = InitNuklear(fontSize);
while (!WindowShouldClose()) { if (nk_begin(NK, "Show", nk_rect(50, 50, 220, 220),
// Update the Nuklear context, along with input NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
UpdateNuklear(ctx); // fixed widget pixel width
nk_layout_row_static(NK, 30, 80, 1);
// Nuklear GUI Code if (nk_button_label(NK, "button")) {
// https://github.com/Immediate-Mode-UI/Nuklear/wiki/Window // event handling
if (nk_begin(ctx, "Nuklear", nk_rect(100, 100, 220, 220),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
if (nk_button_label(ctx, "Button")) {
// Button was clicked!
}
} }
nk_end(ctx); // fixed widget window ratio width
nk_layout_row_dynamic(NK, 30, 2);
// Render if (nk_option_label(NK, "easy", op == EASY)) op = EASY;
BeginDrawing(); if (nk_option_label(NK, "hard", op == HARD)) op = HARD;
ClearBackground(RAYWHITE); // custom widget pixel width
nk_layout_row_begin(NK, NK_STATIC, 30, 2);
// Render the Nuklear GUI {
DrawNuklear(ctx); nk_layout_row_push(NK, 50);
nk_label(NK, "Volume:", NK_TEXT_LEFT);
EndDrawing(); nk_layout_row_push(NK, 110);
nk_slider_float(NK, 0, &value, 1.0f, 0.1f);
}
nk_layout_row_end(NK);
} }
nk_end(NK);
// De-initialize the Nuklear GUI }
UnloadNuklear(ctx);
bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
CloseWindow(); appDesc->render = (BzAppRenderFunc) render;
return 0; appDesc->useNuklear = true;
return true;
} }

View File

@@ -59,14 +59,22 @@ void render(float dt, Game *game) {
camera->zoom += ((float)GetMouseWheelMove() * 0.05f); camera->zoom += ((float)GetMouseWheelMove() * 0.05f);
BeginDrawing();
BeginMode2D(*camera); BeginMode2D(*camera);
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
bzTileMapDraw(&game->map); bzTileMapDraw(&game->map);
EndMode2D(); EndMode2D();
EndDrawing();
if (nk_begin(NK, "Show", nk_rect(50, 50, 220, 220),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_CLOSABLE)) {
// fixed widget pixel width
nk_layout_row_static(NK, 30, 80, 1);
if (nk_button_label(NK, "button")) {
// event handling
}
}
nk_end(NK);
} }
bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) { bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
@@ -80,6 +88,7 @@ bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
appDesc->render = (BzAppRenderFunc) render; appDesc->render = (BzAppRenderFunc) render;
appDesc->userData = &GAME; appDesc->userData = &GAME;
appDesc->useNuklear = true;
return true; return true;
} }