Sign in to follow this  

SDL Error Code

This topic is 3855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I copied the local "How to make tetris in an hour" SDL version, stuck it into Xcode, and built it. The following error message showed up after building:
GameInit > File not Found: blocks.bmp
OnInit > Game Init failed!
I found the code that was causing the problem:
	surfBlocks = SDL_LoadBMP("blocks.bmp");

	if(!surfBlocks) {
		cout << "GameInit > File not Found: blocks.bmp" << endl;
		return false;
	}
I stuck the "blocks.bmp" file into my project and tried it again. No beans. So where does "SDL_LoadBMP()" look when finding bitmaps? From one website, it says it's under ~.app/Contents/Resources/ , and when I looked there, the file was right there!

Share this post


Link to post
Share on other sites
Quote:
Original post by Replicon
If there's no path, it should look in the same directory as your exe, unless it changes its current working directory behind the scenes.


What do I do if there is a path/ can I create a path?

Share this post


Link to post
Share on other sites
It will look in the directory enclosing your .app by default. That is, if your app is in /Users/pie/Code/Foobar/build, then that's where it will look for your data.

If you give it a relative path ("data/foobar.png"), it will look in the path relative to your .app.

You can modify the SDLmain.m file to make it look inside the application bundle (so the data directory comes along with your application), but I generally only do this for release builds.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ravuya
It will look in the directory enclosing your .app by default. That is, if your app is in /Users/pie/Code/Foobar/build, then that's where it will look for your data.

If you give it a relative path ("data/foobar.png"), it will look in the path relative to your .app.

You can modify the SDLmain.m file to make it look inside the application bundle (so the data directory comes along with your application), but I generally only do this for release builds.


Thanks for the help. How would I make it look inside the application? And how do I get it to find the file in debug mode?

Share this post


Link to post
Share on other sites
in the directory the .xcode is during build xcode will create another folder called "build". in this folder it will create:

debug
or release
or default

folder. in this folder your app will appear after successfull building it. if it's the debug,release or default folder is effected by your current target, selected in xcode. you can set your current build configuration in project/set build configuration.

your working directory seems to be set in this function:


- (void) setupWorkingDirectory:(BOOL)shouldChdir
{
if (shouldChdir)
{
char parentdir[MAXPATHLEN];
CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
if (CFURLGetFileSystemRepresentation(url2, true, (UInt8 *)parentdir, MAXPATHLEN)) {
assert ( chdir (parentdir) == 0 ); /* chdir to the binary app's parent */
}
CFRelease(url);
CFRelease(url2);
}

}


don't know if you got the same SDLmain.m. but maybe just use the search engine to get more info about "CFBundleCopyBundleURL".

EDIT: in the end you just need to change parentdir. parentdir should be created with CFURLGetFileSystemRepresentation. if you commend the url2 line out and use url in CFURLGetFileSystemRepresentation your working directory should be set in the app. just try it out.

otherwise just place all resources beside the .app!



[Edited by - Eitsch on May 28, 2007 3:38:50 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Eitsch
in the directory the .xcode is during build xcode will create another folder called "build". in this folder it will create:

debug
or release
or default

folder. in this folder your app will appear after successfull building it. if it's the debug,release or default folder is effected by your current target, selected in xcode. you can set your current build configuration in project/set build configuration.

your working directory seems to be set in this function:


- (void) setupWorkingDirectory:(BOOL)shouldChdir
{
if (shouldChdir)
{
char parentdir[MAXPATHLEN];
CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
if (CFURLGetFileSystemRepresentation(url2, true, (UInt8 *)parentdir, MAXPATHLEN)) {
assert ( chdir (parentdir) == 0 ); /* chdir to the binary app's parent */
}
CFRelease(url);
CFRelease(url2);
}

}


don't know if you got the same SDLmain.m. but maybe just use the search engine to get more info about "CFBundleCopyBundleURL". otherwise just place all resources beside the .app!


Ok I didn't understand most of that... but I managed to figure it out from what I could understand...

Share this post


Link to post
Share on other sites
In order to copy the file into your .app when it is built, you must add it to a Copy Files build step.

Open your target, go down to Build Steps, and there should be a build step for Copy Files. I think the SDL Xcode template produces one Copy Files step for Resources and another one to copy the frameworks into Frameworks. Since your file is already inside the Resources directory in the bundle, it doesn't look like you have to do this step.

You'll probably need to change the code that Eitsch provided to chdir into the Resources folder of the bundle rather than the root directory of the bundle. That's easy; just change his code to copy the resources URL of the bundle instead of the bundle URL, like so:
- (void) setupWorkingDirectory:(BOOL)shouldChdir
{
if (shouldChdir)
{
char parentdir[MAXPATHLEN];
CFURLRef url = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle()); /* get the resources component instead of the main bundle URL */
if (CFURLGetFileSystemRepresentation(url, true, (UInt8 *)parentdir, MAXPATHLEN)) { /* convert to a path */
assert ( chdir (parentdir) == 0 ); /* chdir to the Resources component of the bundle */
}
CFRelease(url);
CFRelease(url2);
}
}

IIRC, this should work. I'm away from my Mac so I can't confirm if this compiles properly, but I've used something similar in the past. Check out the CFBundle reference documentation for more fun stuff you can do with app bundles.

Also, don't forget that shouldChdir won't be true in this code when you run it from Xcode! You'll have to modify the call to setupWorkingDirectory (in the same file) to pass YES instead of its normal argument.

Share this post


Link to post
Share on other sites

This topic is 3855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this