I have a 2D array based tile map, and I'm having trouble splitting the tilesheet. I know the logic behind it, but I'm most likely writing it the wrong way.
My understanding of it is this: the numbers in the array represent a tile on the tilesheet.
(dw, ill just post code cause idk how to put it into words)
here is my drawing function:
Code: Select all
void DrawSubImage(int x, int y, int clipX, int clipY, SDL_Surface* source)
{
SDL_Rect pos = {x, y};
SDL_Rect clip = {clipX * 32, clipY*32, 32, 32}; //the x and y position ON the image
SDL_BlitSurface(source, &clip, screen, &pos);
}
DrawSubImage(30, 30, 2, 3, tiles);
And it will draw the second tile on the third row if the image.
Here is the map drawing function, with the logic I understand is correct.
Code: Select all
RenderMap()
{
//for this example, the tile dimensions are 32x32
int sheetW =tiles->GetTexWidth()/tileWidth; //the width of the sheet divided by the height of the tile. Same as surface->h
int sheetH = tiles->GetTexHeight()/tileHeight;
int maxTiles = sheetW*sheetH; //max number of tiles on the tilesheet
int clipX, clipY=0; //position on the tilesheet
for(int x=0; x<mapW; x++)
{
for(int y=0; y<mapH; y++)
{
for(int clipX=0; clipX<maxTiles; clipX++)
{
//if a number in the array is the same one as the index
if(map[x][y] == clipX)
{
//if we reach the end of the tilesheet
if(clipX >= sheetW)
{
clipX=0; //go back to the start of the sheet
clipY++; //go down a row
}
tiles->DrawSubImage(x*tileWidth, y*tileHeight, clipX, clipY, tiles); //draw all the tiles
}
}
}
}
When I test the application, it drops down to the next row of tiles, but it stops there and doesn't continue to draw anymore tiles.
I've tried many different ways, this is just an example of how I'm implimenting it. If anything looks confusing, please ask and thankyou very much for taking your time to help me Btw, Im aware I have explained it wrong/confusingly so if you have any doubts about any of it, its most likely my inability to explain shit easily
Thanks for yo time peeps, take it easy.