docs/README-macosx.md
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Thu, 21 May 2015 21:25:14 +0200
changeset 9609 897b25f560d0
parent 9066 c2af3ff967cc
child 9774 3478d2a45b31
permissions -rw-r--r--
Fixed undefined key access in test program.
gabomdq@9023
     1
Mac OS X
gabomdq@9023
     2
==============================================================================
gabomdq@9023
     3
gabomdq@9023
     4
These instructions are for people using Apple's Mac OS X (pronounced
gabomdq@9023
     5
"ten").
gabomdq@9023
     6
gabomdq@9023
     7
From the developer's point of view, OS X is a sort of hybrid Mac and
gabomdq@9023
     8
Unix system, and you have the option of using either traditional
gabomdq@9023
     9
command line tools or Apple's IDE Xcode.
gabomdq@9023
    10
gabomdq@9023
    11
To build SDL using the command line, use the standard configure and make
gabomdq@9023
    12
process:
gabomdq@9023
    13
gabomdq@9023
    14
	./configure
gabomdq@9023
    15
	make
gabomdq@9023
    16
	sudo make install
gabomdq@9023
    17
gabomdq@9023
    18
You can also build SDL as a Universal library (a single binary for both
gabomdq@9023
    19
32-bit and 64-bit Intel architectures), on Mac OS X 10.7 and newer, by using
gabomdq@9023
    20
the fatbuild.sh script in build-scripts:
gabomdq@9023
    21
	sh build-scripts/fatbuild.sh
gabomdq@9023
    22
	sudo build-scripts/fatbuild.sh install
gabomdq@9023
    23
This script builds SDL with 10.5 ABI compatibility on i386 and 10.6
gabomdq@9023
    24
ABI compatibility on x86_64 architectures.  For best compatibility you
gabomdq@9023
    25
should compile your application the same way.  A script which wraps
gabomdq@9023
    26
gcc to make this easy is provided in test/gcc-fat.sh
gabomdq@9023
    27
gabomdq@9023
    28
Please note that building SDL requires at least Xcode 4.6 and the 10.7 SDK
gabomdq@9023
    29
(even if you target back to 10.5 systems). PowerPC support for Mac OS X has
gabomdq@9023
    30
been officially dropped as of SDL 2.0.2.
gabomdq@9023
    31
gabomdq@9023
    32
To use the library once it's built, you essential have two possibilities:
gabomdq@9023
    33
use the traditional autoconf/automake/make method, or use Xcode.
gabomdq@9023
    34
gabomdq@9023
    35
==============================================================================
gabomdq@9023
    36
Caveats for using SDL with Mac OS X
gabomdq@9023
    37
==============================================================================
gabomdq@9023
    38
gabomdq@9023
    39
Some things you have to be aware of when using SDL on Mac OS X:
gabomdq@9023
    40
gabomdq@9023
    41
- If you register your own NSApplicationDelegate (using [NSApp setDelegate:]),
gabomdq@9023
    42
  SDL will not register its own. This means that SDL will not terminate using
gabomdq@9023
    43
  SDL_Quit if it receives a termination request, it will terminate like a 
gabomdq@9023
    44
  normal app, and it will not send a SDL_DROPFILE when you request to open a
gabomdq@9023
    45
  file with the app. To solve these issues, put the following code in your 
gabomdq@9023
    46
  NSApplicationDelegate implementation:
gabomdq@9023
    47
gabomdq@9023
    48
philipp@9066
    49
    - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
philipp@9066
    50
    {
philipp@9066
    51
        if (SDL_GetEventState(SDL_QUIT) == SDL_ENABLE) {
philipp@9066
    52
            SDL_Event event;
philipp@9066
    53
            event.type = SDL_QUIT;
philipp@9066
    54
            SDL_PushEvent(&event);
philipp@9066
    55
        }
philipp@9066
    56
    
philipp@9066
    57
        return NSTerminateCancel;
philipp@9066
    58
    }
philipp@9066
    59
    
philipp@9066
    60
    - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
philipp@9066
    61
    {
philipp@9066
    62
        if (SDL_GetEventState(SDL_DROPFILE) == SDL_ENABLE) {
philipp@9066
    63
            SDL_Event event;
philipp@9066
    64
            event.type = SDL_DROPFILE;
philipp@9066
    65
            event.drop.file = SDL_strdup([filename UTF8String]);
philipp@9066
    66
            return (SDL_PushEvent(&event) > 0);
philipp@9066
    67
        }
philipp@9066
    68
    
philipp@9066
    69
        return NO;
philipp@9066
    70
    }
gabomdq@9023
    71
gabomdq@9023
    72
==============================================================================
gabomdq@9023
    73
Using the Simple DirectMedia Layer with a traditional Makefile
gabomdq@9023
    74
==============================================================================
gabomdq@9023
    75
gabomdq@9023
    76
An existing autoconf/automake build system for your SDL app has good chances
gabomdq@9023
    77
to work almost unchanged on OS X. However, to produce a "real" Mac OS X binary
gabomdq@9023
    78
that you can distribute to users, you need to put the generated binary into a
gabomdq@9023
    79
