我只是想用glfw学习bgfx,我的代码确实可以编译,但是当我尝试运行该程序时,我遇到了一些错误
#define WDW_WIDTH 1280
#define WDW_HEIGHT 720
#define GLFW_INCLUDE_NONE
#if defined (_WIN32)
#define GLFW_EXPOSE_NATIVE_WIN32
#elif defined (__linux__)
#define GLFW_EXPOSE_NATIVE_X11
#elif defined (__APPLE__)
#define GLFW_EXPOSE_NATIVE_COCOA
#endif
#include <iostream>
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
auto GetNativeWindow(GLFWwindow* window) {
#if defined (GLFW_EXPOSE_NATIVE_WIN32)
return glfwGetWin32Window(window);
#elif defined (GLFW_EXPOSE_NATIVE_X11)
return (void*)(uintptr_t)glfwGetX11Window(window);
#elif defined (GLFW_EXPOSE_NATIVE_COCOA)
return glfwGetCocoaWindow(window);
#else
std::cout << "undefined platform\n";
#endif
}
Display* GetNativeDisplay() {
#if defined (GLFW_EXPOSE_NATIVE_WIN32)
return NULL;
#elif defined (GLFW_EXPOSE_NATIVE_X11)
return glfwGetX11Display();
#elif defined (GLFW_EXPOSE_NATIVE_COCOA)
return NULL;
#else
std::cout << "undefined platform\n";
return NULL;
#endif
}
static void error_callback(int error, const char* description) {
std::cout << "Error [" << error << "] : " << description << "\n";
}
int main() {
glfwSetErrorCallback(error_callback);
if (!glfwInit()) {
error_callback(1, "failed to init glfw");
glfwTerminate();
}
GLFWwindow* window = glfwCreateWindow(WDW_WIDTH, WDW_HEIGHT, "GAME", nullptr, nullptr);
if (window == nullptr) {
error_callback(2, "failed to create window");
glfwTerminate();
}
glfwMakeContextCurrent(window);
bgfx::Init bgfx_init;
bgfx_init.type = bgfx::RendererType::Count; // Automatically choose a renderer.
bgfx_init.vendorId = BGFX_PCI_ID_NONE;
bgfx_init.resolution.width = WDW_WIDTH;
bgfx_init.resolution.height = WDW_HEIGHT;
bgfx_init.resolution.reset = BGFX_RESET_VSYNC;
if (!bgfx::init(bgfx_init)) {
error_callback(1, "failed to init bgfx");
glfwTerminate();
}
bgfx::PlatformData pd;
pd.ndt = GetNativeDisplay();
pd.nwh = GetNativeWindow(window);
bgfx::setPlatformData(pd);
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443355FF, 1.0f, 0);
bgfx::setViewRect(0, 0, 0, WDW_WIDTH, WDW_HEIGHT);
while (!glfwWindowShouldClose(window)) {
bgfx::frame();
glfwPollEvents();
}
bgfx::shutdown();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
错误:
Error [1] : failed to init bgfx
Error [65537] : The GLFW library is not initialized
Error [65537] : The GLFW library is not initialized
Segmentation fault (core dumped)
当我删除 bgfx 代码只留下 glfw 时,一切都正常工作,并且 glfw 初始化没有错误(也许)。
我也尝试过使用SDL2,但它也有相同的错误(分段错误(核心转储))。
glfw 和 bgfx 库与 vcpkg 一起安装 并使用 makefile 进行构建,我的操作系统是 Linux Ubuntu 22.04.3 LTS
我只是想学习bgfx。
修复方法是不使用
bgfx::setPlatformData(pd);
设置平台数据。
相反,我将平台数据设置到 bgfx_init 变量中
bgfx::Init bgfx_init;
bgfx_init.type = bgfx::RendererType::Count; // Automatically choose a renderer.
bgfx_init.vendorId = BGFX_PCI_ID_NONE;
bgfx_init.resolution.width = WDW_WIDTH;
bgfx_init.resolution.height = WDW_HEIGHT;
bgfx_init.resolution.reset = BGFX_RESET_VSYNC;
bgfx_init.platformData = pd;
if (!bgfx::init(bgfx_init)) {
error_callback(1, "failed to init bgfx");
glfwTerminate();
}