private void AddNew(object texture, SpriteVertexLayout.Struct data) { //Create new segment with initial values var newSegment = new SpriteSegment(); newSegment.Texture = texture; newSegment.Sprites.Add(data); sprites.Add(newSegment); //Create reference for segment in dictionary if (!textureSprites.ContainsKey(texture)) { textureSprites.Add(texture, new List <SpriteSegment>()); } textureSprites[texture].Add(newSegment); spriteCount++; CheckForFullBuffer(); }
private void AddIn(SpriteSegment segment, SpriteVertexLayout.Struct data) { segment.Sprites.Add(data); spriteCount++; CheckForFullBuffer(); }
/// <summary> /// Draws a region of a texture on the screen. /// </summary> /// <param name="texture">The shader resource view of the texture to draw</param> /// <param name="position">Position of the center of the texture in the chosen coordinate system</param> /// <param name="size">Size of the texture in the chosen coordinate system. The size is specified in the screen's coordinate system.</param> /// <param name="center">Specify the texture's center in the chosen coordinate system. The center is specified in the texture's local coordinate system. E.g. for <paramref name="coordinateType"/>=CoordinateType.SNorm, the texture's center is defined by (0, 0).</param> /// <param name="rotationAngle">The angle in radians to rotate the texture. Positive values mean counter-clockwise rotation. Rotations can only be applied for relative or absolute coordinates. Consider using the Degrees or Radians helper structs.</param> /// <param name="coordinateType">A custom coordinate system in which to draw the texture</param> /// <param name="color">The color with which to multiply the texture</param> /// <param name="texCoords">Texture coordinates for the top left corner</param> /// <param name="texCoordsSize">Size of the region in texture coordinates</param> protected internal void Draw(object texture, STRVector position, STRVector size, STRVector center, double rotationAngle, STRVector texCoords, STRVector texCoordsSize, STRColor color, CoordinateType coordinateType) { if (texture == null) { return; } size.X = Math.Abs(size.X); size.Y = Math.Abs(size.Y); //Difference vectors from the center to the texture edges (in screen coordinates). STRVector left, up, right, down; if (coordinateType == CoordinateType.UNorm) { left = new STRVector(0 - center.X * size.X, 0); up = new STRVector(0, 0 - center.Y * size.Y); right = new STRVector((1 - center.X) * size.X, 0); down = new STRVector(0, (1 - center.Y) * size.Y); } else if (coordinateType == CoordinateType.SNorm) { left = new STRVector((-1 - center.X) * size.X / 2, 0); up = new STRVector(0, (1 - center.Y) * size.Y / 2); right = new STRVector((1 - center.X) * size.X / 2, 0); down = new STRVector(0, (-1 - center.Y) * size.Y / 2); } else { left = new STRVector(-center.X, 0); up = new STRVector(0, -center.Y); right = new STRVector(size.X - center.X, 0); down = new STRVector(0, size.Y - center.Y); } if (rotationAngle != 0) { if (coordinateType != CoordinateType.Absolute && coordinateType != CoordinateType.Relative) { //Normalized coordinates tend to be skewed when applying rotation throw new ArgumentException("Rotation is only allowed for relative or absolute coordinates", "rotationAngle"); } float sine = (float)Math.Sin(rotationAngle); float cosine = (float)Math.Cos(rotationAngle); left = Rotate(left, sine, cosine); right = Rotate(right, sine, cosine); up = Rotate(up, sine, cosine); down = Rotate(down, sine, cosine); } var data = new SpriteVertexLayout.Struct(); data.TexCoord = texCoords; data.TexCoordSize = texCoordsSize; data.Color = color.ToArgb(); data.TopLeft = ConvertCoordinate(position + up + left, coordinateType); data.TopRight = ConvertCoordinate(position + up + right, coordinateType); data.BottomLeft = ConvertCoordinate(position + down + left, coordinateType); data.BottomRight = ConvertCoordinate(position + down + right, coordinateType); if (AllowReorder) { //Is there already a sprite for this texture? if (textureSprites.ContainsKey(texture)) { //Add the sprite to the last segment for this texture var Segment = textureSprites[texture].Last(); AddIn(Segment, data); } else { //Add a new segment for this texture AddNew(texture, data); } } else { //Add a new segment for this texture AddNew(texture, data); } }