我正在尝试用 bgfx 和 sdl2 制作一个“Hello Quad”。我能够使用 sdl 打开一个窗口,没有问题,但是当我尝试添加简单的 bgfx 启动器时,我的程序挂起,没有错误。这是完整的代码:
#include <iostream>
#include <fstream>
#include <vector>
#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
#include <bx/math.h>
template <typename T>
void print(T t)
{
std::cout << t << std::endl;
}
template <typename T, typename... Args>
void print(T t, Args... args)
{
std::cout << t << " ";
print(args...);
}
int main(int argc, char *argv[])
{
print("program start");
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
return 1;
}
print("sdl inited");
// Create a window
SDL_Window *window = SDL_CreateWindow("bgfx with SDL2", 100, 100, 800, 600, SDL_WINDOW_SHOWN);
if (window == nullptr)
{
std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
print("window created");
// Get the window's native handle
SDL_SysWMinfo wmi;
SDL_VERSION(&wmi.version);
if (!SDL_GetWindowWMInfo(window, &wmi))
{
std::cerr << "Could not get window information! SDL_Error: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
print("got window native handle");
// Set up the bgfx platform data with the native window handle
bgfx::PlatformData pd;
#if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
pd.nwh = (void *)(uintptr_t)wmi.info.x11.window;
#elif BX_PLATFORM_OSX
pd.nwh = wmi.info.cocoa.window;
#elif BX_PLATFORM_WINDOWS
pd.nwh = wmi.info.win.window;
#endif
print("connected bgfx and sdl");
pd.ndt = nullptr;
pd.context = nullptr;
pd.backBuffer = nullptr;
pd.backBufferDS = nullptr;
print("prepared pd");
bgfx::setPlatformData(pd);
print("set pd");
// Initialize bgfx
bgfx::Init init;
print("created bgfx init object");
init.debug = BGFX_DEBUG_TEXT;
init.type = bgfx::RendererType::Count; // Automatically choose the renderer
init.resolution.width = 800;
init.resolution.height = 600;
init.resolution.reset = BGFX_RESET_VSYNC;
print("set init object props");
if (!bgfx::init(init))
{
std::cerr << "Failed to initialize bgfx!" << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
print("bgfx inited");
...
// main loop
return 0;
}
打印到控制台的最后一个调试语句是“set init object props”...不是失败时的错误消息或成功时的下一条调试消息。 所以我在调试模式下编译了该项目(包括 sdl 和 bgfx)并使用 lldb 运行它,这就是我得到的:
program start
sdl inited
window created
got window native handle
connected bgfx and sdl
prepared pd
set pd
created bgfx init object
set init object props
2024-08-19 12:58:15.890213-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (3417): BGFX bgfx platform data like window handle or backbuffer is not set, creating headless device.
2024-08-19 12:58:15.890234-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (3449): BGFX Init...
2024-08-19 12:58:15.890248-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (3456): BGFX Version 1.108.7238 (commit: 8065659e90a2673dd2ac4b12f193604a631833e3)
2024-08-19 12:58:15.891525-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (1833): BGFX Creating rendering thread.
2024-08-19 12:58:15.891593-0700 HelloQuad[35299:1439942] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx.cpp (1842): BGFX Running in multi-threaded mode
2024-08-19 12:58:15.891618-0700 HelloQuad[35299:1440041] /Users/.../libs/bgfx.cmake/bgfx/src/bgfx_p.h (2926): BGFX render thread start
2024-08-19 12:58:15.892255-0700 HelloQuad[35299:1440041] /Users/.../libs/bgfx.cmake/bgfx/src/renderer_mtl.mm (379): BGFX Init.
就是这样...它挂了。当我在挂起期间暂停执行时:
Process 35299 stopped
* thread #1, name = 'bgfx - renderer backend thread', queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
frame #0: 0x000000018b26d7f0 libsystem_kernel.dylib`semaphore_wait_trap + 8
libsystem_kernel.dylib`semaphore_wait_trap:
-> 0x18b26d7f0 <+8>: ret
libsystem_kernel.dylib`semaphore_wait_signal_trap:
0x18b26d7f4 <+0>: mov x16, #-0x25
0x18b26d7f8 <+4>: svc #0x80
0x18b26d7fc <+8>: ret
Target 0: (HelloQuad) stopped.
我还发现了10年前的这个: https://github.com/bkaradzic/bgfx/issues/501,其中表示添加此指令:“BGFX_CONFIG_MULTITHREADED=0” 所以我尝试了一下,但它不起作用 - 有趣的是,bgfx 调试消息仍然说了同样的事情:“以多线程模式运行”,“渲染线程启动”,所以我不确定这是否是因为 10 年前的指令只是可能不会像以前那样做,或者我搞乱了编译/链接(这有点复杂,因为我使用的是 CMake 而不是 GENie 版本),但是,由于代码编译和链接得很好,而且我能够打开sdl 窗口就可以了,我排除了这个选项。
我真的不知道如何继续调试这个...
有完全相同的问题!尝试在 M4/IOS 13 上运行,并尝试使用 SDL2...有运气解决吗?