TMX问题,在正确位置绘制瓷砖(c++)

TMX issue with Drawing tiles in correct position (c++)

本文关键字:c++ 绘制 位置 问题 TMX      更新时间:2023-10-16

TMX映射加载正确,但它似乎定位不正确。

我在这里使用TMX Parser:https://code.google.com/p/tmx-parser/

它加载TMX很好,没有任何错误。但它只是根据瓷砖在精灵表中的位置来定位瓷砖。

这是代码示例:

void Game::DrawMap()
{
SDL_Rect rect_CurTile;
SDL_Rect pos;
int DrawX;
int DrawY;
for (int i = 0; i < map->GetNumLayers(); ++i) 
{
    // Get a layer.
    currLayer = map->GetLayer(i);
    for (int x = 0; x < currLayer->GetWidth(); ++x) 
     {
         for (int y = 0; y < currLayer->GetHeight(); ++y) 
             {
                 int CurTile = currLayer->GetTileId(x, y);
                int Num_Of_Cols = 8;
                int tileset_col = (CurTile % Num_Of_Cols);
                tileset_col++;
                int tileset_row = (CurTile / Num_Of_Cols);
                rect_CurTile.x = (1 + (32 + 1) * tileset_col);
                rect_CurTile.y = (1 + (32 + 1) * tileset_row);
                rect_CurTile.w = 32;
                rect_CurTile.h = 32;
                DrawX = (x * 32); 
                DrawY = (y * 32); 
                pos.x = DrawX;
                pos.y = DrawY;
                pos.w = 32;
                pos.h = 32;
                apply_surfaceClip(DrawX,DrawY, surfaceTileset, destSurface, &rect_CurTile); 
                sprTexture = SDL_CreateTextureFromSurface(mRenderer,destSurface);
                SDL_RenderCopy(mRenderer,sprTexture,&rect_CurTile,&pos);
         }
    }
}
void apply_surfaceClip( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}

我解决了使用两层时画零的问题,这是完成的样本

for (int i = 0; i < map->GetNumLayers(); ++i) 
{
    // Get a layer.
    currLayer = map->GetLayer(i);
    for (int x = 0; x < currLayer->GetWidth(); ++x) 
     {
         for (int y = 0; y < currLayer->GetHeight(); ++y) 
             {
                int CurTile = currLayer->GetTileId(x, y);
                if(CurTile == 0)
                {
                    continue;
                }
                int Num_Of_Cols = 8;
                int tileset_col = (CurTile % Num_Of_Cols);
                int tileset_row = (CurTile / Num_Of_Cols);
                std::cout << CurTile << std::endl;
                rect_CurTile.x = (1 + (32 + 1) * tileset_col);
                rect_CurTile.y = (1 + (32 + 1) * tileset_row);
                rect_CurTile.w = 32;
                rect_CurTile.h = 32;
                DrawX = (x * 32); 
                DrawY = (y * 32); 
                pos.x = DrawX;
                pos.y = DrawY;
                pos.w = 32;
                pos.h = 32;
                apply_surfaceClip(DrawX,DrawY, surfaceTileset, destSurface, &rect_CurTile); 
                sprTexture = SDL_CreateTextureFromSurface(mRenderer,destSurface);
                SDL_RenderCopy(mRenderer,sprTexture,&rect_CurTile,&pos);
         }
    }
}