Created
February 1, 2019 14:34
-
-
Save declank/4319dfb57b191e2a3f0ea9e10baf0907 to your computer and use it in GitHub Desktop.
bgfx + SDL starting point (proper threading)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
//#define SDL_MAIN_HANDLED | |
#include <SDL.h> | |
#include <SDL_syswm.h> | |
#include <bgfx/bgfx.h> | |
#include <bgfx/platform.h> | |
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; | |
} | |
int32_t run() | |
{ | |
init(); | |
bgfx::frame(); | |
setWindowSize(); | |
while(update()) {} | |
shutdown(); | |
} | |
int32_t threadFunc(bx::Thread* _thread, void* _userData) | |
{ | |
BX_UNUSED(_thread); | |
BX_UNUSED(_userData); | |
int32_t result = run(); | |
SDL_Event event; | |
SDL_QuitEvent& qev = event.quit; | |
qev.type = SDL_QUIT; | |
SDL_PushEvent(&event); | |
return result; | |
} | |
int main() | |
{ | |
SDL_Init(0 | SDL_INIT_GAMECONTROLLER | SDL_INIT_SOUND); | |
SDL_Window* window = SDL_CreateWindow("bgfx", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); | |
sdlSetWindow(window); | |
bgfx::renderFrame(); | |
bx::Thread thread; | |
thread.init(threadFunc, NULL); | |
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_KEYDOWN: | |
{ | |
SDL_KeyboardEvent& keyEvent = event.key; | |
printf("Key press scancode %d\n", keyEvent.keysym.scancode); | |
if (keyEvent.keysym.scancode == SDL_SCANCODE_ESCAPE) | |
{ | |
exit = true; | |
} | |
} | |
break; | |
} | |
} | |
} | |
while (bgfx::RenderFrame::NoContext != bgfx::renderFrame() ) {}; | |
thread.shutdown(); | |
SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return thread.getExitCode(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment