Пример #1
0
        internal void DrawInternal(Texture2D texture, Rectangle? sourceRect, Rectangle destRect, Color4 color, Vector2 scale, float dx, float dy, float depth, float rot)
        {
            // Flush if the current texture is valid
            // and the new texture differs from the current texture
            if (CurrentTexture.TextureId != -1 && texture.TextureId != CurrentTexture.TextureId)
                Flush ();

            // Set the current texture to the new texture
            CurrentTexture = texture;

            // Flush if the vertex or index counts exceeds the maximum
            if (indexCount + 6 >= MAX_INDICES || vertexCount + 4 >= MAX_VERTICES)
                Flush ();

            // Construct source rectangle
            Rectangle source = sourceRect ?? new Rectangle (0, 0, texture.Width, texture.Height);

            var quat = new Quaternion (rot, 0, 0);
            var pos = new Vector2 (destRect.X, destRect.Y);
            var size = new Vector2 (destRect.Width * scale.X, destRect.Height * scale.Y);

            var sin = (float) Math.Sin (rot);
            var cos = (float) Math.Cos (rot);

            float x = pos.X;
            float y = pos.Y;
            float w = size.X;
            float h = size.Y;

            // Top left
            Vertices[vertexCount++] = new Vertex2D (
                pos: new Vector3 (
                    x + dx * cos - dy * sin,
                    y + dx * sin + dy * cos,
                    z: depth
                ),
                texcoord:
                new Vector2 (
                    x: source.X / (float) texture.Width,
                    y: source.Y / (float) texture.Height
                ),
                color: color
            );

            // Top right
            Vertices[vertexCount++] = new Vertex2D (
                pos: new Vector3 (
                    x + (dx + w) * cos - dy * sin,
                    y + (dx + w) * sin + dy * cos,
                    z: depth
                ),
                texcoord: new Vector2 (
                    x: (source.X + source.Width) / (float) texture.Width,
                    y: source.Y / (float) texture.Height
                ),
                color: color
            );

            // Bottom left
            Vertices[vertexCount++] = new Vertex2D (
                pos: new Vector3 (
                    x + dx * cos - (dy + h) * sin,
                    y + dx * sin + (dy + h) * cos,
                    z: depth
                ),
                texcoord: new Vector2 (
                    x: source.X / (float) texture.Width,
                    y: (source.Y + source.Height) / (float) texture.Height
                ),
                color: color
            );

            // Bottom right
            Vertices[vertexCount++] = new Vertex2D (
                pos: new Vector3 (
                    x + (dx + w) * cos - (dy + h) * sin,
                    y + (dx + w) * sin + (dy + h) * cos,
                    z: depth
                ),
                texcoord: new Vector2 (
                    x: (source.X + source.Width) / (float) texture.Width,
                    y: (source.Y + source.Height) / (float) texture.Height
                ),
                color: color
            );

            // Increment index count
            indexCount += 6;
        }
