[MacOS X] Universal binary

BlitzMax Forums/BlitzMax Beginners Area/[MacOS X] Universal binary

JonasL(Posted 2007) [#1]
I just can't figure out how to make BlitzMax build my bmx applications as universal binaries on a PPC machine. They are built as PowerPC only. I can easily build for both architechtures in XCode on a PPC. What is up with this?


JazzieB(Posted 2007) [#2]
Simple answer is that you can't ... yet. I think Mark is thinking of adding this functionality with the next OS update (Leopard). There's a thread somewhere where someone else was asking about this issue.

Ideally, you need to have both a PPC and an Intel Mac to ensure that your programs work on both platforms, as this isn't necessarily the case. Just because it compiled, doesn't mean it will run. Of course, the communtity will always be able to test for you, but I'd prefer to be able to test myself as well, as it's then quicker to make any changes.


tonyg(Posted 2007) [#3]
Doesn't this tutorial cover it?


JazzieB(Posted 2007) [#4]
What he's asking is why can't BlitzMax build a universal binary. That tutorial is great, used it myself, but he will need access to both a PowerPC and an Intel based Mac to use it.


Grey Alien(Posted 2007) [#5]
Are Universal Binaries twice the size due to having two exes (or whatever it's called on the Mac) in one or is it cleverer than that?


JazzieB(Posted 2007) [#6]
You quite literally have two EXEs, so they will be twice the size.

Because of this you need to consider whether to use Incbin or not, as all the media will be included twice as well!

I did post a question in the Mac forum whether there was any way of including the media in the application bundle (still accessible, but a lot tidier), but I didn't get a reply. Indiepath found a way to do this some time ago, but he's never revealed how - even after I asked.

Currently, I've gone back to putting all my media in a Data folder, which is then split into Gfx, Sfx, etc. I'm hoping I'll find a way to include it in the bundle before I release my next game.


Grey Alien(Posted 2007) [#7]
It's OK as I don't currently IncBin. Thanks for the clarification.


JonasL(Posted 2007) [#8]
I did post a question in the Mac forum whether there was any way of including the media in the application bundle (still accessible, but a lot tidier), but I didn't get a reply. Indiepath found a way to do this some time ago, but he's never revealed how - even after I asked.


If putting data files in the (MacOS X) application bundle and then reading them from a BMX application, as you would any file, is what you need to do, here is my solution:

1) Build your application and right-click on it in the Finder, selecting "Show Bundle Content" in the contextual menu.

2) Copy your files to the "mygame.app/Contents/Resources/" folder (e.g. copy the file "myimage.png" to "mygame.app/Contents/Resources/myimage.png").

3) Paste this function into your application:


'-- Returns the location of a resource contained in the app bundle
' @ resourceName : String - filename including filename extension
' @ subDirName   : String - subdirectory of the bundle’s resources directory
Function GetBundleResourceURL:String(resourceName:String, subDirName:String="")
	Local url:String = Left(AppFile, Instr(AppFile, ".app/", 0)) + "app/Contents/Resources/"
	If(subDirName) Then url :+ subDirName + "/"
	url :+ resourceName
	Return url
End Function

Local image:TImage = LoadImage(GetBundleResourceURL("myimage.png"))

If Not (image) Then End

Graphics 640, 480

While Not KeyHit(KEY_ESCAPE)
	Cls
	DrawImage image, 32, 32
	Flip
Wend

End

This works because application bundles are just plain folders (directories). MacOS X Finder just tricks us into believing that they are files. Also, compiling and building your BMX app doesn’t delete the application bundle and its contents, it merely replaces the executable. That is why you don’t need to copy the resource files every time you re-compile.

You should, of course, put calls to GetBundleResourceURL() inside a compile directive to check if the source is compiled on MacOS. Further, I usually keep my data in a separate data folder in the application directory during development to keep Win and Mac in sync. Also, I’m not using the above function, since it is kind of a hack. I use the proper CFBundleCopyResourceURL() and other Carbon functions. However, then you have to include a C file and besides the hack should be quite safe anyway. However, it is not production quality, but should convey the general idea of how to implement this.


JazzieB(Posted 2007) [#9]
OMG. Is it really that simple? I was going to throw a test together, but comments on other threads seemed to suggest it wasn't.

Can you write files in a similar manner?

I do as you do and am currently keeping all my resources in a seperate data folder for the time being, while I work on all three versions of the project.


JonasL(Posted 2007) [#10]
Can you write files in a similar manner?


Yes, but you should never write to the application bundle (unless you really, really want more support mails than you care to handle). The reason for this is that applications have the same access permissions as the user that launch them. Applications are usually installed in the Applications folder, where only administrators have write access by default.

If you need to write data on MacOS X you should write to the user's Preferences folder or the Documents folder depending on what kind of file you are writing. A file like "mygame.ini" would go into the "/Users/jonasled/Library/Preferences/mygame/" folder and save files would go into "/Users/jonasled/Documents/mygame User Data/" or any location that the user selects. If write fails you should present the user with a standard dialog box to select a location.

I have a framework to do all of this, since doing it right, that is, the safe way, requires a lot of Carbon function calls. However, I'm using design patterns a lot so it might not be to the taste of most BMX coders. Further, I have not finished implementing equivalents for Win.

Jonas