SDL  2.0
docs/README-ios.md
Go to the documentation of this file.
1 iOS
2 ======
3 
4 ==============================================================================
5 Building the Simple DirectMedia Layer for iOS 5.1+
6 ==============================================================================
7 
8 Requirements: Mac OS X 10.8 or later and the iOS 7+ SDK.
9 
10 Instructions:
11 
12 2. Open SDL.xcodeproj (located in Xcode-iOS/SDL) in Xcode.
13 4. Select your desired target, and hit build.
14 
15 There are three build targets:
16 - libSDL.a:
17  Build SDL as a statically linked library
18 - testsdl:
19  Build a test program (there are known test failures which are fine)
20 - Template:
21  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.
22 
23 
24 ==============================================================================
25 Build SDL for iOS from the command line
26 ==============================================================================
27 
28 1. Follow step 1 above.
29 2. cd (PATH WHERE THE SDL CODE IS)/build-scripts
30 3. ./iosbuild.sh
31 
32 If everything goes fine, you should see a build/ios directory, inside there's
33 two directories "lib" and "include".
34 "include" contains a copy of the SDL headers that you'll need for your project,
35 make sure to configure XCode to look for headers there.
36 "lib" contains find two files, libSDL2.a and libSDL2main.a, you have to add both
37 to your XCode project. These libraries contain three architectures in them,
38 armv6 for legacy devices, armv7, and i386 (for the simulator).
39 By default, iosbuild.sh will autodetect the SDK version you have installed using
40 xcodebuild -showsdks, and build for iOS >= 3.0, you can override this behaviour
41 by setting the MIN_OS_VERSION variable, ie:
42 
43 MIN_OS_VERSION=4.2 ./iosbuild.sh
44 
45 ==============================================================================
46 Using the Simple DirectMedia Layer for iOS
47 ==============================================================================
48 
49 FIXME: This needs to be updated for the latest methods
50 
51 Here is the easiest method:
52 1. Build the SDL library (libSDL2.a) and the iPhone SDL Application template.
53 2. 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.
54 3. Start a new project using the template. The project should be immediately ready for use with SDL.
55 
56 Here is a more manual method:
57 1. Create a new iOS view based application.
58 2. Build the SDL static library (libSDL2.a) for iOS 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 iOS and in the iOS Simulator.
59 3. Include the SDL header files in your project.
60 4. Remove the ApplicationDelegate.h and ApplicationDelegate.m files -- SDL for iOS provides its own UIApplicationDelegate. Remove MainWindow.xib -- SDL for iOS produces its user interface programmatically.
61 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.
62 
63 ==============================================================================
64 Notes -- Retina / High-DPI and window sizes
65 ==============================================================================
66 
67 Window and display mode sizes in SDL are in "screen coordinates" (or "points",
68 in Apple's terminology) rather than in pixels. On iOS this means that a window
69 created on an iPhone 6 will have a size in screen coordinates of 375 x 667,
70 rather than a size in pixels of 750 x 1334. All iOS apps are expected to
71 size their content based on screen coordinates / points rather than pixels,
72 as this allows different iOS devices to have different pixel densities
73 (Retina versus non-Retina screens, etc.) without apps caring too much.
74 
75 By default SDL will not use the full pixel density of the screen on
76 Retina/high-dpi capable devices. Use the SDL_WINDOW_ALLOW_HIGHDPI flag when
77 creating your window to enable high-dpi support.
78 
79 When high-dpi support is enabled, SDL_GetWindowSize() and display mode sizes
80 will still be in "screen coordinates" rather than pixels, but the window will
81 have a much greater pixel density when the device supports it, and the
82 SDL_GL_GetDrawableSize() or SDL_GetRendererOutputSize() functions (depending on
83 whether raw OpenGL or the SDL_Render API is used) can be queried to determine
84 the size in pixels of the drawable screen framebuffer.
85 
86 Some OpenGL ES functions such as glViewport expect sizes in pixels rather than
87 sizes in screen coordinates. When doing 2D rendering with OpenGL ES, an
88 orthographic projection matrix using the size in screen coordinates
89 (SDL_GetWindowSize()) can be used in order to display content at the same scale
90 no matter whether a Retina device is used or not.
91 
92 ==============================================================================
93 Notes -- Application events
94 ==============================================================================
95 
96 On iOS the application goes through a fixed life cycle and you will get
97 notifications of state changes via application events. When these events
98 are delivered you must handle them in an event callback because the OS may
99 not give you any processing time after the events are delivered.
100 
101 e.g.
102 
103  int HandleAppEvents(void *userdata, SDL_Event *event)
104  {
105  switch (event->type)
106  {
107  case SDL_APP_TERMINATING:
108  /* Terminate the app.
109  Shut everything down before returning from this function.
110  */
111  return 0;
112  case SDL_APP_LOWMEMORY:
113  /* You will get this when your app is paused and iOS wants more memory.
114  Release as much memory as possible.
115  */
116  return 0;
118  /* Prepare your app to go into the background. Stop loops, etc.
119  This gets called when the user hits the home button, or gets a call.
120  */
121  return 0;
123  /* This will get called if the user accepted whatever sent your app to the background.
124  If the user got a phone call and canceled it, you'll instead get an SDL_APP_DIDENTERFOREGROUND event and restart your loops.
125  When you get this, you have 5 seconds to save all your state or the app will be terminated.
126  Your app is NOT active at this point.
127  */
128  return 0;
130  /* This call happens when your app is coming back to the foreground.
131  Restore all your state here.
132  */
133  return 0;
135  /* Restart your loops here.
136  Your app is interactive and getting CPU again.
137  */
138  return 0;
139  default:
140  /* No special processing, add it to the event queue */
141  return 1;
142  }
143  }
144 
145  int main(int argc, char *argv[])
146  {
147  SDL_SetEventFilter(HandleAppEvents, NULL);
148 
149  ... run your main loop
150 
151  return 0;
152  }
153 
154 
155 ==============================================================================
156 Notes -- Accelerometer as Joystick
157 ==============================================================================
158 
159 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.
160 
161 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.
162 
163 ==============================================================================
164 Notes -- OpenGL ES
165 ==============================================================================
166 
167 Your SDL application for iOS uses OpenGL ES for video by default.
168 
169 OpenGL ES for iOS 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().
170 
171 If your application doesn't use OpenGL's depth buffer, you may find significant performance improvement by setting SDL_GL_DEPTH_SIZE to 0.
172 
173 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 0.
174 
175 OpenGL ES on iOS doesn't use the traditional system-framebuffer setup provided in other operating systems. Special care must be taken because of this:
176 
177 - The drawable Renderbuffer must be bound to the GL_RENDERBUFFER binding point when SDL_GL_SwapWindow() is called.
178 - The drawable Framebuffer Object must be bound while rendering to the screen and when SDL_GL_SwapWindow() is called.
179 - If multisample antialiasing (MSAA) is used and glReadPixels is used on the screen, the drawable framebuffer must be resolved to the MSAA resolve framebuffer (via glBlitFramebuffer or glResolveMultisampleFramebufferAPPLE), and the MSAA resolve framebuffer must be bound to the GL_READ_FRAMEBUFFER binding point, before glReadPixels is called.
180 
181 The above objects can be obtained via SDL_GetWindowWMInfo() (in SDL_syswm.h).
182 
183 ==============================================================================
184 Notes -- Keyboard
185 ==============================================================================
186 
187 The SDL keyboard API has been extended to support on-screen keyboards:
188 
189 void SDL_StartTextInput()
190  -- enables text events and reveals the onscreen keyboard.
191 
192 void SDL_StopTextInput()
193  -- disables text events and hides the onscreen keyboard.
194 
196  -- returns whether or not text events are enabled (and the onscreen keyboard is visible)
197 
198 
199 ==============================================================================
200 Notes -- Reading and Writing files
201 ==============================================================================
202 
203 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.
204 
205 Once your application is installed its directory tree looks like:
206 
207  MySDLApp Home/
208  MySDLApp.app
209  Documents/
210  Library/
211  Preferences/
212  tmp/
213 
214 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".
215 
216 More information on this subject is available here:
217 http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html
218 
219 ==============================================================================
220 Notes -- iPhone SDL limitations
221 ==============================================================================
222 
223 Windows:
224  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).
225 
226 Textures:
227  The optimal texture formats on iOS are SDL_PIXELFORMAT_ABGR8888, SDL_PIXELFORMAT_ABGR8888, SDL_PIXELFORMAT_BGR888, and SDL_PIXELFORMAT_RGB24 pixel formats.
228 
229 Loading Shared Objects:
230  This is disabled by default since it seems to break the terms of the iOS SDK agreement for iOS versions prior to iOS 8. It can be re-enabled in SDL_config_iphoneos.h.
231 
232 ==============================================================================
233 Game Center
234 ==============================================================================
235 
236 Game Center integration might require 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:
237 
238  int SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam);
239 
240 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.
241 
242 e.g.
243 
244  extern "C"
245  void ShowFrame(void*)
246  {
247  ... do event handling, frame logic and rendering ...
248  }
249 
250  int main(int argc, char *argv[])
251  {
252  ... initialize game ...
253 
254  #if __IPHONEOS__
255  // Initialize the Game Center for scoring and matchmaking
256  InitGameCenter();
257 
258  // Set up the game to run in the window animation callback on iOS
259  // so that Game Center and so forth works correctly.
261  #else
262  while ( running ) {
263  ShowFrame(0);
264  DelayFrame();
265  }
266  #endif
267  return 0;
268  }
269 
270 ==============================================================================
271 Deploying to older versions of iOS
272 ==============================================================================
273 
274 SDL supports deploying to older versions of iOS than are supported by the latest version of Xcode, all the way back to iOS 6.1
275 
276 In order to do that you need to download an older version of Xcode:
277 https://developer.apple.com/download/more/?name=Xcode
278 
279 Open the package contents of the older Xcode and your newer version of Xcode and copy over the folders in Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport
280 
281 Then open the file Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/SDKSettings.plist and add the versions of iOS you want to deploy to the key Root/DefaultProperties/DEPLOYMENT_TARGET_SUGGESTED_VALUES
282 
283 Open your project and set your deployment target to the desired version of iOS
284 
285 Finally, remove GameController from the list of frameworks linked by your application and edit the build settings for "Other Linker Flags" and add -weak_framework GameController
GLenum GLenum GLenum GLenum GLenum scale
GLuint GLenum matrix
void loop()
Definition: checkkeys.c:152
GLAPI void GLAPIENTRY glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
one
Definition: e_pow.c:78
EGLSetBlobFuncANDROID EGLGetBlobFuncANDROID get
Definition: eglext.h:470
GLdouble s
Definition: SDL_opengl.h:2063
EGLSurface EGLnsecsANDROID time
Definition: eglext.h:518
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 * e
#define GL_RENDERBUFFER
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
static int available()
Definition: video.c:356
GLfloat GLfloat GLfloat GLfloat h
struct xkb_state * state
static SDL_Event events[EVENT_BUF_SIZE]
Definition: testgesture.c:35
two
Definition: e_pow.c:78
const GLfloat * m
GLfixed units
#define SDL_CreateWindow
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
GLfixed GLfixed GLint GLint order
GLuint GLint GLboolean GLint GLenum access
GLfixed GLfixed GLint GLint GLfixed points
const GLubyte GLuint GLuint GLuint GLuint alpha GLboolean GLboolean GLboolean GLboolean alpha GLint GLint GLsizei GLsizei GLenum type GLenum GLint GLenum GLint GLint GLsizei GLsizei GLint border GLenum GLint GLint GLint GLint GLint GLsizei GLsizei height GLsizei GLsizei GLenum GLenum const GLvoid *pixels GLenum GLint GLint GLint GLint j2 GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble zFar GLenum GLenum GLint *params GLenum GLenum GLint *params GLenum GLenum GLint *params GLenum GLenum GLfloat *params GLenum GLint GLenum GLenum GLvoid *pixels GLenum GLint GLenum GLint *params GLenum GLenum GLint *params GLenum GLsizei const GLvoid *pointer GLenum GLenum const GLint *params GLenum GLfloat GLfloat GLint GLint const GLfloat *points GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat *points GLint GLfloat GLfloat GLint GLfloat GLfloat v2 GLenum GLenum const GLint *params GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble zFar GLenum GLsizei const GLuint *values GLsizei const GLuint const GLclampf *priorities GLfloat GLfloat GLfloat GLfloat w glReadPixels
Definition: SDL_glfuncs.h:342
static SDL_AudioDeviceID device
Definition: loopwave.c:37
#define SDL_GL_SetAttribute
#define SDL_GetWindowSize
GLenum GLsizei GLsizei GLint * values
#define SDL_GL_GetDrawableSize
GLenum GLenum GLuint texture
#define SDL_StopTextInput
EGLImageKHR EGLint EGLint * handle
Definition: eglext.h:937
EGLSetBlobFuncANDROID set
Definition: eglext.h:470
struct _cl_event * event
int frame
Definition: teststreaming.c:60
GLuint GLsizei const GLuint const GLintptr const GLsizeiptr * sizes
const GLubyte * c
GLenum mode
static Uint32 callback(Uint32 interval, void *param)
Definition: testtimer.c:34
GLenum GLenum variable
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
#define I(x, y, z)
Definition: SDL_test_md5.c:76
#define SDL_StartTextInput
GLsizeiptr size
#define GL_READ_FRAMEBUFFER
GLenum GLenum GLsizei const GLuint GLboolean enabled
#define SDL_GetWindowWMInfo
GLenum target
#define SDL_IPHONE_MAX_GFORCE
#define NULL
Definition: begin_code.h:164
SDL_bool
Definition: SDL_stdinc.h:139
GLuint buffer
GLboolean enable
GLint GLint GLsizei GLsizei GLsizei depth
Definition: SDL_opengl.h:1572
GLuint framebuffer
#define SDL_JoystickGetAxis
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
#define SDL_GetRendererOutputSize
EGLint EGLint * formats
Definition: eglext.h:718
#define SDL_SetEventFilter
The type used to identify a window.
Definition: SDL_sysvideo.h:73
GLuint color
General event structure.
Definition: SDL_events.h:525
GLbitfield GLuint program
#define main
Definition: SDL_main.h:104
GLuint GLenum option
EGLSurface EGLint attribute
Definition: eglext.h:263
GLboolean GLboolean GLboolean GLboolean a
GLuint in
SDL_Renderer * screen
EGLDeviceEXT * devices
Definition: eglext.h:621
GLboolean GLboolean g
#define SDL_IsTextInputActive
#define SDL_GL_SwapWindow
GLdouble GLdouble t
Definition: SDL_opengl.h:2071
GLbyte by
#define SDL_iPhoneSetAnimationCallback