c++ - 使用 tmx-parser 和 SDL 的 2d tile 渲染

标签 c++ rendering 2d sdl isometric

我正在努力获取在 Tiled (http://mapeditor.org) 中创建的 map ,以便在使用 tmx-parser (http://code.google.com/p/tmx-parser/) 解析 map 后呈现).我已经在正确的位置渲染了图 block ,但我似乎无法从图 block 集中渲染正确的图 block 。我正在使用来自平铺的 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); 
            }
        }
    }
}

谁能指出我做错了什么?

最佳答案

我经过一番蛮力找到了答案,这里是更改后的工作代码,如果其他人需要的话: 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); 
        }
    }
}
}

关于c++ - 使用 tmx-parser 和 SDL 的 2d tile 渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9041947/

相关文章:

javascript - 如何在 Firefox 中将透明 PNG 绘制到 Canvas 上?

javascript - 三、js性能

algorithm - 逆向复数二维查找表

python - 如何在 matplotlib 中平滑 2D 颜色图

C++ 双下标重载 : cannot convert from 'type' to 'type &'

c++ - 在 main 内声明的数组抛出堆栈溢出,在 main 外声明的数组不会

asp.net-mvc - MVC 预览版 5 - 将 View 呈现为字符串以进行测试

c++ - 存储不同类型的变量

C++ 运算符重载未按预期工作

c++ - 尝试获取目标链接库信息时获取属性返回空变量