Ejemplo n.º 1
0
        internal void InternalDraw(Texture2D texture, Vector2 position,
                                   Rectangle?sourceRectangle, Color color, float rotation, Vector2 origin,
                                   Vector2 scale, SpriteEffects effects, float layerDepth)
        {
            if (texture == null)
            {
                throw new ArgumentNullException("texture");
            }
            if (!inBatch || disposed)
            {
                throw new InvalidOperationException("SpriteBatch is in incorrect state");
            }

            // Need to change batch
            if (texture != lastTexture || color != lastColor || !CanAddSprite)
            {
                if (lastTexture != null)                // if lastTexture == null, this is the first batch
                {
                    RenderSprites();
                }
                lastTexture = texture;
                lastColor   = color;
            }

            Rectangle rect = sourceRectangle.HasValue ? sourceRectangle.Value : texture.SourceRect;

            // From Krab.Geometry.Transform2D.CreateTranslateScaleRotateTranslate
            float M11, M12, M21, M22, OffsetX, OffsetY;
            float cos = (float)Math.Cos(rotation);
            float sin = (float)Math.Sin(rotation);

            M11     = cos * scale.X; M12 = sin * scale.X;
            M21     = -sin * scale.Y; M22 = cos * scale.Y;
            OffsetX = -origin.X * cos * scale.X + origin.Y * sin * scale.Y + position.X;
            OffsetY = -origin.Y * cos * scale.Y - origin.X * sin * scale.X + position.Y;

            // Initial sprite width and height:
            float w = rect.Width;
            float h = rect.Height;

            // Create and transform position vertices (from Krab.Geometry.Transform2D.Transform)
            SpriteVertices s = new SpriteVertices();

            s.v0.position.X = w * M11 + 0 * M21 + OffsetX;
            s.v0.position.Y = w * M12 + 0 * M22 + OffsetY;
            s.v1.position.X = w * M11 + h * M21 + OffsetX;
            s.v1.position.Y = w * M12 + h * M22 + OffsetY;
            s.v2.position.X = 0 * M11 + 0 * M21 + OffsetX;
            s.v2.position.Y = 0 * M12 + 0 * M22 + OffsetY;
            s.v3.position.X = 0 * M11 + h * M21 + OffsetX;
            s.v3.position.Y = 0 * M12 + h * M22 + OffsetY;

            // Setup texture coordinates
            texture.texture.FillTextureCoordinates(rect, effects, ref s);

            AddSprite(ref s);
        }
Ejemplo n.º 2
0
        internal void FillTextureCoordinates(Rectangle r, SpriteEffects flip, ref SpriteVertices verts)
        {
            int fh = (int)flip & 1;             // SpriteEffects.FlipHorizontally = 1
            int fv = (int)flip >> 8;            // SpriteEffects.FlipVertically = 256

            verts.v0.uv = new Vector2(texWidthRatio * (r.X + ((1 - fh) * r.Width)), texHeightRatio * (r.Y + ((fv) * r.Height)));
            verts.v1.uv = new Vector2(texWidthRatio * (r.X + ((1 - fh) * r.Width)), texHeightRatio * (r.Y + ((1 - fv) * r.Height)));
            verts.v2.uv = new Vector2(texWidthRatio * (r.X + ((fh) * r.Width)), texHeightRatio * (r.Y + ((fv) * r.Height)));
            verts.v3.uv = new Vector2(texWidthRatio * (r.X + ((fh) * r.Width)), texHeightRatio * (r.Y + ((1 - fv) * r.Height)));
        }
Ejemplo n.º 3
0
        internal void FillTextureCoordinates(Rectangle r, SpriteEffects flip, ref SpriteVertices verts)
        {
            int fh = (int)flip & 1; // SpriteEffects.FlipHorizontally = 1
            int fv = (int)flip >> 8; // SpriteEffects.FlipVertically = 256

            verts.v0.uv = new Vector2(texWidthRatio * (r.X + ((1-fh) * r.Width)), texHeightRatio * (r.Y + ((  fv) * r.Height)));
            verts.v1.uv = new Vector2(texWidthRatio * (r.X + ((1-fh) * r.Width)), texHeightRatio * (r.Y + ((1-fv) * r.Height)));
            verts.v2.uv = new Vector2(texWidthRatio * (r.X + ((  fh) * r.Width)), texHeightRatio * (r.Y + ((  fv) * r.Height)));
            verts.v3.uv = new Vector2(texWidthRatio * (r.X + ((  fh) * r.Width)), texHeightRatio * (r.Y + ((1-fv) * r.Height)));
        }
