Click here to Skip to main content
15,891,657 members
Articles / Multimedia / DirectX

Endogine sprite engine

Rate me:
Please Sign up or sign in to vote.
4.84/5 (53 votes)
17 Jul 200615 min read 716.9K   22.1K   216  
Sprite engine for D3D and GDI+ (with several game examples).
using System;

namespace Endogine
{
    /// <summary>
    /// Summary description for Vector4.
    /// </summary>
    public class Vector4
    {
        public float X, Y, Z, W;
        public Vector4(float x, float y, float z, float w)
        {
            this.X = x;
            this.Y = y;
            this.Z = z;
            this.W = w;
        }
        public Vector4(Vector3 v, float w)
        {
            this.X = v.X;
            this.Y = v.Y;
            this.Z = v.Z;
            this.W = w;
        }
        public Vector4()
        {
        }

        public Vector3 ToVector3()
        {
            return new Vector3(X, Y, Z);
        }

        public Vector4 Copy()
        {
            return new Vector4(X, Y, Z, W);
        }

        public float this[int position]
        {
            get
            {
                switch (position)
                {
                    case 0:
                        return this.X;
                    case 1:
                        return this.Y;
                    case 2:
                        return this.Z;
                    case 3:
                        return this.W;
                    default:
                        throw new Exception("Out of bounds in Vector4: " + position);
                }
            }
            set
            {
                switch (position)
                {
                    case 0:
                        this.X = value;
                        break;
                    case 1:
                        this.Y = value;
                        break;
                    case 2:
                        this.Z = value;
                        break;
                    case 3:
                        this.W = value;
                        break;
                    default:
                        throw new Exception("Out of bounds in Vector4: " + position);
                }
            }
        }

        public static Vector4 operator -(Vector4 v1, Vector4 v2)
        {
            return new Vector4(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z, v1.W - v2.W);
        }
        public static Vector4 operator +(Vector4 v1, Vector4 v2)
        {
            return new Vector4(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z, v1.W + v2.W);
        }
        public static Vector4 operator *(Vector4 v1, Vector4 v2)
        {
            return new Vector4(v1.X * v2.X, v1.Y * v2.Y, v1.Z * v2.Z, v1.W * v2.W);
        }
        public static Vector4 operator *(Vector4 v1, float f)
        {
            return new Vector4(v1.X * f, v1.Y * f, v1.Z * f, v1.W * f);
        }
        public static Vector4 operator /(Vector4 v1, Vector4 v2)
        {
            return new Vector4(v1.X / v2.X, v1.Y / v2.Y, v1.Z / v2.Z, v1.W / v2.W);
        }
        public static Vector4 operator /(Vector4 v1, float f)
        {
            return new Vector4(v1.X / f, v1.Y / f, v1.Z / f, v1.W / f);
        }

        public static Vector4 Cross(Vector4 v1, Vector4 v2)
        {
            throw new Exception("Method not implemented: Vector4.Dot()");
            //return new Vector4(
            //    v1.Y * v2.Z - v2.Y * v1.Z,
            //    v1.Z * v2.X - v2.Z * v1.X,
            //    v1.X * v2.Y - v2.X * v1.Y);
        }

        public void Project(object viewport, Matrix4 projection, Matrix4 view, Matrix4 world)
        {
        }
        public void Unproject(object viewport, Matrix4 projection, Matrix4 view, Matrix4 world)
        {
        }

        public float Length
        {
            get { return (float)Math.Sqrt(X * X + Y * Y + Z * Z + W * W); }
        }
        public float LengthSquared
        {
            get { return (X * X + Y * Y + Z * Z + W * W); }
        }

        public void Normalize()
        {
            float len = this.Length;
            this.W /= len;
            this.X /= len;
            this.Y /= len;
            this.Z /= len;
        }
        public float Dot(Vector4 v1)
        {
            throw new Exception("Method not implemented: Vector4.Dot()");
            //return this.X * v1.X + this.Y * v1.Y + this.Z * v1.Z;
        }
        public Vector4 Cross(Vector4 v1)
        {
            return Vector4.Cross(this, v1);
        }

        public override string ToString()
        {
            return this.X.ToString() + ";" + this.Y + ";" + this.Z +";" + this.W;
        }
        public static Vector4 FromString(string val)
        {
            string[] vals = val.Split(';');
            Vector4 v = new Vector4();
            v.X = Convert.ToSingle(vals[0]);
            v.Y = Convert.ToSingle(vals[1]);
            v.Z = Convert.ToSingle(vals[2]);
            v.W = Convert.ToSingle(vals[3]);

            return v;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions