0

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;
}
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by nuclear
0

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.