【问题标题】:2d tile rendering using tmx-parser and SDL使用 tmx-parser 和 SDL 进行 2d 平铺渲染
【发布时间】:2012-01-28 00:41:14
【问题描述】:

我正在使用 tmx-parser (http://code.google.com/p/tmx-parser/) 解析地图后,在 Tiled (http://mapeditor.org) 中创建地图以进行渲染)。我已经将瓷砖渲染在正确的位置,但我似乎无法让它从瓷砖集中渲染正确的瓷砖。我正在使用 tiled 中的 isometric_grass_and_water 示例对其进行测试。

这是我的渲染代码。

void Map::RenderMapIsometric(SDL_Surface *SurfaceDest)
    {
        for (int i = 0; i < map->GetNumLayers(); ++i) 
        {
            // Get a layer.
        this->layer = map->GetLayer(i);

        for (int x = 0; x < layer->GetWidth(); ++x) 
        {
            for (int y = 0; y < layer->GetHeight(); ++y) 
            {
                int CurTile = layer->GetTileGid(x, y);

                if(CurTile == 0)
                {
                    continue;
                }

                int tileset_col = (CurTile % (TilesetWidth / this->tileset->GetTileWidth()));
                int tileset_row = (CurTile / (TilesetWidth / this->tileset->GetTileWidth()));

                std::cout << CurTile << std::endl;

                SDL_Rect rect_CurTile;
                rect_CurTile.x = (this->tileset->GetMargin() + (this->tileset->GetTileWidth() + this->tileset->GetSpacing()) * tileset_col);
                rect_CurTile.y = (this->tileset->GetMargin() + (this->tileset->GetTileHeight() + this->tileset->GetSpacing()) * tileset_row);
                rect_CurTile.w = this->tileset->GetTileWidth();
                rect_CurTile.h = this->tileset->GetTileHeight();

                int DrawX = (x * this->tileset->GetTileWidth() / 2) + (y * this->tileset->GetTileWidth() / 2);
                int DrawY = (y * this->tileset->GetTileHeight() / 2) - (x * this->tileset->GetTileHeight() / 2);

                apply_surfaceClip(DrawX, DrawY, surf_Tileset, SurfaceDest, &rect_CurTile); 
            }
        }
    }
}

谁能指出我做错了什么?

【问题讨论】:

    标签: c++ rendering 2d sdl isometric


    【解决方案1】:

    经过一番蛮力后,我找到了答案,如果其他人需要,我会更改工作代码: PS:Num_Of_Cols 和 (TilesetWidth / TileWidth) 是一回事

    void Map::RenderMapIsometric(SDL_Surface *SurfaceDest)
    {
    
    for (int i = 0; i < map->GetNumLayers(); ++i) 
        {
            // Get a layer.
            this->layer = map->GetLayer(i);
    
        for (int x = 0; x < layer->GetWidth(); ++x) 
        {
            for (int y = 0; y < layer->GetHeight(); ++y) 
            {
                int CurTile = layer->GetTileGid(x, y);
    
                if(CurTile == 0)
                {
                    continue;
                }
    
                //CurTile = tileset->GetFirstGid() + CurTile;
                CurTile--;
    
                int tileset_col = (CurTile % Num_Of_Cols);
                int tileset_row = (CurTile / Num_Of_Cols);
    
                SDL_Rect rect_CurTile;
                rect_CurTile.x = (this->tileset->GetMargin() + (this->tileset->GetTileWidth() + this->tileset->GetSpacing()) * tileset_col);
                rect_CurTile.y = (this->tileset->GetMargin() + (this->tileset->GetTileHeight() + this->tileset->GetSpacing()) * tileset_row);
                rect_CurTile.w = this->tileset->GetTileWidth();
                rect_CurTile.h = this->tileset->GetTileHeight();
    
                int DrawX = (x * this->tileset->GetTileWidth() / 2) + (y * this->tileset->GetTileWidth() / 2);
                int DrawY = (y * this->tileset->GetTileHeight() / 2) - (x * this->tileset->GetTileHeight() / 2);
    
                apply_surfaceClip(DrawX, DrawY, surf_Tileset, SurfaceDest, &rect_CurTile); 
            }
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-05
      • 2016-03-23
      • 2012-11-11
      • 2020-08-24
      相关资源
      最近更新 更多