[SDL] iPhone SDL main loop

Holmes A. Futrell hfutrell at umail.ucsb.edu
Sun Nov 30 14:34:53 PST 2008


Hello,

In order to retain strict compatibility with other SDL programs and in  
order to not require the use of any Objective-C, SDL for iPhone had to  
take some extreme steps in terms of control flow.

In a normal iPhone application, the main() function calls  
UIApplicationMain.  After this, control flow isn't really in the hands  
of your code, it's in the hands of the Cocoa Touch library.  After  
this point, your application mainly responds to events caught by the  
main event loop, and then returns control to the main event loop.  But  
doing things this way isn't just the orthodox way of doing things on  
the iPhone platform -- if you don't do it this way your app will not  
function properly.

But this model of control flow is very much incompatible with the way  
things are done in a normal SDL application.  A normal SDL application  
retains control throughout the entire program execution.  It typically  
contains a main() function with a for() loop that loops until the user  
decides to quit.

So how to reconcile these two approaches?  The way I chose to do it  
was to place the main() function in the SDL library itself.  This main  
function calls UIApplicationMain, as is necessary.  Your main function  
will be renamed SDL_main(), or something to this effect, and called  
later by SDL.  So how do we return control to the Cocoa Touch Library  
periodically (as is necessary)?  We do this whenever you draw to the  
screen.  Control then returns back to where you were, and you'll never  
even notice it left :P

The end result is that a programmer gets to program in the way that  
SDL applications are normally made (as one big for loop), but the  
control flow follows the pattern that the user interface libraries on  
iPhone expect and require in order to function correctly.

It's an approach that is not without it's flaws (getting the SDL  
application to respond to a quit event correctly required a horrible  
hack), but one that provides compatibility across platforms and  
generally works just fine.

I hope that answers your question,

- Holmes




Quoting WB <warrick_buchanan at hotmail.com>:

> We have started to have a quick look at SDL1.3 specifically for the  
> iPhone.  We
> are curious how the main loop is handled and it seems different then all the
> code samples we have seen using an NSTimer path or a separate thread (which
> seems overkill)?
>
> Could anyone give a rough overview how this works currently in SDL1.3 for the
> iPhone as it seems interesting?  We have been digging through the code but we
> are no MacOS experts is why :)
>
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>



-- 
Holmes Futrell
hfutrell at umail.ucsb.edu




More information about the SDL mailing list