0

TMX 地图加载正确,但似乎我的图块定位不正确。

我从这里使用 TMX 解析器:https ://code.google.com/p/tmx-parser/

它可以很好地加载 TMX,没有错误。但它只是根据它们在 spritesheet 中的位置来定位图块。

这是代码示例:

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 );
}
4

1 回答 1

0

我解决了问题是当使用两层时它正在绘制零这里是完成的样本

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);
         }
    }
}
于 2015-02-12T11:35:58.410 回答