diff --git a/src/sagotmx/tmx_struct.h b/src/sagotmx/tmx_struct.h index a9a9052..6cd1f80 100644 --- a/src/sagotmx/tmx_struct.h +++ b/src/sagotmx/tmx_struct.h @@ -212,7 +212,7 @@ struct Image { struct TileSet { - std::string firstgid; + int firstgid = 0; std::string source; TileSet* alternativeSource = nullptr; //< Set to non-null to use an alternative TileSet. Must be set if source is set. std::string name; @@ -313,10 +313,14 @@ inline TileSet node2tileset(rapidxml::xml_node<> * tileset_node) { bool found = false; rapidxml::xml_node<> * image_node = getElement(tileset_node, "image", found); if (found) { + std::cout << "Found image for " << ts.name << ":" << ts.source << "\n"; setValueFromAttribute(image_node, "source", ts.image.source); setValueFromAttribute(image_node, "width", ts.image.width); setValueFromAttribute(image_node, "height", ts.image.height); } + else { + std::cout << "Did not find an image node for " << ts.name << ":" << ts.source << "\n"; + } rapidxml::xml_node<> * terraintypes_node = tileset_node->first_node("terraintypes"); if (terraintypes_node) { for (rapidxml::xml_node<> * terrain_node = terraintypes_node->first_node("terrain"); terrain_node; terrain_node = terrain_node->next_sibling()) { @@ -441,8 +445,8 @@ inline void xml_add_tileset(std::iostream& io, const TileMap& m, size_t tile_set io << " name=\"" << ts.name << "\" tilewidth=\""<\n"; - if (ts.source.length() == 0) { - //Write image here + if (ts.image.source.length() > 0) { + io << "\n"; } io << "\n"; } @@ -531,18 +535,25 @@ inline std::string tilemap2string(const TileMap& m) { * @param[out] h The tile height will be written to this pointer if not NULL. May be null. */ inline void getTextureLocationFromGid(const TileMap& tm, int gid, std::string* imageFile, int* x, int* y, int* w, int* h ) { - //Currently hardcoded to one tileset + //Currently hardcodeed to one tileset const TileSet *ts = nullptr; if (tm.tileset.size() < 1) { return; } ts = &(tm.tileset.at(0)); - //TODO: Check this. Is "firstgid" from the tiledset related? - gid--; //Gids starts at 1 by default + for (size_t i = 1; i < tm.tileset.size(); ++i) { + const TileSet *newTs = &(tm.tileset.at(i)); + if (newTs->firstgid > gid) { + break; + } + ts = newTs; + } + gid -= ts->firstgid; //Final gid needs to be set before we check for alternative source while (ts->alternativeSource) { //Follow source links ts = ts->alternativeSource; } + //std::cout << "ts: " << ts->name << ", " << ts->image.source << ", " << ts->image.height << ", " << ts->image.width << "\n"; if (imageFile) { *imageFile = ts->image.source; } diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index 656ef3f..f7b07c1 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp @@ -71,7 +71,7 @@ struct Game::GameImpl { bool drawCollision = true; int topx = 0.0; int topy = 0.0; - int world_mouse_x = 0; //Mouse cooridinates relative to the world + int world_mouse_x = 0; //Mouse coordinates relative to the world int world_mouse_y = 0; char direction = 0; Uint32 lastUpdate = 0; diff --git a/src/saland/GameDraw.cpp b/src/saland/GameDraw.cpp index d3a7832..6840d47 100644 --- a/src/saland/GameDraw.cpp +++ b/src/saland/GameDraw.cpp @@ -65,7 +65,7 @@ void DrawLayer(SDL_Renderer* renderer, sago::SagoSpriteHolder* sHolder, const sa std::string imageFile; getTextureLocationFromGid(tm, gid, &imageFile, &part.x, &part.y, &part.w, &part.h); imageFile = imageFile.substr(12); - imageFile = imageFile.substr(0,imageFile.length()-4); + imageFile = imageFile.substr(0, imageFile.length()-4); SDL_Texture* texture = sHolder->GetDataHolder().getTexturePtr(imageFile); Draw(renderer, texture, 32 * i - topx, 32 * j - topy, part); }