![]() Have a public struct that acts as the API. Have an internal class that actually holds the SDL_Palette *. You can do this using the standard copy-on-write technique: ![]() For example, an SDL_Palette might make sense as a value type, and if you think that’s the case then you should model it as a struct. Remember that, just because something is allocated on the heap, doesn’t mean you have to model it as a class. SDL_Colour is clearly best handled as a Swift struct. SDL_Keycode probably should be modelled as a Swift enum with a raw value. For example:Īn SDL_Window * obviously has identity, and thus should be modelled as a Swift class. If it’s the latter, then you’d want to model things in the way that makes most sense in Swift. What sort of wrapper are you trying to create?Ī minimal wrapper that just smooths over the biggest bumps?Ī full-featured wrapper that presents the API in a way that a Swift developer will feel immediately at home? You need to start by deciding on how ‘nice’ you want to be to your clients. ![]() Is there a conventional technique for wrapping these objects? So you could write something like: struct SDLWindow Īnd, if everything inlines, that should boil down to similar machine code to what you'd get from using the C interface directly, with a little more type safety and ergonomics than the default C import.ĪPI notes could work too, but it would mean learning yet another not-particularly-well-documented language of attributes and annotations, and I think you can get similar results wrapping entirely in Swift. If you do go for the wrapping approach, then wrapping C values in Swift structs rather than classes should be more or less "zero cost", since the layout and calling convention of a single-field struct in Swift is the same as the single field. SDL_Window ), or can I use API Notes to express these types more Swiftily, and add extensions where it’s useful? Do API Notes files work with swift build?ĭo I need to create an entire parallel class hierarchy, wrapping each of the SDL types (e.g. SwiftName: 'SDLWindow.init(title:x:y:width:height:flags:)īut SDLWindow is still unknown to Swift. modulemap file, with the following contents: Classes: I tried putting ClibSDL2.apinotes in next to the. SDL_CreateWindow() takes a few parameters, one that is best expressed as an OptionSet.Įven getting API Notes to work is confusing. SDL_Window), or can I use API Notes to express these types more Swiftily, and add extensions where it’s useful?įor example, SDL_Window has a bunch of methods like SDL_CreateWindow() and SDL_GetWindowSurface(). typedef struct SDL_Window SDL_Window), which import as OpaquePointer.ĭo I need to create an entire parallel class hierarchy, wrapping each of the SDL types (e.g. One of the problems is that SDL defines a lot of constants with macros, and those don’t import nicely. I’ve been making some progress in incorporating SDL into my Swift app, but I want to make it better.
0 Comments
Leave a Reply. |