Sign in to follow this  
Noegddgeon

SDL Image Not Showing Up Mac OS X

Recommended Posts

Hello, everybody. I'm using SDL on my Mac, using XCode, and when I compile the following program, making sure that "hello.bmp" is in the same folder as the program, the image does not load:
#include "SDL.h"

int main (int argc, char* args[])
{
	SDL_Surface* hello = NULL;
	SDL_Surface* screen = NULL;

	SDL_Init (SDL_INIT_EVERYTHING);
	
	screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
	
	hello = SDL_LoadBMP("hello.bmp");
	
	SDL_BlitSurface(hello, NULL, screen, NULL);
	SDL_Flip(screen);
	SDL_Delay(2000);
	
	SDL_FreeSurface(hello);
	
	SDL_Quit();
	
	return 0;
}
This is just barebone, easy SDL and the problem is surely easy to solve. Basically, the point of the program is to put a picture on the screen, a .bmp with a picture of "Hello, world" on it, and then close after two seconds. However, I just get a black screen. Help would be very much appreciated. :] Colton

Share this post


Link to post
Share on other sites
Most likely, the working directory is setup incorrectly, so that the application doesn't see the bmp file at all.

XCode generally sets the working directory to the build directory (i.e. Build/Debug or Build/Release), although this varies by version. If you launch your application from the Finder, SDL will instead set the working directory to the directory the app bundle resides in.

You can print out the result of getcwd() to make sure that the working directory is in fact what you expect it to be.

Share this post


Link to post
Share on other sites
Also, be sure to check return values, error codes, and error messages where appropriate. (For example, SDL_LoadBMP() will return NULL if an error has occurred, at which point you can most likely get more information about the error using SDL_GetError()).

Share this post


Link to post
Share on other sites
swiftcoder,

Thanks for your reply. I recoded the program like this:


#include "SDL.h"
#include <iostream>

using namespace std;

int main (int argc, char* args[])
{
SDL_Surface* hello = NULL;
SDL_Surface* screen = NULL;
char title[50] = "";

SDL_Init (SDL_INIT_EVERYTHING);

screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);

hello = SDL_LoadBMP("hello.bmp");

SDL_BlitSurface(hello, NULL, screen, NULL);
SDL_Flip(screen);
cout << getcwd(title, 120) << endl;
SDL_Delay(2000);

SDL_FreeSurface(hello);

SDL_Quit();

return 0;
}


It didn't show any output through Terminal or the Console, and you'll have to forgive my noob skills with C++ :p. I'm sure I coded it incorrectly. Please tell me how I may recode this to output the current directory while running the program. I appreciate your time.

Colton

Share this post


Link to post
Share on other sites
Okay, I take that back, it actually did show me the working directory. It was saying that it's the "Default" directory, where my app bundle is located. There, I placed the .bmp file, and yet it still doesn't show it when I run the program.

Hmm... :/ ?

I appreciate all of your guys' help. jyk, I just saw your post and I think I should try that LoadBMP function. However, I don't know how to use it to test for errors. I would very much appreciate if you could tell me how to do so.

Thanks much. :]

Colton

Share this post


Link to post
Share on other sites
Quote:
jyk, I just saw your post and I think I should try that LoadBMP function. However, I don't know how to use it to test for errors. I would very much appreciate if you could tell me how to do so.
Well, you're already using the 'load BMP' function :-) Anyway, a simple error test would look like this:
hello = SDL_LoadBMP("hello.bmp");
if (hello == NULL) {
std::cout << "The image 'hello.bmp' could not be loaded due to the following SDL error: "
<< SDL_GetError() << std::endl;
return 1; // Or whatever you want to do here
}

Share this post


Link to post
Share on other sites
jyk, what you suggested was perfect. It didn't solve the problem, but it helped me to pinpoint it. :]

Here's the Console output for the program upon executing it once more:

2010-02-11 11:41:22.961 Load_Image1[3279:10b] Warning once: This application, or a library it uses, is using NSQuickDrawView, which has been deprecated. Apps should cease use of QuickDraw and move to Quartz.
Load_Image1(3279,0xa0580720) malloc: *** mmap(size=4294737920) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
The image could not be located due to the following error: Out of memory

The Debugger has exited with status 1.The Debugger has exited with status 1.

***

I understand that the NSQuickDrawView stuff doesn't have to deal with what the true problem is. At first, I found that it wasn't saved as a proper .bmp file. I fixed that, but now the aforementioned problem exists. What can I do to fix it?

I appreciate your help very much.

Colton

Share this post


Link to post
Share on other sites
For another reference to go by, here's the current source code of the program:


#include "SDL.h"
#include <iostream>

using namespace std;

int main (int argc, char* args[])
{
SDL_Surface* hello = NULL;
SDL_Surface* screen = NULL;
char title[50] = "";

SDL_Init (SDL_INIT_EVERYTHING);

screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);

hello = SDL_LoadBMP("hello.bmp");

if (hello == NULL)
{
cout << "The image could not be located due to the following error: "
<< SDL_GetError() << endl;

return 1;
}

SDL_BlitSurface(hello, NULL, screen, NULL);
SDL_Flip(screen);
cout << getcwd(title, 120) << endl;
SDL_Delay(2000);

SDL_FreeSurface(hello);

SDL_Quit();

return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Noegddgeon
Load_Image1(3279,0xa0580720) malloc: *** mmap(size=4294737920) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
The image could not be located due to the following error: Out of memory

The Debugger has exited with status 1.The Debugger has exited with status 1.

At first, I found that it wasn't saved as a proper .bmp file. I fixed that, but now the aforementioned problem exists. What can I do to fix it?
My best guess is that the bmp header is still messed up, causing it to try and load a much larger chunk of image from disk than is actually there.

Are you by any chance using a PPC Mac?

Share this post


Link to post
Share on other sites
swiftcoder,

No, I am using an Intel Mac. Thank you for your reply. You're probably headed in the right direction... to be honest, I'm at a loss when it comes to what it could possibly be that is messing up my program. Is there anything that you can recommend I try to fix it? :] Thank you much for your time and patience.

Colton

Share this post


Link to post
Share on other sites
Quote:
Original post by Noegddgeon
I'm at a loss when it comes to what it could possibly be that is messing up my program.
I am still banking on the BMP file being messed up. Try re-saving it in Preview, and in a non-Apple program (i.e. Photoshop) as well.

Also, what are the dimensions (width, height and channels) of the bmp file?

Share this post


Link to post
Share on other sites
swiftcoder,

I re-downloaded the .bmp and it works as of now. :] Thank you very much for your help, though... I appreciate it much. I'm hoping I start getting to answering some truly good questions sometime in the near future. :p

Colton

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