Пример #2
0
        internal void DrawInternal(Texture2D texture, Rectangle?sourceRect, Rectangle destRect, Color4 color, Vector2 scale, float dx, float dy, float depth, float rot)
        {
            // Flush if the current texture is valid
            // and the new texture differs from the current texture
            if (CurrentTexture.TextureId != -1 && texture.TextureId != CurrentTexture.TextureId)
            {
                Flush();
            }

            // Set the current texture to the new texture
            CurrentTexture = texture;

            // Flush if the vertex or index counts exceeds the maximum
            if (indexCount + 6 >= MAX_INDICES || vertexCount + 4 >= MAX_VERTICES)
            {
                Flush();
            }

            // Construct source rectangle
            Rectangle source = sourceRect ?? new Rectangle(0, 0, texture.Width, texture.Height);

            var quat = new Quaternion(rot, 0, 0);
            var pos  = new Vector2(destRect.X, destRect.Y);
            var size = new Vector2(destRect.Width * scale.X, destRect.Height * scale.Y);

            var sin = (float)Math.Sin(rot);
            var cos = (float)Math.Cos(rot);

            float x = pos.X;
            float y = pos.Y;
            float w = size.X;
            float h = size.Y;

            // Top left
            Vertices[vertexCount++] = new Vertex2D(
                pos: new Vector3(
                    x + dx * cos - dy * sin,
                    y + dx * sin + dy * cos,
                    z: depth
                    ),
                texcoord:
                new Vector2(
                    x: source.X / (float)texture.Width,
                    y: source.Y / (float)texture.Height
                    ),
                color: color
                );

            // Top right
            Vertices[vertexCount++] = new Vertex2D(
                pos: new Vector3(
                    x + (dx + w) * cos - dy * sin,
                    y + (dx + w) * sin + dy * cos,
                    z: depth
                    ),
                texcoord: new Vector2(
                    x: (source.X + source.Width) / (float)texture.Width,
                    y: source.Y / (float)texture.Height
                    ),
                color: color
                );

            // Bottom left
            Vertices[vertexCount++] = new Vertex2D(
                pos: new Vector3(
                    x + dx * cos - (dy + h) * sin,
                    y + dx * sin + (dy + h) * cos,
                    z: depth
                    ),
                texcoord: new Vector2(
                    x: source.X / (float)texture.Width,
                    y: (source.Y + source.Height) / (float)texture.Height
                    ),
                color: color
                );

            // Bottom right
            Vertices[vertexCount++] = new Vertex2D(
                pos: new Vector3(
                    x + (dx + w) * cos - (dy + h) * sin,
                    y + (dx + w) * sin + (dy + h) * cos,
                    z: depth
                    ),
                texcoord: new Vector2(
                    x: (source.X + source.Width) / (float)texture.Width,
                    y: (source.Y + source.Height) / (float)texture.Height
                    ),
                color: color
                );

            // Increment index count
            indexCount += 6;
        }
Пример #3
0
        public void Draw(Texture2D texture, Rectangle? sourceRect, Rectangle destRect, Color4 color, Vector2 scale, float depth = 0, SpriteEffects effects = SpriteEffects.None)
        {
            // Flush if the current texture is valid
            // and the new texture differs from the current texture
            if (CurrentTexture.TextureId != -1 && texture.TextureId != CurrentTexture.TextureId)
                Flush ();

            // Set the current texture to the new texture
            CurrentTexture = texture;

            // Flush if the vertex or index counts exceeds the maximum
            if (indexCount + 6 >= MAX_INDICES || vertexCount + 4 >= MAX_VERTICES)
                Flush ();

            // Construct source rectangle
            Rectangle source = sourceRect ?? new Rectangle (0, 0, texture.Width, texture.Height);

            // Decompose destination rectangle
            float x = destRect.X;
            float y = destRect.Y;
            float w = destRect.Width * scale.X;
            float h = destRect.Height * scale.Y;

            // Decompose source rectangle
            float srcX = source.X;
            float srcY = source.Y;
            float srcW = source.Width;
            float srcH = source.Height;

            var topLeftCoord = new Vector2 (
                x: srcX / (float) texture.Width,
                y: srcY / (float) texture.Height);

            var topRightCoord = new Vector2 (
                x: (srcX + srcW) / (float) texture.Width,
                y: srcY / (float) texture.Height);

            var bottomLeftCoord = new Vector2 (
                x: srcX / (float) texture.Width,
                y: (srcY + srcH) / (float) texture.Height);

            var bottomRightCoord = new Vector2 (
                x: (srcX + srcW) / (float) texture.Width,
                y: (srcY + srcH) / (float) texture.Height);

            // Flip the texture horizontally if requested
            if (effects.HasFlag (SpriteEffects.FlipHorizontal)) {
                SwapVec (ref topLeftCoord, ref topRightCoord);
                SwapVec (ref bottomLeftCoord, ref bottomRightCoord);
            }

            // Flip the texture horizontally if requested
            if (effects.HasFlag (SpriteEffects.FlipVertical)) {
                SwapVec (ref topLeftCoord, ref bottomLeftCoord);
                SwapVec (ref topRightCoord, ref bottomRightCoord);
            }

            // Top left
            Vertices[vertexCount++] = new Vertex2D (
                new Vector3 (x, y, depth),
                topLeftCoord,
                color
            );

            // Top right
            Vertices[vertexCount++] = new Vertex2D (
                new Vector3 (x + w, y, depth),
                topRightCoord,
                color
            );

            // Bottom left
            Vertices[vertexCount++] = new Vertex2D (
                new Vector3 (x, y + h, depth),
                bottomLeftCoord,
                color
            );

            // Bottom right
            Vertices[vertexCount++] = new Vertex2D (
                new Vector3 (x + w, y + h, depth),
                bottomRightCoord,
                color
            );

            // Increment the index count
            indexCount += 6;
        }
