Browse Source

Implemented TileMapObject2D flipping horizontally and vertically, fixed a bug with bit shift

pmatsula 7 years ago
parent
commit
ce04549e25

+ 12 - 6
Source/Urho3D/Urho2D/TileMapDefs2D.h

@@ -136,12 +136,12 @@ public:
     Tile2D();
 
     /// Return gid.
-    unsigned GetGid() const { return gid_; }
-    /// Return tile flip X.
+    int GetGid() const { return gid_ & ~FLIP_MASK; }
+    /// Return flip X.
     bool GetFlipX() const { return gid_ & FLIP_HORIZONTAL; }
-    /// Return tile flip Y.
+    /// Return flip Y.
     bool GetFlipY() const { return gid_ & FLIP_VERTICAL; }
-    /// Return tile flip diagonally.
+    /// Return flip XY.
     bool GetFlipXY() const { return gid_ & FLIP_DIAGONAL; }
 
     /// Return sprite.
@@ -189,7 +189,13 @@ public:
     const Vector2& GetPoint(unsigned index) const;
 
     /// Return tile Gid.
-    int GetTileGid() const { return gid_; }
+    int GetTileGid() const { return gid_ & ~FLIP_MASK; }
+    /// Return tile flip X.
+    bool GetTileFlipX() const { return gid_ & FLIP_HORIZONTAL; }
+    /// Return tile flip Y.
+    bool GetTileFlipY() const { return gid_ & FLIP_VERTICAL; }
+    /// Return tile flip XY.
+    bool GetTileFlipXY() const { return gid_ & FLIP_DIAGONAL; }
 
     /// Return tile sprite.
     Sprite2D* GetTileSprite() const;
@@ -214,7 +220,7 @@ private:
     /// Points(for polygon and polyline).
     Vector<Vector2> points_;
     /// Gid (for tile).
-    int gid_;
+    unsigned gid_;
     /// Sprite (for tile).
     SharedPtr<Sprite2D> sprite_;
     /// Property set.

+ 1 - 0
Source/Urho3D/Urho2D/TileMapLayer2D.cpp

@@ -389,6 +389,7 @@ void TileMapLayer2D::SetObjectGroup(const TmxObjectGroup2D* objectGroup)
         {
             auto* staticSprite = objectNode->CreateComponent<StaticSprite2D>();
             staticSprite->SetSprite(object->GetTileSprite());
+            staticSprite->SetFlip(object->GetTileFlipX(), object->GetTileFlipY());
             staticSprite->SetLayer(drawOrder_);
             staticSprite->SetOrderInLayer((int)((10.0f - object->GetPosition().y_) * 100));
 

+ 6 - 4
Source/Urho3D/Urho2D/TmxFile2D.cpp

@@ -196,8 +196,10 @@ bool TmxTileLayer2D::Load(const XMLElement& element, const TileMapInfo2D& info)
             for (int x = 0; x < width_; ++x)
             {
                 // buffer contains 32-bit integers in little-endian format
-                unsigned gid = (buffer[currentIndex+3] << 24) | (buffer[currentIndex+2] << 16)
-                             | (buffer[currentIndex+1] << 8) | buffer[currentIndex];
+                unsigned gid = ((unsigned)buffer[currentIndex+3] << 24)
+                             | ((unsigned)buffer[currentIndex+2] << 16)
+                             | ((unsigned)buffer[currentIndex+1] << 8)
+                             | (unsigned)buffer[currentIndex];
                 if (gid > 0)
                 {
                     SharedPtr<Tile2D> tile(new Tile2D());
@@ -278,8 +280,8 @@ void TmxObjectGroup2D::StoreObject(const XMLElement& objectElem, const SharedPtr
 
         case OT_TILE:
             object->position_ = info.ConvertPosition(position);
-            object->gid_ = objectElem.GetInt("gid");
-            object->sprite_ = tmxFile_->GetTileSprite(object->gid_);
+            object->gid_ = objectElem.GetUInt("gid");
+            object->sprite_ = tmxFile_->GetTileSprite(object->gid_ & ~FLIP_MASK);
 
             if (objectElem.HasAttribute("width") || objectElem.HasAttribute("height"))
             {