Sign in to follow this  

I need to show tiles without looping through a grid

This topic is 4353 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

How can my engine draw only the tiles I need (A-F) without looking at each cell to determine if there is a tile there. The grey lines are the grid (that i dont want to search every cell of) The black boxes are images (sprites or tiles) A-F The red lines are the anchor points for the images. So how can I do this? Some sort of list? http://devimg.net/Image.html?716A thanks for any help

Share this post


Link to post
Share on other sites
You could, for example, use the normal map-matrix to generate an array of placed tiles. This array would contain structs containing the position and a pointer to the image for each tile. The array could be sorted according to the image to be as cache-friendly as possible. When drawing, just iterate through the array and place each tile on its coordinates.
I assume your maps are larger than one screen, so to avoid having to see if a tile is visible you could divide the map into blocks each as large as a screen and have an array for each block. You'd then draw the 4 arrays that can be visible without doing any finer culling.

Share this post


Link to post
Share on other sites
I implement it as a view with the visual edges in world position.
And from the edges you'll get the grids cell (columns and rows) index and offset.
You draw each row from the top view edge to the bottom view edge's limit.
You draw each tile column from the left view edge to the right view edge's limit.


typedef struct {
SDL_Rect *frame;
Uint32 leftEdgePos;
Uint32 rightEdgePos;
Uint32 topEdgePos;
Uint32 bottomEdgePos;
Uint16 centerXoffset;
Uint16 centerYoffset;
} View;


#define WU 40 // The World Unit, the grid's cell size


// Gets the cell grid's index from an axis position
Uint16 IndexOffsetWU(Uint32 pos, Uint8 *offset)
{
*offset = pos % WU;
return (pos - *offset) / WU;
}


Unit16 mapColCount;
Uint16 mapRowCount;
Tile *mapTiles;

Tile *GetMapTile(Uint16 row, Uint16 col)
{
Tile *tile;

if (row < mapRowCount && col < mapColCount)
{
tile = mapTiles + (row * mapColCount) + col;
}
else tile = NULL;

return tile;
}



void DrawTiles(View *view)
{
Uint16 topRowIndex, topRowOffset;
topRowIndex = IndexOffsetWU(view->topEdgePos,&topRowOffset);

Uint16 bottomRowIndex, bottomRowOffset;
bottomRowIndex = IndexOffsetWU(view->bottomEdgePos,&BottomRowOffset);

Uint16 leftColIndex, leftColOffset;
leftColIndex = IndexOffsetWU(view->leftEdgePos,&leftColOffset);

Uint16 rightColIndex, rightColOffset;
RightColIndex = IndexOffsetWU(RightEdge,&RightColOffset);

// Set start index
Uint16 rowIndex = topRowIndex; // the current row index
Uint16 colIndex = leftColIndex;// the current col index
Tile *pTile = GetMapTile(topRowIndex,leftColIndex);

Uint16 startX = frame->x - leftColOffset;
Uint16 startY = frame->y - topRowOffset;

Uint16 drawX = startX; // the current draw x position
Uint16 drawY = startY; // the current draw y position

// The drawing cycle
for (;;) { // No nested loop here!!!

DrawTile(*pTile,drawX,drawY); // Draw the tile

if (colIndex == rightColIndex) // Have we reached the col end
{
if (rowIndex == bottomRowIndex) break; // Are we done?
rowIndex++; // A new row
drawY += WU; // Move down the draw y-position
colIndex = leftColIndex; // Reset colIndex to it's start
drawX = startX; // Reset the draw x-position
pTile = GetMapTile(rowIndex,colIndex);
}
else
{
colIndex++;
drawX += WU;
pTile++;
}
}
}



Share this post


Link to post
Share on other sites

This topic is 4353 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.

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