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;
}

Recommended Answers

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().

Jump to Post

All 2 Replies

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().

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.