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;
BzAppDeinitFunc deinit;
bool useNuklear;
void *userData;
} BzAppDesc;
extern struct nk_context *NK;
extern bool bzMain(BzAppDesc *appDesc, int argc, const char **argv);
#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
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);
// Initialize modules
if (appDesc.useNuklear) {
NK = InitNuklear(16);
}
// User initialize
if (appDesc.init && !appDesc.init(appDesc.userData)) {
return 1;
}
while (!WindowShouldClose()) {
if (NK)
UpdateNuklear(NK);
if (appDesc.update)
appDesc.update(0.0f, appDesc.userData);
BeginDrawing();
if (appDesc.render)
appDesc.render(0.0f, appDesc.userData);
if (NK)
DrawNuklear(NK);
EndDrawing();
}
// User deinitialize
@@ -102,6 +119,10 @@ int main(int argc, const char **argv) {
appDesc.deinit(appDesc.userData);
// Deinitialize modules
if (NK) {
UnloadNuklear(NK);
NK = NULL;
}
CloseWindow();
bzLoggerDeinit();

View File

@@ -1,39 +1,40 @@
#include <raylib-nuklear.h>
#define BZ_ENTRYPOINT
#include <breeze.h>
int main() {
InitWindow(640, 480, "raylib-nuklear example");
enum {EASY, HARD};
static int op = EASY;
static float value = 0.6f;
static int i = 20;
// Create the Nuklear Context
int fontSize = 10;
struct nk_context *ctx = InitNuklear(fontSize);
void render(float dt, int *game) {
ClearBackground(WHITE);
while (!WindowShouldClose()) {
// Update the Nuklear context, along with input
UpdateNuklear(ctx);
// Nuklear GUI Code
// https://github.com/Immediate-Mode-UI/Nuklear/wiki/Window
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!
}
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(ctx);
// Render
BeginDrawing();
ClearBackground(RAYWHITE);
// Render the Nuklear GUI
DrawNuklear(ctx);
EndDrawing();
// fixed widget window ratio width
nk_layout_row_dynamic(NK, 30, 2);
if (nk_option_label(NK, "easy", op == EASY)) op = EASY;
if (nk_option_label(NK, "hard", op == HARD)) op = HARD;
// custom widget pixel width
nk_layout_row_begin(NK, NK_STATIC, 30, 2);
{
nk_layout_row_push(NK, 50);
nk_label(NK, "Volume:", NK_TEXT_LEFT);
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);
CloseWindow();
return 0;
bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
appDesc->render = (BzAppRenderFunc) render;
appDesc->useNuklear = true;
return true;
}

View File

@@ -59,14 +59,22 @@ void render(float dt, Game *game) {
camera->zoom += ((float)GetMouseWheelMove() * 0.05f);
BeginDrawing();
BeginMode2D(*camera);
ClearBackground(RAYWHITE);
bzTileMapDraw(&game->map);
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) {
@@ -80,6 +88,7 @@ bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
appDesc->render = (BzAppRenderFunc) render;
appDesc->userData = &GAME;
appDesc->useNuklear = true;
return true;
}