Пример #4
0
        public void Draw(Texture2D texture, Rectangle?sourceRect, Rectangle destRect, Color4 color, Vector2 scale, float depth = 0, SpriteEffects effects = SpriteEffects.None)
        {
            // Flush if the current texture is valid
            // and the new texture differs from the current texture
            if (CurrentTexture.TextureId != -1 && texture.TextureId != CurrentTexture.TextureId)
            {
                Flush();
            }

            // Set the current texture to the new texture
            CurrentTexture = texture;

            // Flush if the vertex or index counts exceeds the maximum
            if (indexCount + 6 >= MAX_INDICES || vertexCount + 4 >= MAX_VERTICES)
            {
                Flush();
            }

            // Construct source rectangle
            Rectangle source = sourceRect ?? new Rectangle(0, 0, texture.Width, texture.Height);

            // Decompose destination rectangle
            float x = destRect.X;
            float y = destRect.Y;
            float w = destRect.Width * scale.X;
            float h = destRect.Height * scale.Y;

            // Decompose source rectangle
            float srcX = source.X;
            float srcY = source.Y;
            float srcW = source.Width;
            float srcH = source.Height;

            var topLeftCoord = new Vector2(
                x: srcX / (float)texture.Width,
                y: srcY / (float)texture.Height);

            var topRightCoord = new Vector2(
                x: (srcX + srcW) / (float)texture.Width,
                y: srcY / (float)texture.Height);

            var bottomLeftCoord = new Vector2(
                x: srcX / (float)texture.Width,
                y: (srcY + srcH) / (float)texture.Height);

            var bottomRightCoord = new Vector2(
                x: (srcX + srcW) / (float)texture.Width,
                y: (srcY + srcH) / (float)texture.Height);

            // Flip the texture horizontally if requested
            if (effects.HasFlag(SpriteEffects.FlipHorizontal))
            {
                SwapVec(ref topLeftCoord, ref topRightCoord);
                SwapVec(ref bottomLeftCoord, ref bottomRightCoord);
            }

            // Flip the texture horizontally if requested
            if (effects.HasFlag(SpriteEffects.FlipVertical))
            {
                SwapVec(ref topLeftCoord, ref bottomLeftCoord);
                SwapVec(ref topRightCoord, ref bottomRightCoord);
            }

            // Top left
            Vertices[vertexCount++] = new Vertex2D(
                new Vector3(x, y, depth),
                topLeftCoord,
                color
                );

            // Top right
            Vertices[vertexCount++] = new Vertex2D(
                new Vector3(x + w, y, depth),
                topRightCoord,
                color
                );

            // Bottom left
            Vertices[vertexCount++] = new Vertex2D(
                new Vector3(x, y + h, depth),
                bottomLeftCoord,
                color
                );

            // Bottom right
            Vertices[vertexCount++] = new Vertex2D(
                new Vector3(x + w, y + h, depth),
                bottomRightCoord,
                color
                );

            // Increment the index count
            indexCount += 6;
        }