use raylib to render nuklear GUI failed

470 Views Asked by At

I try to use Nuklear with Raylib 3.5, but the rendering failed without any error message, but a white window.

Since Nuklear can use with any renderer at the front end, I try to combine Raylib with Nuklear. as far as I can see, the Raylib uses OpenGL 3.4 as its renderer.

My idea is really simple:

  1. use "GetWindowHandle()" from Raylib to get the GLFWwindow instance created by the "InitWindow()" function.
  2. Then, using the "nk_glfw3_init()" function to pick it up.

However, this idea failed. What I got is just an empty blank window without neither Nuklear nor Raylib rendering results.

I'm quite new to the OpenGL and Nuklear. Could anyone show me some light?

Below is the simple C code for this naive experiment.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <limits.h>
#include <time.h>

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_IMPLEMENTATION
#define NK_GLFW_GL3_IMPLEMENTATION
#define NK_KEYSTATE_BASED_INPUT
#include "nuklear.h"
#include "nuklear_glfw_gl3.h"

#include "raylib.h"

#define MAX_VERTEX_BUFFER 512 * 1024
#define MAX_ELEMENT_BUFFER 128 * 1024

int main(void)
{

    int screenWidth = 800;
    int screenHeight = 600;

    struct nk_glfw glfw = {0};
    struct nk_context *ctx;

    struct nk_font_atlas *atlas;
    

    InitWindow(screenWidth, screenHeight, "raylib combining with Nuklear");
    SetTargetFPS(60);               

    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to setup GLEW\n");
        exit(1);
    }

    static GLFWwindow *win; 
    win = GetWindowHandle();

    ctx = nk_glfw3_init( &glfw, win , NK_GLFW3_INSTALL_CALLBACKS);
    nk_glfw3_font_stash_begin(&glfw, &atlas);
    nk_glfw3_font_stash_end(&glfw);
    
    while (!WindowShouldClose())    
    {

        nk_glfw3_new_frame(&glfw);

        if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
            NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
            NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
        {
            enum {EASY, HARD};
            static int op = EASY;
            static int property = 20;
            nk_layout_row_static(ctx, 30, 80, 1);
            if (nk_button_label(ctx, "button"))
                TraceLog(LOG_INFO, "DISPLAY: Nuklear button clicked!");

            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;

            nk_layout_row_dynamic(ctx, 25, 1);
            nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);

        }
        nk_end(ctx);

        BeginDrawing();
        nk_glfw3_new_frame(&glfw);
        DrawFPS(10,10);
        ClearBackground(RAYWHITE);
        DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);

        //glViewport(0, 0, screenWidth, screenHeight);
        nk_glfw3_render(&glfw, NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
        //glfwSwapBuffers(win);
        EndDrawing();
    
    }

    CloseWindow();       
    return 0;
}
2

There are 2 best solutions below

0
On BEST ANSWER

Made some raylib and nuklear integration library over at raylib-nuklear. Rather than using the nuklear gl rendering, it's using raylib's drawing methods directly.

0
On

This question is a bit out of my knowledge, but you can choose a different version of OpenGL to render with, you can choose to complie with an older version of OpenGL.

And of course double check so everything is linked correctly.

Hope you solve the problem!