0

TMX マップは正しく読み込まれていますが、タイルが正しく配置されていないようです。

ここから TMX パーサーを使用しています: 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 );
}
4

1 に答える 1

0

問題を修正しました 2 つのレイヤーを使用しているときにゼロを描画していた問題を修正しました ここに完成したサンプルがあります

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 に答える