README.iOS
author Sam Lantinga <slouken@libsdl.org>
Sat, 18 May 2013 12:48:50 -0700
changeset 7190 11612d544fcd
parent 6654 2ecfb25be1e2
permissions -rw-r--r--
Added mobile application events, with implementations for iOS and Android
slouken@2765
     1
==============================================================================
slouken@2765
     2
Building the Simple DirectMedia Layer for iPhone OS 2.0
slouken@2765
     3
==============================================================================
slouken@2765
     4
slouken@2765
     5
Requirements: Mac OS X v10.5 or later and the iPhone SDK.
slouken@2765
     6
slouken@2765
     7
Instructions:
slouken@6159
     8
1.  Open SDL.xcodeproj (located in Xcode-iOS/SDL) in XCode.
slouken@6159
     9
2.  Select your desired target, and hit build.
slouken@2765
    10
slouken@2765
    11
There are three build targets:
slouken@6159
    12
- libSDL.a:
slouken@6159
    13
	Build SDL as a statically linked library
slouken@6159
    14
- testsdl
slouken@6159
    15
	Build a test program (there are known test failures which are fine)
slouken@2765
    16
- Template:
slouken@2765
    17
	Package a project template together with the SDL for iPhone static libraries and copies of the SDL headers.  The template includes proper references to the SDL library and headers, skeleton code for a basic SDL program, and placeholder graphics for the application icon and startup screen.
slouken@2765
    18
slouken@2765
    19
==============================================================================
gabomdq@6333
    20
Build SDL for iOS from the command line
gabomdq@6333
    21
==============================================================================
gabomdq@6333
    22
gabomdq@6333
    23
1. cd (PATH WHERE THE SDL CODE IS)/build-scripts
gabomdq@6333
    24
2. ./iosbuild.sh
gabomdq@6333
    25
gabomdq@6333
    26
If everything goes fine, you should see a build/ios directory, inside there's
gabomdq@6333
    27
two directories "lib" and "include". 
gabomdq@6333
    28
"include" contains a copy of the SDL headers that you'll need for your project,
gabomdq@6333
    29
make sure to configure XCode to look for headers there.
gabomdq@6333
    30
"lib" contains find two files, libSDL2.a and libSDL2main.a, you have to add both 
gabomdq@6333
    31
to your XCode project. These libraries contain three architectures in them,
gabomdq@6333
    32
armv6 for legacy devices, armv7, and i386 (for the simulator).
gabomdq@6333
    33
By default, iosbuild.sh will autodetect the SDK version you have installed using 
gabomdq@6333
    34
xcodebuild -showsdks, and build for iOS >= 3.0, you can override this behaviour 
gabomdq@6333
    35
by setting the MIN_OS_VERSION variable, ie:
gabomdq@6333
    36
gabomdq@6333
    37
MIN_OS_VERSION=4.2 ./iosbuild.sh
gabomdq@6333
    38
gabomdq@6333
    39
==============================================================================
slouken@6159
    40
Using the Simple DirectMedia Layer for iOS
slouken@2765
    41
==============================================================================
slouken@2765
    42
slouken@6159
    43
FIXME: This needs to be updated for the latest methods
slouken@6159
    44
slouken@2765
    45
Here is the easiest method:
slouken@6159
    46
1.  Build the SDL libraries (libSDL.a and libSDLSimulator.a) and the iPhone SDL Application template.
slouken@6159
    47
1.  Install the iPhone SDL Application template by copying it to one of XCode's template directories.  I recommend creating a directory called "SDL" in "/Developer/Platforms/iOS.platform/Developer/Library/XCode/Project Templates/" and placing it there.
slouken@2765
    48
2.  Start a new project using the template.  The project should be immediately ready for use with SDL.
slouken@2765
    49
slouken@2765
    50
Here is a more manual method:
slouken@2765
    51
1.  Create a new iPhone view based application.
slouken@6159
    52
2.  Build the SDL static libraries (libSDL.a and libSDLSimulator.a) for iPhone and include them in your project.  XCode will ignore the library that is not currently of the correct architecture, hence your app will work both on iPhone and in the iPhone Simulator.
slouken@2765
    53
3.  Include the SDL header files in your project.
slouken@2765
    54
4.  Remove the ApplicationDelegate.h and ApplicationDelegate.m files -- SDL for iPhone provides its own UIApplicationDelegate.  Remove MainWindow.xib -- SDL for iPhone produces its user interface programmatically.
slouken@2765
    55
5.  Delete the contents of main.m and program your app as a regular SDL program instead.  You may replace main.m with your own main.c, but you must tell XCode not to use the project prefix file, as it includes Objective-C code.
slouken@2765
    56
slouken@2765
    57
==============================================================================
slouken@7190
    58
Notes -- Application events
slouken@7190
    59
==============================================================================
slouken@7190
    60
slouken@7190
    61
On iOS the application goes through a fixed life cycle and you will get
slouken@7190
    62
