954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

SDL question

Just recently started learning SDL and have this problem, this program should properly display my background and message on the screen, but instead its just a black screen. Did the Blit, did the Flip, maybe im missing something? Heres the code:

P.S i know i might forgot to add some simple line or something, but i just cant find it ^^

#include <SDL.h>
#include <string>

SDL_Surface* Load_image(std::string filename)
{
	SDL_Surface* loadedimage = 0;
	SDL_Surface* optimizedimage = 0;

	loadedimage = SDL_LoadBMP(filename.c_str());

	if ( loadedimage != 0 )
	{
	SDL_Surface* optimizedimage = SDL_DisplayFormat(loadedimage);

	SDL_FreeSurface(loadedimage);
	}

	return optimizedimage;
}

void apply_surface(int x,int y, SDL_Surface* source, SDL_Surface* destination)
{
	SDL_Rect offset;

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

	SDL_BlitSurface(source,0,destination,&offset);
}

int main(int argc,char** argv)
{
	SDL_Surface* screen = 0;
	SDL_Surface* background = 0;
	SDL_Surface* message = 0;

	if ( SDL_Init(SDL_INIT_EVERYTHING) == -1 )
	{
		return 1;
	}

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

	if ( screen == 0 )
	{
		return 1;
	}

	SDL_WM_SetCaption("Hello world","player.bmp");

	message = Load_image("hello.bmp");
	background = Load_image("background.bmp");

	apply_surface(0,0,background,screen);
	apply_surface(180,140,message,screen);

	if ( SDL_Flip(screen) == -1 )
	{
		return 1;
	}

	SDL_Delay(2000);

	SDL_FreeSurface(message);
	SDL_FreeSurface(background);

	SDL_Quit();

	return 0;
}
nuclear
Junior Poster in Training
94 posts since Aug 2011
Reputation Points: 2
Solved Threads: 3
 

Your Load_image() function is always gonna return 0.

On line 13 of what you posted above delete SDL_Surface* because it is making a new variable with the same name as you have out of the if() statement and is not changing the value outside of the if().

sfuo
Practically a Master Poster
656 posts since Jul 2009
Reputation Points: 164
Solved Threads: 99
 

Thanks! Cant believe it was something stupid like this yet again, happens to me sometimes ;)

nuclear
Junior Poster in Training
94 posts since Aug 2011
Reputation Points: 2
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: