public ParticleEditor()
        {
            InitializeComponent();

            SGP.ManagedDirect3D d3d = SGP.ManagedDirect3D.Instance;
            d3d.InitManagedDirect3D(panel1);
            SGP.ManagedTextureManager mtm = SGP.ManagedTextureManager.Instance;
            mtm.InitManagedTextureManager(d3d.Device, d3d.Sprite);

            XElement xRoot;
            if (File.Exists("Config.xml"))
            {
                xRoot = XElement.Load("Config.xml");

                XAttribute xPath = xRoot.Attribute("Path");
                filepath = xPath.Value;
            }

            if (filepath == null)
            {
                FolderBrowserDialog fbd = new FolderBrowserDialog();
                fbd.ShowDialog();
                filepath = fbd.SelectedPath;
            }

            Alive = true;
            emitter.Circlejpg = tm.LoadTexture("../../circle.png", Color.FromArgb(255, 255, 255, 255).ToArgb());
            Vector2D pos = new Vector2D();
            pos.Init(230, 270);
            emitter.Pos = pos;
            emitter.Type = Shape.POINT;
            cbShape.SelectedIndex = 0;
        }
        public void Update(float fElapsedTime)
        {
            if (spawnRate < spawnTimer)
            {
                if( particles.Count > numSpawned  && particleCount != 0)
                    numSpawned++;
                spawnTimer = 0;
                Random r = new Random();
                float l = (float)r.Next((int)(spawnMin * 100), (int)(spawnMax * 100));
                spawnRate = l / 100.0f;
            }

            spawnTimer += fElapsedTime;

            for (int i = 0; i < numSpawned; i++)
            {
                particles[i].Update(fElapsedTime);

                if (particles[i].CurLife >= particles[i].MaxLife)
                {
                    particles.Remove(particles[i]);

                    if (particles.Count == 0)
                    {
                        if (looping == true)
                            InitParticle();
                        else
                            alive = false;
                    }

                    i--;
                    numSpawned--;
                    continue;
                }

                float time = particles[i].MaxLife;

                // Color changing over time
                Color oldColor = particles[i].Color;
                float dtA, dtR, dtG, dtB;

                dtA = (float)((colorEnd.A - colorStart.A) / time);
                dtR = (float)((colorEnd.R - colorStart.R) / time);
                dtG = (float)((colorEnd.G - colorStart.G) / time);
                dtB = (float)((colorEnd.B - colorStart.B) / time);

                dtA *= fElapsedTime;
                dtR *= fElapsedTime;
                dtG *= fElapsedTime;
                dtB *= fElapsedTime;

                particles[i].myColor.A += dtA / 255;
                particles[i].myColor.R += dtR / 255;
                particles[i].myColor.G += dtG / 255;
                particles[i].myColor.B += dtB / 255;

                float a = particles[i].myColor.A;
                float r = particles[i].myColor.R;
                float g = Math.Abs(particles[i].myColor.G);
                float b = Math.Abs(particles[i].myColor.B);

                Color newColor = Color.FromArgb((int)(a * 255), (int)(Math.Abs(r) * 255), (int)(g * 255), (int)(b* 255));
                particles[i].Color = newColor;

                // Rotation changing over time
                float oldRot = particles[i].Rotation;
                float dtRot = (rotationEnd - rotationStart) / time;
                dtRot *= fElapsedTime;
                float newRot = dtRot + oldRot;
                particles[i].Rotation = newRot;

                // Scale changing over time
                float oldScale = particles[i].Scale;
                float dtScale = (scaleEnd - scaleStart) / time;
                dtScale *= fElapsedTime;
                float newScale = dtScale + oldScale;
                particles[i].Scale = newScale;

                // Velocity changing over time
                Vector2D oldVel = particles[i].CurVelocity;
                float dtX = (particles[i].VelocityEnd.x - particles[i].VelocityStart.x) / time;
                float dtY = (particles[i].VelocityEnd.y - particles[i].VelocityStart.y) / time;

                dtX = dtX * fElapsedTime;
                dtY = dtY * fElapsedTime;

                Vector2D newVel = new Vector2D();
                newVel.Init(dtX + oldVel.x, dtY + oldVel.y);
                particles[i].CurVelocity = newVel;
            }
        }
        private void btPreview_Click(object sender, EventArgs e)
        {
            int R = int.Parse(btColorStart.BackColor.R.ToString());
            int G = int.Parse(btColorStart.BackColor.G.ToString());
            int B = int.Parse(btColorStart.BackColor.B.ToString());
            emitter.ColorStart = Color.FromArgb(int.Parse(nudStartAlpha.Value.ToString()), R, G, B);

            R = int.Parse(btColorEnd.BackColor.R.ToString());
            G = int.Parse(btColorEnd.BackColor.G.ToString());
            B = int.Parse(btColorEnd.BackColor.B.ToString());
            emitter.ColorEnd = Color.FromArgb(int.Parse(nudEndAlpha.Value.ToString()), R, G, B);

            emitter.ParticleCount = (int)nudParticleNum.Value;
            float pi = float.Parse(Math.PI.ToString());
            double rad = Math.PI / 180;
            double r = (double)nudRotStart.Value * rad;
            emitter.RotationStart = float.Parse(r.ToString());
            r = (double)nudRotEnd.Value * rad;
            emitter.RotationEnd = float.Parse(r.ToString());

            emitter.ScaleStart = float.Parse(nudScaleStart.Value.ToString());
            emitter.ScaleEnd = float.Parse(nudScaleEnd.Value.ToString());
            emitter.SpawnMax = float.Parse(nudSpawnMax.Value.ToString());
            emitter.SpawnMin = float.Parse(nudSpawnMin.Value.ToString());
            emitter.LifeMax = float.Parse(nudLifeMax.Value.ToString());
            emitter.LifeMin = float.Parse(nudLifeMin.Value.ToString());

            Vector2D tmp = new Vector2D();
            tmp.Init((float)nudVelMaxStartX.Value, (float)nudVelMaxStartY.Value);
            emitter.VelStartMax = tmp;
            tmp.Init((float)nudVelMaxEndX.Value, (float)nudVelMaxEndY.Value);
            emitter.VelEndMax = tmp;
            tmp.Init((float)nudVelMinStartX.Value, (float)nudVelMinStartY.Value);
            emitter.VelStartMin = tmp;
            tmp.Init((float)nudVelMinEndX.Value, (float)nudVelMinEndY.Value);
            emitter.VelEndMin = tmp;

            emitter.Looping = cbLooping.Checked;
            int i = cbShape.SelectedIndex;

            if (i == 0)
                emitter.Type = Shape.POINT;
            else if( i == 1 )
                emitter.Type = Shape.CIRCLE;
            else if( i == 2 )
                emitter.Type = Shape.SQUARE;
            else if( i == 3 )
                emitter.Type = Shape.LINE;

            Vector2D pos = new Vector2D();
            pos.Init(230, 270);
            emitter.Pos = pos;

            if (emitter.Imgpath == null)
            {
                string s = "No Image Selected";
                DialogResult mb = MessageBox.Show(s);
                return;
            }

            emitter.InitParticle();
        }
        public void InitParticle()
        {
            Clear();

            Random rand = new Random();
            rand.Next();
            for (int i = 0; i < particleCount; i++)
            {
                // Sets values to the starting values
                Particle tmp = new Particle();
                tmp.Id = tm.LoadTexture(imgpath, 0);

                tmp.Color = colorStart;
                tmp.Rotation = rotationStart;
                tmp.Scale = scaleStart;
                tmp.Rotation = rotationStart;
                tmp.Source = new Rectangle(0, 0, tm.GetTextureWidth(tmp.Id), tm.GetTextureHeight(tmp.Id));

                tmp.myColor = new MyColor();
                tmp.myColor.A = colorStart.A / 255;
                tmp.myColor.R = colorStart.R / 255;
                tmp.myColor.G = colorStart.G / 255;
                tmp.myColor.B = colorStart.B / 255;

                // Finds random values
                //double theta = double.Parse(angle.ToString());
                //double rot = double.Parse(angleRotation.ToString());

                //float dirXMin = -(float)(Math.Sin(rot));
                //float dirYMin = -(float)(Math.Cos(rot));
                //float dirXMax = -(float)(Math.Sin(theta + rot));
                //float dirYMax = -(float)(Math.Cos(theta + rot));
                //float dirX;
                //float dirY;

                //dirX = (float)(rand.Next(-1, 1) * rand.NextDouble() * Math.Abs((double)(dirXMax) - (double)(dirXMin)));
                //dirY = (float)(rand.Next(-1, 1) * rand.NextDouble() * Math.Abs((double)(dirYMax) - (double)(dirYMin)));

                //dirX = (float)(dirX + dirXMin);
                //dirY = (float)(dirY + dirYMin);

                //if (angle == 2 * Math.PI)
                //{
                //  dirX = (float)rand.Next(-100, 100);
                //  dirY = (float)rand.Next(-100, 100);
                //  dirX = dirX / 100;
                //  dirY = dirY / 100;
                //}

                if (type == Shape.CIRCLE)
                {
                    Vector2D ps;
                    ps.x = pos.x + (rand.Next((int)(-radius-(tmp.Source.Right/2)), (int)(radius+(tmp.Source.Right/2))));
                    ps.y = pos.y + (rand.Next((int)(-radius-(tmp.Source.Bottom/2)), (int)(radius+(tmp.Source.Bottom/2))));
                    tmp.Pos = ps;
                }
                else if (type == Shape.SQUARE)
                {
                    Vector2D ps;
                    ps.x = (pos.X-width/2) + (rand.Next(0, width));
                    ps.y = (pos.Y-height/2) + (rand.Next(0, height));
                    tmp.Pos = ps;
                }
                else if (type == Shape.LINE)
                {
                    Vector2D ps = new Vector2D();
                    if( point.x > point2.x )
                        ps.x = (rand.Next((int)point2.x, (int)point.x));
                    else
                        ps.x = (rand.Next((int)point.x, (int)point2.x));

                   if( point.y > point2.y )
                       ps.y = (rand.Next((int)point2.y, (int)point.y));
                   else
                        ps.y = (rand.Next((int)point.x, (int)point2.y));

                    tmp.Pos = ps;
                }
                else
                {
                    tmp.Pos = pos;
                }

                // Finds the start velocity
                Vector2D vel;
                float x = (float)rand.Next((int)(velStartMin.x * 100), (int)(velStartMax.x * 100));
                float y = (float)rand.Next((int)(velStartMin.y * 100), (int)(velStartMax.y * 100));
                vel.x = (x / 100.0f);
                vel.y = (y / 100.0f);
                vel.y *= -1;
                tmp.VelocityStart = vel;

                // Finds the end velocity
                x = (float)rand.Next((int)(velEndMin.x * 100), (int)(velEndMax.x * 100));
                y = (float)rand.Next((int)(velEndMin.y * 100), (int)(velEndMax.y * 100));
                vel.x = (x / 100.0f);
                vel.y = (y / 100.0f);
                vel.y *= -1;
                tmp.VelocityEnd = vel;

                tmp.CurVelocity = tmp.VelocityStart;

                float life = (float)rand.Next((int)(lifeMin * 100), (int)(lifeMax * 100));
                tmp.MaxLife = (life / 100.0f);

                particles.Add(tmp);
            }

            float sr = (float)(rand.Next( (int)(spawnMin * 100), (int)(spawnMax * 100) ));
            spawnRate = sr / 100.0f;
            spawnTimer = 0;
        }