so called "bundle", which basically is a fancy folder with a name like
gabomdq@9023
    80
"MyCoolGame.app".
gabomdq@9023
    81
gabomdq@9023
    82
To get this build automatically, add something like the following rule to
gabomdq@9023
    83
your Makefile.am:
gabomdq@9023
    84
gabomdq@9023
    85
bundle_contents = APP_NAME.app/Contents
gabomdq@9023
    86
APP_NAME_bundle: EXE_NAME
gabomdq@9023
    87
	mkdir -p $(bundle_contents)/MacOS
gabomdq@9023
    88
	mkdir -p $(bundle_contents)/Resources
gabomdq@9023
    89
	echo "APPL????" > $(bundle_contents)/PkgInfo
gabomdq@9023
    90
	$(INSTALL_PROGRAM) $< $(bundle_contents)/MacOS/
gabomdq@9023
    91
gabomdq@9023
    92
You should replace EXE_NAME with the name of the executable. APP_NAME is what
gabomdq@9023
    93
will be visible to the user in the Finder. Usually it will be the same
gabomdq@9023
    94
as EXE_NAME but capitalized. E.g. if EXE_NAME is "testgame" then APP_NAME 
gabomdq@9023
    95
usually is "TestGame". You might also want to use @PACKAGE@ to use the package
gabomdq@9023
    96
name as specified in your configure.in file.
gabomdq@9023
    97
gabomdq@9023
    98
If your project builds more than one application, you will have to do a bit
gabomdq@9023
    99
more. For each of your target applications, you need a separate rule.
gabomdq@9023
   100
gabomdq@9023
   101
If you want the created bundles to be installed, you may want to add this
gabomdq@9023
   102
rule to your Makefile.am:
gabomdq@9023
   103
gabomdq@9023
   104
install-exec-hook: APP_NAME_bundle
gabomdq@9023
   105
	rm -rf $(DESTDIR)$(prefix)/Applications/APP_NAME.app
gabomdq@9023
   106
	mkdir -p $(DESTDIR)$(prefix)/Applications/
gabomdq@9023
   107
	cp -r $< /$(DESTDIR)$(prefix)Applications/
gabomdq@9023
   108
gabomdq@9023
   109
This rule takes the Bundle created by the rule from step 3 and installs them
gabomdq@9023
   110
into $(DESTDIR)$(prefix)/Applications/.
gabomdq@9023
   111
gabomdq@9023
   112
Again, if you want to install multiple applications, you will have to augment
gabomdq@9023
   113
the make rule accordingly.
gabomdq@9023
   114
gabomdq@9023
   115
gabomdq@9023
   116
But beware! That is only part of the story! With the above, you end up with
gabomdq@9023
   117
a bare bone .app bundle, which is double clickable from the Finder. But
gabomdq@9023
   118
there are some more things you should do before shipping your product...
gabomdq@9023
   119
gabomdq@9023
   120
1) The bundle right now probably is dynamically linked against SDL. That 
gabomdq@9023
   121
   means that when you copy it to another computer, *it will not run*,
gabomdq@9023
   122
   unless you also install SDL on that other computer. A good solution
gabomdq@9023
   123
   for this dilemma is to static link against SDL. On OS X, you can
gabomdq@9023
   124
   achieve that by linking against the libraries listed by
gabomdq@9023
   125
     sdl-config --static-libs
gabomdq@9023
   126
   instead of those listed by
gabomdq@9023
   127
     sdl-config --libs
gabomdq@9023
   128
   Depending on how exactly SDL is integrated into your build systems, the
gabomdq@9023
   129
   way to achieve that varies, so I won't describe it here in detail
gabomdq@9023
   130
2) Add an 'Info.plist' to your application. That is a special XML file which
gabomdq@9023
   131
   contains some meta-information about your application (like some copyright
gabomdq@9023
   132
   information, the version of your app, the name of an optional icon file,
gabomdq@9023
   133
   and other things). Part of that information is displayed by the Finder
gabomdq@9023
   134
   when you click on the .app, or if you look at the "Get Info" window.
gabomdq@9023
   135
   More information about Info.plist files can be found on Apple's homepage.
gabomdq@9023
   136
gabomdq@9023
   137
gabomdq@9023
   138
As a final remark, let me add that I use some of the techniques (and some
gabomdq@9023
   139
variations of them) in Exult and ScummVM; both are available in source on
gabomdq@9023
   140
the net, so feel free to take a peek at them for inspiration!
gabomdq@9023
   141
gabomdq@9023
   142
gabomdq@9023
   143
==============================================================================
gabomdq@9023
   144
Using the Simple DirectMedia Layer with Xcode
gabomdq@9023
   145
==============================================================================
gabomdq@9023
   146
gabomdq@9023
   147
These instructions are for using Apple's Xcode IDE to build SDL applications.
gabomdq@9023
   148
gabomdq@9023
   149
- First steps
gabomdq@9023
   150
gabomdq@9023
   151
The first thing to do is to unpack the Xcode.tar.gz archive in the
gabomdq@9023
   152
