SDL on OS X uses preprocessor tricks to overload main() with their own entry point, written in Objective C, which calls the user’s main.
These tricks make the lives of non-C SDL users (e.g: the Haskell bindings) very difficult.
Is there a good reason for this?
Why couldn’t SDL do the objective-C Cocoa initialization in SDL_init?
The approach for Mac OS X is not much different from the approach for other non-Linux platforms (Windows, old Mac, BeOS). You could ask the SDL developers themselves why they did it this way, but I can see several reasons they may have chosen to do this:
SDL_init, you’d need to make it an optional subsystem so as not to inconvenience developers who don’t need it.SDL_initand invoke[app run]on it to finish initializing the application and launching, you’d never return. If you didn’t callrunthere, you’d have to create a separate SDL function to set up the application run loop. This could complicate the SDL library quite a bit. The approach that was chosen avoids all this, by letting the framework take care of all the application set up first, and invoke theSDL_main()routine fromapplicationDidFinishLaunching.main()toSDL_main()takes care of that for you!I’m guessing the last of these reasons is the primary driver behind the redefinition of main in
SDL_main.h, which I agree is an ugly hack.If you’re prepared to give up that level of cross-platform portability for your library and apps, I’d suggest simply modifying your
SDL_main.hto remove the following line:and removing the following from the
SDLMain.min your project:You shouldn’t even need to recompile SDL if you do this. Note that
SDLMain.mis already set up to invokeSDL_main()without the preprocessor hack, and nothing else in SDL is going to use this, so in this way you can you simply provideSDL_main()as your game’s entry point.If you want to go the other way, taking over
main()yourself, you’d still want to get rid of the#define main SDL_mainhack inSDL_main.h, but other than that, you’re not beholden to themain()that SDL provides for you. First, note thatSDLMain.{h,m}are not part of the library proper; you must include them separately in your project. Second, note the following comments inSDLMain.h:That sounds to me like an invitation to go roll your own if these aren’t working for you, starting with
SDLMain.{h,m}as a model. And if you’re rolling your own, you can do what you want! For that matter, you could write the equivalent ofSDLMain.min Haskell, using HOC, if that’s what you want. Unless you’re a whiz with HOC, though, I’d keep it simple.