public BatchItem(Texture2D texture, Vector2 position, Nullable <Rectangle> sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 size, SpriteBatch.SpriteEffects effects, float layerDepth, SpriteBatch.SpriteSortMode sortMode) { this.texture = texture; Vector4 tempText; if (sourceRectangle.HasValue) { tempText = new Vector4((float)sourceRectangle.Value.X / texture.Width, (float)sourceRectangle.Value.Y / texture.Height, (float)sourceRectangle.Value.Width / texture.Width, (float)sourceRectangle.Value.Height / texture.Height); } else { tempText = new Vector4(0, 0, 1, 1); } InitBatchItem(position, color, rotation, origin, new Vector2(size.X, size.Y), effects, layerDepth, sortMode, tempText); }
private void InitBatchItem(Vector2 position, Color color, float rotation, Vector2 origin, Vector2 size, SpriteBatch.SpriteEffects effects, float layerDepth, SpriteBatch.SpriteSortMode sortMode, Vector4 tempText) { if ((effects & SpriteBatch.SpriteEffects.FlipVertically) != 0) { texTopLeft.X = tempText.X + tempText.Z; texBottomRight.X = tempText.X; } else { texTopLeft.X = tempText.X; texBottomRight.X = tempText.X + tempText.Z; } if ((effects & SpriteBatch.SpriteEffects.FlipHorizontally) != 0) { texTopLeft.Y = tempText.Y + tempText.W; texBottomRight.Y = tempText.Y; } else { texTopLeft.Y = tempText.Y; texBottomRight.Y = tempText.Y + tempText.W; } positions = new Vector3[4]; positions[0] = new Vector3(-origin.X, -origin.Y, layerDepth); positions[1] = new Vector3(-origin.X + size.X, -origin.Y, layerDepth); positions[2] = new Vector3(-origin.X, -origin.Y + size.Y, layerDepth); positions[3] = new Vector3(-origin.X + size.X, -origin.Y + size.Y, layerDepth); if (rotation != 0.0f || ((rotation = rotation % (float)(Math.PI * 2)) != 0.0f)) { float cosR = (float)Math.Cos(rotation);//TODO: correct rotation float sinR = (float)Math.Sin(rotation); for (int i = 0; i < positions.Length; i++) { positions[i] = new Vector3(positions[i].X * cosR - positions[i].Y * sinR, positions[i].Y * cosR + positions[i].X * sinR, positions[i].Z); } } for (int i = 0; i < positions.Length; i++) { positions[i] += new Vector3(origin.X + position.X, origin.Y + position.Y, 0.0f); } this.color = color; switch (sortMode) { case SpriteBatch.SpriteSortMode.BackToFront: this.sortingKey = -layerDepth; break; case SpriteBatch.SpriteSortMode.FrontToBack: this.sortingKey = layerDepth; break; case SpriteBatch.SpriteSortMode.Texture: this.sortingKey = texture.GetHashCode(); break; } }