top level SDL directory (where the Xcode.tar.gz archive resides).
gabomdq@9023
   153
Because Stuffit Expander will unpack the archive into a subdirectory,
gabomdq@9023
   154
you should unpack the archive manually from the command line:
gabomdq@9023
   155
	cd [path_to_SDL_source]
gabomdq@9023
   156
	tar zxf Xcode.tar.gz
gabomdq@9023
   157
This will create a new folder called Xcode, which you can browse
gabomdq@9023
   158
normally from the Finder.
gabomdq@9023
   159
gabomdq@9023
   160
- Building the Framework
gabomdq@9023
   161
gabomdq@9023
   162
The SDL Library is packaged as a framework bundle, an organized
gabomdq@9023
   163
relocatable folder hierarchy of executable code, interface headers,
gabomdq@9023
   164
and additional resources. For practical purposes, you can think of a 
gabomdq@9023
   165
framework as a more user and system-friendly shared library, whose library
gabomdq@9023
   166
file behaves more or less like a standard UNIX shared library.
gabomdq@9023
   167
gabomdq@9023
   168
To build the framework, simply open the framework project and build it. 
gabomdq@9023
   169
By default, the framework bundle "SDL.framework" is installed in 
gabomdq@9023
   170
/Library/Frameworks. Therefore, the testers and project stationary expect
gabomdq@9023
   171
it to be located there. However, it will function the same in any of the
gabomdq@9023
   172
following locations:
gabomdq@9023
   173
gabomdq@9023
   174
    ~/Library/Frameworks
gabomdq@9023
   175
    /Local/Library/Frameworks
gabomdq@9023
   176
    /System/Library/Frameworks
gabomdq@9023
   177
gabomdq@9023
   178
- Build Options
gabomdq@9023
   179
    There are two "Build Styles" (See the "Targets" tab) for SDL.
gabomdq@9023
   180
    "Deployment" should be used if you aren't tweaking the SDL library.
gabomdq@9023
   181
    "Development" should be used to debug SDL apps or the library itself.
gabomdq@9023
   182
gabomdq@9023
   183
- Building the Testers
gabomdq@9023
   184
    Open the SDLTest project and build away!
gabomdq@9023
   185
gabomdq@9023
   186
- Using the Project Stationary
gabomdq@9023
   187
    Copy the stationary to the indicated folders to access it from
gabomdq@9023
   188
    the "New Project" and "Add target" menus. What could be easier?
gabomdq@9023
   189
gabomdq@9023
   190
- Setting up a new project by hand
gabomdq@9023
   191
    Some of you won't want to use the Stationary so I'll give some tips:
gabomdq@9023
   192
    * Create a new "Cocoa Application"
gabomdq@9023
   193
    * Add src/main/macosx/SDLMain.m , .h and .nib to your project
gabomdq@9023
   194
    * Remove "main.c" from your project
gabomdq@9023
   195
    * Remove "MainMenu.nib" from your project
gabomdq@9023
   196
    * Add "$(HOME)/Library/Frameworks/SDL.framework/Headers" to include path
gabomdq@9023
   197
    * Add "$(HOME)/Library/Frameworks" to the frameworks search path
gabomdq@9023
   198
    * Add "-framework SDL -framework Foundation -framework AppKit" to "OTHER_LDFLAGS"
gabomdq@9023
   199
    * Set the "Main Nib File" under "Application Settings" to "SDLMain.nib"
gabomdq@9023
   200
    * Add your files
gabomdq@9023
   201
    * Clean and build
gabomdq@9023
   202
gabomdq@9023
   203
- Building from command line
gabomdq@9023
   204
    Use pbxbuild in the same directory as your .pbproj file
gabomdq@9023
   205
gabomdq@9023
   206
- Running your app
gabomdq@9023
   207
    You can send command line args to your app by either invoking it from
gabomdq@9023
   208
    the command line (in *.app/Contents/MacOS) or by entering them in the
gabomdq@9023
   209
    "Executables" panel of the target settings.
gabomdq@9023
   210
    
gabomdq@9023
   211
- Implementation Notes
gabomdq@9023
   212
    Some things that may be of interest about how it all works...
gabomdq@9023
   213
    * Working directory
gabomdq@9023
   214
        As defined in the SDL_main.m file, the working directory of your SDL app
gabomdq@9023
   215
        is by default set to its parent. You may wish to change this to better
gabomdq@9023
   216
        suit your needs.
gabomdq@9023
   217
    * You have a Cocoa App!
gabomdq@9023
   218
        Your SDL app is essentially a Cocoa application. When your app
gabomdq@9023
   219
        starts up and the libraries finish loading, a Cocoa procedure is called,
gabomdq@9023
   220
        which sets up the working directory and calls your main() method.
gabomdq@9023
   221
        You are free to modify your Cocoa app with generally no consequence 
gabomdq@9023
   222
        to SDL. You cannot, however, easily change the SDL window itself.
gabomdq@9023
   223
        Functionality may be added in the future to help this.
gabomdq@9023
   224
gabomdq@9023
   225
gabomdq@9023
   226
Known bugs are listed in the file "BUGS"