Page 1 of 1

Trouble using SDL_DisplayFormat()

Posted: Wed Mar 23, 2011 6:15 pm
by Irony
Hey, so I've been trying to implement the SDL library, but I've run into a problem: SDL_DisplayFormat() isn't working! I stepped through my code, and the image seems to be loaded into temp, but when it gets passed to SDL_DisplayFormat, the function returns NULL. Any idea why this is? Or is there a way to further debug? It seemed to be working earlier, but I can't figure out what broke it.
temp and surface.sourceImg are SDL_Surface*.

Code: Select all

bool loadImage(string fileName, Image& surface, SDL_Rect tileSize, SDL_Rect imageSize)
{
	// Load the un-optimized picture
	SDL_Surface* temp = IMG_Load(fileName.c_str());
	if(temp == NULL)
	{
		surface.sourceImg = NULL;
		return false;
	}
	
	// Load the optimized picture
	surface.sourceImg = SDL_DisplayFormat(temp);
	if(surface.sourceImg == NULL)
	{
		SDL_FreeSurface(temp);
		temp = NULL;
		return false;
	}
	SDL_FreeSurface(temp);

	// Initialize the rest of the Image class
	imageSize.x = (imageSize.x / tileSize.x);	// Convert from pixels to tiles
	imageSize.y = (imageSize.y / tileSize.y);
	surface.tileSize = tileSize;				// Copy the tile size
	surface.tileCount =  imageSize.x * imageSize.y;	// Calculate the tile count
	surface.tiles[surface.tileCount];			// Initialize the tile array
	for(int i = 0; i < surface.tileCount; i++)	// Setup all the coordinates
	{
		surface.tiles[i].x = i % imageSize.x;
		surface.tiles[i].y = i / imageSize.y;
	}
	return true;
}

Re: Trouble using SDL_DisplayFormat()

Posted: Wed Mar 23, 2011 8:01 pm
by dandymcgee
Are you calling this after SDL_Init()?

Re: Trouble using SDL_DisplayFormat()

Posted: Wed Mar 23, 2011 8:44 pm
by Irony
Yes. I made sure SDL_Init was called first, in this function:

Code: Select all

int initializeGraphics()
{
	// Initialize the SDL
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1) return 5;

	// Load the graphics files (TODO: Read this information from a text file)
	string fileName = "newTerrain.png";
	SDL_Rect imgSize;
	imgSize.x = imgSize.y = 64;
	SDL_Rect tileSize;
	tileSize.x = tileSize.y = 16;
	if(!loadImage(fileName, terrainSheet, tileSize, imgSize)) return 10;

	// Initialize the screen
	screen = SDL_SetVideoMode( 640, 400, 32, SDL_SWSURFACE );
	if(screen == NULL) return 15;

	return 0;
}
The weird return values were just to figure out where it was breaking.

Re: Trouble using SDL_DisplayFormat()

Posted: Thu Mar 24, 2011 10:23 am
by dr-snipe
How big is the image you're trying to load? I've had a similar problem before and I believe I may have been trying to load too big of an image (750x650 or something like that).

Also, did you initiate the image format you're using for SDL_image?

Re: Trouble using SDL_DisplayFormat()

Posted: Thu Mar 24, 2011 11:37 am
by Web
I'm kinda new to using SDL but kinda sounds like a linking problem, are you sure you have -lSDL_Image set up right?

Re: Trouble using SDL_DisplayFormat()

Posted: Thu Mar 24, 2011 12:49 pm
by TheBuzzSaw
SDL_DisplayFormat works by converting the target surface to a surface compatible with the current display. So, you cannot call it before you have called SDL_SetVideoMode. Load your image after that call, and everything should work fine.

Also, use SDL_DisplayFormatAlpha on images loaded using IMG_Load.

Re: Trouble using SDL_DisplayFormat()

Posted: Thu Mar 24, 2011 1:50 pm
by Irony
TheBuzzSaw wrote:SDL_DisplayFormat works by converting the target surface to a surface compatible with the current display. So, you cannot call it before you have called SDL_SetVideoMode. Load your image after that call, and everything should work fine.

Also, use SDL_DisplayFormatAlpha on images loaded using IMG_Load.
Sweet! That fixed it. And I assume you meant I should replace SDL_DisplayFormat instead of IMG_Load?

Also, while the program appears to compile successfully, I get a lot of messages like:
'MonkeyLabs.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Cannot find or open the PDB file
'MonkeyLabs.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Cannot find or open the PDB file
Any idea why this is happening?

Re: Trouble using SDL_DisplayFormat()

Posted: Thu Mar 24, 2011 2:21 pm
by TheBuzzSaw
No. I am saying that if you use IMG_Load, you should use SDL_DisplayFormatAlpha instead of SDL_DisplayFormat. Since IMG_Load can load transparent formats (GIF/PNG), using SDL_DisplayFormatAlpha will properly enable those transparencies. On non-transparent formats, it'll work fine too, so it's good to just be consistent.