Exemple #1
0
        public void Add(TextureRegion region, Color color, float x, float y, float width, float height, float rotation)
        {
            SpriteInfo sprite = new SpriteInfo(region, color, x, y, width, height, rotation, count);

            if (!sprites.ContainsKey(region.texture))
            {
                sprites[region.texture] = new List <SpriteInfo>();
            }
            sprites[region.texture].Add(sprite);

            count++;
        }
        public void Add(TextureRegion region, Color color, float x, float y, float z, float width, float height, float rotation_x, float rotation_y, float rotation_z)
        {
            SpriteInfo sprite = new SpriteInfo(region, color, x, y, z, width, height, rotation_x, rotation_y, rotation_z, count);

            if (!sprites.ContainsKey(region.texture))
                sprites[region.texture] = new List<SpriteInfo>();
            sprites[region.texture].Add(sprite);

            count++;
        }
Exemple #3
0
        public void End()
        {
            if (!has_begun)
            {
                throw new InvalidOperationException("Begin must be called before End can be called.");
            }

            if (primitivebatch == null || material == null)
            {
                Warm(device);
            }

            material.Parameters["TextureEnabled"].SetValue(true);
            material.Parameters["World"].SetValue(Matrix.Identity);

            if (is_ortho)
            {
                material.Parameters["View"].SetValue(view);
                material.Parameters["Projection"].SetValue(proj);
            }
            else
            {
                material.Parameters["View"].SetValue(camera.view);
                material.Parameters["Projection"].SetValue(camera.projection);
            }

            Matrix matrix;
            float  a, b, c, d, e, f;

            foreach (Texture2D texture in sprites.Keys)
            {
                List <SpriteInfo> list = sprites[texture];

                if (list.Count <= 0)
                {
                    continue;
                }

                material.Parameters["Texture"].SetValue(texture);
                material.CurrentTechnique.Passes[0].Apply();

                primitivebatch.Begin(Primitive.Quad);
                float texture_pixel_width  = 1.0f / texture.Width;
                float texture_pixel_height = 1.0f / texture.Height;

                for (int i = 0; i < list.Count; i++)
                {
                    SpriteInfo    sprite = list[i];
                    TextureRegion region = sprite.region;

                    float index = 0;
                    if (is_ortho)
                    {
                        index  = 1 - (((float)(sprite.index)) / count);
                        index *= -0.09f;
                    }

                    matrix = Matrix.Identity;
                    if (sprite.rotation_z != 0 || sprite.rotation_x != 0 || sprite.rotation_y != 0)
                    {
                        a = (float)Math.Cos(sprite.rotation_x);
                        b = (float)Math.Sin(sprite.rotation_x);

                        c = (float)Math.Cos(sprite.rotation_y);
                        d = (float)Math.Sin(sprite.rotation_y);

                        e = (float)Math.Cos(sprite.rotation_z);
                        f = (float)Math.Sin(sprite.rotation_z);

                        matrix.M11 = (c * e);
                        matrix.M12 = (c * f);
                        matrix.M13 = -d;
                        matrix.M21 = (e * b * d - a * f);
                        matrix.M22 = ((e * a) + (f * b * d));
                        matrix.M23 = (b * c);
                        matrix.M31 = (e * a * d + b * f);
                        matrix.M33 = (a * c);
                        matrix.M32 = -(b * e - f * a * d);
                    }

                    matrix.M41 = sprite.x;
                    matrix.M42 = sprite.y;
                    matrix.M43 = sprite.z + index;

                    primitivebatch.SetTransform(ref matrix);
                    primitivebatch.SetColor(sprite.color);
                    primitivebatch.SetTextureCoords(region.u * texture_pixel_width, region.v * texture_pixel_height);

                    if (is_ortho)                     //in ortho the view is upside down
                    {
                        primitivebatch.AddVertex(0, 0, 0, 0, 0);
                        primitivebatch.AddVertex(0, 0 + sprite.height, 0, 0, 0 + region.height * texture_pixel_height);
                        primitivebatch.AddVertex(0 + sprite.width, 0 + sprite.height, 0, 0 + region.width * texture_pixel_width, 0 + region.height * texture_pixel_height);
                        primitivebatch.AddVertex(0 + sprite.width, 0, 0, 0 + region.width * texture_pixel_width, 0);
                    }
                    else
                    {
                        primitivebatch.AddVertex(0, 0 + sprite.height, 0, 0, 0);
                        primitivebatch.AddVertex(0, 0, 0, 0, 0 + region.height * texture_pixel_height);
                        primitivebatch.AddVertex(0 + sprite.width, 0, 0, 0 + region.width * texture_pixel_width, 0 + region.height * texture_pixel_height);
                        primitivebatch.AddVertex(0 + sprite.width, 0 + sprite.height, 0, 0 + region.width * texture_pixel_width, 0);
                    }
                }

                primitivebatch.End();

                list.Clear();
            }

            has_begun = false;
        }