【问题标题】:TMX issue with Drawing tiles in correct position (c++)在正确位置绘制瓷砖的 TMX 问题(c++)
【发布时间】:2015-04-13 02:29:57
【问题描述】:

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

【问题讨论】:

    标签: c++ tmx


    【解决方案1】:

    我解决了问题是使用两层时它在绘制零这里是完成的示例

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多