Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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:</p> <ul> <li>This keeps the dependencies of SDL code, which is focused on initializing SDL-specific subsystems (video, audio, timing, etc.) limited to the specific subsystems that SDL is especially designed to work with. I.e. this way, SDL stays lean and mean.</li> <li>It avoids having to introduce a new platform-specific subsystem for application initialization. Not everyone is going to want the bare-bones application object and menu that SDL sets up for Mac apps, not by a long shot – so if you were going to put it into <code>SDL_init</code>, you'd need to make it an optional subsystem so as not to inconvenience developers who don't need it.</li> <li>It handles inversion of control correctly, which is how Mac OS X and other application frameworks typically operate, while maintaining the operational semantics of SDL routines. SDL_init assumes it's going to be returning to the caller after initialization is complete, but if you tried naively to create an application object in <code>SDL_init</code> and invoke <code>[app run]</code> on it to finish initializing the application and launching, you'd never return. If you didn't call <code>run</code> there, 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 the <code>SDL_main()</code> routine from <code>applicationDidFinishLaunching</code>.</li> <li>It makes it easy to convert SDL demos coded on Linux over to Mac OS X. You don't even have to rename main – the preprocessor renaming of <code>main()</code> to <code>SDL_main()</code> takes care of that for you!</li> </ul> <p>I'm guessing the last of these reasons is the primary driver behind the redefinition of main in <code>SDL_main.h</code>, which I agree is an ugly hack.</p> <p>If you're prepared to give up that level of cross-platform portability for your library and apps, I'd suggest simply modifying your <code>SDL_main.h</code> to remove the following line:</p> <pre><code>#define main SDL_main </code></pre> <p>and removing the following from the <code>SDLMain.m</code> in your project:</p> <pre><code>#ifdef main # undef main #endif </code></pre> <p>You shouldn't even need to recompile SDL if you do this. Note that <code>SDLMain.m</code> is already set up to invoke <code>SDL_main()</code> without the preprocessor hack, and nothing else in SDL is going to use this, so in this way you can you simply provide <code>SDL_main()</code> as your game's entry point.</p> <p>If you want to go the other way, taking over <code>main()</code> yourself, you'd still want to get rid of the <code>#define main SDL_main</code> hack in <code>SDL_main.h</code>, but other than that, you're not beholden to the <code>main()</code> that SDL provides for you. First, note that <code>SDLMain.{h,m}</code> are not part of the library proper; you must include them separately in your project. Second, note the following comments in <code>SDLMain.h</code>:</p> <pre><code>/* SDLMain.m - main entry point for our Cocoa-ized SDL app Initial Version: Darrell Walisser &lt;dwaliss1@purdue.edu&gt; Non-NIB-Code &amp; other changes: Max Horn &lt;max@quendi.de&gt; Feel free to customize this file to suit your needs */ </code></pre> <p>That sounds to me like an invitation to go roll your own if these aren't working for you, starting with <code>SDLMain.{h,m}</code> as a model. And if you're rolling your own, you can do what you want! For that matter, you could write the equivalent of <code>SDLMain.m</code> in Haskell, using HOC, if that's what you want. Unless you're a whiz with HOC, though, I'd keep it simple.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload