使用 tmx-parser 和 SDL 进行 2d 图块渲染

发布于 2024-12-29 08:02:09 字数 1916 浏览 7 评论 0原文

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

谁能指出我做错了什么?

I'm working on getting an map created in Tiled (http://mapeditor.org) to render after parsing the map with tmx-parser (http://code.google.com/p/tmx-parser/). I've got the tiles to render in the correct positions, but i can't seem to get it to render the correct tiles from the tileset. I'm using the isometric_grass_and_water example from tiled to test it.

Here is my rendering code.

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

Can anyone point out what i'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同展鸳鸯锦 2025-01-05 08:02:09

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

I found the answer after some brute force here is the changed working code if anyone else needs it:
PS: Num_Of_Cols is the same thing as (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); 
        }
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文