Sign in to follow this  
Jaqen

Need help using Xcode+SDL

Recommended Posts

I've been sitting here beating myself up trying to figure out why I couldn't get an image to draw after deciding to setup SDL on my macbook so I could start programming on that. Nothing I did would work. I didn't see a thing wrong so I finally decided to try the exact same code on my windows setup and it worked perfectly, loading the image right away. Is there something I'm missing that's specific to Xcode? Do I have to load the images into Xcode as a resource or something? I've tried both types of paths such as "Users/Me/Desktop/program/images/foo.png" and just "images/foo.png". I can't even tell if it's finding the image but I assume it is. Inside main() is this:
Engine myEngine(1024, 768, 32);
Image myImage("images/foo.png");
myImage.draw(0, 0, myEngine.getScreen());
SDL_Flip(myEngine.getScreen());

(and yeah I loaded up SDL Image framework in it) This is my first time using Xcode and trying to program on mac and so far it's not going too well. It doesn't seem like Xcode is a very good IDE at all, but maybe there's more to learn about it that will make it more user friendly. Thanks in advance!

Share this post


Link to post
Share on other sites
Quote:
Do I have to load the images into Xcode as a resource or something?
No, you can load your resources from disk in the same way you would in Windows.
Quote:
I can't even tell if it's finding the image but I assume it is.
Hm, I don't get this part. How can you not tell if it's finding the image? How are you loading the images exactly? Almost any API will generate some sort of error message if the file in question can't be found or fails to load for some reason.
Quote:
This is my first time using Xcode and trying to program on mac and so far it's not going too well. It doesn't seem like Xcode is a very good IDE at all, but maybe there's more to learn about it that will make it more user friendly.
I can sympathize - the Mac development environment can be a bit tricky in some ways.

Without more information I can only venture a guess here, but it sounds to me as if it might be a working directory issue (that is, your application is looking for the files in a directory other than the one you've placed them in). It looks like you tried absolute paths also, but again, without more info I can't really tell you why that didn't work.

The SDL 'main' function sets the working directory manually on start up, and IINM it sets it to the same directory that the executable or application resides in (for example, build/Debug). If you haven't already, you might try moving your files there and see if it helps.

If that doesn't work, perhaps you could provide some more information, such as how you're loading your images and what sort of error message you're getting (if any).

Share this post


Link to post
Share on other sites
main.cpp

int main(int argc, char *argv[])
{
Engine myEngine(1024, 768, 32);

Image myImage("images/foo.png");
myImage.draw(0, 0, myEngine.getScreen());
SDL_Flip(myEngine.getScreen());

SDL_Delay(5000);

SDL_Quit();
return(0);
}






engine.cpp

Engine::Engine(int screenWidth, int screenHeight, int screenBPP)
: isRunning(true), screenWidth(screenWidth), screenHeight(screenHeight), screenBPP(screenBPP), screen(SDL_SetVideoMode(screenWidth, screenHeight, screenBPP, SDL_SWSURFACE))
{
// Init all SDL.
SDL_Init(SDL_INIT_EVERYTHING);

SDL_WM_SetCaption("Foo", NULL);
}

SDL_Surface *Engine::getScreen()
{
return screen;
}






image.cpp

Image::Image(string filename)
: filename(filename)
{
surface = loadImage();
}

SDL_Surface *Image::loadImage()
{
SDL_Surface *loadedImage = NULL;
SDL_Surface *optimizedImage = NULL;

loadedImage = IMG_Load(filename.c_str());

if(loadedImage != NULL)
{
// Create an optimized image.
optimizedImage = SDL_DisplayFormat(loadedImage);

// Free the old image.
SDL_FreeSurface(loadedImage);
}

return optimizedImage;
}

void Image::draw(int x, int y, SDL_Surface* destination)
{
SDL_Rect offset;

offset.x = x;
offset.y = y;

// Apply the image to the surface.
SDL_BlitSurface(
surface, // Source surface
NULL,
destination, // Destination surface
&offset // Where on the destination the source will be applied to.
);
}

SDL_Surface *Image::getSurface()
{
return surface;
}






And yeah the code is mostly from the Lazy Foo tutorials.

I'm also a bit confused on something else. Xcode created SDLMain.m with all this prewritten code but then I also have my own main.cpp with its own main() method. Would that be interfering with the one in SDLMain.m?

What directory do you guys usually put your images and stuff in? Mine are just in a folder called images in the root directory of my project folder created by Xcode, along with my source files.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jaqen
I'm also a bit confused on something else. Xcode created SDLMain.m with all this prewritten code but then I also have my own main.cpp with its own main() method. Would that be interfering with the one in SDLMain.m?
SDL does some truly horrific things to try and end up with an identical main function on all platforms (they do the same with WinMain on Windows). Suffice it to say, that the main function in SDLmain.m is the one which will be called, and that will setup all the fancy OS X stuff, and then call your main function.
Quote:
What directory do you guys usually put your images and stuff in? Mine are just in a folder called images in the root directory of my project folder created by Xcode, along with my source files.
By default, SDLmain.m sets the working directory to whatever directory the application is run from, and in the case of XCode, that would be either Build/Debug/ or Build/Release. From either of those locations, if your images folder is in the root directory of project, then you need to load your image like so: '../../images/foo.png'.

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
By default, SDLmain.m sets the working directory to whatever directory the application is run from, and in the case of XCode, that would be either Build/Debug/ or Build/Release. From either of those locations, if your images folder is in the root directory of project, then you need to load your image like so: '../../images/foo.png'.


Thank you! This fixed it. Now I feel dumb.

However these paths seem like a bad idea because I'm guessing that if I ever distributed a release version, the path would still be ../../ so it'd be a good idea to have my images folder in both debug and release folders?

Share this post


Link to post
Share on other sites
Quote:
Original post by Jaqen
However these paths seem like a bad idea because I'm guessing that if I ever distributed a release version, the path would still be ../../ so it'd be a good idea to have my images folder in both debug and release folders?
You could also edit SDLMain.m to set the working directory to the application's internal 'Resources' directory, and add a 'Copy Files' build phase to the target in XCode, to copy the images directory into the app bundle. This is the preferred setup for when you distribute, but if you have large data directories, copying them every build during development can slow things down.

Share this post


Link to post
Share on other sites

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