Ejemplo n.º 4
0
 private unsafe void AddSprite(ref SpriteVertices sprite)
 {
     Debug.Assert(CanAddSprite);
     ((SpriteVertices *)spriteArray)[_spriteArrayCount++] = sprite;
 }
Ejemplo n.º 5
0
 private unsafe void AddSprite(ref SpriteVertices sprite)
 {
     Debug.Assert(CanAddSprite);
     ((SpriteVertices*)spriteArray)[_spriteArrayCount++] = sprite;
 }
Ejemplo n.º 6
0
        internal void InternalDraw(Texture2D texture, Vector2 position,
				Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin,
				Vector2 scale, SpriteEffects effects, float layerDepth)
        {
            if(texture == null)
                throw new ArgumentNullException("texture");
            if(!inBatch || disposed)
                throw new InvalidOperationException("SpriteBatch is in incorrect state");

            // Need to change batch
            if(texture != lastTexture || color != lastColor || !CanAddSprite)
            {
                if(lastTexture != null) // if lastTexture == null, this is the first batch
                    RenderSprites();
                lastTexture = texture;
                lastColor = color;
            }

            Rectangle r = sourceRectangle.HasValue ? sourceRectangle.Value : texture.Bounds;

            // From Krab.Geometry.Transform2D.CreateTranslateScaleRotateTranslate
            float M11, M12, M21, M22, OffsetX, OffsetY;
            float cos = (float)Math.Cos(rotation);
            float sin = (float)Math.Sin(rotation);
            M11 =  cos * scale.X; M12 = sin * scale.X;
            M21 = -sin * scale.Y; M22 = cos * scale.Y;
            OffsetX = -origin.X * cos * scale.X + origin.Y * sin * scale.Y + position.X;
            OffsetY = -origin.Y * cos * scale.Y - origin.X * sin * scale.X + position.Y;

            // Initial sprite width and height:
            float w = r.Width;
            float h = r.Height;

            // Create and transform position vertices (from Krab.Geometry.Transform2D.Transform)
            SpriteVertices s = new SpriteVertices();
            s.v0.position.X = w * M11 + 0 * M21 + OffsetX;
            s.v0.position.Y = w * M12 + 0 * M22 + OffsetY;
            s.v1.position.X = w * M11 + h * M21 + OffsetX;
            s.v1.position.Y = w * M12 + h * M22 + OffsetY;
            s.v2.position.X = 0 * M11 + 0 * M21 + OffsetX;
            s.v2.position.Y = 0 * M12 + 0 * M22 + OffsetY;
            s.v3.position.X = 0 * M11 + h * M21 + OffsetX;
            s.v3.position.Y = 0 * M12 + h * M22 + OffsetY;

            // Setup texture coordinates
            int fh = (int)effects & 1; // SpriteEffects.FlipHorizontally = 1
            int fv = (int)effects >> 8; // SpriteEffects.FlipVertically = 256
            s.v0.uv.X = lastTexture.texWidthRatio * (r.X + ((1-fh) * r.Width));
            s.v1.uv.X = lastTexture.texWidthRatio * (r.X + ((1-fh) * r.Width));
            s.v2.uv.X = lastTexture.texWidthRatio * (r.X + ((  fh) * r.Width));
            s.v3.uv.X = lastTexture.texWidthRatio * (r.X + ((  fh) * r.Width));
            s.v0.uv.Y = lastTexture.texHeightRatio * (r.Y + ((  fv) * r.Height));
            s.v1.uv.Y = lastTexture.texHeightRatio * (r.Y + ((1-fv) * r.Height));
            s.v2.uv.Y = lastTexture.texHeightRatio * (r.Y + ((  fv) * r.Height));
            s.v3.uv.Y = lastTexture.texHeightRatio * (r.Y + ((1-fv) * r.Height));

            AddSprite(ref s);
        }