Trouble using SDL_DisplayFormat()

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Irony
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 9
Joined: Fri Jan 28, 2011 2:23 am
Current Project: Pong
Favorite Gaming Platforms: PC
Programming Language of Choice: C++

Trouble using SDL_DisplayFormat()

Post 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;
}
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Trouble using SDL_DisplayFormat()

Post by dandymcgee »

Are you calling this after SDL_Init()?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Irony
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 9
Joined: Fri Jan 28, 2011 2:23 am
Current Project: Pong
Favorite Gaming Platforms: PC
Programming Language of Choice: C++

Re: Trouble using SDL_DisplayFormat()

Post 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.
User avatar
dr-snipe
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 42
Joined: Sun Dec 19, 2010 10:09 pm
Programming Language of Choice: C++, Java, PHP
Contact:

Re: Trouble using SDL_DisplayFormat()

Post 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?
User avatar
Web
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 30
Joined: Sat Mar 12, 2011 11:53 pm
Current Project: somewhat tinkering on my own game
Favorite Gaming Platforms: Snes, Sega saturn, Nes, Sega cd, Sega dreamcast
Programming Language of Choice: C++, Sdl
Location: Saint marys, West Virgina
Contact:

Re: Trouble using SDL_DisplayFormat()

Post 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?
Pinky are you pondering what im pondering?
I think so Brain but where is the ‘any’ key?”
ImageImage
ImageImage
ImageImage
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Trouble using SDL_DisplayFormat()

Post 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.
Irony
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 9
Joined: Fri Jan 28, 2011 2:23 am
Current Project: Pong
Favorite Gaming Platforms: PC
Programming Language of Choice: C++

Re: Trouble using SDL_DisplayFormat()

Post 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?
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Trouble using SDL_DisplayFormat()

Post 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.
Post Reply