notifications of state changes via application events. When these events
slouken@7190
    63
are delivered you must handle them in an event callback because the OS may
slouken@7190
    64
not give you any processing time after the events are delivered.
slouken@7190
    65
slouken@7190
    66
e.g.
slouken@7190
    67
slouken@7190
    68
int HandleAppEvents(void *userdata, SDL_Event *event)
slouken@7190
    69
{
slouken@7190
    70
    switch (event->type)
slouken@7190
    71
    {
slouken@7190
    72
    case SDL_APP_TERMINATING:
slouken@7190
    73
        /* Terminate the app.
slouken@7190
    74
           Shut everything down before returning from this function.
slouken@7190
    75
        */
slouken@7190
    76
        return 0;
slouken@7190
    77
    case SDL_APP_LOWMEMORY:
slouken@7190
    78
        /* You will get this when your app is paused and iOS wants more memory.
slouken@7190
    79
           Release as much memory as possible.
slouken@7190
    80
        */
slouken@7190
    81
        return 0;
slouken@7190
    82
    case SDL_APP_WILLENTERBACKGROUND:
slouken@7190
    83
        /* Prepare your app to go into the background.  Stop loops, etc.
slouken@7190
    84
           This gets called when the user hits the home button, or gets a call.
slouken@7190
    85
        */
slouken@7190
    86
        return 0;
slouken@7190
    87
    case SDL_APP_DIDENTERBACKGROUND:
slouken@7190
    88
        /* This will get called if the user accepted whatever sent your app to the background.
slouken@7190
    89
           If the user got a phone call and canceled it, you'll instead get an SDL_APP_DIDENTERFOREGROUND event and restart your loops.
slouken@7190
    90
           When you get this, you have 5 seconds to save all your state or the app will be terminated.
slouken@7190
    91
           Your app is NOT active at this point.
slouken@7190
    92
        */
slouken@7190
    93
        return 0;
slouken@7190
    94
    case SDL_APP_WILLENTERFOREGROUND:
slouken@7190
    95
        /* This call happens when your app is coming back to the foreground.
slouken@7190
    96
           Restore all your state here.
slouken@7190
    97
        */
slouken@7190
    98
        return 0;
slouken@7190
    99
    case SDL_APP_DIDENTERFOREGROUND:
slouken@7190
   100
        /* Restart your loops here.
slouken@7190
   101
           Your app is interactive and getting CPU again.
slouken@7190
   102
        */
slouken@7190
   103
        return 0;
slouken@7190
   104
    default:
slouken@7190
   105
        /* No special processing, add it to the event queue */
slouken@7190
   106
        return 1;
slouken@7190
   107
    }
slouken@7190
   108
}
slouken@7190
   109
slouken@7190
   110
int main(int argc, char *argv[])
slouken@7190
   111
{
slouken@7190
   112
    SDL_SetEventFilter(HandleAppEvents, NULL);
slouken@7190
   113
slouken@7190
   114
    ... run your main loop
slouken@7190
   115
slouken@7190
   116
    return 0;
slouken@7190
   117
}
slouken@7190
   118
slouken@7190
   119
    
slouken@7190
   120
==============================================================================
slouken@2765
   121
Notes -- Accelerometer as Joystick
slouken@2765
   122
==============================================================================
slouken@2765
   123
slouken@2765
   124
SDL for iPhone supports polling the built in accelerometer as a joystick device.  For an example on how to do this, see the accelerometer.c in the demos directory.
slouken@2765
   125
slouken@2765
   126
The main thing to note when using the accelerometer with SDL is that while the iPhone natively reports accelerometer as floating point values in units of g-force, SDL_JoystickGetAxis reports joystick values as signed integers.  Hence, in order to convert between the two, some clamping and scaling is necessary on the part of the iPhone SDL joystick driver.  To convert SDL_JoystickGetAxis reported values BACK to units of g-force, simply multiply the values by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
slouken@2765
   127
slouken@2765
   128
==============================================================================
slouken@2765
   129
Notes -- OpenGL ES
slouken@2765
   130
==============================================================================
slouken@2765
   131
slouken@2765
   132
Your SDL application for iPhone uses OpenGL ES for video by default.
slouken@2765
   133
slouken@4666
   134
OpenGL ES for iPhone supports several display pixel formats, such as RGBA8 and RGB565, which provide a 32 bit and 16 bit color buffer respectively.  By default, the implementation uses RGB565, but you may use RGBA8 by setting each color component to 8 bits in SDL_GL_SetAttribute.
slouken@2765
   135
slouken@2765
   136
If your application doesn't use OpenGL's depth buffer, you may find significant performance improvement by setting SDL_GL_DEPTH_SIZE to 0.
slouken@2765
   137
slouken@2765
   138
Finally, if your application completely redraws the screen each frame, you may find significant performance improvement by setting the attribute SDL_GL_RETAINED_BACKING to 1.
slouken@2765
   139
slouken@2765
   140
==============================================================================
slouken@2765
   141
