BabylonNative with Flutter Desktop Windows

Hi!

I’m exploring the possibility of using BabylonNative with Flutter Desktop Windows. I’ve successfully compiled and ran the samples for both and saw the win32 HWND handles.

BabylonReactNative or reasons for Flutter aside, any direction or advise on the right approach to “combine” them for a win32 n00b? i.e. Flutter for UI, BabylonNative for 3D in Apps

Thanks in advance.

BabylonNative’s app.cpp:

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    hInst = hInstance; // Store instance handle in our global variable

    HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

    if (!hWnd)
    {
        return FALSE;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    RefreshBabylon(hWnd);

    return TRUE;
}

Flutter Desktop Windows’ win32_window.cpp:

bool Win32Window::CreateAndShow(const std::wstring& title,
                                const Point& origin,
                                const Size& size) {
  Destroy();

  const wchar_t* window_class =
      WindowClassRegistrar::GetInstance()->GetWindowClass();

  const POINT target_point = {static_cast<LONG>(origin.x),
                              static_cast<LONG>(origin.y)};
  HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTONEAREST);
  UINT dpi = FlutterDesktopGetDpiForMonitor(monitor);
  double scale_factor = dpi / 96.0;

  HWND window = CreateWindow(
      window_class, title.c_str(), WS_OVERLAPPEDWINDOW | WS_VISIBLE,
      Scale(origin.x, scale_factor), Scale(origin.y, scale_factor),
      Scale(size.width, scale_factor), Scale(size.height, scale_factor),
      nullptr, nullptr, GetModuleHandle(nullptr), this);

  if (!window) {
    return false;
  }

  return OnCreate();
}

Hi @BenjaminRNG
Integrating BabylonNative should be straightforward.
For Win32, you need a window HWND and an access to the Windows API message pump.
Basically, when a resize event, close event, paint event is issued, you should process it with the appropriate call to BabylonNative API.

I don’t see that message processing in your snippet but I’m pretty sure such mecanism exists with Flutter.

My approach for integrating BN with a new platform is to be sure it compiles and links fine. I would suggest to verify that first by calling any BN api function in your initinstance. If it compiles and links fine, then you can start thinking about using the provided window.