Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wuxianggujun/549c8cbcc8c6b6a751daac493624617d to your computer and use it in GitHub Desktop.
Save wuxianggujun/549c8cbcc8c6b6a751daac493624617d to your computer and use it in GitHub Desktop.
Using SDL3 and BGFX
/*
* Basic SDL3 and BGFX initialization
*
* By: Shogunate
* Date: July 8th, 2024
* Email: [email protected]
*/
#include <windows.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
void BgfxInit( void* nwh, void* ndt )
{
bgfx::Init init;
init.type = bgfx::RendererType::OpenGL;
init.vendorId = BGFX_PCI_ID_NONE;
init.platformData.nwh = nwh;
init.platformData.ndt = ndt;
init.resolution.width = 1280;
init.resolution.height = 720;
//init.resolution.reset = BGFX_RESET_VSYNC;
if( !bgfx::init( init ) )
__debugbreak();
//bgfx::reset( 1280, 720, BGFX_RESET_VSYNC );
bgfx::setDebug( BGFX_DEBUG_TEXT );
bgfx::setViewClear( 0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030FF, 1.0f, 0 );
}
int counter = 0;
inline bool sdlSetWindow( SDL_Window* window )
{
bgfx::PlatformData pd;
#if defined(SDL_PLATFORM_WIN32)
pd.nwh = (HWND) SDL_GetProperty( SDL_GetWindowProperties( window ), SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL );
pd.ndt = NULL;
#elif defined(SDL_PLATFORM_MACOS)
pd.nwh = (__bridge NSWindow *) SDL_GetProperty( SDL_GetWindowProperties( window ), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, NULL );
pd.ndt = NULL;
#elif defined(SDL_PLATFORM_LINUX)
if( SDL_strcmp( SDL_GetCurrentVideoDriver(), "x11" ) == 0 )
{
pd.ndt = (Display *) SDL_GetProperty( SDL_GetWindowProperties( window ), SDL_PROP_WINDOW_X11_DISPLAY_POINTER, NULL );
pd.nwh = (Window) SDL_GetNumberProperty( SDL_GetWindowProperties( window ), SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0 );
}
else if( SDL_strcmp( SDL_GetCurrentVideoDriver(), "wayland" ) == 0 )
{
/*struct wl_display *display*/ pd.ndt = (struct wl_display *) SDL_GetProperty( SDL_GetWindowProperties( window ), SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, NULL );
/*struct wl_surface *surface*/ pd.nwh = (struct wl_surface *) SDL_GetProperty( SDL_GetWindowProperties( window ), SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, NULL );
}
#endif
pd.context = NULL;
pd.backBuffer = NULL;
pd.backBufferDS = NULL;
bgfx::setPlatformData(pd);
BgfxInit( pd.nwh, pd.ndt );
return true;
}
int SDL_main(int argc, char *argv[])
{
SDL_Init(0);
SDL_Window* window = SDL_CreateWindow( "Bgfx SDL3", 1280, 720, 0 );
sdlSetWindow( window );
bool exit = false;
SDL_Event event;
while( !exit )
{
bgfx::setViewRect( 0, 0, 0, uint16_t(1280), uint16_t(720) );
bgfx::touch(0);
bgfx::dbgTextClear();
bgfx::dbgTextPrintf( 0, 1, 0x4f, "Counter: %d", counter++ );
bgfx::frame();
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_EVENT_QUIT:
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
exit = true;
break;
}
}
}
bgfx::shutdown();
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment