README-ios.txt
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Wed, 05 Jun 2013 12:48:44 -0700
changeset 7277 1290cd7f34af
parent 7241 28602f5ca96d
child 8477 ad08f0d710f3
permissions -rw-r--r--
Mac: Fix invalid PS3 controller mapping.

PS3 controller had swapped D-Pad down & left buttons, as well as X & Y buttons.
Thanks to Alex Szpakowski for the bug report and fix.

Fixes http://bugzilla.libsdl.org/show_bug.cgi?id=1891
slouken@7222
     1
==============================================================================
slouken@7222
     2
Building the Simple DirectMedia Layer for iPhone OS 2.0
slouken@7222
     3
==============================================================================
slouken@7222
     4
slouken@7222
     5
Requirements: Mac OS X v10.5 or later and the iPhone SDK.
slouken@7222
     6
slouken@7222
     7
Instructions:
slouken@7222
     8
1.  Open SDL.xcodeproj (located in Xcode-iOS/SDL) in XCode.
slouken@7222
     9
2.  Select your desired target, and hit build.
slouken@7222
    10
slouken@7222
    11
There are three build targets:
slouken@7222
    12
- libSDL.a:
slouken@7222
    13
	Build SDL as a statically linked library
slouken@7222
    14
- testsdl
slouken@7222
    15
	Build a test program (there are known test failures which are fine)
slouken@7222
    16
- Template:
slouken@7222
    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@7222
    18
slouken@7222
    19
==============================================================================
slouken@7222
    20
Build SDL for iOS from the command line
slouken@7222
    21
==============================================================================
slouken@7222
    22
slouken@7222
    23
1. cd (PATH WHERE THE SDL CODE IS)/build-scripts
slouken@7222
    24
2. ./iosbuild.sh
slouken@7222
    25
slouken@7222
    26
If everything goes fine, you should see a build/ios directory, inside there's
slouken@7222
    27
two directories "lib" and "include". 
slouken@7222
    28
"include" contains a copy of the SDL headers that you'll need for your project,
slouken@7222
    29
make sure to configure XCode to look for headers there.
slouken@7222
    30
"lib" contains find two files, libSDL2.a and libSDL2main.a, you have to add both 
slouken@7222
    31
to your XCode project. These libraries contain three architectures in them,
slouken@7222
    32
armv6 for legacy devices, armv7, and i386 (for the simulator).
slouken@7222
    33
By default, iosbuild.sh will autodetect the SDK version you have installed using 
slouken@7222
    34
xcodebuild -showsdks, and build for iOS >= 3.0, you can override this behaviour 
slouken@7222
    35
by setting the MIN_OS_VERSION variable, ie:
slouken@7222
    36
philipp@7241
    37
MIN_OS_VERSION=4.2 ./iosbuild.sh
slouken@7222
    38
slouken@7222
    39
==============================================================================
slouken@7222
    40
Using the Simple DirectMedia Layer for iOS
slouken@7222
    41
==============================================================================
slouken@7222
    42
slouken@7222
    43
FIXME: This needs to be updated for the latest methods
slouken@7222
    44
slouken@7222
    45
Here is the easiest method:
slouken@7222
    46
1.  Build the SDL libraries (libSDL.a and libSDLSimulator.a) and the iPhone SDL Application template.
slouken@7222
    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@7222
    48
2.  Start a new project using the template.  The project should be immediately ready for use with SDL.
slouken@7222
    49
slouken@7222
    50
Here is a more manual method:
slouken@7222
    51
1.  Create a new iPhone view based application.
slouken@7222
    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@7222
    53
3.  Include the SDL header files in your project.
slouken@7222
    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@7222
    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@7222
    56
slouken@7222
    57
==============================================================================
slouken@7222
    58
Notes -- Application events
slouken@7222
    59
==============================================================================
slouken@7222
    60
slouken@7222
    61
On iOS the application goes through a fixed life cycle and you will get
slouken@7222
    62
notifications of state changes via application events. When these events
slouken@7222
    63
are delivered you must handle them in an event callback because the OS may
slouken@7222
    64
not give you any processing time after the events are delivered.
slouken@7222
    65
slouken@7222
    66
e.g.
slouken@7222
    67
slouken@7222
    68
int HandleAppEvents(void *userdata, SDL_Event *event)
slouken@7222
    69
{
slouken@7222
    70
    switch (event->type)
slouken@7222
    71
    {
slouken@7222
    72
    case SDL_APP_TERMINATING:
slouken@7222
    73
        /* Terminate the app.
slouken@7222
    74
           Shut everything down before returning from this function.
slouken@7222
    75
        */
slouken@7222
    76
        return 0;
slouken@7222
    77
    case SDL_APP_LOWMEMORY:
slouken@7222
    78
        /* You will get this when your app is paused and iOS wants more memory.
slouken@7222
    79
           Release as much memory as possible.
slouken@7222
    80
        */
slouken@7222
    81
        return 0;
slouken@7222
    82
    case SDL_APP_WILLENTERBACKGROUND:
slouken@7222
    83
        /* Prepare your app to go into the background.  Stop loops, etc.
slouken@7222
    84
           This gets called when the user hits the home button, or gets a call.
slouken@7222
    85
        */
slouken@7222
    86
        return 0;
slouken@7222
    87
    case SDL_APP_DIDENTERBACKGROUND:
slouken@7222
    88
        /* This will get called if the user accepted whatever sent your app to the background.
slouken@7222
    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@7222
    90
           When you get this, you have 5 seconds to save all your state or the app will be terminated.
slouken@7222
    91
           Your app is NOT active at this point.
slouken@7222
    92
        */
slouken@7222
    93
        return 0;
slouken@7222
    94
    case SDL_APP_WILLENTERFOREGROUND:
slouken@7222
    95
        /* This call happens when your app is coming back to the foreground.
slouken@7222
    96
           Restore all your state here.
slouken@7222
    97
        */
slouken@7222
    98
        return 0;
slouken@7222
    99
    case SDL_APP_DIDENTERFOREGROUND:
slouken@7222
   100
        /* Restart your loops here.
slouken@7222
   101
           Your app is interactive and getting CPU again.
slouken@7222
   102
        */
slouken@7222
   103
        return 0;
slouken@7222
   104
    default:
slouken@7222
   105
        /* No special processing, add it to the event queue */
slouken@7222
   106
        return 1;
slouken@7222
   107
    }
slouken@7222
   108
}
slouken@7222
   109
slouken@7222
   110
int main(int argc, char *argv[])
slouken@7222
   111
{
slouken@7222
   112
    SDL_SetEventFilter(HandleAppEvents, NULL);
slouken@7222
   113
slouken@7222
   114
    ... run your main loop
slouken@7222
   115
slouken@7222
   116
    return 0;
slouken@7222
   117
}
slouken@7222
   118
slouken@7222
   119
    
slouken@7222
   120
==============================================================================
slouken@7222
   121
Notes -- Accelerometer as Joystick
slouken@7222
   122
==============================================================================
slouken@7222
   123
slouken@7222
   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@7222
   125
slouken@7222
   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@7222
   127
slouken@7222
   128
==============================================================================
slouken@7222
   129
Notes -- OpenGL ES
slouken@7222
   130
==============================================================================
slouken@7222
   131
slouken@7222
   132
Your SDL application for iPhone uses OpenGL ES for video by default.
slouken@7222
   133
slouken@7222
   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@7222
   135
slouken@7222
   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@7222
   137
slouken@7222
   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@7222
   139
slouken@7222
   140
==============================================================================
slouken@7222
   141
Notes -- Keyboard
slouken@7222
   142
==============================================================================
slouken@7222
   143
slouken@7222
   144
The SDL keyboard API has been extended to support on-screen keyboards:
slouken@7222
   145
slouken@7222
   146
void SDL_StartTextInput()
slouken@7222
   147
	-- enables text events and reveals the onscreen keyboard.
slouken@7222
   148
void SDL_StopTextInput()
slouken@7222
   149
	-- disables text events and hides the onscreen keyboard.
slouken@7222
   150
SDL_bool SDL_IsTextInputActive()
slouken@7222
   151
	-- returns whether or not text events are enabled (and the onscreen keyboard is visible)
slouken@7222
   152
slouken@7222
   153
==============================================================================
slouken@7222
   154
Notes -- Reading and Writing files
slouken@7222
   155
==============================================================================
slouken@7222
   156
slouken@7222
   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@7222
   158
slouken@7222
   159
Once your application is installed its directory tree looks like:
slouken@7222
   160
philipp@7241
   161
MySDLApp Home/
slouken@7222
   162
	MySDLApp.app
slouken@7222
   163
	Documents/
slouken@7222
   164
	Library/
slouken@7222
   165
		Preferences/
slouken@7222
   166
	tmp/
slouken@7222
   167
slouken@7222
   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@7222
   169
philipp@7241
   170
More information on this subject is available here:
slouken@7222
   171
http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html
slouken@7222
   172
slouken@7222
   173
==============================================================================
slouken@7222
   174
Notes -- iPhone SDL limitations
slouken@7222
   175
==============================================================================
slouken@7222
   176
slouken@7222
   177
Windows:
slouken@7222
   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@7222
   179
slouken@7222
   180
Textures:
slouken@7222
   181
	The optimal texture formats on iOS are SDL_PIXELFORMAT_ABGR8888, SDL_PIXELFORMAT_ABGR8888, SDL_PIXELFORMAT_BGR888, and SDL_PIXELFORMAT_RGB24 pixel formats.
slouken@7222
   182
slouken@7222
   183
Loading Shared Objects:
slouken@7222
   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@7222
   185
slouken@7222
   186
==============================================================================
slouken@7222
   187
Game Center 
slouken@7222
   188
==============================================================================
slouken@7222
   189
slouken@7222
   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:
slouken@7222
   191
    
slouken@7222
   192
int SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam);
slouken@7222
   193
slouken@7222
   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.
slouken@7222
   195
slouken@7222
   196
e.g.
slouken@7222
   197
slouken@7222
   198
extern "C"
slouken@7222
   199
void ShowFrame(void*)
slouken@7222
   200
{
slouken@7222
   201
    ... do event handling, frame logic and rendering
slouken@7222
   202
}
slouken@7222
   203
slouken@7222
   204
int main(int argc, char *argv[])
slouken@7222
   205
{
slouken@7222
   206
   ... initialize game ...
slouken@7222
   207
slouken@7222
   208
#if __IPHONEOS__
slouken@7222
   209
        // Initialize the Game Center for scoring and matchmaking
slouken@7222
   210
        InitGameCenter();
slouken@7222
   211
slouken@7222
   212
        // Set up the game to run in the window animation callback on iOS
slouken@7222
   213
        // so that Game Center and so forth works correctly.
slouken@7222
   214
        SDL_iPhoneSetAnimationCallback(window, 1, ShowFrame, NULL);
slouken@7222
   215
#else
slouken@7222
   216
        while ( running ) {
slouken@7222
   217
                ShowFrame(0);
slouken@7222
   218
                DelayFrame();
slouken@7222
   219
        }
slouken@7222
   220
#endif
slouken@7222
   221
        return 0;
slouken@7222
   222
}