-1

I am newbie in this field getting too much trouble in simple platform game for one of my project module if you guys could help or share some code. This is what I have done till now.

I could parse a Tmx Map (created using tiled map editor)and display it using TmxParser library, But I don't know how to handle Map Object , Although I could get on internet how to parse also used an example given in the library it self. But I am not good at programming I would like to share code.

Objective : To make a simple Platform game where player can move and jump Tools to be used : C++, Tiled , tmxparser and SDL

please help thanks in advance! here is my code

#include <iostream>
#include <map>
#include <string>
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_image.h>
#include <tmxparser/Tmx.h>
#include <tmxparser/TmxLayer.h>
#include <tmxparser/TmxMap.h>
#include <tmxparser/TmxMapTile.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char * argv[])
{ SDL_Window *sdlWindow;
    SDL_Renderer *sdlRenderer;
    SDL_CreateWindowAndRenderer(640, 480, SDL_WINDOW_MAXIMIZED, &sdlWindow, &sdlRenderer);
    SDL_SetWindowTitle(sdlWindow, "Tiled Test");
    SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 0, 255);
    SDL_RenderClear(sdlRenderer);
    SDL_RenderPresent(sdlRenderer);
    SDL_Event event;
    SDL_Texture* background = SDL_CreateTexture(sdlRenderer,
                               SDL_PIXELFORMAT_ARGB8888,
                               SDL_TEXTUREACCESS_STREAMING,
                               640, 480);

    Tmx::Map *map = new Tmx::Map();
    std::string fileName = (argc > 1) ? argv[1] : "./example/example.tmx";
    map->ParseFile(fileName);

    if (map->HasError())
    {
        printf("error code: %d\n", map->GetErrorCode());
        printf("error text: %s\n", map->GetErrorText().c_str());

        return map->GetErrorCode();
    }

    printf("====================================\n");
    printf("Map\n");
    printf("====================================\n");

    printf("Version: %1.1f\n", map->GetVersion());
    printf("Orientation: %d\n", map->GetOrientation());
    if (!map->GetBackgroundColor().empty())
        printf("Background Color (hex): %s\n",
               map->GetBackgroundColor().c_str());
    printf("Render Order: %d\n", map->GetRenderOrder());
    if (map->GetStaggerAxis())
        printf("Stagger Axis: %d\n", map->GetStaggerAxis());
    if (map->GetStaggerIndex())
        printf("Stagger Index: %d\n", map->GetStaggerIndex());
    printf("Width: %d\n", map->GetWidth());
    printf("Height: %d\n", map->GetHeight());
    printf("Tile Width: %d\n", map->GetTileWidth());
    printf("Tile Height: %d\n", map->GetTileHeight());
    std::map<const char*, SDL_Texture*> tilesets;
    // Iterate through the tilesets.
    for (int i = 0; i < map->GetNumTilesets(); ++i)
    {
        printf("                                    \n");
        printf("====================================\n");
        printf("Tileset : %02d\n", i);
        printf("====================================\n");

        // Get a tileset.
        const Tmx::Tileset *tileset = map->GetTileset(i);

        // Print tileset information.
        printf("Name: %s\n", tileset->GetName().c_str());
        printf("Margin: %d\n", tileset->GetMargin());
        printf("Spacing: %d\n", tileset->GetSpacing());
        printf("First gid: %d\n", tileset->GetFirstGid());
        printf("Image Width: %d\n", tileset->GetImage()->GetWidth());
        printf("Image Height: %d\n", tileset->GetImage()->GetHeight());
        printf("Image Source: %s\n", tileset->GetImage()->GetSource().c_str());

        //create tecture for tileset
        std::string location = "./example/" + tileset->GetImage()->GetSource();
                tilesets.insert(std::pair<const char*, SDL_Texture*>(tileset->GetName().c_str(), SDL_CreateTextureFromSurface(sdlRenderer, IMG_Load(location.c_str()))));

        if (!tileset->GetImage()->GetTransparentColor().empty())
            printf("Transparent Color (hex): %s\n",
                   tileset->GetImage()->GetTransparentColor().c_str());

        if (tileset->GetTiles().size() > 0)
        {
            // Get a tile from the tileset.
            const Tmx::Tile *tile = *(tileset->GetTiles().begin());

            // Print the properties of a tile.
            std::map<std::string, std::string> list =
                    tile->GetProperties().GetList();
            std::map<std::string, std::string>::iterator iter;
            for (iter = list.begin(); iter != list.end(); ++iter)
            {
                printf("%s = %s\n", iter->first.c_str(), iter->second.c_str());
            }

            if (tile->IsAnimated())
            {
                printf(
                        "Tile is animated: %d frames with total duration of %dms.\n",
                        tile->GetFrameCount(), tile->GetTotalDuration());

                const std::vector<Tmx::AnimationFrame> &frames =
                        tile->GetFrames();

                int i = 0;
                for (std::vector<Tmx::AnimationFrame>::const_iterator it =
                        frames.begin(); it != frames.end(); it++, i++)
                {
                    printf("\tFrame %d: Tile ID = %d, Duration = %dms\n", i,
                            it->GetTileID(), it->GetDuration());
                }
            }

            if(tile->HasObjects())
            {
                printf(
                        "Tile has objects.\n");


                // Iterate through all Collision objects in the tile.
                for (int j = 0; j < tile->GetNumObjects(); ++j)
                {
                    // Get an object.
                    const Tmx::Object *object = tile->GetObject(j);

                    // Print information about the object.
                    printf("Object Name: %s\n", object->GetName().c_str());
                    printf("Object Position: (%03d, %03d)\n", object->GetX(),
                           object->GetY());
                    printf("Object Size: (%03d, %03d)\n", object->GetWidth(),
                           object->GetHeight());

                    // Print Polygon points.
                    const Tmx::Polygon *polygon = object->GetPolygon();
                    if (polygon != 0)
                    {
                        for (int i = 0; i < polygon->GetNumPoints(); i++)
                        {
                            const Tmx::Point &point = polygon->GetPoint(i);
                            printf("Object Polygon: Point %d: (%f, %f)\n", i, point.x,
                                   point.y);
                        }
                    }

                    // Print Polyline points.
                    const Tmx::Polyline *polyline = object->GetPolyline();
                    if (polyline != 0)
                    {
                        for (int i = 0; i < polyline->GetNumPoints(); i++)
                        {
                            const Tmx::Point &point = polyline->GetPoint(i);
                            printf("Object Polyline: Point %d: (%f, %f)\n", i, point.x,
                                   point.y);
                        }
                    }
                }

            }
        }
    }

    // Iterate through the tile layers.
    for (int i = 0; i < map->GetNumTileLayers(); ++i)
    {
        printf("                                    \n");
        printf("====================================\n");
        printf("Layer : %02d/%s \n", i, map->GetTileLayer(i)->GetName().c_str());
        printf("====================================\n");

        // Get a layer.
        const Tmx::TileLayer *tileLayer = map->GetTileLayer(i);

        for (int y = 0; y < tileLayer->GetHeight(); ++y)
        {
            for (int x = 0; x < tileLayer->GetWidth(); ++x)
            {
                if (tileLayer->GetTileTilesetIndex(x, y) == -1)
                {
                    printf("........    ");
                }
                else
                {
                    // Get the tile's id and gid.
                    printf("%03d(%03d)", tileLayer->GetTileId(x, y), tileLayer->GetTileGid(x, y));

                    const Tmx::Tileset *tileset = map->GetTileset(tileLayer->GetTile(x,y).tilesetId);//map->FindTileset(layer->GetTileId(x, y));

                                      if (tileset != 0)
                                      {
                                          SDL_Rect rect; rect.x = x*tileset->GetTileWidth(); rect.y=y*tileset->GetTileHeight();rect.w=tileset->GetTileWidth();rect.h=tileset->GetTileHeight();
                                          const Tmx::MapTile *tile = &tileLayer->GetTile(x,y);
                                          tilesets.find(tileset->GetName().c_str());
                                          int tilesPerRow = tileset->GetImage()->GetWidth() / tileset->GetTileWidth();
                                          int tilyyyy = (tileLayer->GetTileId(x, y)) / tilesPerRow;
                                          SDL_Rect srcRect; srcRect.x = ((tileLayer->GetTileId(x, y) - (tilyyyy*tilesPerRow))*tileset->GetTileWidth()); srcRect.y = tilyyyy*tileset->GetTileHeight(), srcRect.h = tileset->GetTileHeight(); srcRect.w = tileset->GetTileWidth();
                                          SDL_Texture* tester = tilesets.find(tileset->GetName().c_str())->second;
                                          SDL_RenderCopy(sdlRenderer, tester, &srcRect,&rect);
                                          SDL_RenderPresent(sdlRenderer);
                    }




                    // Find a tileset for that id.
                    //const Tmx::Tileset *tileset = map->FindTileset(layer->GetTileId(x, y));
                    if (tileLayer->IsTileFlippedHorizontally(x, y))
                    {
                        printf("h");
                    }
                    else
                    {
                        printf(" ");
                    }
                    if (tileLayer->IsTileFlippedVertically(x, y))
                    {
                        printf("v");
                    }
                    else
                    {
                        printf(" ");
                    }
                    if (tileLayer->IsTileFlippedDiagonally(x, y))
                    {
                        printf("d ");
                    }
                    else
                    {
                        printf("  ");
                    }
                }
            }

            printf("\n");
        }
    }

    printf("\n\n");

    // Iterate through all of the object groups.
    for (int i = 0; i < map->GetNumObjectGroups(); ++i)
    {
        printf("                                    \n");
        printf("====================================\n");
        printf("Object group : %02d\n", i);
        printf("====================================\n");

        // Get an object group.
        const Tmx::ObjectGroup *objectGroup = map->GetObjectGroup(i);

        // Iterate through all objects in the object group.
        for (int j = 0; j < objectGroup->GetNumObjects(); ++j)
        {
            // Get an object.
            const Tmx::Object *object = objectGroup->GetObject(j);

            // Print information about the object.
            printf("Object Name: %s\n", object->GetName().c_str());
            printf("Object Position: (%03d, %03d)\n", object->GetX(),
                    object->GetY());
            printf("Object Size: (%03d, %03d)\n", object->GetWidth(),
                    object->GetHeight());

            // Print Polygon points.
            const Tmx::Polygon *polygon = object->GetPolygon();
            if (polygon != 0)
            {
                for (int i = 0; i < polygon->GetNumPoints(); i++)
                {
                    const Tmx::Point &point = polygon->GetPoint(i);
                    printf("Object Polygon: Point %d: (%f, %f)\n", i, point.x,
                            point.y);
                }
            }

            // Print Polyline points.
            const Tmx::Polyline *polyline = object->GetPolyline();
            if (polyline != 0)
            {
                for (int i = 0; i < polyline->GetNumPoints(); i++)
                {
                    const Tmx::Point &point = polyline->GetPoint(i);
                    printf("Object Polyline: Point %d: (%f, %f)\n", i, point.x,
                            point.y);
                }
            }
        }
    }

    delete map;
    bool quit = false;
    while (!quit)
    {
      while (SDL_PollEvent(&event))
      {
        switch (event.type)
        {
          case SDL_QUIT:
            quit = true;
            break;
        }
      }
    }
    SDL_DestroyRenderer(sdlRenderer);
    SDL_DestroyWindow(sdlWindow);
    SDL_Quit();
    return 0;
    return 0;
}

1 Answers1

0

The code is working fine and should be able to display example.tmx with no problem (make sure you have a "example" directory with the needed files in there). As for information on Map object, I don't think there's other way other than to read the tiled map format (available here : http://doc.mapeditor.org/reference/tmx-map-format/ ). Also you could get help from Tiled's forum (http://forum.mapeditor.org/) if you have further questions.

As for your objective to "To make a simple Platform game where player can move and jump ", that in itself is a quite broad topic and I don't think that could be answered fully here.

Hope that helps.

user2924261
  • 117
  • 2
  • 9
  • yes I could display exmaple.tmx but the objects present in it are not visible! – Priyanshu Shukla Dec 18 '15 at 09:11
  • @Priyanshu Shukla You can draw line(s) using SDL_RenderDrawLine or SDL_RenderDrawLines. The above code that you got from the web is just a proof of concept, and it didn't flip the tiles neither display animated tiles (the torches) either, but it should be more than enough to get you off the ground. Have you checked the source code for tmxparser on how the parser store coords for map objects? You can use that to render the lines using the above SDL functions. – user2924261 Dec 18 '15 at 09:45