Notes -- Keyboard
slouken@2765
   142
==============================================================================
slouken@2765
   143
slouken@6392
   144
The SDL keyboard API has been extended to support on-screen keyboards:
slouken@2765
   145
slouken@6654
   146
void SDL_StartTextInput()
slouken@6654
   147
	-- enables text events and reveals the onscreen keyboard.
slouken@6654
   148
void SDL_StopTextInput()
slouken@6654
   149
	-- disables text events and hides the onscreen keyboard.
slouken@6654
   150
SDL_bool SDL_IsTextInputActive()
slouken@6654
   151
	-- returns whether or not text events are enabled (and the onscreen keyboard is visible)
slouken@2765
   152
slouken@2765
   153
==============================================================================
slouken@2765
   154
Notes -- Reading and Writing files
slouken@2765
   155
==============================================================================
slouken@2765
   156
slouken@2765
   157
Each application installed on iPhone resides in a sandbox which includes its own Application Home directory.  Your application may not access files outside this directory.
slouken@2765
   158
slouken@2765
   159
Once your application is installed its directory tree looks like:
slouken@2765
   160
slouken@2765
   161
MySDLApp Home/
slouken@2765
   162
	MySDLApp.app
slouken@2765
   163
	Documents/
slouken@2765
   164
	Library/
slouken@2765
   165
		Preferences/
slouken@2765
   166
	tmp/
slouken@2765
   167
slouken@2765
   168
When your SDL based iPhone application starts up, it sets the working directory to the main bundle (MySDLApp Home/MySDLApp.app), where your application resources are stored.  You cannot write to this directory.  Instead, I advise you to write document files to "../Documents/" and preferences to "../Library/Preferences".  
slouken@2765
   169
slouken@2765
   170
More information on this subject is available here:
slouken@6159
   171
http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html
slouken@2765
   172
slouken@2765
   173
==============================================================================
slouken@2765
   174
Notes -- iPhone SDL limitations
slouken@2765
   175
==============================================================================
slouken@2765
   176
slouken@2765
   177
Windows:
slouken@6589
   178
	Full-size, single window applications only.  You cannot create multi-window SDL applications for iPhone OS.  The application window will fill the display, though you have the option of turning on or off the menu-bar (pass SDL_CreateWindow the flag SDL_WINDOW_BORDERLESS).
slouken@2765
   179
slouken@2765
   180
Textures:
slouken@6589
   181
	The optimal texture formats on iOS are SDL_PIXELFORMAT_ABGR8888, SDL_PIXELFORMAT_ABGR8888, SDL_PIXELFORMAT_BGR888, and SDL_PIXELFORMAT_RGB24 pixel formats.
slouken@2765
   182
slouken@2765
   183
Loading Shared Objects:
slouken@2765
   184
	This is disabled by default since it seems to break the terms of the iPhone SDK agreement.  It can be re-enabled in SDL_config_iphoneos.h.
slouken@2765
   185
gabomdq@6534
   186
==============================================================================
gabomdq@6534
   187
Game Center 
gabomdq@6534
   188
==============================================================================
gabomdq@6534
   189
gabomdq@6534
   190
Game Center integration requires that you break up your main loop in order to yield control back to the system. In other words, instead of running an endless main loop, you run each frame in a callback function, using:
gabomdq@6534
   191
    
gabomdq@6534
   192
int SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam);
gabomdq@6534
   193
gabomdq@6534
   194
This will set up the given function to be called back on the animation callback, and then you have to return from main() to let the Cocoa event loop run.
gabomdq@6534
   195
gabomdq@6534
   196
e.g.
gabomdq@6534
   197
gabomdq@6534
   198
extern "C"
gabomdq@6534
   199
void ShowFrame(void*)
gabomdq@6534
   200
{
slouken@6589
   201
    ... do event handling, frame logic and rendering
gabomdq@6534
   202
}
gabomdq@6534
   203
gabomdq@6534
   204
int main(int argc, char *argv[])
gabomdq@6534
   205
{
gabomdq@6534
   206
   ... initialize game ...
gabomdq@6534
   207
gabomdq@6534
   208
#if __IPHONEOS__
gabomdq@6534
   209
        // Initialize the Game Center for scoring and matchmaking
gabomdq@6534
   210
        InitGameCenter();
gabomdq@6534
   211
gabomdq@6534
   212
        // Set up the game to run in the window animation callback on iOS
gabomdq@6534
   213
        // so that Game Center and so forth works correctly.
slouken@6589
   214
        SDL_iPhoneSetAnimationCallback(window, 1, ShowFrame, NULL);
gabomdq@6534
   215
#else
slouken@6589
   216
        while ( running ) {
gabomdq@6534
   217
                ShowFrame(0);
gabomdq@6534
   218
                DelayFrame();
gabomdq@6534
   219
        }
gabomdq@6534
   220
#endif
gabomdq@6534
   221
        return 0;
slouken@6589
   222
}