10 #include <SDL_syswm.h>
11 #include <bgfx/platform.h>
26 if (SDL_Init(SDL_INIT_VIDEO) != 0) {
27 SDL_LogError(SDL_LOG_CATEGORY_ERROR,
"Error initializing SDL: %s\n", SDL_GetError());
32 mainWindow = SDL_CreateWindow(
35 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
36 windowWidth, windowHeight,
41 SDL_LogError(SDL_LOG_CATEGORY_ERROR,
"Error creating window: %s\n", SDL_GetError());
47 SDL_VERSION(&wmi.version);
48 if (!SDL_GetWindowWMInfo(mainWindow, &wmi)) {
49 SDL_LogError(SDL_LOG_CATEGORY_ERROR,
"Couldn't get window information: %s", SDL_GetError());
53 bgfx::PlatformData pd;
54 switch (wmi.subsystem)
56 #if defined(SDL_VIDEO_DRIVER_WINDOWS)
57 case SDL_SYSWM_WINDOWS:
59 pd.nwh = wmi.info.win.window;
62 #if defined(SDL_VIDEO_DRIVER_X11)
64 pd.ndt = wmi.info.x11.display;
65 pd.nwh = (
void*)(uintptr_t)wmi.info.x11.window;
68 #if defined(SDL_VIDEO_DRIVER_COCOA)
71 pd.nwh = wmi.info.cocoa.window;
74 #if defined(SDL_VIDEO_DRIVER_VIVANTE)
75 case SDL_SYSWM_VIVANTE:
76 pd.ndt = wmi.info.vivante.display;
77 pd.nwh = wmi.info.vivante.window;
80 default: SDL_LogError(SDL_LOG_CATEGORY_ERROR,
"Unsupported window system");
83 for (
int sysCursorIndex = 0; sysCursorIndex < SDL_NUM_SYSTEM_CURSORS; sysCursorIndex++)
85 systemCursors[sysCursorIndex] = SDL_CreateSystemCursor(SDL_SystemCursor(sysCursorIndex));
90 pd.backBuffer =
nullptr;
91 pd.backBufferDS =
nullptr;
92 bgfx::setPlatformData(pd);
99 bool BaseApp::initAppThread()
101 if (!bgfx::init())
return false;
102 bgfx::reset(uint32_t(windowWidth), uint32_t(windowHeight), 0 | BGFX_RESET_VSYNC);
103 bgfx::setDebug(BGFX_DEBUG_TEXT);
106 bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x303030ff, 1.0f, 0);
113 void BaseApp::shutdownAppThread() { bgfx::shutdown(); }
115 void BaseApp::shutdown()
117 for (
auto& systemCursorPtr : systemCursors)
119 SDL_FreeCursor(systemCursorPtr);
120 systemCursorPtr =
nullptr;
123 SDL_DestroyWindow(mainWindow);
124 mainWindow =
nullptr;
129 void BaseApp::executeLoopOnce()
135 while (SDL_PollEvent(&event))
137 transfer.PushEvent(event);
142 mouseState.buttonsMask = SDL_GetMouseState(&mouseState.x, &mouseState.y);
143 if (!(SDL_GetWindowFlags(mainWindow) & SDL_WINDOW_MOUSE_FOCUS)) {
144 mouseState.x = mouseState.y = std::numeric_limits<int>::lowest();
146 transfer.PushMouseState(mouseState);
152 bgfx::RenderFrame::Enum ret = bgfx::renderFrame();
153 if (ret == bgfx::RenderFrame::Exiting) requireExit();
156 void BaseApp::executeAppLoopOnce()
158 static bool debugDisplay =
false;
159 const Inputs inputs = inputsManager.receiveAndProcessEvents();
160 for (
const SDL_Event& event : inputs.events)
165 case SDL_QUIT: requireExit();
break;
167 if (event.key.keysym.sym == SDLK_F1) {
168 debugDisplay = !debugDisplay;
174 bgfx::setDebug(BGFX_DEBUG_TEXT | (debugDisplay ? BGFX_DEBUG_STATS : 0));
176 bgfx::setViewRect(0, 0, 0, uint16_t(windowWidth), uint16_t(windowHeight));
189 std::thread appThread{&BaseApp::runAppThread,
this};
195 while (bgfx::RenderFrame::NoContext != bgfx::renderFrame())
201 void BaseApp::runAppThread()
203 if (!initAppThread()) {
210 executeAppLoopOnce();