How to use Nuklear with OpenGL 4.1 and GLFW

188 Views Asked by At

Trying to integrate Nuklear into a new C++ project and been getting lots of linker errors.

I've tried setting up a simple UI class with header and CPP files as below based on the Nuklear repo:

#pragma once
#include <glad/glad.h>
#include <GLFW/glfw3.h>

#define MAX_VERTEX_BUFFER 512 * 1024
#define MAX_ELEMENT_BUFFER 128 * 1024
#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_STANDARD_BOOL
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_COMMAND_USERDATA
#define NK_UINT_DRAW_INDEX
#define NK_GLFW_GL4_IMPLEMENTATION
#define NK_IMPLEMENTATION
#include <nuklear.h>
#include <nuklear_glfw_gl3.h>

class UI
{
public:
    UI(GLFWwindow *window);
    ~UI();
    struct nk_context *ctx;
    void RenderWindow();
};
#include "UI.h"

UI::UI(GLFWwindow *window)
{
    ctx = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
}

UI::~UI()
{
    nk_glfw3_shutdown();
}

void UI::RenderWindow()
{
    nk_glfw3_new_frame();
    if (nk_begin(ctx, "In-Game Window", nk_rect(10, 10, 200, 200),
                 NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE |
                     NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE))
    {
        // Nuklear widgets go here

        // Example widget: a button
        if (nk_button_label(ctx, "Click Me!"))
        {
            // Handle button click
        }
    }
    nk_end(ctx);
}

I've tried including Nuklear in various ways from my CMakeLists.txt; it seems to work best when I cloned the whole Nuklear repo as a submodule and added the following to include it in the project:

file(GLOB_RECURSE SOURCES "external/Nuklear/src/*.c")

But this setup gives me the below errors:

<projpath>/external/Nuklear/src/nuklear_buffer.c:113:9: error: call to undeclared function 'NK_MEMCPY'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        NK_MEMCPY(temp, b->memory.ptr, buffer_size);
        ^
<projpath>/external/Nuklear/src/nuklear_buffer.c:128:9: error: call to undeclared function 'NK_MEMCPY'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
        NK_MEMCPY(dst, src, back_size);
        ^
<projpath>/external/Nuklear/src/nuklear_buffer.c:191:5: error: call to undeclared function 'NK_MEMCPY'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    NK_MEMCPY(mem, memory, size);
    ^
3 errors generated.

If I don't include the .c files directly (which seems like it would be okay since Nuklear can be header-only) I get errors like below that indicate GL functions have not been included:

<projpath>/external/Nuklear/demo/glfw_opengl4/nuklear_glfw_gl4.h:202:9: error: use of undeclared identifier 'glCreateVertexArrays'

I'm using GLAD for the OpenGL functions, but I am trying to use OpenGL 4.1, so I think the errors are from trying to use newer OpenGL functions. How do I tell Nuklear to use an older version of OpenGL? Or am I missing some other config?

I have the normal boilerplate code for a GLFW window etc. How do I fix these linker errors?

0

There are 0 best solutions below