Jump to content
  • Advertisement
Sign in to follow this  
Chryzmo

[SDL] Tileset Blitting Problems

This topic is 4825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to load a .GIF image using SDL_image, then blit the image into seperate tiles, which I store in an array of SDL_Surfaces*. I keep getting an error from the SDL_BlitSurface function. Here is the code:

// The Tile array is a member of the CDisplay class.  I have tried setting it 
// to NULL in the constructor as well as just leaving it out of the constructor.

void CDisplay::LoadTiles()
{
	SDL_Surface* Tiles;
	SDL_Rect dstRect;
	SDL_Rect srcRect;
	int iTileNumber = 0;
	
	dstRect.x = 0;
	dstRect.y = 0;
	dstRect.h = dstRect.w = 100;
	
	srcRect.y = srcRect.y = 1;
	srcRect.h = srcRect.w = 100;
		
	if ((Tiles = IMG_Load("gfx/tiles1.gif")) == NULL)
	{
		cout << "Could not open tileset." << endl;
	} else
	{
		for (int y = 1; y <= 132; y += 33)
		{
			for (int x = 1; x <= 132; x += 33)
			{
				srcRect.x = x;
				srcRect.y = y;
			
				if (SDL_BlitSurface(Tiles, &srcRect, Tile[iTileNumber], &dstRect)==-1)
				{
					cout << "Tile Blit Failed! Error -1." << endl;
					fprintf(stderr, "%s\n", SDL_GetError());
				} else
				{
					cout << "Blitting Succeeded." << endl;
				}
				iTileNumber++;
			}
		}
	}
	SDL_FreeSurface(Tiles);
}



The output I am getting is: Tile Blit Failed! Error -1. SDL_UpperBlit: passed a NULL surface I tried creating a 2D program using SDL last summer and I got it to display the tiles, but I do not think I did anything except create the array of SDL_Surfaces to hold the tiles in. Unless there is something I forgot. Do I have to initialize the SDL_Surfaces to something before I can blit something else on them? Thanks, Chris (edit: Source formatting)

Share this post


Link to post
Share on other sites
Advertisement
You don't have an array of "SDL_Surface*"s, you have one.
So you can't index into it here:
Tile[iTileNumber]

instead of trying to do multiple SDL_Surfaces, it's almost always better to put your tiles into one image, then manipulate the srcrect to only blit parts of it.
For example, if your tiles are 100x100 and you have 5 tiles, you'd put them horizontaly on the image
Then for srcrect, you'd set it up like this:

srcRect.x=iTileNumber*100;
srcRect.y=0;
srcRect.w=srcRect.h=100;

An example drawing function:


srcRect.y=0;
srcRect.w=srcRect.h=100;
for(int y=0;y<map_height;y++){
for(int x=0;x<map_width;x++){
srcRect.x=map[x][y]*100;
dstRect.x=x*100;
detRect.y=y*100;
SDL_BlitSurface(Tiles, &srcRect, screen, &dstRect);
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!