我尝试在 Raylib 3.5 中使用 Nuklar,但渲染失败,没有任何错误消息,而是一个白色窗口。
由于 Nuklar 可以与前端的任何渲染器一起使用,因此我尝试将 Raylib 与 Nuklar 结合使用。据我所知,Raylib 使用 OpenGL 3.4 作为其渲染器。
我的想法很简单:
- 使用 Raylib 中的“GetWindowHandle()”获取由“InitWindow()”函数创建的 GLFWwindow 实例。
- 然后,使用“nk_glfw3_init()”函数来获取它。
然而,这个想法失败了。我得到的只是一个空的空白窗口,既没有 Nuklar 也没有 Raylib 渲染结果。
我对 OpenGL 和 Nuklar 很陌生。谁能给我点灯?
下面是这个幼稚实验的简单 C 代码。
#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;
}