-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
I'm very interested in using Dart for native desktop applications, and for game development in particular. In other languages I typically use the SDL library for window management and input handling, so of course I would like to use it in Dart as well via dart:ffi. However, when running a barebones sample program on macOS, it crashes with a native exception.
Here's my program:
import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef sdl_createwindow_func = Pointer<Void> Function(
Pointer<Utf8> title, Int32 x, Int32 y, Int32 w, Int32 h, Uint32 flags);
typedef sdl_createwindow_dart_func = Pointer<Void> Function(
Pointer<Utf8> title, int x, int y, int w, int h, int flags);
void main(List<String> arguments) {
final dylib = DynamicLibrary.open('libSDL2.dylib');
final SDL_CreateWindow =
dylib.lookupFunction<sdl_createwindow_func, sdl_createwindow_dart_func>(
'SDL_CreateWindow');
final window =
SDL_CreateWindow(Utf8.toUtf8('Hello from Dart!'), 0, 0, 640, 480, 0);
}Running this program produces this output:
2020-07-03 15:47:13.178 dart[25981:345613] *** Assertion failure in -[SDLApplication _setAccentColor:], NSApplication.m:7526
2020-07-03 15:47:13.180 dart[25981:345613] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[NSApplication _setAccentColor:] should only be called on the main thread.'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff2695b36f __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007fff6535aa56 objc_exception_throw + 48
2 CoreFoundation 0x00007fff26983f12 +[NSException raise:format:arguments:] + 88
3 Foundation 0x00007fff29337739 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 AppKit 0x00007fff23c7f533 -[NSApplication _setAccentColor:] + 344
5 AppKit 0x00007fff2380de10 -[NSApplication finishLaunching] + 357
6 libSDL2-2.0.0.dylib 0x0000000117c569e1 Cocoa_RegisterApp + 1617
7 libSDL2-2.0.0.dylib 0x0000000117c5b7ac Cocoa_CreateDevice + 12
8 libSDL2-2.0.0.dylib 0x0000000117c2bdcc SDL_VideoInit_REAL + 300
9 libSDL2-2.0.0.dylib 0x0000000117c2df37 SDL_CreateWindow_REAL + 55
10 ??? 0x000000010e103f7e 0x0 + 4530913150
11 ??? 0x000000010efb329b 0x0 + 4546310811
)
libc++abi.dylib: terminating with uncaught exception of type NSException
zsh: abort dart bin/sdl2test.dart
This happens with both Dart VM and dart2native. It appears that macOS requires UI calls to be made from the main thread, and the current structure of Dart does not appear to allow for this.
This is a total roadblock to writing graphical applications on macOS with Dart, so I would really like to find a solution to this. If there's already a cross-platform way to force execution to the main thread, I would love to hear about it. If not, could such a feature be implemented?
Dart VM version: 2.8.4 (stable) (Wed Jun 3 12:26:04 2020 +0200) on "macos_x64"