Ejemplo n.º 1
0
        public static void Draw(Texture texture, Vector2 position, Vector2 size, SharpDX.Rectangle?rect, Color4 color, Vector2 center, float rotation = 0)
        {
            if (lastTexture != texture)
            {
                Push();
                lastTexture = texture;
            }

            Matrix trans = Matrix.Translation(-center.X, -center.Y, 0) *
                           Matrix.Scaling(size.X, size.Y, 0) *
                           Matrix.RotationZ(rotation) *
                           Matrix.Translation(position.X, position.Y, 0);

            Vector2 sizeConverted = new Vector2();

            sizeConverted.X = texture.Width;
            sizeConverted.Y = texture.Height;

            Vector4 cut = new Vector4(0, 0, 1, 1);

            if (rect.HasValue)
            {
                cut.X = ((float)rect.Value.Left / texture.Width);
                cut.Y = ((float)rect.Value.Top / texture.Height);
                cut.Z = ((float)rect.Value.Right / texture.Width);
                cut.W = ((float)rect.Value.Bottom / texture.Height);

                sizeConverted.X = rect.Value.Width;
                sizeConverted.Y = rect.Value.Height;
            }

            vertices.Add(new Vertex2D(Vector2.Transform(new Vector2(0, 0), trans), new Vector2(cut.X, cut.Y), color));
            vertices.Add(new Vertex2D(Vector2.Transform(new Vector2(sizeConverted.X, 0), trans), new Vector2(cut.Z, cut.Y), color));
            vertices.Add(new Vertex2D(Vector2.Transform(new Vector2(0, sizeConverted.Y), trans), new Vector2(cut.X, cut.W), color));
            vertices.Add(new Vertex2D(Vector2.Transform(new Vector2(sizeConverted.X, sizeConverted.Y), trans), new Vector2(cut.Z, cut.W), color));

            spritesDrawn++;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Function to update the projection data.
        /// </summary>
        private void UpdateProjection()
        {
            if ((!_needsWvpUpdate) ||
                (_graphics.RenderTargets[0] == null))
            {
                return;
            }

            GorgonRenderTargetView target = _graphics.RenderTargets[0];

            DX.Matrix.OrthoOffCenterLH(0,
                                       target.Width,
                                       target.Height,
                                       0,
                                       0,
                                       1.0f,
                                       out DX.Matrix projectionMatrix);

            _wvpBuffer.Buffer.SetData(ref projectionMatrix);

            _targetBounds   = new DX.Rectangle(0, 0, target.Width, target.Height);
            _needsWvpUpdate = false;
        }
Ejemplo n.º 3
0
            /// <summary>
            /// Crops the sprite.
            /// </summary>
            /// <param name="rect">The rectangle.</param>
            /// <param name="scale">if set to <c>true</c>, crops with the scale.</param>
            public void Crop(SharpDX.Rectangle rect, bool scale = false)
            {
                _crop = rect;

                if (scale)
                {
                    _crop = new SharpDX.Rectangle(
                        (int) (_scale.X * rect.X), (int) (_scale.Y * rect.Y), (int) (_scale.X * rect.Width),
                        (int) (_scale.Y * rect.Height));
                }
            }
Ejemplo n.º 4
0
            /// <summary>
            /// Crops the sprite.
            /// </summary>
            /// <param name="x">The x.</param>
            /// <param name="y">The y.</param>
            /// <param name="w">The width.</param>
            /// <param name="h">The height.</param>
            /// <param name="scale">if set to <c>true</c>, crops with the scale.</param>
            public void Crop(int x, int y, int w, int h, bool scale = false)
            {
                _crop = new SharpDX.Rectangle(x, y, w, h);

                if (scale)
                {
                    _crop = new SharpDX.Rectangle(
                        (int) (_scale.X * x), (int) (_scale.Y * y), (int) (_scale.X * w), (int) (_scale.Y * h));
                }
            }
Ejemplo n.º 5
0
        /// <summary>
        /// Function to set the alpha channel for a specific buffer in the image.
        /// </summary>
        /// <param name="buffer">The buffer to set the alpha channel on.</param>
        /// <param name="alphaValue">The value to set.</param>
        /// <param name="updateAlphaRange">[Optional] The range of alpha values in the buffer that will be updated.</param>
        /// <param name="region">[Optional] The region in the buffer to update.</param>
        /// <returns>The fluent interface for the buffer that was updated.</returns>
        /// <exception cref="ArgumentNullException">Thrown when the <paramref name="buffer"/> parameter is <b>null</b>.</exception>
        /// <exception cref="ArgumentException">Thrown if the buffer format is compressed.</exception>
        /// <remarks>
        /// <para>
        /// This will set the alpha channel for the image data in the <paramref name="buffer"/> to a discrete value specified by <paramref name="alphaValue"/>.
        /// </para>
        /// <para>
        /// If the <paramref name="updateAlphaRange"/> parameter is set, then the alpha values in the <paramref name="buffer"/> will be examined and if the alpha value is less than the minimum range or
        /// greater than the maximum range, then the <paramref name="alphaValue"/> will <b>not</b> be set on the alpha channel.
        /// </para>
        /// <para>
        /// If the <paramref name="region"/> is not specified, then the entire buffer is updated, otherwise only the values within the <paramref name="region"/> are updated.
        /// </para>
        /// </remarks>
        public static IGorgonImageBuffer SetAlpha(this IGorgonImageBuffer buffer, float alphaValue, GorgonRangeF?updateAlphaRange = null, DX.Rectangle?region = null)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            // If we don't have an alpha channel, then don't do anything.
            if (!buffer.FormatInformation.HasAlpha)
            {
                return(buffer);
            }

            // We don't support compressed formats.
            if (buffer.FormatInformation.IsCompressed)
            {
                throw new ArgumentException(string.Format(Resources.GORIMG_ERR_FORMAT_NOT_SUPPORTED, buffer.Format), nameof(buffer));
            }

            if (updateAlphaRange == null)
            {
                updateAlphaRange = new GorgonRangeF(0, 1);
            }

            var fullRect = new DX.Rectangle(0, 0, buffer.Width - 1, buffer.Height - 1);

            if (region == null)
            {
                region = fullRect;
            }
            else
            {
                region = DX.Rectangle.Intersect(region.Value, fullRect);
            }

            unsafe
            {
                byte *src   = (byte *)buffer.Data;
                uint  alpha = (uint)(alphaValue * 255.0f);
                uint  min   = (uint)(updateAlphaRange.Value.Minimum * 255.0f);
                uint  max   = (uint)(updateAlphaRange.Value.Maximum * 255.0f);
                int   pitch = buffer.PitchInformation.RowPitch / buffer.Width;

                for (int y = region.Value.Top; y <= region.Value.Bottom; ++y)
                {
                    byte *horzPtr = src + region.Value.Left * buffer.FormatInformation.SizeInBytes;
                    ImageUtilities.SetAlphaScanline(horzPtr, region.Value.Width * pitch, horzPtr, region.Value.Width * pitch, buffer.Format, alpha, min, max);
                    src += buffer.PitchInformation.RowPitch;
                }
            }

            return(buffer);
        }