void UpdateQuadAt(int tileCoordX, int tileCoordY, bool updateBuffer = true) { int flattenedTileIndex = FlattenedTileIndex(tileCoordX, tileCoordY); CCTileGidAndFlags tileGID = tileGIDAndFlagsArray[flattenedTileIndex]; CCTileMapVertAndIndexBuffer drawBuffer = drawBufferManager.GetDrawBufferAtIndex(flattenedTileIndex); int adjustedTileIndex = flattenedTileIndex - drawBuffer.TileStartIndex; if (tileGID.Gid == 0) { drawBuffer.QuadsVertexBuffer.Data[adjustedTileIndex] = new CCV3F_C4B_T2F_Quad(); if (updateBuffer) { drawBuffer.QuadsVertexBuffer.UpdateBuffer(adjustedTileIndex, 1); } return; } float left, right, top, bottom, vertexZ; vertexZ = TileVertexZ(tileCoordX, tileCoordY); CCSize tileSize = TileSetInfo.TileTexelSize * CCTileMapLayer.DefaultTexelToContentSizeRatios; CCSize texSize = TileSetInfo.TilesheetSize; CCPoint tilePos = TilePosition(tileCoordX, tileCoordY); var quad = drawBuffer.QuadsVertexBuffer.Data[adjustedTileIndex]; // vertices if ((tileGID.Flags & CCTileFlags.TileDiagonal) != 0) { left = tilePos.X; right = tilePos.X + tileSize.Height; bottom = tilePos.Y + tileSize.Width; top = tilePos.Y; } else { left = tilePos.X; right = tilePos.X + tileSize.Width; bottom = tilePos.Y + tileSize.Height; top = tilePos.Y; } float temp; if ((tileGID.Flags & CCTileFlags.Vertical) != 0) { temp = top; top = bottom; bottom = temp; } if ((tileGID.Flags & CCTileFlags.Horizontal) != 0) { temp = left; left = right; right = temp; } if ((tileGID.Flags & CCTileFlags.TileDiagonal) != 0) { // FIXME: not working correcly quad.BottomLeft.Vertices = new CCVertex3F(left, bottom, vertexZ); quad.BottomRight.Vertices = new CCVertex3F(left, top, vertexZ); quad.TopLeft.Vertices = new CCVertex3F(right, bottom, vertexZ); quad.TopRight.Vertices = new CCVertex3F(right, top, vertexZ); } else { quad.BottomLeft.Vertices = new CCVertex3F(left, bottom, vertexZ); quad.BottomRight.Vertices = new CCVertex3F(right, bottom, vertexZ); quad.TopLeft.Vertices = new CCVertex3F(left, top, vertexZ); quad.TopRight.Vertices = new CCVertex3F(right, top, vertexZ); } // texcoords CCRect tileTexture = TileSetInfo.TextureRectForGID(tileGID.Gid); left = ((tileTexture.Origin.X) / texSize.Width) + 0.5f / texSize.Width; right = left + ((tileTexture.Size.Width) / texSize.Width) - 1.0f / texSize.Width; bottom = ((tileTexture.Origin.Y) / texSize.Height) + 0.5f / texSize.Height; top = bottom + ((tileTexture.Size.Height) / texSize.Height) - 1.0f / texSize.Height; quad.BottomLeft.TexCoords = new CCTex2F(left, bottom); quad.BottomRight.TexCoords = new CCTex2F(right, bottom); quad.TopLeft.TexCoords = new CCTex2F(left, top); quad.TopRight.TexCoords = new CCTex2F(right, top); quad.BottomLeft.Colors = CCColor4B.White; quad.BottomRight.Colors = CCColor4B.White; quad.TopLeft.Colors = CCColor4B.White; quad.TopRight.Colors = CCColor4B.White; drawBuffer.QuadsVertexBuffer.Data[adjustedTileIndex] = quad; if (updateBuffer) { drawBuffer.QuadsVertexBuffer.UpdateBuffer(adjustedTileIndex, 1); } }
public void UpdateQuad(int flattenedTileIndex, ref CCTileGidAndFlags tileGID, bool updateBuffer = false) { int adjustedTileIndex = flattenedTileIndex - TileStartIndex; int adjustedStartVertexIndex = adjustedTileIndex * NumOfCornersPerQuad; if (tileGID.Gid == 0) { for (int i = 0; i < NumOfCornersPerQuad; i++) { QuadsVertexBuffer.Data[adjustedStartVertexIndex + i] = new CCV3F_C4B_T2F(); } if (updateBuffer) { QuadsVertexBuffer.UpdateBuffer(adjustedStartVertexIndex, NumOfCornersPerQuad); } return; } else if (tileGID.Gid < TileSetInfo.FirstGid || tileGID.Gid > TileSetInfo.LastGid) { return; } float left, right, top, bottom, vertexZ; vertexZ = TileMapLayer.TileVertexZ(flattenedTileIndex); CCSize tileSize = TileSetInfo.TileTexelSize * CCTileMapLayer.DefaultTexelToContentSizeRatios; CCSize texSize = TileSetInfo.TilesheetSize; CCPoint tilePos = TileMapLayer.TilePosition(flattenedTileIndex); var quadVertexData = QuadsVertexBuffer.Data; var quad = new CCV3F_C4B_T2F_Quad(); // vertices if ((tileGID.Flags & CCTileFlags.TileDiagonal) != 0) { left = tilePos.X; right = tilePos.X + tileSize.Height; bottom = tilePos.Y + tileSize.Width; top = tilePos.Y; } else { left = tilePos.X; right = tilePos.X + tileSize.Width; bottom = tilePos.Y + tileSize.Height; top = tilePos.Y; } float temp; if ((tileGID.Flags & CCTileFlags.Vertical) != 0) { temp = top; top = bottom; bottom = temp; } if ((tileGID.Flags & CCTileFlags.Horizontal) != 0) { temp = left; left = right; right = temp; } if ((tileGID.Flags & CCTileFlags.TileDiagonal) != 0) { // FIXME: not working correcly quad.BottomLeft.Vertices = new CCVertex3F(left, bottom, vertexZ); quad.BottomRight.Vertices = new CCVertex3F(left, top, vertexZ); quad.TopLeft.Vertices = new CCVertex3F(right, bottom, vertexZ); quad.TopRight.Vertices = new CCVertex3F(right, top, vertexZ); } else { quad.BottomLeft.Vertices = new CCVertex3F(left, bottom, vertexZ); quad.BottomRight.Vertices = new CCVertex3F(right, bottom, vertexZ); quad.TopLeft.Vertices = new CCVertex3F(left, top, vertexZ); quad.TopRight.Vertices = new CCVertex3F(right, top, vertexZ); } // texcoords CCRect tileTexture = TileSetInfo.TextureRectForGID(tileGID.Gid); left = ((tileTexture.Origin.X) / texSize.Width) + 0.5f / texSize.Width; right = left + ((tileTexture.Size.Width) / texSize.Width) - 1.0f / texSize.Width; bottom = ((tileTexture.Origin.Y) / texSize.Height) + 0.5f / texSize.Height; top = bottom + ((tileTexture.Size.Height) / texSize.Height) - 1.0f / texSize.Height; quad.BottomLeft.TexCoords = new CCTex2F(left, bottom); quad.BottomRight.TexCoords = new CCTex2F(right, bottom); quad.TopLeft.TexCoords = new CCTex2F(left, top); quad.TopRight.TexCoords = new CCTex2F(right, top); quad.BottomLeft.Colors = CCColor4B.White; quad.BottomRight.Colors = CCColor4B.White; quad.TopLeft.Colors = CCColor4B.White; quad.TopRight.Colors = CCColor4B.White; QuadsVertexBuffer.Data.Elements[adjustedStartVertexIndex] = quad.TopLeft; QuadsVertexBuffer.Data.Elements[adjustedStartVertexIndex + 1] = quad.BottomLeft; QuadsVertexBuffer.Data.Elements[adjustedStartVertexIndex + 2] = quad.TopRight; QuadsVertexBuffer.Data.Elements[adjustedStartVertexIndex + 3] = quad.BottomRight; if (updateBuffer) { QuadsVertexBuffer.UpdateBuffer(adjustedStartVertexIndex, NumOfCornersPerQuad); } }
public CCSprite ExtractTile(int column, int row, bool addToTileMapLayer = true) { if (!AreValidTileCoordinates(column, row)) { return(null); } CCTileGidAndFlags gidAndFlags = TileGIDAndFlags(column, row); int flattendedIndex = FlattenedTileIndex(column, row); CCRect texRect = TileSetInfo.TextureRectForGID(gidAndFlags.Gid); CCSprite tileSprite = new CCSprite(tileSetTexture, texRect); tileSprite.ContentSize = texRect.Size * CCTileMapLayer.DefaultTexelToContentSizeRatios; tileSprite.Position = TilePosition(column, row); tileSprite.VertexZ = TileVertexZ(column, row); tileSprite.AnchorPoint = CCPoint.Zero; tileSprite.Opacity = Opacity; tileSprite.FlipX = false; tileSprite.FlipY = false; tileSprite.Rotation = 0.0f; if ((gidAndFlags.Flags & CCTileFlags.TileDiagonal) != 0) { CCSize halfContentSize = tileSprite.ContentSize * 0.5f; tileSprite.AnchorPoint = CCPoint.AnchorMiddle; tileSprite.Position += new CCPoint(halfContentSize.Width, halfContentSize.Height); CCTileFlags horAndVertFlag = gidAndFlags.Flags & (CCTileFlags.Horizontal | CCTileFlags.Vertical); // Handle the 4 diagonally flipped states. if (horAndVertFlag == CCTileFlags.Horizontal) { tileSprite.Rotation = 90.0f; } else if (horAndVertFlag == CCTileFlags.Vertical) { tileSprite.Rotation = 270.0f; } else if (horAndVertFlag == (CCTileFlags.Vertical | CCTileFlags.Horizontal)) { tileSprite.Rotation = 90.0f; tileSprite.FlipX = true; } else { tileSprite.Rotation = 270.0f; tileSprite.FlipX = true; } } else { if ((gidAndFlags.Flags & CCTileFlags.Horizontal) != 0) { tileSprite.FlipX = true; } if ((gidAndFlags.Flags & CCTileFlags.Vertical) != 0) { tileSprite.FlipY = true; } } if (addToTileMapLayer) { AddChild(tileSprite, flattendedIndex, flattendedIndex); } RemoveTile(column, row); return(tileSprite); }