Skip to content

Instantly share code, notes, and snippets.

@zlash
Created February 19, 2017 04:57
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save zlash/abf8d4bc2efb795a02361e4820a2da10 to your computer and use it in GitHub Desktop.
Save zlash/abf8d4bc2efb795a02361e4820a2da10 to your computer and use it in GitHub Desktop.
Minimal bgfx + SDL2
#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
#include <bx/bx.h>
#include <bx/mutex.h>
#include <bx/thread.h>
void threadInit()
{
bgfx::init();
bgfx::reset(800, 600, BGFX_RESET_VSYNC);
// Enable debug text.
bgfx::setDebug(BGFX_DEBUG_TEXT /*| BGFX_DEBUG_STATS*/);
// Set view 0 clear state.
bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
}
int counter = 0;
int threadMain(void* hoge)
{
while (1) {
bgfx::setViewRect(0, 0, 0, uint16_t(800), uint16_t(600));
bgfx::touch(0);
bgfx::dbgTextClear();
bgfx::dbgTextPrintf(0, 1, 0x4f, "Counter:%d", counter++);
bgfx::frame();
}
}
inline bool sdlSetWindow(SDL_Window* _window)
{
SDL_SysWMinfo wmi;
SDL_VERSION(&wmi.version);
if (!SDL_GetWindowWMInfo(_window, &wmi)) {
return false;
}
bgfx::PlatformData pd;
#if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
pd.ndt = wmi.info.x11.display;
pd.nwh = (void*)(uintptr_t)wmi.info.x11.window;
#elif BX_PLATFORM_OSX
pd.ndt = NULL;
pd.nwh = wmi.info.cocoa.window;
#elif BX_PLATFORM_WINDOWS
pd.ndt = NULL;
pd.nwh = wmi.info.win.window;
#elif BX_PLATFORM_STEAMLINK
pd.ndt = wmi.info.vivante.display;
pd.nwh = wmi.info.vivante.window;
#endif // BX_PLATFORM_
pd.context = NULL;
pd.backBuffer = NULL;
pd.backBufferDS = NULL;
bgfx::setPlatformData(pd);
return true;
}
bx::Thread thread;
int main(void)
{
SDL_Init(0);
SDL_Window* window = SDL_CreateWindow("bgfx", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
sdlSetWindow(window);
bgfx::renderFrame();
threadInit();
bgfx::frame();
thread.init(threadMain);
bool exit = false;
SDL_Event event;
while (!exit) {
bgfx::renderFrame();
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
exit = true;
break;
case SDL_WINDOWEVENT: {
const SDL_WindowEvent& wev = event.window;
switch (wev.event) {
case SDL_WINDOWEVENT_RESIZED:
case SDL_WINDOWEVENT_SIZE_CHANGED:
break;
case SDL_WINDOWEVENT_CLOSE:
exit = true;
break;
}
} break;
}
}
}
bgfx::shutdown();
while (bgfx::RenderFrame::NoContext != bgfx::renderFrame()) {
};
thread.shutdown();
SDL_DestroyWindow(window);
SDL_Quit();
}
@bitsydoge
Copy link

https://gist.github.com/Coldragon/b2b22f1762db352baac94df7c0bee26e
Done it in C (it's the hello world and this one combined with little edit)

@ryancheung
Copy link

not work with latest bgfx version.

@RyanTylerRae
Copy link

RyanTylerRae commented Apr 2, 2019

Running the sample with SDL2 (2.0.9)

> engine.exe!bx::debugBreak() Line 38 C++
engine.exe!bgfx::CallbackStub::fatal(const char * _filePath, unsigned short _line, bgfx::Fatal::Enum _code, const char * _str) Line 67 C++
engine.exe!bgfx::fatal(const char * _filePath, unsigned short _line, bgfx::Fatal::Enum _code, const char * _format, ...) Line 431 C++
engine.exe!bgfx::submit(unsigned short _id, bgfx::ProgramHandle _program, bgfx::OcclusionQueryHandle _occlusionQuery, unsigned int _depth, bool _preserveState) Line 4881 C++
engine.exe!bgfx::submit(unsigned short _id, bgfx::ProgramHandle _program, unsigned int _depth, bool _preserveState) Line 4876 C++
engine.exe!bgfx::touch(unsigned short _id) Line 4870 C++
engine.exe!threadMain(bx::Thread * __formal, void * __formal) Line 46 C++
engine.exe!bx::Thread::entry() Line 311 C++
engine.exe!bx::ThreadInternal::threadFunc(void * _arg) Line 81 C++

\source\external\bgfx.cmake-master\bgfx\src\bgfx.cpp (3393): BGFX Init complete.
\source\external\bgfx.cmake-master\bgfx\src\bgfx.cpp (1383): BGFX CHECK Must be called from render thread.
\source\external\bgfx.cmake-master\bgfx\src\bgfx.cpp (4881): BGFX CHECK Must be called from main thread.
engine.exe has triggered a breakpoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment