Sign in to follow this  
Serenity54

[Solved]SDL black screen

Recommended Posts

Hey, can anyone tell me why this won't work?
void ApplySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
	SDL_Rect offset;
	offset.x = x;
	offset.y = y;
	SDL_BlitSurface(source, clip, destination, &offset);
}
...
...
	for(int col = 0; col < 20; col++)
	{
		for(int row = 0; row < 15; row++)
		{
			ApplySurface(col*32, row*32, cell1, map);
			/*
				if I replace map with screen and remove the
				ApplySurface below, it works fine, but I
				want to 'build' it to the surface just once and
				then just redraw it later as a surface
			*/
		}
	}

	ApplySurface(0,0, map, screen);

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




This code shows a black screen. As I said in the comment in the above code, I want to 'build' the map onto a single SDL surface to draw later, but I don't know or understand why this code doesn't work. The reason I want to do this is to save processing time, but is it really going to matter? P.S. sorry if this has been posted before, I wasn't sure what to search for. [Edited by - Serenity54 on February 29, 2008 4:18:13 PM]

Share this post


Link to post
Share on other sites
I don't think this will help, but here's the rest of the code.


//
// Global variables
//
SDL_Event wEvent;
SDL_Surface *screen = NULL;

//
// Initialize SDL
//
bool Init(char* title, int w, int h, int bpp = 32, Uint32 flags = SDL_DOUBLEBUF|SDL_SWSURFACE)
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}

screen = SDL_SetVideoMode(w, h, bpp, flags);
if(screen == NULL)
{
return false;
}

SDL_WM_SetCaption(title, NULL);

return true;
}


//
// Kill SDL
//
void Kill()
{
SDL_FreeSurface(screen);
SDL_Quit();
}


//
// Load an image from a file
//
SDL_Surface* LoadImage(char* path)
{
SDL_Surface *loadedImage = NULL;
SDL_Surface *optimizedImage = NULL;

//Load image
loadedImage = IMG_Load(path);
if(loadedImage != NULL)
{
//optimize
optimizedImage = SDL_DisplayFormat(loadedImage);
if(optimizedImage != NULL)
{
//Remove 'magic purple' from the image
Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 0xFF, 0, 0xFF);
SDL_SetColorKey(optimizedImage,SDL_SRCCOLORKEY, colorkey);
}
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}

//
// Apply an SDL surface to another surface
//
void ApplySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, clip, destination, &offset);
}


//
// Mainline
//
int main(int argc, char* argv[])
{
if(!Init("SDL Test", 640, 480, NULL, SDL_SWSURFACE)) // 20 columns, 15 rows
{
return 1;
}

SDL_Surface* cell1 = NULL;
SDL_Surface* cell2 = NULL;
SDL_Surface* map = NULL;
cell1=LoadImage("grass_1_night.gif");
cell2=LoadImage("brickwall_1_night.gif");

if(cell1 == NULL || cell2 == NULL)
{
return 1;
}

for(int col = 0; col < 20; col++)
{
for(int row = 0; row < 15; row++)
{
ApplySurface(col*32, row*32, cell1, map);
}
}

ApplySurface(0,0, map, screen);

bool done = false;
while(!done)
{
while(SDL_PollEvent(&wEvent))
{
switch(wEvent.type)
{
case SDL_QUIT:
done = true;
break;
}
}

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

SDL_FreeSurface(cell1);
SDL_FreeSurface(cell2);
SDL_FreeSurface(map);
Kill();
return 0;
}




I believe the problem has something to do with blitting surfaces to a single surface, then blitting that single surface to the surface I want to flip.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
The variable "map" is NULL. To create a surface to blit onto, use SDL_CreateRGBSurface().

Other than that, don't call SDL_FreeSurface on the surface returned by SDL_SetVideoMode(), the documentation expressly says not to.


Perfect, that fixed it, thanks! What do the last four parameters for SDL_CreateRGBSurface do exactly?

Share this post


Link to post
Share on other sites
They specify the order of the reg, green, blue and alpha channels. In a 32 bit surface each channel (a byte long) is packed into a single 32 bit integer. I use the example on the linked page, for this:

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif


Share this post


Link to post
Share on other sites
Quote:
Original post by Serenity54
And setting them all to 0 will revert to the systems default? or SDLs default?


Looking at the SDL source, it appears to be a system default. SDL has no default, AFAICT.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this